Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | #include "shill/dbus_properties.h" |
| 8 | |
| 9 | using std::string; |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 10 | using std::vector; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 11 | using testing::Test; |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | class DBusPropertiesTest : public Test { |
| 16 | }; |
| 17 | |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 18 | TEST_F(DBusPropertiesTest, GetObjectPath) { |
| 19 | static const char kTestProperty[] = "RandomKey"; |
| 20 | static const char kTestValue[] = "/random/path"; |
| 21 | static const char kOldValue[] = "/random/oldpath"; |
| 22 | string value = kOldValue; |
| 23 | DBusPropertiesMap props; |
| 24 | EXPECT_FALSE(DBusProperties::GetObjectPath(props, kTestProperty, &value)); |
| 25 | EXPECT_EQ(kOldValue, value); |
| 26 | props[kTestProperty].writer().append_path(kTestValue); |
| 27 | EXPECT_TRUE(DBusProperties::GetObjectPath(props, kTestProperty, &value)); |
| 28 | EXPECT_EQ(kTestValue, value); |
| 29 | } |
| 30 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 31 | TEST_F(DBusPropertiesTest, GetString) { |
| 32 | static const char kTestProperty[] = "RandomKey"; |
| 33 | static const char kTestValue[] = "random-value"; |
| 34 | static const char kOldValue[] = "old-value"; |
| 35 | string value = kOldValue; |
| 36 | DBusPropertiesMap props; |
| 37 | EXPECT_FALSE(DBusProperties::GetString(props, kTestProperty, &value)); |
| 38 | EXPECT_EQ(kOldValue, value); |
| 39 | props[kTestProperty].writer().append_string(kTestValue); |
| 40 | EXPECT_TRUE(DBusProperties::GetString(props, kTestProperty, &value)); |
| 41 | EXPECT_EQ(kTestValue, value); |
| 42 | } |
| 43 | |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 44 | TEST_F(DBusPropertiesTest, GetStrings) { |
| 45 | static const char kTestProperty[] = "RandomKey"; |
| 46 | static const char kTestValue[] = "random-value"; |
| 47 | static const char kOldValue[] = "old-value"; |
| 48 | vector<string> test_values; |
| 49 | test_values.push_back(kTestValue); |
| 50 | test_values.push_back(kTestValue); |
| 51 | |
| 52 | vector<string> values; |
| 53 | DBusPropertiesMap props; |
| 54 | EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values)); |
| 55 | EXPECT_EQ(0, values.size()); |
| 56 | values.push_back(kOldValue); |
| 57 | EXPECT_FALSE(DBusProperties::GetStrings(props, kTestProperty, &values)); |
| 58 | EXPECT_EQ(1, values.size()); |
| 59 | EXPECT_EQ(kOldValue, values[0]); |
| 60 | ::DBus::Variant v; |
| 61 | ::DBus::MessageIter writer = v.writer(); |
| 62 | writer << test_values; |
| 63 | props[kTestProperty] = v; |
| 64 | EXPECT_TRUE(DBusProperties::GetStrings(props, kTestProperty, &values)); |
| 65 | EXPECT_EQ(2, values.size()); |
| 66 | EXPECT_EQ(kTestValue, values[0]); |
| 67 | EXPECT_EQ(kTestValue, values[1]); |
| 68 | } |
| 69 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 70 | TEST_F(DBusPropertiesTest, GetUint32) { |
| 71 | static const char kTestProperty[] = "AKey"; |
| 72 | const uint32 kTestValue = 35; |
| 73 | const uint32 kOldValue = 74; |
| 74 | uint32 value = kOldValue; |
| 75 | DBusPropertiesMap props; |
| 76 | EXPECT_FALSE(DBusProperties::GetUint32(props, kTestProperty, &value)); |
| 77 | EXPECT_EQ(kOldValue, value); |
| 78 | props[kTestProperty].writer().append_uint32(kTestValue); |
| 79 | EXPECT_TRUE(DBusProperties::GetUint32(props, kTestProperty, &value)); |
| 80 | EXPECT_EQ(kTestValue, value); |
| 81 | } |
| 82 | |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 83 | TEST_F(DBusPropertiesTest, GetUint16) { |
| 84 | static const char kTestProperty[] = "version"; |
| 85 | const uint16 kTestValue = 77; |
| 86 | const uint16 kOldValue = 88; |
| 87 | uint16 value = kOldValue; |
| 88 | DBusPropertiesMap props; |
| 89 | EXPECT_FALSE(DBusProperties::GetUint16(props, kTestProperty, &value)); |
| 90 | EXPECT_EQ(kOldValue, value); |
| 91 | props[kTestProperty].writer().append_uint16(kTestValue); |
| 92 | EXPECT_TRUE(DBusProperties::GetUint16(props, kTestProperty, &value)); |
| 93 | EXPECT_EQ(kTestValue, value); |
| 94 | } |
| 95 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 96 | TEST_F(DBusPropertiesTest, GetRpcIdentifiers) { |
| 97 | static const char kTestProperty[] = "paths"; |
| 98 | static const char kOldTestPath0[] = "/org/chromium/Something/Old"; |
| 99 | static const char kOldTestPath1[] = "/org/chromium/Else/Old"; |
| 100 | static const char kTestPath0[] = "/org/chromium/Something"; |
| 101 | static const char kTestPath1[] = "/org/chromium/Else"; |
| 102 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 103 | RpcIdentifiers value; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 104 | value.push_back(kOldTestPath0); |
| 105 | value.push_back(kOldTestPath1); |
| 106 | DBusPropertiesMap props; |
| 107 | EXPECT_FALSE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value)); |
| 108 | ASSERT_EQ(2, value.size()); |
| 109 | EXPECT_EQ(kOldTestPath0, value[0]); |
| 110 | EXPECT_EQ(kOldTestPath1, value[1]); |
| 111 | |
| 112 | vector<DBus::Path> paths; |
| 113 | paths.push_back(kTestPath0); |
| 114 | paths.push_back(kTestPath1); |
| 115 | DBus::MessageIter writer = props[kTestProperty].writer(); |
| 116 | writer << paths; |
| 117 | EXPECT_TRUE(DBusProperties::GetRpcIdentifiers(props, kTestProperty, &value)); |
| 118 | ASSERT_EQ(2, value.size()); |
| 119 | EXPECT_EQ(kTestPath0, value[0]); |
| 120 | EXPECT_EQ(kTestPath1, value[1]); |
| 121 | } |
| 122 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 123 | TEST_F(DBusPropertiesTest, ConvertPathsToRpcIdentifiers) { |
| 124 | static const char kOldTestPath[] = "/org/chromium/Something/Old"; |
| 125 | static const char kTestPath0[] = "/org/chromium/Something"; |
| 126 | static const char kTestPath1[] = "/org/chromium/Else"; |
| 127 | |
| 128 | RpcIdentifiers ids(1, kOldTestPath); |
| 129 | vector<DBus::Path> paths; |
| 130 | paths.push_back(kTestPath0); |
| 131 | paths.push_back(kTestPath1); |
| 132 | DBusProperties::ConvertPathsToRpcIdentifiers(paths, &ids); |
| 133 | ASSERT_EQ(2, ids.size()); |
| 134 | EXPECT_EQ(kTestPath0, ids[0]); |
| 135 | EXPECT_EQ(kTestPath1, ids[1]); |
| 136 | } |
| 137 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 138 | TEST_F(DBusPropertiesTest, ConvertKeyValueStoreToMap) { |
| 139 | static const char kStringKey[] = "StringKey"; |
| 140 | static const char kStringValue[] = "StringValue"; |
| 141 | static const char kBoolKey[] = "BoolKey"; |
| 142 | const bool kBoolValue = true; |
| 143 | static const char kInt32Key[] = "Int32Key"; |
| 144 | const int32 kInt32Value = 123; |
| 145 | static const char kUint32Key[] = "Uint32Key"; |
| 146 | const uint32 kUint32Value = 654; |
| 147 | KeyValueStore store; |
| 148 | store.SetString(kStringKey, kStringValue); |
| 149 | store.SetBool(kBoolKey, kBoolValue); |
| 150 | store.SetInt(kInt32Key, kInt32Value); |
| 151 | store.SetUint(kUint32Key, kUint32Value); |
| 152 | DBusPropertiesMap props; |
| 153 | props["RandomKey"].writer().append_string("RandomValue"); |
| 154 | DBusProperties::ConvertKeyValueStoreToMap(store, &props); |
| 155 | EXPECT_EQ(4, props.size()); |
| 156 | string string_value; |
| 157 | EXPECT_TRUE(DBusProperties::GetString(props, kStringKey, &string_value)); |
| 158 | EXPECT_EQ(kStringValue, string_value); |
| 159 | bool bool_value = !kBoolValue; |
| 160 | EXPECT_TRUE(DBusProperties::GetBool(props, kBoolKey, &bool_value)); |
| 161 | EXPECT_EQ(kBoolValue, bool_value); |
| 162 | int32 int32_value = ~kInt32Value; |
| 163 | EXPECT_TRUE(DBusProperties::GetInt32(props, kInt32Key, &int32_value)); |
| 164 | EXPECT_EQ(kInt32Value, int32_value); |
| 165 | uint32 uint32_value = ~kUint32Value; |
| 166 | EXPECT_TRUE(DBusProperties::GetUint32(props, kUint32Key, &uint32_value)); |
| 167 | EXPECT_EQ(kUint32Value, uint32_value); |
| 168 | } |
| 169 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 170 | } // namespace shill |