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 <array>
18 #include <cstdio>
19 #include <fstream>
20 #include <string>
21 
22 #include <android-base/properties.h>
23 #include <android/api-level.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 
27 namespace android {
28 namespace kernel {
29 
30 class KernelLoopConfigTest : public ::testing::Test {
31  protected:
32   const int first_api_level_;
KernelLoopConfigTest()33   KernelLoopConfigTest()
34       : first_api_level_(std::stoi(
35             android::base::GetProperty("ro.product.first_api_level", "0"))) {}
should_run() const36   bool should_run() const {
37     // TODO check for APEX support (for upgrading devices)
38     return first_api_level_ >= __ANDROID_API_Q__;
39   }
40 };
41 
TEST_F(KernelLoopConfigTest,ValidLoopConfig)42 TEST_F(KernelLoopConfigTest, ValidLoopConfig) {
43   if (!should_run()) return;
44 
45   static constexpr const char* kCmd =
46       "zcat /proc/config.gz | grep CONFIG_BLK_DEV_LOOP_MIN_COUNT";
47   std::array<char, 256> line;
48 
49   std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(kCmd, "r"), pclose);
50   ASSERT_NE(pipe, nullptr);
51 
52   auto read = fgets(line.data(), line.size(), pipe.get());
53   ASSERT_NE(read, nullptr);
54 
55   auto minCountStr = std::string(read);
56 
57   auto pos = minCountStr.find("=");
58   ASSERT_NE(pos, std::string::npos);
59   ASSERT_GE(minCountStr.length(), pos + 1);
60 
61   int minCountValue = std::stoi(minCountStr.substr(pos + 1));
62   ASSERT_GE(minCountValue, 16);
63 }
64 
TEST_F(KernelLoopConfigTest,ValidLoopParameters)65 TEST_F(KernelLoopConfigTest, ValidLoopParameters) {
66   if (!should_run()) return;
67 
68   std::ifstream max_loop("/sys/module/loop/parameters/max_loop");
69   std::ifstream max_part("/sys/module/loop/parameters/max_part");
70 
71   std::string max_loop_str;
72   std::string max_part_str;
73 
74   std::getline(max_loop, max_loop_str);
75   std::getline(max_part, max_part_str);
76 
77   int max_part_value = std::stoi(max_part_str);
78   EXPECT_LE(max_part_value, 7);
79 
80   int max_loop_value = std::stoi(max_loop_str);
81   EXPECT_EQ(0, max_loop_value);
82 }
83 
84 }  // namespace kernel
85 }  // namespace android
86