Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "android-base/properties.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame^] | 21 | #include <atomic> |
| 22 | #include <chrono> |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 23 | #include <string> |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame^] | 24 | #include <thread> |
| 25 | |
| 26 | using namespace std::chrono_literals; |
Elliott Hughes | 5a7788c | 2016-09-21 16:53:15 -0700 | [diff] [blame] | 27 | |
| 28 | TEST(properties, smoke) { |
| 29 | android::base::SetProperty("debug.libbase.property_test", "hello"); |
| 30 | |
| 31 | std::string s = android::base::GetProperty("debug.libbase.property_test", ""); |
| 32 | ASSERT_EQ("hello", s); |
| 33 | |
| 34 | android::base::SetProperty("debug.libbase.property_test", "world"); |
| 35 | s = android::base::GetProperty("debug.libbase.property_test", ""); |
| 36 | ASSERT_EQ("world", s); |
| 37 | |
| 38 | s = android::base::GetProperty("this.property.does.not.exist", ""); |
| 39 | ASSERT_EQ("", s); |
| 40 | |
| 41 | s = android::base::GetProperty("this.property.does.not.exist", "default"); |
| 42 | ASSERT_EQ("default", s); |
| 43 | } |
| 44 | |
| 45 | TEST(properties, empty) { |
| 46 | // Because you can't delete a property, people "delete" them by |
| 47 | // setting them to the empty string. In that case we'd want to |
| 48 | // keep the default value (like cutils' property_get did). |
| 49 | android::base::SetProperty("debug.libbase.property_test", ""); |
| 50 | std::string s = android::base::GetProperty("debug.libbase.property_test", "default"); |
| 51 | ASSERT_EQ("default", s); |
| 52 | } |
| 53 | |
| 54 | static void CheckGetBoolProperty(bool expected, const std::string& value, bool default_value) { |
| 55 | android::base::SetProperty("debug.libbase.property_test", value.c_str()); |
| 56 | ASSERT_EQ(expected, android::base::GetBoolProperty("debug.libbase.property_test", default_value)); |
| 57 | } |
| 58 | |
| 59 | TEST(properties, GetBoolProperty_true) { |
| 60 | CheckGetBoolProperty(true, "1", false); |
| 61 | CheckGetBoolProperty(true, "y", false); |
| 62 | CheckGetBoolProperty(true, "yes", false); |
| 63 | CheckGetBoolProperty(true, "on", false); |
| 64 | CheckGetBoolProperty(true, "true", false); |
| 65 | } |
| 66 | |
| 67 | TEST(properties, GetBoolProperty_false) { |
| 68 | CheckGetBoolProperty(false, "0", true); |
| 69 | CheckGetBoolProperty(false, "n", true); |
| 70 | CheckGetBoolProperty(false, "no", true); |
| 71 | CheckGetBoolProperty(false, "off", true); |
| 72 | CheckGetBoolProperty(false, "false", true); |
| 73 | } |
| 74 | |
| 75 | TEST(properties, GetBoolProperty_default) { |
| 76 | CheckGetBoolProperty(true, "burp", true); |
| 77 | CheckGetBoolProperty(false, "burp", false); |
| 78 | } |
| 79 | |
| 80 | template <typename T> void CheckGetIntProperty() { |
| 81 | // Positive and negative. |
| 82 | android::base::SetProperty("debug.libbase.property_test", "-12"); |
| 83 | EXPECT_EQ(T(-12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45)); |
| 84 | android::base::SetProperty("debug.libbase.property_test", "12"); |
| 85 | EXPECT_EQ(T(12), android::base::GetIntProperty<T>("debug.libbase.property_test", 45)); |
| 86 | |
| 87 | // Default value. |
| 88 | android::base::SetProperty("debug.libbase.property_test", ""); |
| 89 | EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45)); |
| 90 | |
| 91 | // Bounds checks. |
| 92 | android::base::SetProperty("debug.libbase.property_test", "0"); |
| 93 | EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2)); |
| 94 | android::base::SetProperty("debug.libbase.property_test", "1"); |
| 95 | EXPECT_EQ(T(1), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2)); |
| 96 | android::base::SetProperty("debug.libbase.property_test", "2"); |
| 97 | EXPECT_EQ(T(2), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2)); |
| 98 | android::base::SetProperty("debug.libbase.property_test", "3"); |
| 99 | EXPECT_EQ(T(45), android::base::GetIntProperty<T>("debug.libbase.property_test", 45, 1, 2)); |
| 100 | } |
| 101 | |
| 102 | template <typename T> void CheckGetUintProperty() { |
| 103 | // Positive. |
| 104 | android::base::SetProperty("debug.libbase.property_test", "12"); |
| 105 | EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 45)); |
| 106 | |
| 107 | // Default value. |
| 108 | android::base::SetProperty("debug.libbase.property_test", ""); |
| 109 | EXPECT_EQ(T(45), android::base::GetUintProperty<T>("debug.libbase.property_test", 45)); |
| 110 | |
| 111 | // Bounds checks. |
| 112 | android::base::SetProperty("debug.libbase.property_test", "12"); |
| 113 | EXPECT_EQ(T(12), android::base::GetUintProperty<T>("debug.libbase.property_test", 33, 22)); |
| 114 | android::base::SetProperty("debug.libbase.property_test", "12"); |
| 115 | EXPECT_EQ(T(5), android::base::GetUintProperty<T>("debug.libbase.property_test", 5, 10)); |
| 116 | } |
| 117 | |
| 118 | TEST(properties, GetIntProperty_int8_t) { CheckGetIntProperty<int8_t>(); } |
| 119 | TEST(properties, GetIntProperty_int16_t) { CheckGetIntProperty<int16_t>(); } |
| 120 | TEST(properties, GetIntProperty_int32_t) { CheckGetIntProperty<int32_t>(); } |
| 121 | TEST(properties, GetIntProperty_int64_t) { CheckGetIntProperty<int64_t>(); } |
| 122 | |
| 123 | TEST(properties, GetUintProperty_uint8_t) { CheckGetUintProperty<uint8_t>(); } |
| 124 | TEST(properties, GetUintProperty_uint16_t) { CheckGetUintProperty<uint16_t>(); } |
| 125 | TEST(properties, GetUintProperty_uint32_t) { CheckGetUintProperty<uint32_t>(); } |
| 126 | TEST(properties, GetUintProperty_uint64_t) { CheckGetUintProperty<uint64_t>(); } |
Elliott Hughes | 530448e | 2017-02-10 19:02:51 -0800 | [diff] [blame^] | 127 | |
| 128 | TEST(properties, WaitForProperty) { |
| 129 | std::atomic<bool> flag{false}; |
| 130 | std::thread thread([&]() { |
| 131 | std::this_thread::sleep_for(100ms); |
| 132 | android::base::SetProperty("debug.libbase.WaitForProperty_test", "a"); |
| 133 | while (!flag) std::this_thread::yield(); |
| 134 | android::base::SetProperty("debug.libbase.WaitForProperty_test", "b"); |
| 135 | }); |
| 136 | |
| 137 | android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "a"); |
| 138 | flag = true; |
| 139 | android::base::WaitForProperty("debug.libbase.WaitForProperty_test", "b"); |
| 140 | thread.join(); |
| 141 | } |