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 <gtest/gtest.h>
18 #include <statslog.h>
19 
20 namespace android {
21 namespace stats_log_api_gen {
22 
23 /**
24  * Tests native auto generated code for specific atom contains proper ids
25  */
TEST(ApiGenAtomTest,AtomIdConstantsTest)26 TEST(ApiGenAtomTest, AtomIdConstantsTest) {
27     // For reference from the atoms.proto
28     // BleScanStateChanged ble_scan_state_changed = 2
29     //         [(module) = "bluetooth", (module) = "statsdtest"];
30     // ProcessStateChanged process_state_changed = 3 [(module) = "framework", deprecated = true];
31     EXPECT_EQ(android::util::BLE_SCAN_STATE_CHANGED, 2);
32     EXPECT_EQ(android::util::PROCESS_STATE_CHANGED, 3);
33     EXPECT_EQ(android::util::BOOT_SEQUENCE_REPORTED, 57);
34 }
35 
36 /**
37  * Tests native auto generated code for specific atom contains proper enums
38  */
TEST(ApiGenAtomTest,AtomEnumsConstantsTest)39 TEST(ApiGenAtomTest, AtomEnumsConstantsTest) {
40     // For reference from the atoms.proto
41     // message BleScanStateChanged {
42     //     repeated AttributionNode attribution_node = 1
43     //             [(state_field_option).primary_field_first_uid = true];
44 
45     //     enum State {
46     //         OFF = 0;
47     //         ON = 1;
48     //         // RESET indicates all ble stopped. Used when it (re)starts (e.g. after it crashes).
49     //         RESET = 2;
50     //     }
51 
52     EXPECT_EQ(android::util::BLE_SCAN_STATE_CHANGED__STATE__OFF, 0);
53     EXPECT_EQ(android::util::BLE_SCAN_STATE_CHANGED__STATE__ON, 1);
54     EXPECT_EQ(android::util::BLE_SCAN_STATE_CHANGED__STATE__RESET, 2);
55 }
56 
57 /**
58  * Tests complete native auto generated code for specific atom TestAtomReported
59  */
TEST(ApiGenAtomTest,TestAtomReportedApiTest)60 TEST(ApiGenAtomTest, TestAtomReportedApiTest) {
61     // For reference from the atoms.proto
62     // message TestAtomReported {
63     //     repeated AttributionNode attribution_node = 1;
64     //     optional int32 int_field = 2;
65     //     optional int64 long_field = 3;
66     //     optional float float_field = 4;
67     //     optional string string_field = 5;
68     //     optional bool boolean_field = 6;
69     //     enum State {
70     //         UNKNOWN = 0;
71     //         OFF = 1;
72     //         ON = 2;
73     //     }
74     //     optional State state = 7;
75     //     optional TrainExperimentIds bytes_field = 8 [(android.os.statsd.log_mode) = MODE_BYTES];
76     //     repeated int32 repeated_int_field = 9;
77     //     repeated int64 repeated_long_field = 10;
78     //     repeated float repeated_float_field = 11;
79     //     repeated string repeated_string_field = 12;
80     //     repeated bool repeated_boolean_field = 13;
81     //     repeated State repeated_enum_field = 14;
82     // }
83     EXPECT_EQ(android::util::TEST_ATOM_REPORTED, 205);
84 
85     EXPECT_EQ(android::util::TEST_ATOM_REPORTED__STATE__UNKNOWN, 0);
86     EXPECT_EQ(android::util::TEST_ATOM_REPORTED__STATE__OFF, 1);
87     EXPECT_EQ(android::util::TEST_ATOM_REPORTED__STATE__ON, 2);
88 
89     typedef int (*WriteApi)(int32_t code, const int32_t* uid, size_t uid_length,
90                             const std::vector<char const*>& tag, int32_t arg2, int64_t arg3,
91                             float arg4, char const* arg5, bool arg6, int32_t arg7,
92                             const android::util::BytesField& arg8, const std::vector<int32_t>& arg9,
93                             const std::vector<int64_t>& arg10, const std::vector<float>& arg11,
94                             const std::vector<char const*>& arg12, const bool* arg13,
95                             size_t arg13_length, const std::vector<int32_t>& arg14);
96 
97     WriteApi atomWriteApi = &android::util::stats_write;
98 
99     EXPECT_NE(atomWriteApi, nullptr);
100 }
101 
102 }  // namespace stats_log_api_gen
103 }  // namespace android
104