• 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 std::list;
35 using std::map;
36 using std::set;
37 using std::string;
38 
39 namespace chromeos_update_manager {
40 
41 // The DeleterMarker flags a bool variable when the class is destroyed.
42 class DeleterMarker {
43  public:
DeleterMarker(bool * marker)44   explicit DeleterMarker(bool* marker) : marker_(marker) { *marker_ = false; }
45 
~DeleterMarker()46   ~DeleterMarker() { *marker_ = true; }
47 
48  private:
49   friend string BoxedValue::ValuePrinter<DeleterMarker>(const void *);
50 
51   // Pointer to the bool marker.
52   bool* marker_;
53 };
54 
55 template<>
ValuePrinter(const void * value)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 
TEST(UmBoxedValueTest,Deleted)62 TEST(UmBoxedValueTest, Deleted) {
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 
TEST(UmBoxedValueTest,MoveConstructor)73 TEST(UmBoxedValueTest, MoveConstructor) {
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 
TEST(UmBoxedValueTest,MixedList)87 TEST(UmBoxedValueTest, MixedList) {
88   list<BoxedValue> lst;
89   // This is mostly a compile test.
90   lst.emplace_back(new const int{42});
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 
TEST(UmBoxedValueTest,MixedMap)99 TEST(UmBoxedValueTest, MixedMap) {
100   map<int, BoxedValue> m;
101   m.emplace(42, BoxedValue(new const string("Hola mundo!")));
102 
103   auto it = m.find(42);
104   ASSERT_NE(it, m.end());
105   EXPECT_NE(nullptr, it->second.value());
106   EXPECT_EQ(nullptr, m[33].value());
107 }
108 
TEST(UmBoxedValueTest,StringToString)109 TEST(UmBoxedValueTest, StringToString) {
110   EXPECT_EQ("Hej Verden!",
111             BoxedValue(new string("Hej Verden!")).ToString());
112 }
113 
TEST(UmBoxedValueTest,IntToString)114 TEST(UmBoxedValueTest, IntToString) {
115   EXPECT_EQ("42", BoxedValue(new int(42)).ToString());
116 }
117 
TEST(UmBoxedValueTest,Int64ToString)118 TEST(UmBoxedValueTest, Int64ToString) {
119   // -123456789012345 doesn't fit in 32-bit integers.
120   EXPECT_EQ("-123456789012345", BoxedValue(
121       new int64_t(-123456789012345LL)).ToString());
122 }
123 
TEST(UmBoxedValueTest,UnsignedIntToString)124 TEST(UmBoxedValueTest, UnsignedIntToString) {
125   // 4294967295 is the biggest possible 32-bit unsigned integer.
126   EXPECT_EQ("4294967295",
127             BoxedValue(new unsigned int(4294967295U)).ToString());  // NOLINT
128 }
129 
TEST(UmBoxedValueTest,UnsignedInt64ToString)130 TEST(UmBoxedValueTest, UnsignedInt64ToString) {
131   // 18446744073709551615 is the biggest possible 64-bit unsigned integer.
132   EXPECT_EQ("18446744073709551615", BoxedValue(
133       new uint64_t(18446744073709551615ULL)).ToString());
134 }
135 
TEST(UmBoxedValueTest,BoolToString)136 TEST(UmBoxedValueTest, BoolToString) {
137   EXPECT_EQ("false", BoxedValue(new bool(false)).ToString());
138   EXPECT_EQ("true", BoxedValue(new bool(true)).ToString());
139 }
140 
TEST(UmBoxedValueTest,DoubleToString)141 TEST(UmBoxedValueTest, DoubleToString) {
142   EXPECT_EQ("1.501", BoxedValue(new double(1.501)).ToString());
143 }
144 
TEST(UmBoxedValueTest,TimeToString)145 TEST(UmBoxedValueTest, TimeToString) {
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 
TEST(UmBoxedValueTest,TimeDeltaToString)151 TEST(UmBoxedValueTest, TimeDeltaToString) {
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 
TEST(UmBoxedValueTest,ConnectionTypeToString)158 TEST(UmBoxedValueTest, ConnectionTypeToString) {
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 
TEST(UmBoxedValueTest,ConnectionTetheringToString)177 TEST(UmBoxedValueTest, ConnectionTetheringToString) {
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 
TEST(UmBoxedValueTest,SetConnectionTypeToString)192 TEST(UmBoxedValueTest, SetConnectionTypeToString) {
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 
TEST(UmBoxedValueTest,StageToString)203 TEST(UmBoxedValueTest, StageToString) {
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 
TEST(UmBoxedValueTest,DeleterMarkerToString)224 TEST(UmBoxedValueTest, DeleterMarkerToString) {
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 
232 }  // namespace chromeos_update_manager
233