1 /*
2  * Copyright (C) 2018 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 <unistd.h>
18 #include <initializer_list>
19 #include <string>
20 
21 #include <TestProperties.sysprop.h>
22 #include <android-base/properties.h>
23 #include <gtest/gtest.h>
24 
25 // Tests generated sysprop API by:
26 // 1. calling setter with prop_value
27 // 2. comparing raw property string to expected_raw_value
28 // 3. calling getter and comparing the return value to prop_value
29 #define TEST_API(api_name, prop_name, prop_value, expected_raw_value)       \
30   EXPECT_TRUE(api_name(prop_value));                                        \
31   EXPECT_EQ(expected_raw_value, android::base::GetProperty(prop_name, "")); \
32   EXPECT_TRUE(api_name().has_value());                                      \
33   EXPECT_EQ(prop_value, api_name().value())
34 
35 // Tests generated sysprop list API by:
36 // 1. calling setter with prop_value
37 // 2. comparing raw property string to expected_raw_value
38 // 3. calling getter and comparing the return value to prop_value
39 #define TEST_LIST_API(api_name, prop_name, prop_values, expected_raw_value) \
40   EXPECT_TRUE(api_name(prop_values));                                       \
41   EXPECT_EQ(expected_raw_value, android::base::GetProperty(prop_name, "")); \
42   EXPECT_EQ(prop_values, api_name())
43 
44 namespace {
45 
46 template <typename T>
OptionalList(std::initializer_list<T> l)47 std::vector<std::optional<T>> OptionalList(std::initializer_list<T> l) {
48   std::vector<std::optional<T>> ret;
49   for (auto& e : l) ret.push_back(std::make_optional(e));
50   return ret;
51 }
52 
53 }  // namespace
54 
TEST(SyspropTest,CppGenIntegrationTest)55 TEST(SyspropTest, CppGenIntegrationTest) {
56   using namespace android::sysprop::test::TestProperties;
57 
58   TEST_API(test_bool_as_int, "test.bool_as_int", true, "1");
59   TEST_API(test_bool, "test.bool", true, "true");
60   TEST_API(test_int, "test.int", -123, "-123");
61   TEST_API(test_uint, "test.uint", 2147483648, "2147483648");
62   TEST_API(test_long, "test.long", -9876543210, "-9876543210");
63   TEST_API(test_ulong, "test.ulong", 123456789012345, "123456789012345");
64   TEST_API(test_double, "test.double", 3.14159265358979323846,
65            "3.1415926535897931");
66   TEST_API(test_string, "test.string", "hello world", "hello world");
67   TEST_API(test_enum, "test.enum", test_enum_values::C, "c");
68 
69   TEST_LIST_API(test_bool_as_int_list, "test.bool_as_int_list",
70                 OptionalList({true, false}), "1,0");
71 
72   TEST_LIST_API(test_bool_list, "test.bool_list",
73                 OptionalList({false, true, false, false, true}),
74                 "false,true,false,false,true");
75 
76   TEST_LIST_API(test_int_list, "test.int_list", OptionalList({-1337}), "-1337");
77 
78   TEST_LIST_API(test_uint_list, "test.uint_list",
79                 OptionalList({0u, 2147483648u, 1u}), "0,2147483648,1");
80 
81   TEST_LIST_API(test_long_list, "test.long_list",
82                 OptionalList<std::int64_t>({}), "");
83 
84   TEST_LIST_API(test_ulong_list, "test.ulong_list",
85                 OptionalList<std::uint64_t>({123456789012345}),
86                 "123456789012345");
87 
88   TEST_LIST_API(test_double_list, "test.double_list",
89                 OptionalList({0.0, 1.25, -3.0}), "0,1.25,-3");
90 
91   TEST_LIST_API(test_string_list, "test.string_list",
92                 OptionalList<std::string>({"hi,there"}), "hi\\,there");
93 
94   TEST_LIST_API(
95       test_enum_list, "test.enum_list",
96       OptionalList({test_enum_list_values::A, test_enum_list_values::B}), "a,b");
97 }
98