Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 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> |
Nick Desaulniers | de0baf0 | 2019-12-10 09:40:30 -0800 | [diff] [blame] | 21 | #include <android/api-level.h> |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | #include <gtest/gtest.h> |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 24 | #include <sys/stat.h> |
Nick Desaulniers | de0baf0 | 2019-12-10 09:40:30 -0800 | [diff] [blame] | 25 | #include <sys/types.h> |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 26 | #include <sys/utsname.h> |
| 27 | #include <unistd.h> |
| 28 | |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 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_; |
| 36 | KernelHeadersTest() |
| 37 | : first_api_level_(std::stoi( |
| 38 | android::base::GetProperty("ro.product.first_api_level", "0"))) { |
| 39 | } |
| 40 | |
| 41 | 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 | |
Nick Desaulniers | de0baf0 | 2019-12-10 09:40:30 -0800 | [diff] [blame] | 54 | return ((first_api_level_ > __ANDROID_API_Q__) && (kver_pass == true)); |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 55 | } |
| 56 | }; |
| 57 | |
| 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 | |
Steve Muckle | ab7f140 | 2020-07-16 10:23:11 -0700 | [diff] [blame] | 65 | TEST_F(KernelHeadersTest, KheadersExist) { |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 66 | struct stat st; |
| 67 | struct utsname buf; |
Steve Muckle | ab7f140 | 2020-07-16 10:23:11 -0700 | [diff] [blame] | 68 | std::string path = "/sys/kernel/kheaders.tar.xz"; |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 69 | |
| 70 | uname(&buf); |
| 71 | if (!should_run(buf)) return; |
| 72 | |
Steve Muckle | ab7f140 | 2020-07-16 10:23:11 -0700 | [diff] [blame] | 73 | // Make sure the kheaders are available |
Joel Fernandes | 77a1a8b | 2019-06-20 13:56:05 -0400 | [diff] [blame] | 74 | errno = 0; |
| 75 | stat(path.c_str(), &st); |
| 76 | ASSERT_EQ(0, (errno == ENOENT)); |
| 77 | } |
| 78 | |
| 79 | } // namespace kernel |
| 80 | } // namespace android |