1 /*
2  * Copyright (C) 2022, 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 <aidl/android/frameworks/stats/VendorAtom.h>
18 #include <gtest/gtest.h>
19 #include <test_vendor_atoms.h>
20 
21 #include <limits>
22 
23 #include "frameworks/proto_logging/stats/stats_log_api_gen/test_vendor_atoms.pb.h"
24 
25 namespace android {
26 namespace api_gen_vendor_tests {
27 
28 using namespace android::VendorAtoms;
29 using namespace aidl::android::frameworks::stats;
30 
31 using std::string;
32 using std::vector;
33 
34 namespace {
35 
36 static const int32_t kTestIntValue = 100;
37 static const int32_t kTestUidValue = 1000;
38 static const int32_t kTestPidValue = 3000;
39 static const int64_t kTestLongValue = std::numeric_limits<int64_t>::max() - kTestIntValue;
40 static const float kTestFloatValue = (float)kTestIntValue / kTestLongValue;
41 static const bool kTestBoolValue = true;
42 static const char* kTestStringValue = "test_string";
43 static const char* kTestStringValue2 = "test_string2";
44 
45 }  // namespace
46 
47 /**
48  * Tests native auto generated code for specific vendor atom contains proper ids
49  */
TEST(ApiGenVendorAtomTest,AtomIdConstantsTest)50 TEST(ApiGenVendorAtomTest, AtomIdConstantsTest) {
51     EXPECT_EQ(VENDOR_ATOM1, 105501);
52     EXPECT_EQ(VENDOR_ATOM2, 105502);
53     EXPECT_EQ(VENDOR_ATOM4, 105504);
54 }
55 
56 /**
57  * Tests native auto generated code for specific vendor atom contains proper enums
58  */
TEST(ApiGenVendorAtomTest,AtomEnumTest)59 TEST(ApiGenVendorAtomTest, AtomEnumTest) {
60     EXPECT_EQ(VendorAtom1::TYPE_UNKNOWN, 0);
61     EXPECT_EQ(VendorAtom1::TYPE_1, 1);
62     EXPECT_EQ(VendorAtom1::TYPE_2, 2);
63     EXPECT_EQ(VendorAtom1::TYPE_3, 3);
64 
65     EXPECT_EQ(VendorAtom1::ANOTHER_TYPE_UNKNOWN, 0);
66     EXPECT_EQ(VendorAtom1::ANOTHER_TYPE_1, 1);
67     EXPECT_EQ(VendorAtom1::ANOTHER_TYPE_2, 2);
68     EXPECT_EQ(VendorAtom1::ANOTHER_TYPE_3, 3);
69 
70     EXPECT_EQ(VendorAtom2::TYPE_UNKNOWN, 0);
71     EXPECT_EQ(VendorAtom2::TYPE_1, 1);
72     EXPECT_EQ(VendorAtom2::TYPE_2, 2);
73     EXPECT_EQ(VendorAtom2::TYPE_3, 3);
74 
75     EXPECT_EQ(VendorAtom2::ANOTHER_TYPE_UNKNOWN, 0);
76     EXPECT_EQ(VendorAtom2::ANOTHER_TYPE_1, 1);
77     EXPECT_EQ(VendorAtom2::ANOTHER_TYPE_2, 2);
78     EXPECT_EQ(VendorAtom2::ANOTHER_TYPE_3, 3);
79 
80     EXPECT_EQ(VendorAtom4::TYPE_UNKNOWN, 0);
81     EXPECT_EQ(VendorAtom4::TYPE_1, 1);
82 
83     typedef void (*Atom1FuncWithEnum)(VendorAtom1::EnumType arg);
84     typedef void (*Atom1FuncWithEnum2)(VendorAtom1::EnumType2 arg);
85     typedef void (*Atom2FuncWithEnum)(VendorAtom2::EnumType arg);
86     typedef void (*Atom2FuncWithEnum2)(VendorAtom2::EnumType2 arg);
87 
88     Atom1FuncWithEnum f1 = nullptr;
89     Atom1FuncWithEnum2 f2 = nullptr;
90     Atom2FuncWithEnum f3 = nullptr;
91     Atom2FuncWithEnum2 f4 = nullptr;
92 
93     EXPECT_EQ(f1, nullptr);
94     EXPECT_EQ(f2, nullptr);
95     EXPECT_EQ(f3, nullptr);
96     EXPECT_EQ(f4, nullptr);
97 }
98 
TEST(ApiGenVendorAtomTest,buildVendorAtom1ApiTest)99 TEST(ApiGenVendorAtomTest, buildVendorAtom1ApiTest) {
100     typedef VendorAtom (*VendorAtom1BuildFunc)(
101             int32_t code, char const* reverse_domain_name, int32_t enumField1, int32_t enumField2,
102             int32_t int_value32, int64_t int_value64, float float_value, bool bool_value,
103             int32_t enumField3, int32_t enumField4);
104     VendorAtom1BuildFunc func = &createVendorAtom;
105 
106     EXPECT_NE(func, nullptr);
107 
108     VendorAtom atom = func(VENDOR_ATOM1, kTestStringValue, VendorAtom1::TYPE_1, VendorAtom1::TYPE_2,
109                            kTestIntValue, kTestLongValue, kTestFloatValue, kTestBoolValue,
110                            VendorAtom1::ANOTHER_TYPE_2, VendorAtom1::ANOTHER_TYPE_3);
111 
112     EXPECT_EQ(atom.atomId, VENDOR_ATOM1);
113     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
114     EXPECT_EQ(atom.values.size(), static_cast<size_t>(8));
115     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), VendorAtom1::TYPE_1);
116     EXPECT_EQ(atom.values[1].get<VendorAtomValue::intValue>(), VendorAtom1::TYPE_2);
117     EXPECT_EQ(atom.values[2].get<VendorAtomValue::intValue>(), kTestIntValue);
118     EXPECT_EQ(atom.values[3].get<VendorAtomValue::longValue>(), kTestLongValue);
119     EXPECT_EQ(atom.values[4].get<VendorAtomValue::floatValue>(), kTestFloatValue);
120     EXPECT_EQ(atom.values[5].get<VendorAtomValue::boolValue>(), kTestBoolValue);
121     EXPECT_EQ(atom.values[6].get<VendorAtomValue::intValue>(), VendorAtom1::ANOTHER_TYPE_2);
122     EXPECT_EQ(atom.values[7].get<VendorAtomValue::intValue>(), VendorAtom1::ANOTHER_TYPE_3);
123     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
124 }
125 
TEST(ApiGenVendorAtomTest,buildVendorAtom3ApiTest)126 TEST(ApiGenVendorAtomTest, buildVendorAtom3ApiTest) {
127     typedef VendorAtom (*VendorAtom3BuildFunc)(int32_t code, char const* arg1, int32_t arg2);
128     VendorAtom3BuildFunc func = &createVendorAtom;
129 
130     EXPECT_NE(func, nullptr);
131 
132     VendorAtom atom = func(VENDOR_ATOM3, kTestStringValue, kTestIntValue);
133 
134     EXPECT_EQ(atom.atomId, VENDOR_ATOM3);
135     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
136     EXPECT_EQ(atom.values.size(), static_cast<size_t>(1));
137     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), kTestIntValue);
138     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
139 }
140 
TEST(ApiGenVendorAtomTest,buildVendorAtom4ApiTest)141 TEST(ApiGenVendorAtomTest, buildVendorAtom4ApiTest) {
142     typedef VendorAtom (*VendorAtom4BuildFunc)(
143             int32_t code, char const* arg1, float arg2, int32_t arg3, int64_t arg4, bool arg5,
144             int32_t arg6, const vector<bool>& arg7, const vector<float>& arg8,
145             const vector<int32_t>& arg9, const vector<int64_t>& arg10,
146             const vector<char const*>& arg11, const vector<int32_t>& arg12);
147     VendorAtom4BuildFunc func = &createVendorAtom;
148 
149     EXPECT_NE(func, nullptr);
150 
151     const vector<bool> repeatedBool{true, false, true};
152     const vector<float> repeatedFloat{kTestFloatValue, kTestFloatValue + 1.f,
153                                       kTestFloatValue + 2.f};
154     const vector<int32_t> repeatedInt{kTestIntValue, kTestIntValue + 1, kTestIntValue + 2};
155     const vector<int64_t> repeatedLong{kTestLongValue, kTestLongValue + 1, kTestLongValue + 2};
156     const vector<const char*> repeatedString{kTestStringValue, kTestStringValue2, kTestStringValue};
157     const vector<int32_t> repeatedEnum{VendorAtom4::TYPE_1, VendorAtom4::TYPE_UNKNOWN,
158                                        VendorAtom4::TYPE_1};
159 
160     VendorAtom atom = func(VENDOR_ATOM4, kTestStringValue, kTestFloatValue, kTestIntValue,
161                            kTestLongValue, kTestBoolValue, VendorAtom4::TYPE_1, repeatedBool,
162                            repeatedFloat, repeatedInt, repeatedLong, repeatedString, repeatedEnum);
163 
164     EXPECT_EQ(atom.atomId, VENDOR_ATOM4);
165     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
166     EXPECT_EQ(atom.values.size(), static_cast<size_t>(11));
167     EXPECT_EQ(atom.values[0].get<VendorAtomValue::floatValue>(), kTestFloatValue);
168     EXPECT_EQ(atom.values[1].get<VendorAtomValue::intValue>(), kTestIntValue);
169     EXPECT_EQ(atom.values[2].get<VendorAtomValue::longValue>(), kTestLongValue);
170     EXPECT_EQ(atom.values[3].get<VendorAtomValue::boolValue>(), kTestBoolValue);
171     EXPECT_EQ(atom.values[4].get<VendorAtomValue::intValue>(), VendorAtom4::TYPE_1);
172 
173     EXPECT_EQ(atom.values[5].get<VendorAtomValue::repeatedBoolValue>(), repeatedBool);
174     EXPECT_EQ(atom.values[6].get<VendorAtomValue::repeatedFloatValue>(), repeatedFloat);
175     EXPECT_EQ(atom.values[7].get<VendorAtomValue::repeatedIntValue>(), repeatedInt);
176     EXPECT_EQ(atom.values[8].get<VendorAtomValue::repeatedLongValue>(), repeatedLong);
177     EXPECT_TRUE(atom.values[9].get<VendorAtomValue::repeatedStringValue>().has_value());
178     EXPECT_EQ(atom.values[9].get<VendorAtomValue::repeatedStringValue>()->size(),
179               repeatedString.size());
180     const auto& repeatedStringValue = *atom.values[9].get<VendorAtomValue::repeatedStringValue>();
181     for (size_t i = 0; i < repeatedString.size(); i++) {
182         EXPECT_EQ(repeatedString[i], *repeatedStringValue[i]);
183     }
184     EXPECT_EQ(atom.values[10].get<VendorAtomValue::repeatedIntValue>(), repeatedEnum);
185     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
186 }
187 
TEST(ApiGenVendorAtomTest,buildVendorAtom5ApiTest)188 TEST(ApiGenVendorAtomTest, buildVendorAtom5ApiTest) {
189     typedef VendorAtom (*VendorAtom5BuildFunc)(int32_t code, char const* arg1, float arg2,
190                                                int32_t arg3, int64_t arg4,
191                                                const vector<uint8_t>& arg5);
192     VendorAtom5BuildFunc func = &createVendorAtom;
193 
194     EXPECT_NE(func, nullptr);
195 
196     ::android::stats_log_api_gen::TestNestedMessage nestedMessage;
197     nestedMessage.set_float_field(kTestFloatValue);
198     nestedMessage.set_int_field(kTestIntValue);
199     nestedMessage.set_long_field(kTestLongValue);
200 
201     string nestedMessageString;
202     nestedMessage.SerializeToString(&nestedMessageString);
203 
204     vector<uint8_t> nestedMessageBytes(nestedMessageString.begin(), nestedMessageString.end());
205 
206     VendorAtom atom = func(VENDOR_ATOM5, kTestStringValue, kTestFloatValue, kTestIntValue,
207                            kTestLongValue, nestedMessageBytes);
208 
209     EXPECT_EQ(atom.atomId, VENDOR_ATOM5);
210     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
211     EXPECT_EQ(atom.values.size(), static_cast<size_t>(4));
212     EXPECT_EQ(atom.values[0].get<VendorAtomValue::floatValue>(), kTestFloatValue);
213     EXPECT_EQ(atom.values[1].get<VendorAtomValue::intValue>(), kTestIntValue);
214     EXPECT_EQ(atom.values[2].get<VendorAtomValue::longValue>(), kTestLongValue);
215     EXPECT_EQ(atom.values[3].get<VendorAtomValue::byteArrayValue>(), nestedMessageBytes);
216     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
217 
218     string nestedMessageStringResult(atom.values[3].get<VendorAtomValue::byteArrayValue>()->begin(),
219                                      atom.values[3].get<VendorAtomValue::byteArrayValue>()->end());
220     EXPECT_EQ(nestedMessageStringResult, nestedMessageString);
221 
222     ::android::stats_log_api_gen::TestNestedMessage nestedMessageResult;
223     nestedMessageResult.ParseFromString(nestedMessageStringResult);
224     EXPECT_EQ(nestedMessageResult.float_field(), kTestFloatValue);
225     EXPECT_EQ(nestedMessageResult.int_field(), kTestIntValue);
226     EXPECT_EQ(nestedMessageResult.long_field(), kTestLongValue);
227 }
228 
TEST(ApiGenVendorAtomTest,buildAtomWithTruncateTimestampTest)229 TEST(ApiGenVendorAtomTest, buildAtomWithTruncateTimestampTest) {
230     /**
231      * Expected signature equal to VendorAtomWithTrancateTimestampCreateFunc to log
232      * 3 different atoms with truncate_timestamp
233      *      VendorAtomWithTruncateTimestamp truncateTimestampAtom1 = 105510 [
234      *          (android.os.statsd.truncate_timestamp) = true
235      *      ];
236      *      VendorAtomWithTruncateTimestamp2 truncateTimestampAtom2 = 105511 [
237      *          (android.os.statsd.truncate_timestamp) = true
238      *      ];
239      *      VendorAtomWithTruncateTimestamp3 truncateTimestampAtom3 = 105512 [
240      *          (android.os.statsd.truncate_timestamp) = true
241      *      ];
242      *
243      */
244     typedef VendorAtom (*VendorAtomWithTrancateTimestampCreateFunc)(
245             int32_t code, char const* reverse_domain_name, int32_t state);
246     VendorAtomWithTrancateTimestampCreateFunc func = &createVendorAtom;
247 
248     EXPECT_NE(func, nullptr);
249 
250     VendorAtom atom1 = func(TRUNCATE_TIMESTAMP_ATOM1, kTestStringValue,
251                             VendorAtomWithTruncateTimestamp::TEST_STATE_1);
252     EXPECT_EQ(atom1.atomId, TRUNCATE_TIMESTAMP_ATOM1);
253     EXPECT_EQ(atom1.reverseDomainName, kTestStringValue);
254     EXPECT_EQ(atom1.values.size(), static_cast<size_t>(1));
255     EXPECT_EQ(atom1.values[0].get<VendorAtomValue::intValue>(),
256               VendorAtomWithTruncateTimestamp::TEST_STATE_1);
257     EXPECT_NE(atom1.atomAnnotations, std::nullopt);
258     EXPECT_EQ(atom1.atomAnnotations->size(), static_cast<size_t>(1));
259     EXPECT_NE(atom1.atomAnnotations.value()[0], std::nullopt);
260     EXPECT_EQ(atom1.atomAnnotations.value()[0]->annotationId, AnnotationId::TRUNCATE_TIMESTAMP);
261     EXPECT_TRUE(atom1.atomAnnotations.value()[0]->value.get<AnnotationValue::boolValue>());
262 
263     VendorAtom atom2 = func(TRUNCATE_TIMESTAMP_ATOM2, kTestStringValue,
264                             VendorAtomWithTruncateTimestamp2::TEST_STATE_2);
265     EXPECT_EQ(atom2.atomId, TRUNCATE_TIMESTAMP_ATOM2);
266     EXPECT_EQ(atom2.reverseDomainName, kTestStringValue);
267     EXPECT_EQ(atom2.values.size(), static_cast<size_t>(1));
268     EXPECT_EQ(atom2.values[0].get<VendorAtomValue::intValue>(),
269               VendorAtomWithTruncateTimestamp2::TEST_STATE_2);
270     EXPECT_NE(atom2.atomAnnotations, std::nullopt);
271     EXPECT_EQ(atom2.atomAnnotations->size(), static_cast<size_t>(1));
272     EXPECT_NE(atom2.atomAnnotations.value()[0], std::nullopt);
273     EXPECT_EQ(atom2.atomAnnotations.value()[0]->annotationId, AnnotationId::TRUNCATE_TIMESTAMP);
274     EXPECT_TRUE(atom2.atomAnnotations.value()[0]->value.get<AnnotationValue::boolValue>());
275 
276     VendorAtom atom3 = func(TRUNCATE_TIMESTAMP_ATOM2, kTestStringValue, kTestIntValue);
277     EXPECT_EQ(atom3.atomId, TRUNCATE_TIMESTAMP_ATOM2);
278     EXPECT_EQ(atom3.reverseDomainName, kTestStringValue);
279     EXPECT_EQ(atom3.values.size(), static_cast<size_t>(1));
280     EXPECT_EQ(atom3.values[0].get<VendorAtomValue::intValue>(), kTestIntValue);
281     EXPECT_NE(atom3.atomAnnotations, std::nullopt);
282     EXPECT_EQ(atom3.atomAnnotations->size(), static_cast<size_t>(1));
283     EXPECT_NE(atom3.atomAnnotations.value()[0], std::nullopt);
284     EXPECT_EQ(atom3.atomAnnotations.value()[0]->annotationId, AnnotationId::TRUNCATE_TIMESTAMP);
285     EXPECT_TRUE(atom3.atomAnnotations.value()[0]->value.get<AnnotationValue::boolValue>());
286 }
287 
TEST(ApiGenVendorAtomTest,buildAtomWithExclusiveStateAnnotationTest)288 TEST(ApiGenVendorAtomTest, buildAtomWithExclusiveStateAnnotationTest) {
289     /* Expected signature equal to VendorAtomWithStateCreateFunc to log
290      * atoms with similar definitions:
291      *      VendorAtomWithState3 stateAtom3 = 105508
292      */
293     typedef VendorAtom (*VendorAtomWithStateCreateFunc)(
294             int32_t code, char const* reverse_domain_name, int32_t state);
295     VendorAtomWithStateCreateFunc func = &createVendorAtom;
296 
297     EXPECT_NE(func, nullptr);
298 
299     VendorAtom atom = func(STATE_ATOM3, kTestStringValue, VendorAtomWithState3::TEST_STATE_3);
300     EXPECT_EQ(atom.atomId, STATE_ATOM3);
301     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
302     EXPECT_EQ(atom.values.size(), static_cast<size_t>(1));
303     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), VendorAtomWithState3::TEST_STATE_3);
304     EXPECT_NE(atom.valuesAnnotations, std::nullopt);
305     EXPECT_EQ(atom.valuesAnnotations->size(), static_cast<size_t>(1));
306     EXPECT_NE(atom.valuesAnnotations.value()[0], std::nullopt);
307     EXPECT_EQ(atom.valuesAnnotations.value()[0]->valueIndex, 0);
308     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations.size(), static_cast<size_t>(1));
309     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[0].annotationId,
310               AnnotationId::EXCLUSIVE_STATE);
311     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
312                         ->annotations[0]
313                         .value.get<AnnotationValue::boolValue>());
314 
315     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
316 }
317 
TEST(ApiGenVendorAtomTest,buildAtomWithExclusiveStateAndPrimaryFieldAnnotationTest)318 TEST(ApiGenVendorAtomTest, buildAtomWithExclusiveStateAndPrimaryFieldAnnotationTest) {
319     /**
320      * Expected signature equal to VendorAtomWithStateCreateFunc to log atom
321      *      VendorAtomWithState stateAtom1 = 105506
322      * which has 1 primary_field & 1 exclusive_state annotations associated with 2 fields
323      */
324     typedef VendorAtom (*VendorAtomWithStateCreateFunc)(
325             int32_t code, char const* reverse_domain_name, int32_t uid, int32_t state);
326     VendorAtomWithStateCreateFunc func = &createVendorAtom;
327 
328     EXPECT_NE(func, nullptr);
329 
330     VendorAtom atom =
331             func(STATE_ATOM1, kTestStringValue, kTestUidValue, VendorAtomWithState::TEST_STATE_3);
332     EXPECT_EQ(atom.atomId, STATE_ATOM1);
333     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
334     EXPECT_EQ(atom.values.size(), static_cast<size_t>(2));
335     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), kTestUidValue);
336     EXPECT_EQ(atom.values[1].get<VendorAtomValue::intValue>(), VendorAtomWithState::TEST_STATE_3);
337     EXPECT_NE(atom.valuesAnnotations, std::nullopt);
338     EXPECT_EQ(atom.valuesAnnotations->size(), static_cast<size_t>(2));
339     EXPECT_NE(atom.valuesAnnotations.value()[0], std::nullopt);
340     EXPECT_EQ(atom.valuesAnnotations.value()[0]->valueIndex, 0);
341     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations.size(), static_cast<size_t>(1));
342     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[0].annotationId,
343               AnnotationId::PRIMARY_FIELD);
344     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
345                         ->annotations[0]
346                         .value.get<AnnotationValue::boolValue>());
347     EXPECT_NE(atom.valuesAnnotations.value()[1], std::nullopt);
348     EXPECT_EQ(atom.valuesAnnotations.value()[1]->valueIndex, 1);
349     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations.size(), static_cast<size_t>(1));
350     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations[0].annotationId,
351               AnnotationId::EXCLUSIVE_STATE);
352     EXPECT_TRUE(atom.valuesAnnotations.value()[1]
353                         ->annotations[0]
354                         .value.get<AnnotationValue::boolValue>());
355 
356     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
357 }
358 
TEST(ApiGenVendorAtomTest,buildAtomWithExclusiveStateAndTwoPrimaryFieldAnnotationTest)359 TEST(ApiGenVendorAtomTest, buildAtomWithExclusiveStateAndTwoPrimaryFieldAnnotationTest) {
360     /**
361      * Expected signature equal to VendorAtomWithStateCreateFunc to log atom
362      *      VendorAtomWithState2 stateAtom2 = 105507
363      * which has 2 primary_field & 1 exclusive_state annotations associated with 3 fields
364      */
365     typedef VendorAtom (*VendorAtomWithStateCreateFunc)(
366             int32_t code, char const* reverse_domain_name, int32_t uid, int32_t pid, int32_t state);
367     VendorAtomWithStateCreateFunc func = &createVendorAtom;
368 
369     EXPECT_NE(func, nullptr);
370 
371     VendorAtom atom = func(STATE_ATOM2, kTestStringValue, kTestUidValue, kTestPidValue,
372                            VendorAtomWithState2::TEST_STATE_2);
373     EXPECT_EQ(atom.atomId, STATE_ATOM2);
374     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
375     EXPECT_EQ(atom.values.size(), static_cast<size_t>(3));
376     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), kTestUidValue);
377     EXPECT_EQ(atom.values[1].get<VendorAtomValue::intValue>(), kTestPidValue);
378     EXPECT_EQ(atom.values[2].get<VendorAtomValue::intValue>(), VendorAtomWithState2::TEST_STATE_2);
379     EXPECT_NE(atom.valuesAnnotations, std::nullopt);
380     EXPECT_EQ(atom.valuesAnnotations->size(), static_cast<size_t>(3));
381     EXPECT_NE(atom.valuesAnnotations.value()[0], std::nullopt);
382     EXPECT_EQ(atom.valuesAnnotations.value()[0]->valueIndex, 0);
383     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations.size(), static_cast<size_t>(1));
384     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[0].annotationId,
385               AnnotationId::PRIMARY_FIELD);
386     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
387                         ->annotations[0]
388                         .value.get<AnnotationValue::boolValue>());
389     EXPECT_NE(atom.valuesAnnotations.value()[1], std::nullopt);
390     EXPECT_EQ(atom.valuesAnnotations.value()[1]->valueIndex, 1);
391     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations.size(), static_cast<size_t>(1));
392     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations[0].annotationId,
393               AnnotationId::PRIMARY_FIELD);
394     EXPECT_TRUE(atom.valuesAnnotations.value()[1]
395                         ->annotations[0]
396                         .value.get<AnnotationValue::boolValue>());
397     EXPECT_NE(atom.valuesAnnotations.value()[2], std::nullopt);
398     EXPECT_EQ(atom.valuesAnnotations.value()[2]->valueIndex, 2);
399     EXPECT_EQ(atom.valuesAnnotations.value()[2]->annotations.size(), static_cast<size_t>(1));
400     EXPECT_EQ(atom.valuesAnnotations.value()[2]->annotations[0].annotationId,
401               AnnotationId::EXCLUSIVE_STATE);
402     EXPECT_TRUE(atom.valuesAnnotations.value()[2]
403                         ->annotations[0]
404                         .value.get<AnnotationValue::boolValue>());
405 
406     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
407 }
408 
TEST(ApiGenVendorAtomTest,buildAtomWithMultipleAnnotationsPerValueTest)409 TEST(ApiGenVendorAtomTest, buildAtomWithMultipleAnnotationsPerValueTest) {
410     /**
411      * Expected signature equal to VendorAtomWithStateCreateFunc to log atom
412      *      VendorAtomWithState4 stateAtom4 = 105509
413      * which has 4 state annotations associated with single value field (index 0)
414      * testing that TRIGGER_STATE_RESET not be added
415      */
416     typedef VendorAtom (*VendorAtomWithStateCreateFunc)(
417             int32_t code, char const* reverse_domain_name, int32_t state, bool someFlag);
418     VendorAtomWithStateCreateFunc func = &createVendorAtom;
419 
420     EXPECT_NE(func, nullptr);
421 
422     VendorAtom atom = func(STATE_ATOM4, kTestStringValue, VendorAtomWithState4::ON, kTestBoolValue);
423     EXPECT_EQ(atom.atomId, STATE_ATOM4);
424     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
425     EXPECT_EQ(atom.values.size(), static_cast<size_t>(2));
426     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), VendorAtomWithState4::ON);
427     EXPECT_EQ(atom.values[1].get<VendorAtomValue::boolValue>(), kTestBoolValue);
428     EXPECT_NE(atom.valuesAnnotations, std::nullopt);
429     EXPECT_EQ(atom.valuesAnnotations->size(), static_cast<size_t>(2));
430     EXPECT_NE(atom.valuesAnnotations.value()[0], std::nullopt);
431     EXPECT_EQ(atom.valuesAnnotations.value()[0]->valueIndex, 0);
432     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations.size(), static_cast<size_t>(2));
433     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[0].annotationId,
434               AnnotationId::EXCLUSIVE_STATE);
435     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
436                         ->annotations[0]
437                         .value.get<AnnotationValue::boolValue>());
438     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[1].annotationId,
439               AnnotationId::STATE_NESTED);
440     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
441                         ->annotations[1]
442                         .value.get<AnnotationValue::boolValue>());
443     EXPECT_NE(atom.valuesAnnotations.value()[1], std::nullopt);
444     EXPECT_EQ(atom.valuesAnnotations.value()[1]->valueIndex, 1);
445     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations.size(), static_cast<size_t>(1));
446     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations[0].annotationId,
447               AnnotationId::PRIMARY_FIELD);
448     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
449 }
450 
TEST(ApiGenVendorAtomTest,buildAtomWithTriggerResetAnnotationTest)451 TEST(ApiGenVendorAtomTest, buildAtomWithTriggerResetAnnotationTest) {
452     /**
453      * Expected signature equal to CreateStateAtom4ApiWrapper to log atom
454      *      VendorAtomWithState4 stateAtom4 = 105509
455      * which has 4 state annotations associated with single value field (index 0)
456      * testing that TRIGGER_STATE_RESET will be added
457      */
458     typedef VendorAtom (*VendorAtomWithStateCreateFunc)(
459             int32_t code, char const* reverse_domain_name, int32_t state, bool someFlag);
460     VendorAtomWithStateCreateFunc func = &createVendorAtom;
461 
462     EXPECT_NE(func, nullptr);
463 
464     const int kDefaultStateValue = VendorAtomWithState4::OFF;
465 
466     VendorAtom atom =
467             func(STATE_ATOM4, kTestStringValue, VendorAtomWithState4::RESET, kTestBoolValue);
468     EXPECT_EQ(atom.atomId, STATE_ATOM4);
469     EXPECT_EQ(atom.reverseDomainName, kTestStringValue);
470     EXPECT_EQ(atom.values.size(), static_cast<size_t>(2));
471     EXPECT_EQ(atom.values[0].get<VendorAtomValue::intValue>(), VendorAtomWithState4::RESET);
472     EXPECT_EQ(atom.values[1].get<VendorAtomValue::boolValue>(), kTestBoolValue);
473     EXPECT_NE(atom.valuesAnnotations, std::nullopt);
474     EXPECT_EQ(atom.valuesAnnotations->size(), static_cast<size_t>(2));
475     EXPECT_NE(atom.valuesAnnotations.value()[0], std::nullopt);
476     EXPECT_EQ(atom.valuesAnnotations.value()[0]->valueIndex, 0);
477     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations.size(), static_cast<size_t>(3));
478     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[0].annotationId,
479               AnnotationId::EXCLUSIVE_STATE);
480     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
481                         ->annotations[0]
482                         .value.get<AnnotationValue::boolValue>());
483     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[1].annotationId,
484               AnnotationId::STATE_NESTED);
485     EXPECT_TRUE(atom.valuesAnnotations.value()[0]
486                         ->annotations[1]
487                         .value.get<AnnotationValue::boolValue>());
488     EXPECT_EQ(atom.valuesAnnotations.value()[0]->annotations[2].annotationId,
489               AnnotationId::TRIGGER_STATE_RESET);
490     EXPECT_EQ(atom.valuesAnnotations.value()[0]
491                       ->annotations[2]
492                       .value.get<AnnotationValue::intValue>(),
493               kDefaultStateValue);
494     EXPECT_NE(atom.valuesAnnotations.value()[1], std::nullopt);
495     EXPECT_EQ(atom.valuesAnnotations.value()[1]->valueIndex, 1);
496     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations.size(), static_cast<size_t>(1));
497     EXPECT_EQ(atom.valuesAnnotations.value()[1]->annotations[0].annotationId,
498               AnnotationId::PRIMARY_FIELD);
499     EXPECT_EQ(atom.atomAnnotations, std::nullopt);
500 }
501 
502 }  // namespace api_gen_vendor_tests
503 }  // namespace android
504