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 
25 namespace android {
26 namespace kernel {
27 
28 class KernelVersionTest : public ::testing::Test {
29  protected:
30   const std::string arch_;
31   const int first_api_level_;
32   const bool should_run_compiler_test_;
33   const bool should_run_linker_test_;
34   std::string version_;
KernelVersionTest()35   KernelVersionTest()
36       : arch_(android::base::GetProperty("ro.bionic.arch", "")),
37         first_api_level_(std::stoi(
38             android::base::GetProperty("ro.product.first_api_level", "0"))),
39         should_run_compiler_test_(
40             first_api_level_ >= __ANDROID_API_R__ ||
41             (arch_ == "arm64" && first_api_level_ >= __ANDROID_API_Q__)),
42         should_run_linker_test_(first_api_level_ >= __ANDROID_API_R__) {
43     std::ifstream proc_version("/proc/version");
44     std::getline(proc_version, version_);
45   }
46 };
47 
TEST_F(KernelVersionTest,IsntGCC)48 TEST_F(KernelVersionTest, IsntGCC) {
49   if (!should_run_compiler_test_) return;
50   const std::string needle = "gcc version";
51   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
52 }
53 
TEST_F(KernelVersionTest,IsClang)54 TEST_F(KernelVersionTest, IsClang) {
55   if (!should_run_compiler_test_) return;
56   const std::string needle = "clang version";
57   ASSERT_THAT(version_, ::testing::HasSubstr(needle));
58 }
59 
TEST_F(KernelVersionTest,IsntBFD)60 TEST_F(KernelVersionTest, IsntBFD) {
61   if (!should_run_linker_test_) return;
62   const std::string needle = "GNU ld";
63   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
64   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr("GNU Binutils")));
65   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr("binutils")));
66 }
67 
TEST_F(KernelVersionTest,IsntGold)68 TEST_F(KernelVersionTest, IsntGold) {
69   if (!should_run_linker_test_) return;
70   const std::string needle = "GNU gold";
71   ASSERT_THAT(version_, ::testing::Not(::testing::HasSubstr(needle)));
72 }
73 
TEST_F(KernelVersionTest,IsLLD)74 TEST_F(KernelVersionTest, IsLLD) {
75   if (!should_run_linker_test_) return;
76   const std::string needle = "LLD";
77   ASSERT_THAT(version_, ::testing::HasSubstr(needle));
78 }
79 
80 }  // namespace kernel
81 }  // namespace android
82