• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #include "update_engine/update_manager/boxed_value.h"
18 
19 #include <gtest/gtest.h>
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string>
24 
25 #include <base/strings/stringprintf.h>
26 #include <base/time/time.h>
27 
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"
31 
32 using base::Time;
33 using base::TimeDelta;
34 using chromeos_update_engine::ConnectionTethering;
35 using chromeos_update_engine::ConnectionType;
36 using std::list;
37 using std::map;
38 using std::set;
39 using std::string;
40 
41 namespace chromeos_update_manager {
42 
43 // The DeleterMarker flags a bool variable when the class is destroyed.
44 class DeleterMarker {
45  public:
DeleterMarker(bool * marker)46   explicit DeleterMarker(bool* marker) : marker_(marker) { *marker_ = false; }
47 
~DeleterMarker()48   ~DeleterMarker() { *marker_ = true; }
49 
50  private:
51   friend string BoxedValue::ValuePrinter<DeleterMarker>(const void *);
52 
53   // Pointer to the bool marker.
54   bool* marker_;
55 };
56 
57 template<>
ValuePrinter(const void * value)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 
TEST(UmBoxedValueTest,Deleted)64 TEST(UmBoxedValueTest, Deleted) {
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 
TEST(UmBoxedValueTest,MoveConstructor)75 TEST(UmBoxedValueTest, MoveConstructor) {
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 
TEST(UmBoxedValueTest,MixedList)89 TEST(UmBoxedValueTest, MixedList) {
90   list<BoxedValue> lst;
91   // This is mostly a compile test.
92   lst.emplace_back(new const int{42});
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 
TEST(UmBoxedValueTest,MixedMap)101 TEST(UmBoxedValueTest, MixedMap) {
102   map<int, BoxedValue> m;
103   m.emplace(42, BoxedValue(new const string("Hola mundo!")));
104 
105   auto it = m.find(42);
106   ASSERT_NE(it, m.end());
107   EXPECT_NE(nullptr, it->second.value());
108   EXPECT_EQ(nullptr, m[33].value());
109 }
110 
TEST(UmBoxedValueTest,StringToString)111 TEST(UmBoxedValueTest, StringToString) {
112   EXPECT_EQ("Hej Verden!",
113             BoxedValue(new string("Hej Verden!")).ToString());
114 }
115 
TEST(UmBoxedValueTest,IntToString)116 TEST(UmBoxedValueTest, IntToString) {
117   EXPECT_EQ("42", BoxedValue(new int(42)).ToString());
118 }
119 
TEST(UmBoxedValueTest,Int64ToString)120 TEST(UmBoxedValueTest, Int64ToString) {
121   // -123456789012345 doesn't fit in 32-bit integers.
122   EXPECT_EQ("-123456789012345", BoxedValue(
123       new int64_t(-123456789012345LL)).ToString());
124 }
125 
TEST(UmBoxedValueTest,UnsignedIntToString)126 TEST(UmBoxedValueTest, UnsignedIntToString) {
127   // 4294967295 is the biggest possible 32-bit unsigned integer.
128   EXPECT_EQ("4294967295",
129             BoxedValue(new unsigned int(4294967295U)).ToString());  // NOLINT
130 }
131 
TEST(UmBoxedValueTest,UnsignedInt64ToString)132 TEST(UmBoxedValueTest, UnsignedInt64ToString) {
133   // 18446744073709551615 is the biggest possible 64-bit unsigned integer.
134   EXPECT_EQ("18446744073709551615", BoxedValue(
135       new uint64_t(18446744073709551615ULL)).ToString());
136 }
137 
TEST(UmBoxedValueTest,BoolToString)138 TEST(UmBoxedValueTest, BoolToString) {
139   EXPECT_EQ("false", BoxedValue(new bool(false)).ToString());
140   EXPECT_EQ("true", BoxedValue(new bool(true)).ToString());
141 }
142 
TEST(UmBoxedValueTest,DoubleToString)143 TEST(UmBoxedValueTest, DoubleToString) {
144   EXPECT_EQ("1.501", BoxedValue(new double(1.501)).ToString());
145 }
146 
TEST(UmBoxedValueTest,TimeToString)147 TEST(UmBoxedValueTest, TimeToString) {
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 
TEST(UmBoxedValueTest,TimeDeltaToString)153 TEST(UmBoxedValueTest, TimeDeltaToString) {
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 
TEST(UmBoxedValueTest,ConnectionTypeToString)160 TEST(UmBoxedValueTest, ConnectionTypeToString) {
161   EXPECT_EQ("ethernet",
162             BoxedValue(new ConnectionType(ConnectionType::kEthernet))
163             .ToString());
164   EXPECT_EQ("wifi",
165             BoxedValue(new ConnectionType(ConnectionType::kWifi)).ToString());
166   EXPECT_EQ("wimax",
167             BoxedValue(new ConnectionType(ConnectionType::kWimax)).ToString());
168   EXPECT_EQ("bluetooth",
169             BoxedValue(new ConnectionType(ConnectionType::kBluetooth))
170             .ToString());
171   EXPECT_EQ("cellular",
172             BoxedValue(new ConnectionType(ConnectionType::kCellular))
173             .ToString());
174   EXPECT_EQ("Unknown",
175             BoxedValue(new ConnectionType(ConnectionType::kUnknown))
176             .ToString());
177 }
178 
TEST(UmBoxedValueTest,ConnectionTetheringToString)179 TEST(UmBoxedValueTest, ConnectionTetheringToString) {
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 
TEST(UmBoxedValueTest,SetConnectionTypeToString)194 TEST(UmBoxedValueTest, SetConnectionTypeToString) {
195   set<ConnectionType>* set1 = new set<ConnectionType>;
196   set1->insert(ConnectionType::kWimax);
197   set1->insert(ConnectionType::kEthernet);
198   EXPECT_EQ("ethernet,wimax", BoxedValue(set1).ToString());
199 
200   set<ConnectionType>* set2 = new set<ConnectionType>;
201   set2->insert(ConnectionType::kWifi);
202   EXPECT_EQ("wifi", BoxedValue(set2).ToString());
203 }
204 
TEST(UmBoxedValueTest,StageToString)205 TEST(UmBoxedValueTest, StageToString) {
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 
TEST(UmBoxedValueTest,DeleterMarkerToString)226 TEST(UmBoxedValueTest, DeleterMarkerToString) {
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 
234 }  // namespace chromeos_update_manager
235