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