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