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_tracking_"
66 "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(
88 bluetooth::os::GetSystemProperty(kSupportedSyspropName),
89 Optional(StrEq(kSupportedSyspropValue)));
90 EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
91 EXPECT_THAT(
92 bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName),
93 Optional(StrEq(kCorrectPrefixAflagValue)));
94 EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
95 }
96
TEST_F(SyspropsModuleTest,empty_sysprops_file_path_test)97 TEST_F(SyspropsModuleTest, empty_sysprops_file_path_test) {
98 // Verify the state before test
99 EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName), std::nullopt);
100 EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
101 EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName), std::nullopt);
102 EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
103
104 bluetooth::os::ParameterProvider::OverrideSyspropsFilePath("");
105 auto* sysprops_module = new bluetooth::sysprops::SyspropsModule();
106 test_registry_.InjectTestModule(&bluetooth::sysprops::SyspropsModule::Factory, sysprops_module);
107
108 EXPECT_THAT(bluetooth::os::GetSystemProperty(kSupportedSyspropName), std::nullopt);
109 EXPECT_THAT(bluetooth::os::GetSystemProperty(kUnsupportedSyspropName), std::nullopt);
110 EXPECT_THAT(bluetooth::os::GetSystemProperty(kCorrectPrefixAflagName), std::nullopt);
111 EXPECT_THAT(bluetooth::os::GetSystemProperty(kIncorrectPrefixAflagName), std::nullopt);
112 }
113
114 } // namespace testing
115