Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 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 | #include <list> |
| 7 | #include <map> |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 8 | #include <set> |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 9 | #include <string> |
| 10 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 11 | #include <base/strings/stringprintf.h> |
| 12 | #include <base/time/time.h> |
| 13 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 14 | #include "update_engine/policy_manager/boxed_value.h" |
| 15 | #include "update_engine/policy_manager/pmtest_utils.h" |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 16 | #include "update_engine/policy_manager/shill_provider.h" |
| 17 | #include "update_engine/policy_manager/updater_provider.h" |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 18 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 19 | using base::Time; |
| 20 | using base::TimeDelta; |
| 21 | using std::set; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 22 | using std::list; |
| 23 | using std::map; |
| 24 | using std::string; |
| 25 | |
| 26 | namespace chromeos_policy_manager { |
| 27 | |
| 28 | // The DeleterMarker flags a bool variable when the class is destroyed. |
| 29 | class DeleterMarker { |
| 30 | public: |
| 31 | DeleterMarker(bool* marker) : marker_(marker) { *marker_ = false; } |
| 32 | |
| 33 | ~DeleterMarker() { *marker_ = true; } |
| 34 | |
| 35 | private: |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 36 | friend string BoxedValue::ValuePrinter<DeleterMarker>(const void *); |
| 37 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 38 | // Pointer to the bool marker. |
| 39 | bool* marker_; |
| 40 | }; |
| 41 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 42 | template<> |
| 43 | string BoxedValue::ValuePrinter<DeleterMarker>(const void *value) { |
| 44 | const DeleterMarker* val = reinterpret_cast<const DeleterMarker*>(value); |
| 45 | return base::StringPrintf("DeleterMarker:%s", |
| 46 | *val->marker_ ? "true" : "false"); |
| 47 | } |
| 48 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 49 | TEST(PmBoxedValueTest, Deleted) { |
| 50 | bool marker = true; |
| 51 | const DeleterMarker* deleter_marker = new DeleterMarker(&marker); |
| 52 | |
| 53 | EXPECT_FALSE(marker); |
| 54 | BoxedValue* box = new BoxedValue(deleter_marker); |
| 55 | EXPECT_FALSE(marker); |
| 56 | delete box; |
| 57 | EXPECT_TRUE(marker); |
| 58 | } |
| 59 | |
| 60 | TEST(PmBoxedValueTest, MoveConstructor) { |
| 61 | bool marker = true; |
| 62 | const DeleterMarker* deleter_marker = new DeleterMarker(&marker); |
| 63 | |
| 64 | BoxedValue* box = new BoxedValue(deleter_marker); |
| 65 | BoxedValue* new_box = new BoxedValue(std::move(*box)); |
| 66 | // box is now undefined but valid. |
| 67 | delete box; |
| 68 | EXPECT_FALSE(marker); |
| 69 | // The deleter_marker gets deleted at this point. |
| 70 | delete new_box; |
| 71 | EXPECT_TRUE(marker); |
| 72 | } |
| 73 | |
| 74 | TEST(PmBoxedValueTest, MixedList) { |
| 75 | list<BoxedValue> lst; |
| 76 | // This is mostly a compile test. |
| 77 | lst.emplace_back(new const int(42)); |
| 78 | lst.emplace_back(new const string("Hello world!")); |
| 79 | bool marker; |
| 80 | lst.emplace_back(new const DeleterMarker(&marker)); |
| 81 | EXPECT_FALSE(marker); |
| 82 | lst.clear(); |
| 83 | EXPECT_TRUE(marker); |
| 84 | } |
| 85 | |
| 86 | TEST(PmBoxedValueTest, MixedMap) { |
| 87 | map<int, BoxedValue> m; |
| 88 | m.emplace(42, std::move(BoxedValue(new const string("Hola mundo!")))); |
| 89 | |
| 90 | auto it = m.find(42); |
| 91 | ASSERT_NE(it, m.end()); |
| 92 | PMTEST_EXPECT_NOT_NULL(it->second.value()); |
| 93 | PMTEST_EXPECT_NULL(m[33].value()); |
| 94 | } |
| 95 | |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 96 | TEST(PmBoxedValueTest, StringToString) { |
| 97 | EXPECT_EQ("Hej Verden!", |
| 98 | BoxedValue(new string("Hej Verden!")).ToString()); |
| 99 | } |
| 100 | |
| 101 | TEST(PmBoxedValueTest, IntToString) { |
| 102 | EXPECT_EQ("42", BoxedValue(new int(42)).ToString()); |
| 103 | } |
| 104 | |
| 105 | TEST(PmBoxedValueTest, UnsignedIntToString) { |
| 106 | // 4294967295 is the biggest possible 32-bit unsigned integer. |
| 107 | EXPECT_EQ("4294967295", BoxedValue(new unsigned int(4294967295)).ToString()); |
| 108 | } |
| 109 | |
| 110 | TEST(PmBoxedValueTest, UnsignedLongToString) { |
| 111 | EXPECT_EQ("4294967295", BoxedValue(new unsigned long(4294967295)).ToString()); |
| 112 | } |
| 113 | |
| 114 | TEST(PmBoxedValueTest, UnsignedLongLongToString) { |
| 115 | // 18446744073709551615 is the biggest possible 64-bit unsigned integer. |
| 116 | EXPECT_EQ("18446744073709551615", BoxedValue( |
| 117 | new unsigned long long(18446744073709551615ULL)).ToString()); |
| 118 | } |
| 119 | |
| 120 | TEST(PmBoxedValueTest, BoolToString) { |
| 121 | EXPECT_EQ("false", BoxedValue(new bool(false)).ToString()); |
| 122 | EXPECT_EQ("true", BoxedValue(new bool(true)).ToString()); |
| 123 | } |
| 124 | |
| 125 | TEST(PmBoxedValueTest, DoubleToString) { |
| 126 | EXPECT_EQ("1.501", BoxedValue(new double(1.501)).ToString()); |
| 127 | } |
| 128 | |
| 129 | TEST(PmBoxedValueTest, TimeToString) { |
| 130 | // Tue Apr 29 22:30:55 UTC 2014 is 1398810655 seconds since the Unix Epoch. |
| 131 | EXPECT_EQ("4/29/2014 22:30:55 GMT", |
| 132 | BoxedValue(new Time(Time::FromTimeT(1398810655))).ToString()); |
| 133 | } |
| 134 | |
| 135 | TEST(PmBoxedValueTest, TimeDeltaToString) { |
| 136 | // 12345 seconds is 3 hours, 25 minutes and 45 seconds. |
| 137 | EXPECT_EQ("3h25m45s", |
| 138 | BoxedValue(new TimeDelta(TimeDelta::FromSeconds(12345))) |
| 139 | .ToString()); |
| 140 | } |
| 141 | |
| 142 | TEST(PmBoxedValueTest, ConnectionTypeToString) { |
| 143 | EXPECT_EQ("Ethernet", |
| 144 | BoxedValue(new ConnectionType(ConnectionType::kEthernet)) |
| 145 | .ToString()); |
| 146 | EXPECT_EQ("Wifi", |
| 147 | BoxedValue(new ConnectionType(ConnectionType::kWifi)).ToString()); |
| 148 | EXPECT_EQ("Wimax", |
| 149 | BoxedValue(new ConnectionType(ConnectionType::kWimax)).ToString()); |
| 150 | EXPECT_EQ("Bluetooth", |
| 151 | BoxedValue(new ConnectionType(ConnectionType::kBluetooth)) |
| 152 | .ToString()); |
| 153 | EXPECT_EQ("Cellular", |
| 154 | BoxedValue(new ConnectionType(ConnectionType::kCellular)) |
| 155 | .ToString()); |
| 156 | EXPECT_EQ("Unknown", |
| 157 | BoxedValue(new ConnectionType(ConnectionType::kUnknown)) |
| 158 | .ToString()); |
| 159 | } |
| 160 | |
| 161 | TEST(PmBoxedValueTest, ConnectionTetheringToString) { |
| 162 | EXPECT_EQ("Not Detected", |
| 163 | BoxedValue(new ConnectionTethering( |
| 164 | ConnectionTethering::kNotDetected)).ToString()); |
| 165 | EXPECT_EQ("Suspected", |
| 166 | BoxedValue(new ConnectionTethering(ConnectionTethering::kSuspected)) |
| 167 | .ToString()); |
| 168 | EXPECT_EQ("Confirmed", |
| 169 | BoxedValue(new ConnectionTethering(ConnectionTethering::kConfirmed)) |
| 170 | .ToString()); |
| 171 | EXPECT_EQ("Unknown", |
| 172 | BoxedValue(new ConnectionTethering(ConnectionTethering::kUnknown)) |
| 173 | .ToString()); |
| 174 | } |
| 175 | |
| 176 | TEST(PmBoxedValueTest, SetConnectionTypeToString) { |
| 177 | set<ConnectionType>* set1 = new set<ConnectionType>; |
| 178 | set1->insert(ConnectionType::kWimax); |
| 179 | set1->insert(ConnectionType::kEthernet); |
| 180 | EXPECT_EQ("Ethernet,Wimax", BoxedValue(set1).ToString()); |
| 181 | |
| 182 | set<ConnectionType>* set2 = new set<ConnectionType>; |
| 183 | set2->insert(ConnectionType::kWifi); |
| 184 | EXPECT_EQ("Wifi", BoxedValue(set2).ToString()); |
| 185 | } |
| 186 | |
| 187 | TEST(PmBoxedValueTest, StageToString) { |
| 188 | EXPECT_EQ("Idle", |
| 189 | BoxedValue(new Stage(Stage::kIdle)).ToString()); |
| 190 | EXPECT_EQ("Checking For Update", |
| 191 | BoxedValue(new Stage(Stage::kCheckingForUpdate)).ToString()); |
| 192 | EXPECT_EQ("Update Available", |
| 193 | BoxedValue(new Stage(Stage::kUpdateAvailable)).ToString()); |
| 194 | EXPECT_EQ("Downloading", |
| 195 | BoxedValue(new Stage(Stage::kDownloading)).ToString()); |
| 196 | EXPECT_EQ("Verifying", |
| 197 | BoxedValue(new Stage(Stage::kVerifying)).ToString()); |
| 198 | EXPECT_EQ("Finalizing", |
| 199 | BoxedValue(new Stage(Stage::kFinalizing)).ToString()); |
| 200 | EXPECT_EQ("Updated, Need Reboot", |
| 201 | BoxedValue(new Stage(Stage::kUpdatedNeedReboot)).ToString()); |
| 202 | EXPECT_EQ("Reporting Error Event", |
| 203 | BoxedValue(new Stage(Stage::kReportingErrorEvent)).ToString()); |
| 204 | EXPECT_EQ("Attempting Rollback", |
| 205 | BoxedValue(new Stage(Stage::kAttemptingRollback)).ToString()); |
| 206 | } |
| 207 | |
| 208 | TEST(PmBoxedValueTest, DeleterMarkerToString) { |
| 209 | bool marker = false; |
| 210 | BoxedValue value = BoxedValue(new DeleterMarker(&marker)); |
| 211 | EXPECT_EQ("DeleterMarker:false", value.ToString()); |
| 212 | marker = true; |
| 213 | EXPECT_EQ("DeleterMarker:true", value.ToString()); |
| 214 | } |
| 215 | |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 216 | } // namespace chromeos_policy_manager |