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 <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include <stdio.h>
23
24 using namespace android;
25 using namespace testing;
26
27 namespace android {
28 namespace os {
29 namespace statsd {
30
31 using android::util::ProtoOutputStream;
32
33 #ifdef __ANDROID__
34
TEST(StatsServiceTest,TestAddConfig_simple)35 TEST(StatsServiceTest, TestAddConfig_simple) {
36 StatsService service(nullptr, nullptr);
37 StatsdConfig config;
38 config.set_id(12345);
39 string serialized = config.SerializeAsString();
40
41 EXPECT_TRUE(
42 service.addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
43 }
44
TEST(StatsServiceTest,TestAddConfig_empty)45 TEST(StatsServiceTest, TestAddConfig_empty) {
46 StatsService service(nullptr, nullptr);
47 string serialized = "";
48
49 EXPECT_TRUE(
50 service.addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
51 }
52
TEST(StatsServiceTest,TestAddConfig_invalid)53 TEST(StatsServiceTest, TestAddConfig_invalid) {
54 StatsService service(nullptr, nullptr);
55 string serialized = "Invalid config!";
56
57 EXPECT_FALSE(
58 service.addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()}));
59 }
60
TEST(StatsServiceTest,TestGetUidFromArgs)61 TEST(StatsServiceTest, TestGetUidFromArgs) {
62 Vector<String8> args;
63 args.push(String8("-1"));
64 args.push(String8("0"));
65 args.push(String8("1"));
66 args.push(String8("9999999999999999999999999999999999"));
67 args.push(String8("a1"));
68 args.push(String8(""));
69
70 int32_t uid;
71
72 StatsService service(nullptr, nullptr);
73 service.mEngBuild = true;
74
75 // "-1"
76 EXPECT_FALSE(service.getUidFromArgs(args, 0, uid));
77
78 // "0"
79 EXPECT_TRUE(service.getUidFromArgs(args, 1, uid));
80 EXPECT_EQ(0, uid);
81
82 // "1"
83 EXPECT_TRUE(service.getUidFromArgs(args, 2, uid));
84 EXPECT_EQ(1, uid);
85
86 // "999999999999999999"
87 EXPECT_FALSE(service.getUidFromArgs(args, 3, uid));
88
89 // "a1"
90 EXPECT_FALSE(service.getUidFromArgs(args, 4, uid));
91
92 // ""
93 EXPECT_FALSE(service.getUidFromArgs(args, 5, uid));
94
95 // For a non-userdebug, uid "1" cannot be impersonated.
96 service.mEngBuild = false;
97 EXPECT_FALSE(service.getUidFromArgs(args, 2, uid));
98 }
99
100 #else
101 GTEST_LOG_(INFO) << "This test does nothing.\n";
102 #endif
103
104 } // namespace statsd
105 } // namespace os
106 } // namespace android
107