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 | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 14 | #include "update_engine/update_manager/boxed_value.h" |
| 15 | #include "update_engine/update_manager/shill_provider.h" |
| 16 | #include "update_engine/update_manager/umtest_utils.h" |
| 17 | #include "update_engine/update_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; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 21 | using std::list; |
| 22 | using std::map; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 23 | using std::set; |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 24 | using std::string; |
| 25 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 26 | namespace chromeos_update_manager { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 27 | |
| 28 | // The DeleterMarker flags a bool variable when the class is destroyed. |
| 29 | class DeleterMarker { |
| 30 | public: |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame^] | 31 | explicit DeleterMarker(bool* marker) : marker_(marker) { *marker_ = false; } |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 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 | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 49 | TEST(UmBoxedValueTest, Deleted) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 60 | TEST(UmBoxedValueTest, MoveConstructor) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 74 | TEST(UmBoxedValueTest, MixedList) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 75 | list<BoxedValue> lst; |
| 76 | // This is mostly a compile test. |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame^] | 77 | lst.emplace_back(new const int(42)); // NOLINT(readability/casting) |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 86 | TEST(UmBoxedValueTest, MixedMap) { |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 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()); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 92 | UMTEST_EXPECT_NOT_NULL(it->second.value()); |
| 93 | UMTEST_EXPECT_NULL(m[33].value()); |
Alex Deymo | 23949d4 | 2014-02-05 15:20:59 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 96 | TEST(UmBoxedValueTest, StringToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 97 | EXPECT_EQ("Hej Verden!", |
| 98 | BoxedValue(new string("Hej Verden!")).ToString()); |
| 99 | } |
| 100 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 101 | TEST(UmBoxedValueTest, IntToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 102 | EXPECT_EQ("42", BoxedValue(new int(42)).ToString()); |
| 103 | } |
| 104 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 105 | TEST(UmBoxedValueTest, Int64ToString) { |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame^] | 106 | // -123456789012345 doesn't fit in 32-bit integers. |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 107 | EXPECT_EQ("-123456789012345", BoxedValue( |
| 108 | new int64_t(-123456789012345LL)).ToString()); |
| 109 | } |
| 110 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 111 | TEST(UmBoxedValueTest, UnsignedIntToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 112 | // 4294967295 is the biggest possible 32-bit unsigned integer. |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame^] | 113 | EXPECT_EQ("4294967295", |
| 114 | BoxedValue(new unsigned int(4294967295U)).ToString()); // NOLINT |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 117 | TEST(UmBoxedValueTest, UnsignedInt64ToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 118 | // 18446744073709551615 is the biggest possible 64-bit unsigned integer. |
| 119 | EXPECT_EQ("18446744073709551615", BoxedValue( |
Alex Deymo | f967ebe | 2014-05-05 14:46:17 -0700 | [diff] [blame] | 120 | new uint64_t(18446744073709551615ULL)).ToString()); |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 123 | TEST(UmBoxedValueTest, BoolToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 124 | EXPECT_EQ("false", BoxedValue(new bool(false)).ToString()); |
| 125 | EXPECT_EQ("true", BoxedValue(new bool(true)).ToString()); |
| 126 | } |
| 127 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 128 | TEST(UmBoxedValueTest, DoubleToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 129 | EXPECT_EQ("1.501", BoxedValue(new double(1.501)).ToString()); |
| 130 | } |
| 131 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 132 | TEST(UmBoxedValueTest, TimeToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 133 | // Tue Apr 29 22:30:55 UTC 2014 is 1398810655 seconds since the Unix Epoch. |
| 134 | EXPECT_EQ("4/29/2014 22:30:55 GMT", |
| 135 | BoxedValue(new Time(Time::FromTimeT(1398810655))).ToString()); |
| 136 | } |
| 137 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 138 | TEST(UmBoxedValueTest, TimeDeltaToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 139 | // 12345 seconds is 3 hours, 25 minutes and 45 seconds. |
| 140 | EXPECT_EQ("3h25m45s", |
| 141 | BoxedValue(new TimeDelta(TimeDelta::FromSeconds(12345))) |
| 142 | .ToString()); |
| 143 | } |
| 144 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 145 | TEST(UmBoxedValueTest, ConnectionTypeToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 146 | EXPECT_EQ("Ethernet", |
| 147 | BoxedValue(new ConnectionType(ConnectionType::kEthernet)) |
| 148 | .ToString()); |
| 149 | EXPECT_EQ("Wifi", |
| 150 | BoxedValue(new ConnectionType(ConnectionType::kWifi)).ToString()); |
| 151 | EXPECT_EQ("Wimax", |
| 152 | BoxedValue(new ConnectionType(ConnectionType::kWimax)).ToString()); |
| 153 | EXPECT_EQ("Bluetooth", |
| 154 | BoxedValue(new ConnectionType(ConnectionType::kBluetooth)) |
| 155 | .ToString()); |
| 156 | EXPECT_EQ("Cellular", |
| 157 | BoxedValue(new ConnectionType(ConnectionType::kCellular)) |
| 158 | .ToString()); |
| 159 | EXPECT_EQ("Unknown", |
| 160 | BoxedValue(new ConnectionType(ConnectionType::kUnknown)) |
| 161 | .ToString()); |
| 162 | } |
| 163 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 164 | TEST(UmBoxedValueTest, ConnectionTetheringToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 165 | EXPECT_EQ("Not Detected", |
| 166 | BoxedValue(new ConnectionTethering( |
| 167 | ConnectionTethering::kNotDetected)).ToString()); |
| 168 | EXPECT_EQ("Suspected", |
| 169 | BoxedValue(new ConnectionTethering(ConnectionTethering::kSuspected)) |
| 170 | .ToString()); |
| 171 | EXPECT_EQ("Confirmed", |
| 172 | BoxedValue(new ConnectionTethering(ConnectionTethering::kConfirmed)) |
| 173 | .ToString()); |
| 174 | EXPECT_EQ("Unknown", |
| 175 | BoxedValue(new ConnectionTethering(ConnectionTethering::kUnknown)) |
| 176 | .ToString()); |
| 177 | } |
| 178 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 179 | TEST(UmBoxedValueTest, SetConnectionTypeToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 180 | set<ConnectionType>* set1 = new set<ConnectionType>; |
| 181 | set1->insert(ConnectionType::kWimax); |
| 182 | set1->insert(ConnectionType::kEthernet); |
| 183 | EXPECT_EQ("Ethernet,Wimax", BoxedValue(set1).ToString()); |
| 184 | |
| 185 | set<ConnectionType>* set2 = new set<ConnectionType>; |
| 186 | set2->insert(ConnectionType::kWifi); |
| 187 | EXPECT_EQ("Wifi", BoxedValue(set2).ToString()); |
| 188 | } |
| 189 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 190 | TEST(UmBoxedValueTest, StageToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 191 | EXPECT_EQ("Idle", |
| 192 | BoxedValue(new Stage(Stage::kIdle)).ToString()); |
| 193 | EXPECT_EQ("Checking For Update", |
| 194 | BoxedValue(new Stage(Stage::kCheckingForUpdate)).ToString()); |
| 195 | EXPECT_EQ("Update Available", |
| 196 | BoxedValue(new Stage(Stage::kUpdateAvailable)).ToString()); |
| 197 | EXPECT_EQ("Downloading", |
| 198 | BoxedValue(new Stage(Stage::kDownloading)).ToString()); |
| 199 | EXPECT_EQ("Verifying", |
| 200 | BoxedValue(new Stage(Stage::kVerifying)).ToString()); |
| 201 | EXPECT_EQ("Finalizing", |
| 202 | BoxedValue(new Stage(Stage::kFinalizing)).ToString()); |
| 203 | EXPECT_EQ("Updated, Need Reboot", |
| 204 | BoxedValue(new Stage(Stage::kUpdatedNeedReboot)).ToString()); |
| 205 | EXPECT_EQ("Reporting Error Event", |
| 206 | BoxedValue(new Stage(Stage::kReportingErrorEvent)).ToString()); |
| 207 | EXPECT_EQ("Attempting Rollback", |
| 208 | BoxedValue(new Stage(Stage::kAttemptingRollback)).ToString()); |
| 209 | } |
| 210 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 211 | TEST(UmBoxedValueTest, DeleterMarkerToString) { |
David Zeuthen | fe225c1 | 2014-04-29 10:37:35 -0700 | [diff] [blame] | 212 | bool marker = false; |
| 213 | BoxedValue value = BoxedValue(new DeleterMarker(&marker)); |
| 214 | EXPECT_EQ("DeleterMarker:false", value.ToString()); |
| 215 | marker = true; |
| 216 | EXPECT_EQ("DeleterMarker:true", value.ToString()); |
| 217 | } |
| 218 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 219 | } // namespace chromeos_update_manager |