1 /*
2  * Copyright 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 <cutils/properties.h>
18 #include <gtest/gtest.h>
19 
20 #include <string>
21 
22 #include "os/system_properties.h"
23 
24 namespace testing {
25 
26 using bluetooth::os::ClearSystemPropertiesForHost;
27 using bluetooth::os::GetSystemProperty;
28 using bluetooth::os::SetSystemProperty;
29 
TEST(SystemPropertiesTest,set_and_get_test)30 TEST(SystemPropertiesTest, set_and_get_test) {
31   ASSERT_TRUE(SetSystemProperty("persist.bluetooth.factoryreset", "true"));
32   auto ret = GetSystemProperty("persist.bluetooth.factoryreset");
33   ASSERT_TRUE(ret);
34   ASSERT_EQ(ret, "true");
35   ASSERT_TRUE(SetSystemProperty("persist.bluetooth.factoryreset", "false"));
36   ret = GetSystemProperty("persist.bluetooth.factoryreset");
37   ASSERT_TRUE(ret);
38   ASSERT_EQ(ret, "false");
39   ret = GetSystemProperty("persist.bluetooth.factoryreset_do_not_exist");
40   ASSERT_FALSE(ret);
41   if (ClearSystemPropertiesForHost()) {
42     ASSERT_FALSE(GetSystemProperty("persist.bluetooth.factoryreset"));
43   }
44 }
45 
TEST(SystemPropertiesTest,getUint32)46 TEST(SystemPropertiesTest, getUint32) {
47   std::string property("SystemPropertiesTest_getUint32");
48   uint32_t value = 42;
49   ASSERT_TRUE(SetSystemProperty(property, std::to_string(value)));
50   ASSERT_EQ(bluetooth::os::GetSystemPropertyUint32(property, 0), value);
51 }
52 
TEST(SystemPropertiesTest,getUint32BaseHex)53 TEST(SystemPropertiesTest, getUint32BaseHex) {
54   std::string property("SystemPropertiesTest_getUint32BaseHex");
55   uint32_t value = 42;
56   std::stringstream stream;
57   stream << std::showbase << std::hex << value;
58   ASSERT_TRUE(SetSystemProperty(property, stream.str()));
59   ASSERT_EQ(bluetooth::os::GetSystemPropertyUint32Base(property, 0), value);
60   ASSERT_EQ(bluetooth::os::GetSystemPropertyUint32Base(property, 1, 10), 0u);  // if parsed as a dec
61 }
62 
63 }  // namespace testing