David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 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 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 19 | #include <android/binder_interface_utils.h> |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 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; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 33 | using ::ndk::SharedRefBase; |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 34 | |
| 35 | #ifdef __ANDROID__ |
| 36 | |
| 37 | TEST(StatsServiceTest, TestAddConfig_simple) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 38 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 39 | StatsdConfig config; |
| 40 | config.set_id(12345); |
| 41 | string serialized = config.SerializeAsString(); |
| 42 | |
| 43 | EXPECT_TRUE( |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 44 | service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()})); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(StatsServiceTest, TestAddConfig_empty) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 48 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 49 | string serialized = ""; |
| 50 | |
| 51 | EXPECT_TRUE( |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 52 | service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()})); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST(StatsServiceTest, TestAddConfig_invalid) { |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 56 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 57 | string serialized = "Invalid config!"; |
| 58 | |
| 59 | EXPECT_FALSE( |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 60 | service->addConfigurationChecked(123, 12345, {serialized.begin(), serialized.end()})); |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 63 | TEST(StatsServiceTest, TestGetUidFromArgs) { |
| 64 | Vector<String8> args; |
| 65 | args.push(String8("-1")); |
| 66 | args.push(String8("0")); |
| 67 | args.push(String8("1")); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 68 | args.push(String8("a1")); |
| 69 | args.push(String8("")); |
| 70 | |
| 71 | int32_t uid; |
| 72 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 73 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 74 | service->mEngBuild = true; |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 75 | |
| 76 | // "-1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 77 | EXPECT_FALSE(service->getUidFromArgs(args, 0, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 78 | |
| 79 | // "0" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 80 | EXPECT_TRUE(service->getUidFromArgs(args, 1, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 81 | EXPECT_EQ(0, uid); |
| 82 | |
| 83 | // "1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 84 | EXPECT_TRUE(service->getUidFromArgs(args, 2, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 85 | EXPECT_EQ(1, uid); |
| 86 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 87 | // "a1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 88 | EXPECT_FALSE(service->getUidFromArgs(args, 3, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 89 | |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 90 | // "" |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 91 | EXPECT_FALSE(service->getUidFromArgs(args, 4, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 92 | |
| 93 | // For a non-userdebug, uid "1" cannot be impersonated. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 94 | service->mEngBuild = false; |
| 95 | EXPECT_FALSE(service->getUidFromArgs(args, 2, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 96 | } |
| 97 | |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 98 | #else |
| 99 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 100 | #endif |
| 101 | |
| 102 | } // namespace statsd |
| 103 | } // namespace os |
| 104 | } // namespace android |