blob: dc0116fe36aac6bd9549aafbda6f499fdbba1462 [file] [log] [blame]
David Zeuthen21e95262016-07-27 17:58:40 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
David Zeuthenc612e2e2016-09-16 16:44:08 -04004 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
David Zeuthen21e95262016-07-27 17:58:40 -040011 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040012 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
David Zeuthen21e95262016-07-27 17:58:40 -040014 *
David Zeuthenc612e2e2016-09-16 16:44:08 -040015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
David Zeuthen21e95262016-07-27 17:58:40 -040023 */
24
25#include <string.h>
26
27#include <gtest/gtest.h>
28
David Zeuthenbaf59e22016-11-14 15:39:43 -050029#include <libavb/libavb.h>
David Zeuthen21e95262016-07-27 17:58:40 -040030
31TEST(UtilTest, RSAPublicKeyHeaderByteswap) {
32 AvbRSAPublicKeyHeader h;
33 AvbRSAPublicKeyHeader s;
34 uint32_t n32;
35 uint64_t n64;
36
37 n32 = 0x11223344;
38 n64 = 0x1122334455667788;
39
40 h.key_num_bits = htobe32(n32);
41 n32++;
42 h.n0inv = htobe32(n32);
43 n32++;
44
45 EXPECT_NE(0, avb_rsa_public_key_header_validate_and_byteswap(&h, &s));
46
47 n32 = 0x11223344;
48 n64 = 0x1122334455667788;
49
50 EXPECT_EQ(n32, s.key_num_bits);
51 n32++;
52 EXPECT_EQ(n32, s.n0inv);
53 n32++;
54}
55
56TEST(UtilTest, FooterByteswap) {
57 AvbFooter h;
58 AvbFooter s;
59 AvbFooter other;
60 AvbFooter bad;
61 uint64_t n64;
62
63 n64 = 0x1122334455667788;
64
65 memcpy(h.magic, AVB_FOOTER_MAGIC, AVB_FOOTER_MAGIC_LEN);
66 h.version_major = htobe32(AVB_FOOTER_MAJOR_VERSION);
67 h.version_minor = htobe32(AVB_FOOTER_MINOR_VERSION);
68 h.original_image_size = htobe64(n64);
69 n64++;
70 h.vbmeta_offset = htobe64(n64);
71 n64++;
72 h.vbmeta_size = htobe64(n64);
73 n64++;
74
75 EXPECT_NE(0, avb_footer_validate_and_byteswap(&h, &s));
76
77 n64 = 0x1122334455667788;
78
79 EXPECT_EQ((uint32_t)AVB_FOOTER_MAJOR_VERSION, s.version_major);
80 EXPECT_EQ((uint32_t)AVB_FOOTER_MINOR_VERSION, s.version_minor);
81 EXPECT_EQ(n64, s.original_image_size);
82 n64++;
83 EXPECT_EQ(n64, s.vbmeta_offset);
84 n64++;
85 EXPECT_EQ(n64, s.vbmeta_size);
86 n64++;
87
88 // Check that the struct still validates if minor is bigger than
89 // what we expect.
90 other = h;
91 h.version_minor = htobe32(AVB_FOOTER_MINOR_VERSION + 1);
92 EXPECT_NE(0, avb_footer_validate_and_byteswap(&other, &s));
93
94 // Check for bad magic.
95 bad = h;
96 bad.magic[0] = 'x';
97 EXPECT_EQ(0, avb_footer_validate_and_byteswap(&bad, &s));
98
99 // Check for bad major version.
100 bad = h;
101 bad.version_major = htobe32(AVB_FOOTER_MAJOR_VERSION + 1);
102 EXPECT_EQ(0, avb_footer_validate_and_byteswap(&bad, &s));
103}
104
105TEST(UtilTest, KernelCmdlineDescriptorByteswap) {
106 AvbKernelCmdlineDescriptor h;
107 AvbKernelCmdlineDescriptor s;
108 AvbKernelCmdlineDescriptor bad;
109 uint64_t nbf;
David Zeuthenfd41eb92016-11-17 12:24:47 -0500110 uint32_t n32;
David Zeuthen21e95262016-07-27 17:58:40 -0400111
David Zeuthenfd41eb92016-11-17 12:24:47 -0500112 // Specify 40 bytes of data past the end of the descriptor struct.
113 nbf = 40 + sizeof(AvbKernelCmdlineDescriptor) - sizeof(AvbDescriptor);
David Zeuthen21e95262016-07-27 17:58:40 -0400114 h.parent_descriptor.num_bytes_following = htobe64(nbf);
115 h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE);
David Zeuthenfd41eb92016-11-17 12:24:47 -0500116 h.kernel_cmdline_length = htobe32(40);
117
118 n32 = 0x11223344;
119 h.flags = htobe32(n32);
120 n32++;
David Zeuthen21e95262016-07-27 17:58:40 -0400121
122 EXPECT_NE(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&h, &s));
123
David Zeuthenfd41eb92016-11-17 12:24:47 -0500124 n32 = 0x11223344;
125 EXPECT_EQ(n32, s.flags);
126 n32++;
127
David Zeuthen21e95262016-07-27 17:58:40 -0400128 EXPECT_EQ(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE, s.parent_descriptor.tag);
129 EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
David Zeuthenfd41eb92016-11-17 12:24:47 -0500130 EXPECT_EQ(40UL, s.kernel_cmdline_length);
David Zeuthen21e95262016-07-27 17:58:40 -0400131
132 // Check for bad tag.
133 bad = h;
134 bad.parent_descriptor.tag = htobe64(0xf00dd00d);
135 EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s));
136
David Zeuthenfd41eb92016-11-17 12:24:47 -0500137 // Doesn't fit in 41 bytes.
David Zeuthen21e95262016-07-27 17:58:40 -0400138 bad = h;
David Zeuthenfd41eb92016-11-17 12:24:47 -0500139 bad.kernel_cmdline_length = htobe32(41);
David Zeuthen21e95262016-07-27 17:58:40 -0400140 EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s));
141}
142
143TEST(UtilTest, HashtreeDescriptorByteswap) {
144 AvbHashtreeDescriptor h;
145 AvbHashtreeDescriptor s;
146 AvbHashtreeDescriptor bad;
147 uint64_t nbf;
148 uint32_t n32;
149 uint64_t n64;
150
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400151 // Specify 44 bytes of data past the end of the descriptor struct.
152 nbf = 44 + sizeof(AvbHashtreeDescriptor) - sizeof(AvbDescriptor);
David Zeuthen21e95262016-07-27 17:58:40 -0400153 h.parent_descriptor.num_bytes_following = htobe64(nbf);
154 h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASHTREE);
155 h.partition_name_len = htobe32(10);
156 h.salt_len = htobe32(10);
157 h.root_digest_len = htobe32(10);
158
159 n32 = 0x11223344;
160 n64 = 0x1122334455667788;
161
162 h.dm_verity_version = htobe32(n32);
163 n32++;
164 h.image_size = htobe64(n64);
165 n64++;
166 h.tree_offset = htobe64(n64);
167 n64++;
168 h.tree_size = htobe64(n64);
169 n64++;
170 h.data_block_size = htobe32(n32);
171 n32++;
172 h.hash_block_size = htobe32(n32);
173 n32++;
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400174 h.fec_num_roots = htobe32(n32);
175 n32++;
176 h.fec_offset = htobe64(n64);
177 n64++;
178 h.fec_size = htobe64(n64);
179 n64++;
David Zeuthen21e95262016-07-27 17:58:40 -0400180
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400181 EXPECT_TRUE(avb_hashtree_descriptor_validate_and_byteswap(&h, &s));
David Zeuthen21e95262016-07-27 17:58:40 -0400182
183 n32 = 0x11223344;
184 n64 = 0x1122334455667788;
185
186 EXPECT_EQ(n32, s.dm_verity_version);
187 n32++;
188 EXPECT_EQ(n64, s.image_size);
189 n64++;
190 EXPECT_EQ(n64, s.tree_offset);
191 n64++;
192 EXPECT_EQ(n64, s.tree_size);
193 n64++;
194 EXPECT_EQ(n32, s.data_block_size);
195 n32++;
196 EXPECT_EQ(n32, s.hash_block_size);
197 n32++;
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400198 EXPECT_EQ(n32, s.fec_num_roots);
199 n32++;
200 EXPECT_EQ(n64, s.fec_offset);
201 n64++;
202 EXPECT_EQ(n64, s.fec_size);
203 n64++;
David Zeuthen21e95262016-07-27 17:58:40 -0400204
205 EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASHTREE, s.parent_descriptor.tag);
206 EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
207 EXPECT_EQ(10UL, s.partition_name_len);
208 EXPECT_EQ(10UL, s.salt_len);
209 EXPECT_EQ(10UL, s.root_digest_len);
210
211 // Check for bad tag.
212 bad = h;
213 bad.parent_descriptor.tag = htobe64(0xf00dd00d);
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400214 EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
David Zeuthen21e95262016-07-27 17:58:40 -0400215
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400216 // Doesn't fit in 44 bytes (30 + 10 + 10 = 50).
David Zeuthen21e95262016-07-27 17:58:40 -0400217 bad = h;
218 bad.partition_name_len = htobe32(30);
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400219 EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
David Zeuthen21e95262016-07-27 17:58:40 -0400220
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400221 // Doesn't fit in 44 bytes (10 + 30 + 10 = 50).
David Zeuthen21e95262016-07-27 17:58:40 -0400222 bad = h;
223 bad.salt_len = htobe32(30);
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400224 EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
David Zeuthen21e95262016-07-27 17:58:40 -0400225
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400226 // Doesn't fit in 44 bytes (10 + 10 + 30 = 50).
David Zeuthen21e95262016-07-27 17:58:40 -0400227 bad = h;
228 bad.root_digest_len = htobe32(30);
David Zeuthen0b7f1d32016-10-25 17:53:49 -0400229 EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
David Zeuthen21e95262016-07-27 17:58:40 -0400230}
231
232TEST(UtilTest, HashDescriptorByteswap) {
233 AvbHashDescriptor h;
234 AvbHashDescriptor s;
235 AvbHashDescriptor bad;
236 uint64_t nbf;
237
238 // Specify 44 bytes of data past the end of the descriptor struct.
239 nbf = 44 + sizeof(AvbHashDescriptor) - sizeof(AvbDescriptor);
240 h.parent_descriptor.num_bytes_following = htobe64(nbf);
241 h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASH);
242 h.partition_name_len = htobe32(10);
243 h.salt_len = htobe32(10);
244 h.digest_len = htobe32(10);
245
246 EXPECT_NE(0, avb_hash_descriptor_validate_and_byteswap(&h, &s));
247
248 EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASH, s.parent_descriptor.tag);
249 EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
250 EXPECT_EQ(10UL, s.partition_name_len);
251 EXPECT_EQ(10UL, s.salt_len);
252 EXPECT_EQ(10UL, s.digest_len);
253
254 // Check for bad tag.
255 bad = h;
256 bad.parent_descriptor.tag = htobe64(0xf00dd00d);
257 EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
258
259 // Doesn't fit in 44 bytes (30 + 10 + 10 = 50).
260 bad = h;
261 bad.partition_name_len = htobe32(30);
262 EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
263
264 // Doesn't fit in 44 bytes (10 + 30 + 10 = 50).
265 bad = h;
266 bad.salt_len = htobe32(30);
267 EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
268
269 // Doesn't fit in 44 bytes (10 + 10 + 30 = 50).
270 bad = h;
271 bad.digest_len = htobe32(30);
272 EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
273}
274
275TEST(UtilTest, ChainPartitionDescriptorByteswap) {
276 AvbChainPartitionDescriptor h;
277 AvbChainPartitionDescriptor s;
278 AvbChainPartitionDescriptor bad;
279 uint64_t nbf;
280
281 // Specify 36 bytes of data past the end of the descriptor struct.
282 nbf = 36 + sizeof(AvbChainPartitionDescriptor) - sizeof(AvbDescriptor);
283 h.parent_descriptor.num_bytes_following = htobe64(nbf);
284 h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION);
David Zeuthen40ee1da2016-11-23 15:14:49 -0500285 h.rollback_index_location = htobe32(42);
David Zeuthen21e95262016-07-27 17:58:40 -0400286 h.partition_name_len = htobe32(16);
287 h.public_key_len = htobe32(17);
288
289 EXPECT_NE(0, avb_chain_partition_descriptor_validate_and_byteswap(&h, &s));
290
291 EXPECT_EQ(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION, s.parent_descriptor.tag);
292 EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
David Zeuthen40ee1da2016-11-23 15:14:49 -0500293 EXPECT_EQ(42UL, s.rollback_index_location);
David Zeuthen21e95262016-07-27 17:58:40 -0400294 EXPECT_EQ(16UL, s.partition_name_len);
295 EXPECT_EQ(17UL, s.public_key_len);
296
297 // Check for bad tag.
298 bad = h;
299 bad.parent_descriptor.tag = htobe64(0xf00dd00d);
300 EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
301
302 // Check for bad rollback index slot (must be at least 1).
303 bad = h;
David Zeuthen40ee1da2016-11-23 15:14:49 -0500304 bad.rollback_index_location = htobe32(0);
David Zeuthen21e95262016-07-27 17:58:40 -0400305 EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
306
307 // Doesn't fit in 40 bytes (24 + 17 = 41).
308 bad = h;
309 bad.partition_name_len = htobe32(24);
310 EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
311
312 // Doesn't fit in 40 bytes (16 + 25 = 41).
313 bad = h;
314 bad.public_key_len = htobe32(25);
315 EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
316}
317
318TEST(UtilTest, PropertyDescriptorByteswap) {
319 AvbPropertyDescriptor h;
320 AvbPropertyDescriptor s;
321 AvbPropertyDescriptor bad;
322 uint64_t nbf;
323
324 // Specify 40 bytes of data past the end of the descriptor struct.
325 nbf = 40 + sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor);
326 h.parent_descriptor.num_bytes_following = htobe64(nbf);
327 h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_PROPERTY);
328 h.key_num_bytes = htobe64(16);
329 h.value_num_bytes = htobe64(17);
330
331 EXPECT_NE(0, avb_property_descriptor_validate_and_byteswap(&h, &s));
332
333 EXPECT_EQ(AVB_DESCRIPTOR_TAG_PROPERTY, s.parent_descriptor.tag);
334 EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
335 EXPECT_EQ(16UL, s.key_num_bytes);
336 EXPECT_EQ(17UL, s.value_num_bytes);
337
338 // Check for bad tag.
339 bad = h;
340 bad.parent_descriptor.tag = htobe64(0xf00dd00d);
341 EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
342
343 // Doesn't fit in 40 bytes (22 + 17 + 2 = 41).
344 bad = h;
345 bad.key_num_bytes = htobe64(22);
346 EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
347
348 // Doesn't fit in 40 bytes (16 + 23 + 2 = 41).
349 bad = h;
350 bad.value_num_bytes = htobe64(23);
351 EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
352}
353
354TEST(UtilTest, DescriptorByteswap) {
355 AvbDescriptor h;
356 AvbDescriptor s;
357 uint64_t n64;
358
359 n64 = 0x1122334455667788;
360
361 h.num_bytes_following = htobe64(n64);
362 n64++;
363 h.tag = htobe64(n64);
364 n64++;
365
366 EXPECT_NE(0, avb_descriptor_validate_and_byteswap(&h, &s));
367
368 n64 = 0x1122334455667788;
369
370 EXPECT_EQ(n64, s.num_bytes_following);
371 n64++;
372 EXPECT_EQ(n64, s.tag);
373 n64++;
374
375 // Check that we catch if |num_bytes_following| isn't divisble by 8.
376 h.num_bytes_following = htobe64(7);
377 EXPECT_EQ(0, avb_descriptor_validate_and_byteswap(&h, &s));
378}
379
380TEST(UtilTest, SafeAddition) {
381 uint64_t value;
382 uint64_t pow2_60 = 1ULL << 60;
383
384 value = 2;
385 EXPECT_NE(0, avb_safe_add_to(&value, 5));
386 EXPECT_EQ(7UL, value);
387
388 /* These should not overflow */
389 value = 1 * pow2_60;
390 EXPECT_NE(0, avb_safe_add_to(&value, 2 * pow2_60));
391 EXPECT_EQ(3 * pow2_60, value);
392 value = 7 * pow2_60;
393 EXPECT_NE(0, avb_safe_add_to(&value, 8 * pow2_60));
394 EXPECT_EQ(15 * pow2_60, value);
395 value = 9 * pow2_60;
396 EXPECT_NE(0, avb_safe_add_to(&value, 3 * pow2_60));
397 EXPECT_EQ(12 * pow2_60, value);
398 value = 0xfffffffffffffffcUL;
399 EXPECT_NE(0, avb_safe_add_to(&value, 2));
400 EXPECT_EQ(0xfffffffffffffffeUL, value);
401
402 /* These should overflow. */
403 value = 8 * pow2_60;
404 EXPECT_EQ(0, avb_safe_add_to(&value, 8 * pow2_60));
405 value = 0xfffffffffffffffcUL;
406 EXPECT_EQ(0, avb_safe_add_to(&value, 4));
407}
408
409static int avb_validate_utf8z(const char* data) {
410 return avb_validate_utf8(reinterpret_cast<const uint8_t*>(data),
411 strlen(data));
412}
413
414TEST(UtilTest, UTF8Validation) {
415 // These should succeed.
416 EXPECT_NE(0, avb_validate_utf8z("foo bar"));
417 // Encoding of U+00E6 LATIN SMALL LETTER AE: æ
418 EXPECT_NE(0, avb_validate_utf8z("foo \xC3\xA6 bar"));
419 // Encoding of U+20AC EURO SIGN: €
420 EXPECT_NE(0, avb_validate_utf8z("foo \xE2\x82\xAC bar"));
421 // Encoding of U+1F466 BOY: 👦
422 EXPECT_NE(0, avb_validate_utf8z("foo \xF0\x9F\x91\xA6 bar"));
423 // All three runes following each other.
424 EXPECT_NE(0, avb_validate_utf8z("\xC3\xA6\xE2\x82\xAC\xF0\x9F\x91\xA6"));
425
426 // These should fail.
427 EXPECT_EQ(0, avb_validate_utf8z("foo \xF8 bar"));
428 EXPECT_EQ(0, avb_validate_utf8z("\xF8"));
429 // Stops in the middle of Unicode rune.
430 EXPECT_EQ(0, avb_validate_utf8z("foo \xC3"));
431}
432
433TEST(UtilTest, StrConcat) {
434 char buf[8];
435
436 // These should succeed.
437 EXPECT_NE(0, avb_str_concat(buf, sizeof buf, "foo", 3, "bar1", 4));
438
439 // This should fail: Insufficient space.
440 EXPECT_EQ(0, avb_str_concat(buf, sizeof buf, "foo0", 4, "bar1", 4));
441}
442
443TEST(UtilTest, StrStr) {
444 const char* haystack = "abc def abcabc";
445
446 EXPECT_EQ(nullptr, avb_strstr(haystack, "needle"));
447 EXPECT_EQ(haystack, avb_strstr(haystack, "abc"));
448 EXPECT_EQ(haystack + 4, avb_strstr(haystack, "def"));
449 EXPECT_EQ(haystack, avb_strstr(haystack, haystack));
450}
451
David Zeuthena8bb9a02016-10-28 14:36:55 -0400452TEST(UtilTest, StrvFindStr) {
453 const char* strings[] = {"abcabc", "abc", "def", nullptr};
454
455 EXPECT_EQ(nullptr, avb_strv_find_str(strings, "not there", 9));
456 EXPECT_EQ(strings[1], avb_strv_find_str(strings, "abc", 3));
457 EXPECT_EQ(strings[2], avb_strv_find_str(strings, "def", 3));
458 EXPECT_EQ(strings[0], avb_strv_find_str(strings, "abcabc", 6));
459}
460
David Zeuthen21e95262016-07-27 17:58:40 -0400461TEST(UtilTest, StrReplace) {
462 // We don't care about leaking strings from avb_replace().
463 EXPECT_EQ("OK blah bah $(FOO OK blah",
464 std::string(avb_replace("$(FOO) blah bah $(FOO $(FOO) blah",
465 "$(FOO)", "OK")));
466 EXPECT_EQ("OK", std::string(avb_replace("$(FOO)", "$(FOO)", "OK")));
467 EXPECT_EQ(" OK", std::string(avb_replace(" $(FOO)", "$(FOO)", "OK")));
468 EXPECT_EQ("OK ", std::string(avb_replace("$(FOO) ", "$(FOO)", "OK")));
469 EXPECT_EQ("LONGSTRINGLONGSTRING",
470 std::string(avb_replace("$(FOO)$(FOO)", "$(FOO)", "LONGSTRING")));
471}
David Zeuthen8b6973b2016-09-20 12:39:49 -0400472
473TEST(UtilTest, Crc32) {
474 /* Compare with output of crc32(1):
475 *
476 * $ (echo -n foobar > /tmp/crc32_input); crc32 /tmp/crc32_input
477 * 9ef61f95
478 */
479 EXPECT_EQ(uint32_t(0x9ef61f95), avb_crc32((const uint8_t*)"foobar", 6));
480}
481
482TEST(UtilTest, htobe32) {
483 EXPECT_EQ(avb_htobe32(0x12345678), htobe32(0x12345678));
484}
485
486TEST(UtilTest, be32toh) {
487 EXPECT_EQ(avb_be32toh(0x12345678), be32toh(0x12345678));
488}
489
490TEST(UtilTest, htobe64) {
491 EXPECT_EQ(avb_htobe64(0x123456789abcdef0), htobe64(0x123456789abcdef0));
492}
493
494TEST(UtilTest, be64toh) {
495 EXPECT_EQ(avb_be64toh(0x123456789abcdef0), be64toh(0x123456789abcdef0));
496}