• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <kver/kernel_release.h>
25 
26 namespace android {
27 namespace kernel {
28 
29 class KernelVersionTest : public ::testing::Test {
30  protected:
31   const std::string arch_;
32   const int first_api_level_;
33   const bool should_run_compiler_test_;
34   const bool should_run_linker_test_;
35   std::string version_;
KernelVersionTest()36   KernelVersionTest()
37       : arch_(android::base::GetProperty("ro.bionic.arch", "")),
38         first_api_level_(
39             std::stoi(android::base::GetProperty("ro.vendor.api_level", "0"))),
40         should_run_compiler_test_(
41             first_api_level_ >= __ANDROID_API_R__ ||
42             (arch_ == "arm64" && first_api_level_ >= __ANDROID_API_Q__)),
43         should_run_linker_test_(first_api_level_ >= __ANDROID_API_S__) {
44     std::ifstream proc_version("/proc/version");
45     std::getline(proc_version, version_);
46   }
47 };
48 
TEST_F(KernelVersionTest,IsntGCC)49 TEST_F(KernelVersionTest, IsntGCC) {
50   if (!should_run_compiler_test_) return;
51   const std::string needle = "gcc version";
52   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
53 }
54 
TEST_F(KernelVersionTest,IsClang)55 TEST_F(KernelVersionTest, IsClang) {
56   if (!should_run_compiler_test_) return;
57   const std::string needle = "clang version";
58   ASSERT_THAT(version_, ::testing::HasSubstr(needle));
59 }
60 
TEST_F(KernelVersionTest,IsntBFD)61 TEST_F(KernelVersionTest, IsntBFD) {
62   if (!should_run_linker_test_) return;
63   const std::string needle = "GNU ld";
64   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
65   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr("GNU Binutils")));
66   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr("binutils")));
67 }
68 
TEST_F(KernelVersionTest,IsntGold)69 TEST_F(KernelVersionTest, IsntGold) {
70   if (!should_run_linker_test_) return;
71   const std::string needle = "GNU gold";
72   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
73 }
74 
TEST_F(KernelVersionTest,IsLLD)75 TEST_F(KernelVersionTest, IsLLD) {
76   if (!should_run_linker_test_) return;
77   const std::string needle = "LLD";
78   ASSERT_THAT(version_, ::testing::HasSubstr(needle));
79 }
80 
81 // @VsrTest = 3.4.2
TEST_F(KernelVersionTest,IsKleaf)82 TEST_F(KernelVersionTest, IsKleaf) {
83   constexpr uint64_t kMinAndroidRelease = 15;  // Android 15
84   const auto kernel_release =
85       android::kver::KernelRelease::Parse(version_, /* allow_suffix = */ true);
86   if (!kernel_release.has_value()) {
87     GTEST_SKIP()
88         << "The test only applies to android" << kMinAndroidRelease
89         << " or later kernels. The kernel release string does not have the"
90         << " GKI kernel release format: " << version_;
91   }
92   if (kernel_release->android_release() < kMinAndroidRelease) {
93     GTEST_SKIP() << "The test only applies to android" << kMinAndroidRelease
94                  << " or later kernels. This kernel declares android"
95                  << kernel_release->android_release() << ": " << version_;
96   }
97   ASSERT_THAT(version_, ::testing::HasSubstr("kleaf@"))
98       << "android" << kernel_release->android_release()
99       << " kernel is required to be built with Kleaf.";
100 }
101 
102 }  // namespace kernel
103 }  // namespace android
104