1 /*
2  * Copyright 2024 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 "sysprops/sysprops_module.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <filesystem>
23 
24 #include "os/files.h"
25 #include "os/parameter_provider.h"
26 #include "os/system_properties.h"
27 
28 namespace testing {
29 
30 class SyspropsModuleTest : public Test {
31 protected:
SetUp()32   void SetUp() override {
33     EXPECT_TRUE(bluetooth::os::ClearSystemPropertiesForHost());
34     temp_config_ = std::filesystem::temp_directory_path() / "temp_sysprops.conf";
35     temp_override_dir_ = temp_config_ / ".d";
36     DeleteConfigFiles();
37     bluetooth::os::ParameterProvider::OverrideSyspropsFilePath(temp_config_);
38   }
39 
TearDown()40   void TearDown() override {
41     EXPECT_TRUE(bluetooth::os::ClearSystemPropertiesForHost());
42     test_registry_.StopAll();
43     DeleteConfigFiles();
44   }
45 
DeleteConfigFiles()46   void DeleteConfigFiles() {
47     if (std::filesystem::exists(temp_config_)) {
48       EXPECT_TRUE(std::filesystem::remove(temp_config_));
49     }
50     if (std::filesystem::exists(temp_override_dir_)) {
51       EXPECT_GT(std::filesystem::remove_all(temp_override_dir_), 0u);
52       EXPECT_FALSE(std::filesystem::exists(temp_override_dir_));
53     }
54   }
55 
56   bluetooth::TestModuleRegistry test_registry_;
57   std::filesystem::path temp_config_;
58   std::filesystem::path temp_override_dir_;
59 };
60 
61 static const std::string kSupportedSyspropName = "bluetooth.device.class_of_device";
62 static const std::string kSupportedSyspropValue = "0,1,4";
63 static const std::string kUnsupportedSyspropName = "i.am.an.unsupported.sysprop";
64 static const std::string kCorrectPrefixAflagName =
65         "persist.device_config.aconfig_flags.bluetooth.com.android.bluetooth.flags.msft_addr_"
66         "tracking_quirk";
67 static const std::string kCorrectPrefixAflagValue = "true";
68 static const std::string kIncorrectPrefixAflagName =
69         "persist.device_config.aconfig_flags.not_bluetooth.testing_flag";
70 
71 static const std::string kParseConfigTestConfig =
72         "[Sysprops]\n" + kSupportedSyspropName + "=" + kSupportedSyspropValue + "\n" +
73         kUnsupportedSyspropName + "=true\n" + "\n" + "[Aflags]\n" + kCorrectPrefixAflagName + "=" +
74         kCorrectPrefixAflagValue + "\n" + kIncorrectPrefixAflagName + "=true\n";
75 
TEST_F(SyspropsModuleTest,parse_config_test)76 TEST_F(SyspropsModuleTest, parse_config_test) {
77   // Verify the state before test
78   EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName), std::nullopt);
79   EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
80   EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName), std::nullopt);
81   EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
82 
83   EXPECT_TRUE(bluetooth::os::WriteToFile(temp_config_.string(), kParseConfigTestConfig));
84   auto* sysprops_module = new bluetooth::sysprops::SyspropsModule();
85   test_registry_.InjectTestModule(&bluetooth::sysprops::SyspropsModule::Factory, sysprops_module);
86 
87   EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName),
88               Optional(StrEq(kSupportedSyspropValue)));
89   EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
90   EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName),
91               Optional(StrEq(kCorrectPrefixAflagValue)));
92   EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
93 }
94 
TEST_F(SyspropsModuleTest,empty_sysprops_file_path_test)95 TEST_F(SyspropsModuleTest, empty_sysprops_file_path_test) {
96   // Verify the state before test
97   EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName), std::nullopt);
98   EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
99   EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName), std::nullopt);
100   EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
101 
102   bluetooth::os::ParameterProvider::OverrideSyspropsFilePath("");
103   auto* sysprops_module = new bluetooth::sysprops::SyspropsModule();
104   test_registry_.InjectTestModule(&bluetooth::sysprops::SyspropsModule::Factory, sysprops_module);
105 
106   EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName), std::nullopt);
107   EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
108   EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName), std::nullopt);
109   EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
110 }
111 
112 }  // namespace testing
113