1 /*
2  * Copyright (C) 2021 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 #define LOG_TAG "VtsAidlHalStatsTest"
18 
19 #include <aidl/Gtest.h>
20 #include <aidl/Vintf.h>
21 
22 #include <aidl/android/frameworks/stats/IStats.h>
23 #include <android/binder_manager.h>
24 #include <android/log.h>
25 
26 using aidl::android::frameworks::stats::IStats;
27 using aidl::android::frameworks::stats::VendorAtom;
28 using aidl::android::frameworks::stats::VendorAtomValue;
29 
30 class StatsAidlTest : public ::testing::TestWithParam<std::string> {
31    public:
SetUp()32     virtual void SetUp() override {
33         ndk::SpAIBinder binder(AServiceManager_getService(GetParam().c_str()));
34         client = IStats::fromBinder(binder);
35         ASSERT_NE(client, nullptr);
36     }
37 
TearDown()38     virtual void TearDown() override {}
39 
40     std::shared_ptr<IStats> client;
41 };
42 
43 // Validate IStats::reportVendorAtom.
TEST_P(StatsAidlTest,reportVendorAtom)44 TEST_P(StatsAidlTest, reportVendorAtom) {
45     std::vector<VendorAtomValue> values;
46     VendorAtomValue tmp;
47     tmp.set<VendorAtomValue::longValue>(70000);
48     values.push_back(tmp);
49     tmp.set<VendorAtomValue::intValue>(7);
50     values.push_back(tmp);
51     tmp.set<VendorAtomValue::floatValue>(8.5);
52     values.push_back(tmp);
53     tmp.set<VendorAtomValue::stringValue>("test");
54     values.push_back(tmp);
55     tmp.set<VendorAtomValue::intValue>(3);
56     values.push_back(tmp);
57     tmp.set<VendorAtomValue::boolValue>(true);
58     values.push_back(tmp);
59     tmp.set<VendorAtomValue::boolValue>(false);
60     values.push_back(tmp);
61     std::vector<int> emptyRepeatedIntValue = {};
62     tmp.set<VendorAtomValue::repeatedIntValue>(emptyRepeatedIntValue);
63     values.push_back(tmp);
64     std::vector<int> repeatedIntValue = {3, 1, 2};
65     tmp.set<VendorAtomValue::repeatedIntValue>(repeatedIntValue);
66     values.push_back(tmp);
67     std::vector<int64_t> repeatedLongValue = {500000, 430000, 1000001};
68     tmp.set<VendorAtomValue::repeatedLongValue>(repeatedLongValue);
69     values.push_back(tmp);
70     std::vector<float> repeatedFloatValue = {1.5, 2.3, 7.9};
71     tmp.set<VendorAtomValue::repeatedFloatValue>(repeatedFloatValue);
72     values.push_back(tmp);
73     std::vector<std::optional<std::string>> repeatedStringValue = {"str1", "str2", "str3"};
74     tmp.set<VendorAtomValue::repeatedStringValue>(repeatedStringValue);
75     values.push_back(tmp);
76     std::vector<bool> repeatedBoolValue = {true, false, true};
77     tmp.set<VendorAtomValue::repeatedBoolValue>(repeatedBoolValue);
78     values.push_back(tmp);
79     std::vector<uint8_t> byteArrayValue = {21, 50, 3, 10};
80     tmp.set<VendorAtomValue::byteArrayValue>(byteArrayValue);
81     values.push_back(tmp);
82 
83     VendorAtom atom = {.reverseDomainName = "", .atomId = 104999, .values = values};
84     const ndk::ScopedAStatus ret = client->reportVendorAtom(atom);
85 
86     ASSERT_TRUE(ret.isOk());
87 }
88 
89 // Validate IStats::reportVendorAtom - this is a negative test - the error is dumped to logcat
90 // Due to the AIDL reportVendorAtom is oneway API - the return code is not returned to the client
TEST_P(StatsAidlTest,reportVendorAtomInvalidAtomIdLow)91 TEST_P(StatsAidlTest, reportVendorAtomInvalidAtomIdLow) {
92     std::vector<VendorAtomValue> values;
93     VendorAtomValue tmp;
94     tmp.set<VendorAtomValue::longValue>(70000);
95     values.push_back(tmp);
96     tmp.set<VendorAtomValue::intValue>(7);
97     values.push_back(tmp);
98     tmp.set<VendorAtomValue::floatValue>(8.5);
99     values.push_back(tmp);
100     tmp.set<VendorAtomValue::stringValue>("test");
101     values.push_back(tmp);
102     tmp.set<VendorAtomValue::intValue>(3);
103     values.push_back(tmp);
104     VendorAtom atom = {.reverseDomainName = "", .atomId = 1000, .values = values};
105     const ndk::ScopedAStatus ret = client->reportVendorAtom(atom);
106 
107     ASSERT_TRUE(ret.isOk());
108 }
109 
110 // Validate IStats::reportVendorAtom - this is a negative test - the error is dumped to logcat
111 // Due to the AIDL reportVendorAtom is oneway API - the return code is not returned to the client
TEST_P(StatsAidlTest,reportVendorAtomInvalidAtomIdHigh)112 TEST_P(StatsAidlTest, reportVendorAtomInvalidAtomIdHigh) {
113     std::vector<VendorAtomValue> values;
114     VendorAtomValue tmp;
115     tmp.set<VendorAtomValue::longValue>(70000);
116     values.push_back(tmp);
117     tmp.set<VendorAtomValue::intValue>(7);
118     values.push_back(tmp);
119     tmp.set<VendorAtomValue::floatValue>(8.5);
120     values.push_back(tmp);
121     tmp.set<VendorAtomValue::stringValue>("test");
122     values.push_back(tmp);
123     tmp.set<VendorAtomValue::intValue>(3);
124     values.push_back(tmp);
125     VendorAtom atom = {.reverseDomainName = "", .atomId = 300001, .values = values};
126     const ndk::ScopedAStatus ret = client->reportVendorAtom(atom);
127 
128     ASSERT_TRUE(ret.isOk());
129 }
130 
131 // Validate IStats::reportVendorAtom - this is a negative test - the error is dumped to logcat
132 // Due to the AIDL reportVendorAtom is oneway API - the return code is not returned to the client
TEST_P(StatsAidlTest,reportVendorAtomInvalidDomainNameTooLong)133 TEST_P(StatsAidlTest, reportVendorAtomInvalidDomainNameTooLong) {
134     std::vector<VendorAtomValue> values;
135     VendorAtomValue tmp;
136     tmp.set<VendorAtomValue::longValue>(70000);
137     values.push_back(tmp);
138     tmp.set<VendorAtomValue::intValue>(7);
139     values.push_back(tmp);
140     tmp.set<VendorAtomValue::floatValue>(8.5);
141     values.push_back(tmp);
142     tmp.set<VendorAtomValue::stringValue>("test");
143     values.push_back(tmp);
144     tmp.set<VendorAtomValue::intValue>(3);
145     values.push_back(tmp);
146     VendorAtom atom = {.reverseDomainName = "",
147                        .atomId = 100001,
148                        .values = values};
149     const ndk::ScopedAStatus ret = client->reportVendorAtom(atom);
150 
151     ASSERT_TRUE(ret.isOk());
152 }
153 
154 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(StatsAidlTest);
155 INSTANTIATE_TEST_SUITE_P(PerInstance, StatsAidlTest,
156                          testing::ValuesIn(android::getAidlHalInstanceNames(IStats::descriptor)),
157                          android::PrintInstanceNameToString);
158