1 /*
2 * Copyright (C) 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 <string>
18 #include <vector>
19 #include <android-base/file.h>
20 #include <android-base/result.h>
21 #include <gtest/gtest.h>
22 #include "aconfig_storage/aconfig_storage_file.hpp"
23
24 using namespace android::base;
25 using namespace aconfig_storage;
26
verify_value(const FlagValueSummary & flag,const std::string & package_name,const std::string & flag_name,const std::string & flag_val,const std::string & value_type)27 void verify_value(const FlagValueSummary& flag,
28 const std::string& package_name,
29 const std::string& flag_name,
30 const std::string& flag_val,
31 const std::string& value_type) {
32 ASSERT_EQ(flag.package_name, package_name);
33 ASSERT_EQ(flag.flag_name, flag_name);
34 ASSERT_EQ(flag.flag_value, flag_val);
35 ASSERT_EQ(flag.value_type, value_type);
36 }
37
verify_value_info(const FlagValueAndInfoSummary & flag,const std::string & package_name,const std::string & flag_name,const std::string & flag_val,const std::string & value_type,bool is_readwrite,bool has_server_override,bool has_local_override)38 void verify_value_info(const FlagValueAndInfoSummary& flag,
39 const std::string& package_name,
40 const std::string& flag_name,
41 const std::string& flag_val,
42 const std::string& value_type,
43 bool is_readwrite,
44 bool has_server_override,
45 bool has_local_override) {
46 ASSERT_EQ(flag.package_name, package_name);
47 ASSERT_EQ(flag.flag_name, flag_name);
48 ASSERT_EQ(flag.flag_value, flag_val);
49 ASSERT_EQ(flag.value_type, value_type);
50 ASSERT_EQ(flag.is_readwrite, is_readwrite);
51 ASSERT_EQ(flag.has_server_override, has_server_override);
52 ASSERT_EQ(flag.has_local_override, has_local_override);
53 }
54
TEST(AconfigStorageFileTest,test_list_flag)55 TEST(AconfigStorageFileTest, test_list_flag) {
56 auto const test_dir = GetExecutableDirectory();
57 auto const package_map = test_dir + "/package.map";
58 auto const flag_map = test_dir + "/flag.map";
59 auto const flag_val = test_dir + "/flag.val";
60 auto flag_list_result = aconfig_storage::list_flags(
61 package_map, flag_map, flag_val);
62 ASSERT_TRUE(flag_list_result.ok());
63
64 auto const& flag_list = *flag_list_result;
65 ASSERT_EQ(flag_list.size(), 8);
66 verify_value(flag_list[0], "com.android.aconfig.storage.test_1", "disabled_rw",
67 "false", "ReadWriteBoolean");
68 verify_value(flag_list[1], "com.android.aconfig.storage.test_1", "enabled_ro",
69 "true", "ReadOnlyBoolean");
70 verify_value(flag_list[2], "com.android.aconfig.storage.test_1", "enabled_rw",
71 "true", "ReadWriteBoolean");
72 verify_value(flag_list[3], "com.android.aconfig.storage.test_2", "disabled_rw",
73 "false", "ReadWriteBoolean");
74 verify_value(flag_list[4], "com.android.aconfig.storage.test_2", "enabled_fixed_ro",
75 "true", "FixedReadOnlyBoolean");
76 verify_value(flag_list[5], "com.android.aconfig.storage.test_2", "enabled_ro",
77 "true", "ReadOnlyBoolean");
78 verify_value(flag_list[6], "com.android.aconfig.storage.test_4", "enabled_fixed_ro",
79 "true", "FixedReadOnlyBoolean");
80 verify_value(flag_list[7], "com.android.aconfig.storage.test_4", "enabled_rw",
81 "true", "ReadWriteBoolean");
82 }
83
TEST(AconfigStorageFileTest,test_list_flag_with_info)84 TEST(AconfigStorageFileTest, test_list_flag_with_info) {
85 auto const test_dir = GetExecutableDirectory();
86 auto const package_map = test_dir + "/package.map";
87 auto const flag_map = test_dir + "/flag.map";
88 auto const flag_val = test_dir + "/flag.val";
89 auto const flag_info = test_dir + "/flag.info";
90 auto flag_list_result = aconfig_storage::list_flags_with_info(
91 package_map, flag_map, flag_val, flag_info);
92 ASSERT_TRUE(flag_list_result.ok());
93
94 auto const& flag_list = *flag_list_result;
95 ASSERT_EQ(flag_list.size(), 8);
96 verify_value_info(flag_list[0], "com.android.aconfig.storage.test_1", "disabled_rw",
97 "false", "ReadWriteBoolean", true, false, false);
98 verify_value_info(flag_list[1], "com.android.aconfig.storage.test_1", "enabled_ro",
99 "true", "ReadOnlyBoolean", false, false, false);
100 verify_value_info(flag_list[2], "com.android.aconfig.storage.test_1", "enabled_rw",
101 "true", "ReadWriteBoolean", true, false, false);
102 verify_value_info(flag_list[3], "com.android.aconfig.storage.test_2", "disabled_rw",
103 "false", "ReadWriteBoolean", true, false, false);
104 verify_value_info(flag_list[4], "com.android.aconfig.storage.test_2", "enabled_fixed_ro",
105 "true", "FixedReadOnlyBoolean", false, false, false);
106 verify_value_info(flag_list[5], "com.android.aconfig.storage.test_2", "enabled_ro",
107 "true", "ReadOnlyBoolean", false, false, false);
108 verify_value_info(flag_list[6], "com.android.aconfig.storage.test_4", "enabled_fixed_ro",
109 "true", "FixedReadOnlyBoolean", false, false, false);
110 verify_value_info(flag_list[7], "com.android.aconfig.storage.test_4", "enabled_rw",
111 "true", "ReadWriteBoolean", true, false, false);
112 }
113