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