1 /* 2 * Copyright (C) 2020 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 #include "src/HashableDimensionKey.h" 17 18 #include <gtest/gtest.h> 19 20 #include "src/statsd_config.pb.h" 21 #include "statsd_test_util.h" 22 23 #ifdef __ANDROID__ 24 25 using android::util::ProtoReader; 26 27 namespace android { 28 namespace os { 29 namespace statsd { 30 31 /** 32 * Test that #containsLinkedStateValues returns false when the whatKey is 33 * smaller than the primaryKey. 34 */ 35 TEST(HashableDimensionKeyTest, TestContainsLinkedStateValues_WhatKeyTooSmall) { 36 std::vector<Metric2State> mMetric2StateLinks; 37 38 int32_t uid1 = 1000; 39 HashableDimensionKey whatKey = DEFAULT_DIMENSION_KEY; 40 HashableDimensionKey primaryKey; 41 getUidProcessKey(uid1, &primaryKey); 42 43 EXPECT_FALSE(containsLinkedStateValues(whatKey, primaryKey, mMetric2StateLinks, 44 UID_PROCESS_STATE_ATOM_ID)); 45 } 46 47 /** 48 * Test that #containsLinkedStateValues returns false when the linked values 49 * are not equal. 50 */ 51 TEST(HashableDimensionKeyTest, TestContainsLinkedStateValues_UnequalLinkedValues) { 52 int stateAtomId = UID_PROCESS_STATE_ATOM_ID; 53 54 FieldMatcher whatMatcher; 55 whatMatcher.set_field(util::OVERLAY_STATE_CHANGED); 56 FieldMatcher* child11 = whatMatcher.add_child(); 57 child11->set_field(1); 58 59 FieldMatcher stateMatcher; 60 stateMatcher.set_field(stateAtomId); 61 FieldMatcher* child21 = stateMatcher.add_child(); 62 child21->set_field(1); 63 64 std::vector<Metric2State> mMetric2StateLinks; 65 Metric2State ms; 66 ms.stateAtomId = stateAtomId; 67 translateFieldMatcher(whatMatcher, &ms.metricFields); 68 translateFieldMatcher(stateMatcher, &ms.stateFields); 69 mMetric2StateLinks.push_back(ms); 70 71 int32_t uid1 = 1000; 72 int32_t uid2 = 1001; 73 HashableDimensionKey whatKey; 74 getOverlayKey(uid2, "package", &whatKey); 75 HashableDimensionKey primaryKey; 76 getUidProcessKey(uid1, &primaryKey); 77 78 EXPECT_FALSE(containsLinkedStateValues(whatKey, primaryKey, mMetric2StateLinks, stateAtomId)); 79 } 80 81 /** 82 * Test that #containsLinkedStateValues returns false when there is no link 83 * between the key values. 84 */ 85 TEST(HashableDimensionKeyTest, TestContainsLinkedStateValues_MissingMetric2StateLinks) { 86 int stateAtomId = UID_PROCESS_STATE_ATOM_ID; 87 88 std::vector<Metric2State> mMetric2StateLinks; 89 90 int32_t uid1 = 1000; 91 HashableDimensionKey whatKey; 92 getOverlayKey(uid1, "package", &whatKey); 93 HashableDimensionKey primaryKey; 94 getUidProcessKey(uid1, &primaryKey); 95 96 EXPECT_FALSE(containsLinkedStateValues(whatKey, primaryKey, mMetric2StateLinks, stateAtomId)); 97 } 98 99 /** 100 * Test that #containsLinkedStateValues returns true when the key values are 101 * linked and equal. 102 */ 103 TEST(HashableDimensionKeyTest, TestContainsLinkedStateValues_AllConditionsMet) { 104 int stateAtomId = UID_PROCESS_STATE_ATOM_ID; 105 106 FieldMatcher whatMatcher; 107 whatMatcher.set_field(util::OVERLAY_STATE_CHANGED); 108 FieldMatcher* child11 = whatMatcher.add_child(); 109 child11->set_field(1); 110 111 FieldMatcher stateMatcher; 112 stateMatcher.set_field(stateAtomId); 113 FieldMatcher* child21 = stateMatcher.add_child(); 114 child21->set_field(1); 115 116 std::vector<Metric2State> mMetric2StateLinks; 117 Metric2State ms; 118 ms.stateAtomId = stateAtomId; 119 translateFieldMatcher(whatMatcher, &ms.metricFields); 120 translateFieldMatcher(stateMatcher, &ms.stateFields); 121 mMetric2StateLinks.push_back(ms); 122 123 int32_t uid1 = 1000; 124 HashableDimensionKey whatKey; 125 getOverlayKey(uid1, "package", &whatKey); 126 HashableDimensionKey primaryKey; 127 getUidProcessKey(uid1, &primaryKey); 128 129 EXPECT_TRUE(containsLinkedStateValues(whatKey, primaryKey, mMetric2StateLinks, stateAtomId)); 130 } 131 132 } // namespace statsd 133 } // namespace os 134 } // namespace android 135 #else 136 GTEST_LOG_(INFO) << "This test does nothing.\n"; 137 #endif 138