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")); |
| 68 | args.push(String8("9999999999999999999999999999999999")); |
| 69 | args.push(String8("a1")); |
| 70 | args.push(String8("")); |
| 71 | |
| 72 | int32_t uid; |
| 73 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 74 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 75 | service->mEngBuild = true; |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 76 | |
| 77 | // "-1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 78 | EXPECT_FALSE(service->getUidFromArgs(args, 0, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 79 | |
| 80 | // "0" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 81 | EXPECT_TRUE(service->getUidFromArgs(args, 1, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 82 | EXPECT_EQ(0, uid); |
| 83 | |
| 84 | // "1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 85 | EXPECT_TRUE(service->getUidFromArgs(args, 2, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 86 | EXPECT_EQ(1, uid); |
| 87 | |
| 88 | // "999999999999999999" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 89 | EXPECT_FALSE(service->getUidFromArgs(args, 3, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 90 | |
| 91 | // "a1" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 92 | EXPECT_FALSE(service->getUidFromArgs(args, 4, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 93 | |
| 94 | // "" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 95 | EXPECT_FALSE(service->getUidFromArgs(args, 5, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 96 | |
| 97 | // For a non-userdebug, uid "1" cannot be impersonated. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame^] | 98 | service->mEngBuild = false; |
| 99 | EXPECT_FALSE(service->getUidFromArgs(args, 2, uid)); |
Bookatz | d238657 | 2018-12-14 15:53:14 -0800 | [diff] [blame] | 100 | } |
| 101 | |
David Chen | 9fdd403 | 2018-03-20 14:38:56 -0700 | [diff] [blame] | 102 | #else |
| 103 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 104 | #endif |
| 105 | |
| 106 | } // namespace statsd |
| 107 | } // namespace os |
| 108 | } // namespace android |