blob: c1f9a2e9516d9b63e9b5865545d045348f140449 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <errno.h>
Darin Petkov5c0a8af2010-08-24 13:39:13 -07008
Andrew de los Reyescc92cd32010-10-05 16:56:14 -07009#include <map>
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080010#include <string>
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <vector>
Darin Petkov5c0a8af2010-08-24 13:39:13 -070012
Darin Petkov8e447e02013-04-16 16:23:50 +020013#include <base/file_path.h>
14#include <base/file_util.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070015#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040016#include <base/stringprintf.h>
Darin Petkovd3f8c892010-10-12 21:38:45 -070017#include <gtest/gtest.h>
18
19#include "update_engine/test_utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include "update_engine/utils.h"
21
Andrew de los Reyescc92cd32010-10-05 16:56:14 -070022using std::map;
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080023using std::string;
adlr@google.com3defe6a2009-12-04 20:57:17 +000024using std::vector;
25
26namespace chromeos_update_engine {
27
28class UtilsTest : public ::testing::Test { };
29
Chris Sosac1972482013-04-30 22:31:10 -070030TEST(UtilsTest, CanParseECVersion) {
Chris Sosac1972482013-04-30 22:31:10 -070031 // Should be able to parse and valid key value line.
J. Richard Barnette63137e52013-10-28 10:57:29 -070032 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
33 EXPECT_EQ("123456", utils::ParseECVersion(
34 "b=1231a fw_version=123456 a=fasd2"));
35 EXPECT_EQ("12345", utils::ParseECVersion("fw_version=12345"));
36 EXPECT_EQ("00VFA616", utils::ParseECVersion(
Chris Sosac1972482013-04-30 22:31:10 -070037 "vendor=\"sam\" fw_version=\"00VFA616\""));
38
39 // For invalid entries, should return the empty string.
J. Richard Barnette63137e52013-10-28 10:57:29 -070040 EXPECT_EQ("", utils::ParseECVersion("b=1231a fw_version a=fasd2"));
Chris Sosac1972482013-04-30 22:31:10 -070041}
42
J. Richard Barnette30842932013-10-28 15:04:23 -070043
44TEST(UtilsTest, KernelDeviceOfBootDevice) {
45 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("foo"));
46 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda0"));
47 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda1"));
48 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda2"));
49 EXPECT_EQ("/dev/sda2", utils::KernelDeviceOfBootDevice("/dev/sda3"));
50 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda4"));
51 EXPECT_EQ("/dev/sda4", utils::KernelDeviceOfBootDevice("/dev/sda5"));
52 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda6"));
53 EXPECT_EQ("/dev/sda6", utils::KernelDeviceOfBootDevice("/dev/sda7"));
54 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda8"));
55 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/sda9"));
56
57 EXPECT_EQ("/dev/mmcblk0p2",
58 utils::KernelDeviceOfBootDevice("/dev/mmcblk0p3"));
59 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/mmcblk0p4"));
60
61 EXPECT_EQ("/dev/ubi2", utils::KernelDeviceOfBootDevice("/dev/ubi3"));
62 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubi4"));
63
64 EXPECT_EQ("/dev/mtdblock2",
65 utils::KernelDeviceOfBootDevice("/dev/ubiblock3_0"));
66 EXPECT_EQ("/dev/mtdblock4",
67 utils::KernelDeviceOfBootDevice("/dev/ubiblock5_0"));
68 EXPECT_EQ("/dev/mtdblock6",
69 utils::KernelDeviceOfBootDevice("/dev/ubiblock7_0"));
70 EXPECT_EQ("", utils::KernelDeviceOfBootDevice("/dev/ubiblock4_0"));
71}
72
73
adlr@google.com3defe6a2009-12-04 20:57:17 +000074TEST(UtilsTest, NormalizePathTest) {
75 EXPECT_EQ("", utils::NormalizePath("", false));
76 EXPECT_EQ("", utils::NormalizePath("", true));
77 EXPECT_EQ("/", utils::NormalizePath("/", false));
78 EXPECT_EQ("", utils::NormalizePath("/", true));
79 EXPECT_EQ("/", utils::NormalizePath("//", false));
80 EXPECT_EQ("", utils::NormalizePath("//", true));
81 EXPECT_EQ("foo", utils::NormalizePath("foo", false));
82 EXPECT_EQ("foo", utils::NormalizePath("foo", true));
83 EXPECT_EQ("/foo/", utils::NormalizePath("/foo//", false));
84 EXPECT_EQ("/foo", utils::NormalizePath("/foo//", true));
85 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
86 false));
87 EXPECT_EQ("bar/baz/foo/adlr", utils::NormalizePath("bar/baz//foo/adlr",
88 true));
89 EXPECT_EQ("/bar/baz/foo/adlr/", utils::NormalizePath("/bar/baz//foo/adlr/",
90 false));
91 EXPECT_EQ("/bar/baz/foo/adlr", utils::NormalizePath("/bar/baz//foo/adlr/",
92 true));
93 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", false));
94 EXPECT_EQ("\\\\", utils::NormalizePath("\\\\", true));
95 EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", false));
96 EXPECT_EQ("\\:/;$PATH\n\\", utils::NormalizePath("\\://;$PATH\n\\", true));
97 EXPECT_EQ("/spaces s/ ok/s / / /",
98 utils::NormalizePath("/spaces s/ ok/s / / /", false));
99 EXPECT_EQ("/spaces s/ ok/s / / ",
100 utils::NormalizePath("/spaces s/ ok/s / / /", true));
101}
102
103TEST(UtilsTest, ReadFileFailure) {
104 vector<char> empty;
105 EXPECT_FALSE(utils::ReadFile("/this/doesn't/exist", &empty));
106}
107
Darin Petkov8e447e02013-04-16 16:23:50 +0200108TEST(UtilsTest, ReadFileChunk) {
109 FilePath file;
110 EXPECT_TRUE(file_util::CreateTemporaryFile(&file));
111 ScopedPathUnlinker unlinker(file.value());
112 vector<char> data;
113 const size_t kSize = 1024 * 1024;
114 for (size_t i = 0; i < kSize; i++) {
115 data.push_back(i % 255);
116 }
117 EXPECT_TRUE(utils::WriteFile(file.value().c_str(), &data[0], data.size()));
118 vector<char> in_data;
119 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), kSize, 10, &in_data));
120 EXPECT_TRUE(in_data.empty());
121 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 0, -1, &in_data));
122 EXPECT_TRUE(data == in_data);
123 in_data.clear();
124 EXPECT_TRUE(utils::ReadFileChunk(file.value().c_str(), 10, 20, &in_data));
125 EXPECT_TRUE(vector<char>(data.begin() + 10, data.begin() + 10 + 20) ==
126 in_data);
127}
128
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129TEST(UtilsTest, ErrnoNumberAsStringTest) {
130 EXPECT_EQ("No such file or directory", utils::ErrnoNumberAsString(ENOENT));
131}
132
133TEST(UtilsTest, StringHasSuffixTest) {
134 EXPECT_TRUE(utils::StringHasSuffix("foo", "foo"));
135 EXPECT_TRUE(utils::StringHasSuffix("foo", "o"));
136 EXPECT_TRUE(utils::StringHasSuffix("", ""));
137 EXPECT_TRUE(utils::StringHasSuffix("abcabc", "abc"));
138 EXPECT_TRUE(utils::StringHasSuffix("adlrwashere", "ere"));
139 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", "gh"));
140 EXPECT_TRUE(utils::StringHasSuffix("abcdefgh", ""));
141 EXPECT_FALSE(utils::StringHasSuffix("foo", "afoo"));
142 EXPECT_FALSE(utils::StringHasSuffix("", "x"));
143 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "fg"));
144 EXPECT_FALSE(utils::StringHasSuffix("abcdefgh", "ab"));
145}
146
147TEST(UtilsTest, StringHasPrefixTest) {
148 EXPECT_TRUE(utils::StringHasPrefix("foo", "foo"));
149 EXPECT_TRUE(utils::StringHasPrefix("foo", "f"));
150 EXPECT_TRUE(utils::StringHasPrefix("", ""));
151 EXPECT_TRUE(utils::StringHasPrefix("abcabc", "abc"));
152 EXPECT_TRUE(utils::StringHasPrefix("adlrwashere", "adl"));
153 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", "ab"));
154 EXPECT_TRUE(utils::StringHasPrefix("abcdefgh", ""));
155 EXPECT_FALSE(utils::StringHasPrefix("foo", "fooa"));
156 EXPECT_FALSE(utils::StringHasPrefix("", "x"));
157 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "bc"));
158 EXPECT_FALSE(utils::StringHasPrefix("abcdefgh", "gh"));
159}
160
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161TEST(UtilsTest, RecursiveUnlinkDirTest) {
Gilad Arnoldb4346a22013-07-16 06:44:45 -0700162 string first_dir_name;
163 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-a-XXXXXX",
164 &first_dir_name));
165 ASSERT_EQ(0, Chmod(first_dir_name, 0755));
166 string second_dir_name;
167 ASSERT_TRUE(utils::MakeTempDirectory("RecursiveUnlinkDirTest-b-XXXXXX",
168 &second_dir_name));
169 ASSERT_EQ(0, Chmod(second_dir_name, 0755));
170
171 EXPECT_EQ(0, Symlink(string("../") + first_dir_name,
172 second_dir_name + "/link"));
173 EXPECT_EQ(0, System(string("echo hi > ") + second_dir_name + "/file"));
174 EXPECT_EQ(0, Mkdir(second_dir_name + "/dir", 0755));
175 EXPECT_EQ(0, System(string("echo ok > ") + second_dir_name + "/dir/subfile"));
176 EXPECT_TRUE(utils::RecursiveUnlinkDir(second_dir_name));
177 EXPECT_TRUE(utils::FileExists(first_dir_name.c_str()));
178 EXPECT_EQ(0, System(string("rm -rf ") + first_dir_name));
179 EXPECT_FALSE(utils::FileExists(second_dir_name.c_str()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000180 EXPECT_TRUE(utils::RecursiveUnlinkDir("/something/that/doesnt/exist"));
181}
182
Darin Petkov002b2fe2010-11-22 13:53:22 -0800183TEST(UtilsTest, IsSymlinkTest) {
184 string temp_dir;
185 EXPECT_TRUE(utils::MakeTempDirectory("/tmp/symlink-test.XXXXXX", &temp_dir));
186 string temp_file = temp_dir + "temp-file";
187 EXPECT_TRUE(utils::WriteFile(temp_file.c_str(), "", 0));
188 string temp_symlink = temp_dir + "temp-symlink";
189 EXPECT_EQ(0, symlink(temp_file.c_str(), temp_symlink.c_str()));
190 EXPECT_FALSE(utils::IsSymlink(temp_dir.c_str()));
191 EXPECT_FALSE(utils::IsSymlink(temp_file.c_str()));
192 EXPECT_TRUE(utils::IsSymlink(temp_symlink.c_str()));
193 EXPECT_FALSE(utils::IsSymlink("/non/existent/path"));
194 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
195}
196
adlr@google.com3defe6a2009-12-04 20:57:17 +0000197TEST(UtilsTest, TempFilenameTest) {
198 const string original = "/foo.XXXXXX";
199 const string result = utils::TempFilename(original);
200 EXPECT_EQ(original.size(), result.size());
201 EXPECT_TRUE(utils::StringHasPrefix(result, "/foo."));
202 EXPECT_FALSE(utils::StringHasSuffix(result, "XXXXXX"));
203}
204
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700205TEST(UtilsTest, RootDeviceTest) {
206 EXPECT_EQ("/dev/sda", utils::RootDevice("/dev/sda3"));
207 EXPECT_EQ("/dev/mmc0", utils::RootDevice("/dev/mmc0p3"));
Darin Petkovf74eb652010-08-04 12:08:38 -0700208 EXPECT_EQ("", utils::RootDevice("/dev/foo/bar"));
209 EXPECT_EQ("", utils::RootDevice("/"));
210 EXPECT_EQ("", utils::RootDevice(""));
211}
212
213TEST(UtilsTest, SysfsBlockDeviceTest) {
214 EXPECT_EQ("/sys/block/sda", utils::SysfsBlockDevice("/dev/sda"));
215 EXPECT_EQ("", utils::SysfsBlockDevice("/foo/sda"));
216 EXPECT_EQ("", utils::SysfsBlockDevice("/dev/foo/bar"));
217 EXPECT_EQ("", utils::SysfsBlockDevice("/"));
218 EXPECT_EQ("", utils::SysfsBlockDevice("./"));
219 EXPECT_EQ("", utils::SysfsBlockDevice(""));
220}
221
222TEST(UtilsTest, IsRemovableDeviceTest) {
223 EXPECT_FALSE(utils::IsRemovableDevice(""));
224 EXPECT_FALSE(utils::IsRemovableDevice("/dev/non-existent-device"));
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700225}
226
227TEST(UtilsTest, PartitionNumberTest) {
228 EXPECT_EQ("3", utils::PartitionNumber("/dev/sda3"));
229 EXPECT_EQ("3", utils::PartitionNumber("/dev/mmc0p3"));
230}
231
Chris Sosa4f8ee272012-11-30 13:01:54 -0800232TEST(UtilsTest, CompareCpuSharesTest) {
233 EXPECT_LT(utils::CompareCpuShares(utils::kCpuSharesLow,
234 utils::kCpuSharesNormal), 0);
235 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesNormal,
236 utils::kCpuSharesLow), 0);
237 EXPECT_EQ(utils::CompareCpuShares(utils::kCpuSharesNormal,
238 utils::kCpuSharesNormal), 0);
239 EXPECT_GT(utils::CompareCpuShares(utils::kCpuSharesHigh,
240 utils::kCpuSharesNormal), 0);
Darin Petkovc6c135c2010-08-11 13:36:18 -0700241}
242
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700243TEST(UtilsTest, FuzzIntTest) {
244 static const unsigned int kRanges[] = { 0, 1, 2, 20 };
245 for (size_t r = 0; r < arraysize(kRanges); ++r) {
246 unsigned int range = kRanges[r];
247 const int kValue = 50;
248 for (int tries = 0; tries < 100; ++tries) {
249 int value = utils::FuzzInt(kValue, range);
250 EXPECT_GE(value, kValue - range / 2);
251 EXPECT_LE(value, kValue + range - range / 2);
252 }
253 }
254}
255
Andrew de los Reyescc92cd32010-10-05 16:56:14 -0700256TEST(UtilsTest, ApplyMapTest) {
257 int initial_values[] = {1, 2, 3, 4, 6};
258 vector<int> collection(&initial_values[0],
259 initial_values + arraysize(initial_values));
260 EXPECT_EQ(arraysize(initial_values), collection.size());
261 int expected_values[] = {1, 2, 5, 4, 8};
262 map<int, int> value_map;
263 value_map[3] = 5;
264 value_map[6] = 8;
265 value_map[5] = 10;
266
267 utils::ApplyMap(&collection, value_map);
268
269 size_t index = 0;
270 for (vector<int>::iterator it = collection.begin(), e = collection.end();
271 it != e; ++it) {
272 EXPECT_EQ(expected_values[index++], *it);
273 }
274}
275
Darin Petkovd3f8c892010-10-12 21:38:45 -0700276TEST(UtilsTest, RunAsRootGetFilesystemSizeTest) {
277 string img;
278 EXPECT_TRUE(utils::MakeTempFile("/tmp/img.XXXXXX", &img, NULL));
279 ScopedPathUnlinker img_unlinker(img);
280 CreateExtImageAtPath(img, NULL);
281 // Extend the "partition" holding the file system from 10MiB to 20MiB.
282 EXPECT_EQ(0, System(base::StringPrintf(
283 "dd if=/dev/zero of=%s seek=20971519 bs=1 count=1",
284 img.c_str())));
285 EXPECT_EQ(20 * 1024 * 1024, utils::FileSize(img));
286 int block_count = 0;
287 int block_size = 0;
288 EXPECT_TRUE(utils::GetFilesystemSize(img, &block_count, &block_size));
289 EXPECT_EQ(4096, block_size);
290 EXPECT_EQ(10 * 1024 * 1024 / 4096, block_count);
291}
292
Chris Sosad317e402013-06-12 13:47:09 -0700293TEST(UtilsTest, GetInstallDevTest) {
294 string boot_dev = "/dev/sda5";
295 string install_dev;
296 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
297 EXPECT_EQ(install_dev, "/dev/sda3");
298
299 boot_dev = "/dev/sda3";
300 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
301 EXPECT_EQ(install_dev, "/dev/sda5");
302
303 boot_dev = "/dev/sda12";
304 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Liam McLoughlin049d1652013-07-31 18:47:46 -0700305
306 boot_dev = "/dev/ubiblock3_0";
307 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
308 EXPECT_EQ(install_dev, "/dev/ubiblock5_0");
309
310 boot_dev = "/dev/ubiblock5_0";
311 EXPECT_TRUE(utils::GetInstallDev(boot_dev, &install_dev));
312 EXPECT_EQ(install_dev, "/dev/ubiblock3_0");
313
314 boot_dev = "/dev/ubiblock12_0";
315 EXPECT_FALSE(utils::GetInstallDev(boot_dev, &install_dev));
Chris Sosad317e402013-06-12 13:47:09 -0700316}
317
Andrew de los Reyes712b3ac2011-01-07 13:47:52 -0800318namespace {
319gboolean TerminateScheduleCrashReporterUploadTest(void* arg) {
320 GMainLoop* loop = reinterpret_cast<GMainLoop*>(arg);
321 g_main_loop_quit(loop);
322 return FALSE; // Don't call this callback again
323}
324} // namespace {}
325
326TEST(UtilsTest, ScheduleCrashReporterUploadTest) {
327 // Not much to test. At least this tests for memory leaks, crashes,
328 // log errors.
329 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
330 utils::ScheduleCrashReporterUpload();
331 g_timeout_add_seconds(1, &TerminateScheduleCrashReporterUploadTest, loop);
332 g_main_loop_run(loop);
333 g_main_loop_unref(loop);
334}
335
David Zeuthen674c3182013-04-18 14:05:20 -0700336TEST(UtilsTest, FormatTimeDeltaTest) {
337 // utils::FormatTimeDelta() is not locale-aware (it's only used for logging
338 // which is not localized) so we only need to test the C locale
339 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromMilliseconds(100)),
340 "0.1s");
341 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(0)),
342 "0s");
343 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1)),
344 "1s");
345 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(59)),
346 "59s");
347 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(60)),
348 "1m0s");
349 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(61)),
350 "1m1s");
351 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(90)),
352 "1m30s");
353 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(1205)),
354 "20m5s");
355 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3600)),
356 "1h0m0s");
357 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3601)),
358 "1h0m1s");
359 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(3661)),
360 "1h1m1s");
361 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(7261)),
362 "2h1m1s");
363 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86400)),
364 "1d0h0m0s");
365 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(86401)),
366 "1d0h0m1s");
367 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000)),
368 "2d7h33m20s");
369 EXPECT_EQ(utils::FormatTimeDelta(base::TimeDelta::FromSeconds(200000) +
370 base::TimeDelta::FromMilliseconds(1)),
371 "2d7h33m20.001s");
372}
373
David Zeuthen27a48bc2013-08-06 12:06:29 -0700374TEST(UtilsTest, TimeFromStructTimespecTest) {
375 struct timespec ts;
376
377 // Unix epoch (Thursday 00:00:00 UTC on Jan 1, 1970)
378 ts = (struct timespec) {.tv_sec = 0, .tv_nsec = 0};
379 EXPECT_EQ(base::Time::UnixEpoch(), utils::TimeFromStructTimespec(&ts));
380
381 // 42 ms after the Unix billennium (Sunday 01:46:40 UTC on September 9, 2001)
382 ts = (struct timespec) {.tv_sec = 1000 * 1000 * 1000,
383 .tv_nsec = 42 * 1000 * 1000};
384 base::Time::Exploded exploded = (base::Time::Exploded) {
385 .year = 2001, .month = 9, .day_of_week = 0, .day_of_month = 9,
386 .hour = 1, .minute = 46, .second = 40, .millisecond = 42};
387 EXPECT_EQ(base::Time::FromUTCExploded(exploded),
388 utils::TimeFromStructTimespec(&ts));
389}
390
David Zeuthene7f89172013-10-31 10:21:04 -0700391TEST(UtilsTest, DecodeAndStoreBase64String) {
392 base::FilePath path;
393
394 // Ensure we return false on empty strings or invalid base64.
395 EXPECT_FALSE(utils::DecodeAndStoreBase64String("", &path));
396 EXPECT_FALSE(utils::DecodeAndStoreBase64String("not valid base64", &path));
397
398 // Pass known base64 and check that it matches. This string was generated
399 // the following way:
400 //
401 // $ echo "Update Engine" | base64
402 // VXBkYXRlIEVuZ2luZQo=
403 EXPECT_TRUE(utils::DecodeAndStoreBase64String("VXBkYXRlIEVuZ2luZQo=",
404 &path));
405 ScopedPathUnlinker unlinker(path.value());
406 string expected_contents = "Update Engine\n";
407 string contents;
408 EXPECT_TRUE(utils::ReadFile(path.value(), &contents));
409 EXPECT_EQ(contents, expected_contents);
410 EXPECT_EQ(utils::FileSize(path.value()), expected_contents.size());
411}
412
adlr@google.com3defe6a2009-12-04 20:57:17 +0000413} // namespace chromeos_update_engine