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