• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 <string>
18 
19 #include <android-base/test_utils.h>
20 #include <gtest/gtest.h>
21 
22 #include "ApiChecker.h"
23 #include "Common.h"
24 
25 namespace {
26 
27 constexpr const char* kLatestApi =
28     R"(
29 props {
30     owner: Platform
31     module: "android.all_dep"
32     prop {
33         api_name: "dep1"
34         type: Integer
35         scope: Public
36         access: ReadWrite
37         prop_name: "dep1_int"
38         deprecated: true
39     }
40 }
41 props {
42     owner: Platform
43     module: "android.platprop"
44     prop {
45         api_name: "prop1"
46         type: Long
47         scope: Public
48         access: ReadWrite
49         prop_name: "prop1"
50     }
51     prop {
52         api_name: "prop2"
53         type: String
54         scope: Internal
55         access: Readonly
56         prop_name: "ro.prop2"
57     }
58     prop {
59         api_name: "prop3"
60         type: Boolean
61         scope: Public
62         access: ReadWrite
63         prop_name: "ctl.start$prop3"
64     }
65     prop {
66         api_name: "prop4"
67         type: String
68         scope: Public
69         access: Readonly
70         prop_name: "ro.prop4"
71     }
72 }
73 )";
74 
75 constexpr const char* kCurrentApi =
76     R"(
77 props {
78     owner: Platform
79     module: "android.platprop"
80     prop {
81         api_name: "prop1"
82         type: Long
83         scope: Public
84         access: ReadWrite
85         prop_name: "prop1"
86     }
87     prop {
88         api_name: "prop2"
89         type: Integer
90         scope: Public
91         access: Writeonce
92         prop_name: "ro.public.prop2"
93     }
94     prop {
95         api_name: "prop3"
96         type: Boolean
97         scope: Public
98         access: ReadWrite
99         prop_name: "ctl.start$prop3"
100     }
101     prop {
102         api_name: "prop4"
103         type: String
104         scope: Public
105         access: Readonly
106         prop_name: "ro.prop4"
107         deprecated: true
108     }
109 
110 }
111 )";
112 
113 constexpr const char* kInvalidCurrentApi =
114     R"(
115 props {
116     owner: Platform
117     module: "android.platprop"
118     prop {
119         api_name: "prop2"
120         type: Double
121         scope: Public
122         access: Readonly
123         prop_name: "ro.prop2.a"
124     }
125     prop {
126         api_name: "prop3"
127         type: Boolean
128         scope: Internal
129         access: Readonly
130         integer_as_bool: true,
131         prop_name: "ctl.start$prop3"
132     }
133     prop {
134         api_name: "prop4"
135         type: Boolean
136         scope: Internal
137         access: ReadWrite
138         prop_name: "prop4"
139     }
140 }
141 )";
142 
143 }  // namespace
144 
TEST(SyspropTest,ApiCheckerTest)145 TEST(SyspropTest, ApiCheckerTest) {
146   TemporaryFile latest_file;
147   close(latest_file.fd);
148   latest_file.fd = -1;
149   ASSERT_TRUE(android::base::WriteStringToFile(kLatestApi, latest_file.path));
150 
151   std::string err;
152   auto latest_api = ParseApiFile(latest_file.path);
153   ASSERT_RESULT_OK(latest_api);
154 
155   TemporaryFile current_file;
156   close(current_file.fd);
157   current_file.fd = -1;
158   ASSERT_TRUE(android::base::WriteStringToFile(kCurrentApi, current_file.path));
159 
160   auto current_api = ParseApiFile(current_file.path);
161   ASSERT_RESULT_OK(current_api);
162   EXPECT_RESULT_OK(CompareApis(*latest_api, *current_api));
163 
164   TemporaryFile invalid_current_file;
165   close(invalid_current_file.fd);
166   invalid_current_file.fd = -1;
167   ASSERT_TRUE(android::base::WriteStringToFile(kInvalidCurrentApi,
168                                                invalid_current_file.path));
169 
170   auto invalid_current_api = ParseApiFile(invalid_current_file.path);
171   ASSERT_RESULT_OK(invalid_current_api);
172 
173   auto res = CompareApis(*latest_api, *invalid_current_api);
174   EXPECT_FALSE(res.ok());
175 
176   EXPECT_EQ(res.error().message(),
177             "Prop prop1 has been removed\n"
178             "Accessibility of prop prop3 has become more restrictive\n"
179             "Scope of prop prop3 has become more restrictive\n"
180             "Integer-as-bool of prop prop3 has been changed\n"
181             "Type of prop prop4 has been changed\n"
182             "Scope of prop prop4 has become more restrictive\n"
183             "Underlying property of prop prop4 has been changed\n");
184 }
185