1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "StatsService.h"
16 #include "config/ConfigKey.h"
17 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
18 
19 #include <android/binder_interface_utils.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include <stdio.h>
24 
25 using namespace android;
26 using namespace testing;
27 
28 namespace android {
29 namespace os {
30 namespace statsd {
31 
32 using android::util::ProtoOutputStream;
33 using ::ndk::SharedRefBase;
34 
35 #ifdef __ANDROID__
36 
TEST(StatsServiceTest,TestAddConfig_simple)37 TEST(StatsServiceTest, TestAddConfig_simple) {
38     shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr);
39     StatsdConfig config;
40     config.set_id(12345);
41     string serialized = config.SerializeAsString();
42 
43     EXPECT_TRUE(
44             service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
45 }
46 
TEST(StatsServiceTest,TestAddConfig_empty)47 TEST(StatsServiceTest, TestAddConfig_empty) {
48     shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr);
49     string serialized = "";
50 
51     EXPECT_TRUE(
52             service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
53 }
54 
TEST(StatsServiceTest,TestAddConfig_invalid)55 TEST(StatsServiceTest, TestAddConfig_invalid) {
56     shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr);
57     string serialized = "Invalid config!";
58 
59     EXPECT_FALSE(
60             service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
61 }
62 
TEST(StatsServiceTest,TestGetUidFromArgs)63 TEST(StatsServiceTest, TestGetUidFromArgs) {
64     Vector<String8> args;
65     args.push(String8("-1"));
66     args.push(String8("0"));
67     args.push(String8("1"));
68     args.push(String8("a1"));
69     args.push(String8(""));
70 
71     int32_t uid;
72 
73     shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr);
74     service->mEngBuild = true;
75 
76     // "-1"
77     EXPECT_FALSE(service->getUidFromArgs(args, 0, uid));
78 
79     // "0"
80     EXPECT_TRUE(service->getUidFromArgs(args, 1, uid));
81     EXPECT_EQ(0, uid);
82 
83     // "1"
84     EXPECT_TRUE(service->getUidFromArgs(args, 2, uid));
85     EXPECT_EQ(1, uid);
86 
87     // "a1"
88     EXPECT_FALSE(service->getUidFromArgs(args, 3, uid));
89 
90     // ""
91     EXPECT_FALSE(service->getUidFromArgs(args, 4, uid));
92 
93     // For a non-userdebug, uid "1" cannot be impersonated.
94     service->mEngBuild = false;
95     EXPECT_FALSE(service->getUidFromArgs(args, 2, uid));
96 }
97 
98 #else
99 GTEST_LOG_(INFO) << "This test does nothing.\n";
100 #endif
101 
102 }  // namespace statsd
103 }  // namespace os
104 }  // namespace android
105