1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fstream>
18 #include <string>
19
20 #include <android-base/properties.h>
21 #include <android/api-level.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/utsname.h>
27 #include <unistd.h>
28
29 namespace android {
30 namespace kernel {
31
32 class KernelHeadersTest : public ::testing::Test {
33 protected:
34 std::string version_;
35 const int first_api_level_;
KernelHeadersTest()36 KernelHeadersTest()
37 : first_api_level_(std::stoi(
38 android::base::GetProperty("ro.product.first_api_level", "0"))) {
39 }
40
should_run(struct utsname buf) const41 bool should_run(struct utsname buf) const {
42 int kernel_version_major, kernel_version_minor, ret;
43 char dummy;
44 bool kver_pass = false;
45
46 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
47
48 // If kernel version format changes in future, run it anyway
49 if (ret < 2)
50 kver_pass = true;
51 else if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14))
52 kver_pass = true;
53
54 return ((first_api_level_ > __ANDROID_API_Q__) && (kver_pass == true));
55 }
56 };
57
TEST_F(KernelHeadersTest,UnameWorks)58 TEST_F(KernelHeadersTest, UnameWorks) {
59 struct utsname buf;
60
61 // Make sure uname works since we need it in this test
62 ASSERT_EQ(0, uname(&buf));
63 }
64
TEST_F(KernelHeadersTest,KheadersExist)65 TEST_F(KernelHeadersTest, KheadersExist) {
66 struct stat st;
67 struct utsname buf;
68 std::string path = "/sys/kernel/kheaders.tar.xz";
69
70 uname(&buf);
71 if (!should_run(buf)) return;
72
73 // Make sure the kheaders are available
74 errno = 0;
75 stat(path.c_str(), &st);
76 ASSERT_EQ(0, (errno == ENOENT));
77 }
78
79 } // namespace kernel
80 } // namespace android
81