• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * 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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * 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.
23  */
24 
25 #include <endian.h>
26 #include <string.h>
27 
28 #include <gtest/gtest.h>
29 
30 #include <libavb/avb_sha.h>
31 #include <libavb/libavb.h>
32 
33 #include "avb_unittest_util.h"
34 
35 namespace avb {
36 
37 // Subclass BaseAvbToolTest to check for memory leaks.
38 class UtilTest : public BaseAvbToolTest {
39  public:
UtilTest()40   UtilTest() {}
41 };
42 
TEST_F(UtilTest,RSAPublicKeyHeaderByteswap)43 TEST_F(UtilTest, RSAPublicKeyHeaderByteswap) {
44   AvbRSAPublicKeyHeader h;
45   AvbRSAPublicKeyHeader s;
46   uint32_t n32;
47   uint64_t n64;
48 
49   n32 = 0x11223344;
50   n64 = 0x1122334455667788;
51 
52   h.key_num_bits = htobe32(n32);
53   n32++;
54   h.n0inv = htobe32(n32);
55   n32++;
56 
57   EXPECT_NE(0, avb_rsa_public_key_header_validate_and_byteswap(&h, &s));
58 
59   n32 = 0x11223344;
60   n64 = 0x1122334455667788;
61 
62   EXPECT_EQ(n32, s.key_num_bits);
63   n32++;
64   EXPECT_EQ(n32, s.n0inv);
65   n32++;
66 }
67 
TEST_F(UtilTest,FooterByteswap)68 TEST_F(UtilTest, FooterByteswap) {
69   AvbFooter h;
70   AvbFooter s;
71   AvbFooter other;
72   AvbFooter bad;
73   uint64_t n64;
74 
75   n64 = 0x1122334455667788;
76 
77   memcpy(h.magic, AVB_FOOTER_MAGIC, AVB_FOOTER_MAGIC_LEN);
78   h.version_major = htobe32(AVB_FOOTER_VERSION_MAJOR);
79   h.version_minor = htobe32(AVB_FOOTER_VERSION_MINOR);
80   h.original_image_size = htobe64(n64);
81   n64++;
82   h.vbmeta_offset = htobe64(n64);
83   n64++;
84   h.vbmeta_size = htobe64(n64);
85   n64++;
86 
87   EXPECT_NE(0, avb_footer_validate_and_byteswap(&h, &s));
88 
89   n64 = 0x1122334455667788;
90 
91   EXPECT_EQ((uint32_t)AVB_FOOTER_VERSION_MAJOR, s.version_major);
92   EXPECT_EQ((uint32_t)AVB_FOOTER_VERSION_MINOR, s.version_minor);
93   EXPECT_EQ(n64, s.original_image_size);
94   n64++;
95   EXPECT_EQ(n64, s.vbmeta_offset);
96   n64++;
97   EXPECT_EQ(n64, s.vbmeta_size);
98   n64++;
99 
100   // Check that the struct still validates if minor is bigger than
101   // what we expect.
102   other = h;
103   h.version_minor = htobe32(AVB_FOOTER_VERSION_MINOR + 1);
104   EXPECT_NE(0, avb_footer_validate_and_byteswap(&other, &s));
105 
106   // Check for bad magic.
107   bad = h;
108   bad.magic[0] = 'x';
109   EXPECT_EQ(0, avb_footer_validate_and_byteswap(&bad, &s));
110 
111   // Check for bad major version.
112   bad = h;
113   bad.version_major = htobe32(AVB_FOOTER_VERSION_MAJOR + 1);
114   EXPECT_EQ(0, avb_footer_validate_and_byteswap(&bad, &s));
115 }
116 
TEST_F(UtilTest,KernelCmdlineDescriptorByteswap)117 TEST_F(UtilTest, KernelCmdlineDescriptorByteswap) {
118   AvbKernelCmdlineDescriptor h;
119   AvbKernelCmdlineDescriptor s;
120   AvbKernelCmdlineDescriptor bad;
121   uint64_t nbf;
122   uint32_t n32;
123 
124   // Specify 40 bytes of data past the end of the descriptor struct.
125   nbf = 40 + sizeof(AvbKernelCmdlineDescriptor) - sizeof(AvbDescriptor);
126   h.parent_descriptor.num_bytes_following = htobe64(nbf);
127   h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE);
128   h.kernel_cmdline_length = htobe32(40);
129 
130   n32 = 0x11223344;
131   h.flags = htobe32(n32);
132   n32++;
133 
134   EXPECT_NE(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&h, &s));
135 
136   n32 = 0x11223344;
137   EXPECT_EQ(n32, s.flags);
138   n32++;
139 
140   EXPECT_EQ(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE, s.parent_descriptor.tag);
141   EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
142   EXPECT_EQ(40UL, s.kernel_cmdline_length);
143 
144   // Check for bad tag.
145   bad = h;
146   bad.parent_descriptor.tag = htobe64(0xf00dd00d);
147   EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s));
148 
149   // Doesn't fit in 41 bytes.
150   bad = h;
151   bad.kernel_cmdline_length = htobe32(41);
152   EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s));
153 }
154 
TEST_F(UtilTest,HashtreeDescriptorByteswap)155 TEST_F(UtilTest, HashtreeDescriptorByteswap) {
156   AvbHashtreeDescriptor h;
157   AvbHashtreeDescriptor s;
158   AvbHashtreeDescriptor bad;
159   uint64_t nbf;
160   uint32_t n32;
161   uint64_t n64;
162 
163   // Specify 44 bytes of data past the end of the descriptor struct.
164   nbf = 44 + sizeof(AvbHashtreeDescriptor) - sizeof(AvbDescriptor);
165   h.parent_descriptor.num_bytes_following = htobe64(nbf);
166   h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASHTREE);
167   h.partition_name_len = htobe32(10);
168   h.salt_len = htobe32(10);
169   h.root_digest_len = htobe32(10);
170 
171   n32 = 0x11223344;
172   n64 = 0x1122334455667788;
173 
174   h.dm_verity_version = htobe32(n32);
175   n32++;
176   h.image_size = htobe64(n64);
177   n64++;
178   h.tree_offset = htobe64(n64);
179   n64++;
180   h.tree_size = htobe64(n64);
181   n64++;
182   h.data_block_size = htobe32(n32);
183   n32++;
184   h.hash_block_size = htobe32(n32);
185   n32++;
186   h.fec_num_roots = htobe32(n32);
187   n32++;
188   h.fec_offset = htobe64(n64);
189   n64++;
190   h.fec_size = htobe64(n64);
191   n64++;
192 
193   EXPECT_TRUE(avb_hashtree_descriptor_validate_and_byteswap(&h, &s));
194 
195   n32 = 0x11223344;
196   n64 = 0x1122334455667788;
197 
198   EXPECT_EQ(n32, s.dm_verity_version);
199   n32++;
200   EXPECT_EQ(n64, s.image_size);
201   n64++;
202   EXPECT_EQ(n64, s.tree_offset);
203   n64++;
204   EXPECT_EQ(n64, s.tree_size);
205   n64++;
206   EXPECT_EQ(n32, s.data_block_size);
207   n32++;
208   EXPECT_EQ(n32, s.hash_block_size);
209   n32++;
210   EXPECT_EQ(n32, s.fec_num_roots);
211   n32++;
212   EXPECT_EQ(n64, s.fec_offset);
213   n64++;
214   EXPECT_EQ(n64, s.fec_size);
215   n64++;
216 
217   EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASHTREE, s.parent_descriptor.tag);
218   EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
219   EXPECT_EQ(10UL, s.partition_name_len);
220   EXPECT_EQ(10UL, s.salt_len);
221   EXPECT_EQ(10UL, s.root_digest_len);
222 
223   // Check for bad tag.
224   bad = h;
225   bad.parent_descriptor.tag = htobe64(0xf00dd00d);
226   EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
227 
228   // Doesn't fit in 44 bytes (30 + 10 + 10 = 50).
229   bad = h;
230   bad.partition_name_len = htobe32(30);
231   EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
232 
233   // Doesn't fit in 44 bytes (10 + 30 + 10 = 50).
234   bad = h;
235   bad.salt_len = htobe32(30);
236   EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
237 
238   // Doesn't fit in 44 bytes (10 + 10 + 30 = 50).
239   bad = h;
240   bad.root_digest_len = htobe32(30);
241   EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s));
242 }
243 
TEST_F(UtilTest,HashDescriptorByteswap)244 TEST_F(UtilTest, HashDescriptorByteswap) {
245   AvbHashDescriptor h;
246   AvbHashDescriptor s;
247   AvbHashDescriptor bad;
248   uint64_t nbf;
249 
250   // Specify 44 bytes of data past the end of the descriptor struct.
251   nbf = 44 + sizeof(AvbHashDescriptor) - sizeof(AvbDescriptor);
252   h.parent_descriptor.num_bytes_following = htobe64(nbf);
253   h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASH);
254   h.partition_name_len = htobe32(10);
255   h.salt_len = htobe32(10);
256   h.digest_len = htobe32(10);
257 
258   EXPECT_NE(0, avb_hash_descriptor_validate_and_byteswap(&h, &s));
259 
260   EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASH, s.parent_descriptor.tag);
261   EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
262   EXPECT_EQ(10UL, s.partition_name_len);
263   EXPECT_EQ(10UL, s.salt_len);
264   EXPECT_EQ(10UL, s.digest_len);
265 
266   // Check for bad tag.
267   bad = h;
268   bad.parent_descriptor.tag = htobe64(0xf00dd00d);
269   EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
270 
271   // Doesn't fit in 44 bytes (30 + 10 + 10 = 50).
272   bad = h;
273   bad.partition_name_len = htobe32(30);
274   EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
275 
276   // Doesn't fit in 44 bytes (10 + 30 + 10 = 50).
277   bad = h;
278   bad.salt_len = htobe32(30);
279   EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
280 
281   // Doesn't fit in 44 bytes (10 + 10 + 30 = 50).
282   bad = h;
283   bad.digest_len = htobe32(30);
284   EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s));
285 }
286 
TEST_F(UtilTest,ChainPartitionDescriptorByteswap)287 TEST_F(UtilTest, ChainPartitionDescriptorByteswap) {
288   AvbChainPartitionDescriptor h;
289   AvbChainPartitionDescriptor s;
290   AvbChainPartitionDescriptor bad;
291   uint64_t nbf;
292 
293   // Specify 36 bytes of data past the end of the descriptor struct.
294   nbf = 36 + sizeof(AvbChainPartitionDescriptor) - sizeof(AvbDescriptor);
295   h.parent_descriptor.num_bytes_following = htobe64(nbf);
296   h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION);
297   h.rollback_index_location = htobe32(42);
298   h.partition_name_len = htobe32(16);
299   h.public_key_len = htobe32(17);
300 
301   EXPECT_NE(0, avb_chain_partition_descriptor_validate_and_byteswap(&h, &s));
302 
303   EXPECT_EQ(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION, s.parent_descriptor.tag);
304   EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
305   EXPECT_EQ(42UL, s.rollback_index_location);
306   EXPECT_EQ(16UL, s.partition_name_len);
307   EXPECT_EQ(17UL, s.public_key_len);
308 
309   // Check for bad tag.
310   bad = h;
311   bad.parent_descriptor.tag = htobe64(0xf00dd00d);
312   EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
313 
314   // Check for bad rollback index slot (must be at least 1).
315   bad = h;
316   bad.rollback_index_location = htobe32(0);
317   EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
318 
319   // Doesn't fit in 40 bytes (24 + 17 = 41).
320   bad = h;
321   bad.partition_name_len = htobe32(24);
322   EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
323 
324   // Doesn't fit in 40 bytes (16 + 25 = 41).
325   bad = h;
326   bad.public_key_len = htobe32(25);
327   EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s));
328 }
329 
TEST_F(UtilTest,PropertyDescriptorByteswap)330 TEST_F(UtilTest, PropertyDescriptorByteswap) {
331   AvbPropertyDescriptor h;
332   AvbPropertyDescriptor s;
333   AvbPropertyDescriptor bad;
334   uint64_t nbf;
335 
336   // Specify 40 bytes of data past the end of the descriptor struct.
337   nbf = 40 + sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor);
338   h.parent_descriptor.num_bytes_following = htobe64(nbf);
339   h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_PROPERTY);
340   h.key_num_bytes = htobe64(16);
341   h.value_num_bytes = htobe64(17);
342 
343   EXPECT_NE(0, avb_property_descriptor_validate_and_byteswap(&h, &s));
344 
345   EXPECT_EQ(AVB_DESCRIPTOR_TAG_PROPERTY, s.parent_descriptor.tag);
346   EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following);
347   EXPECT_EQ(16UL, s.key_num_bytes);
348   EXPECT_EQ(17UL, s.value_num_bytes);
349 
350   // Check for bad tag.
351   bad = h;
352   bad.parent_descriptor.tag = htobe64(0xf00dd00d);
353   EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
354 
355   // Doesn't fit in 40 bytes (22 + 17 + 2 = 41).
356   bad = h;
357   bad.key_num_bytes = htobe64(22);
358   EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
359 
360   // Doesn't fit in 40 bytes (16 + 23 + 2 = 41).
361   bad = h;
362   bad.value_num_bytes = htobe64(23);
363   EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s));
364 }
365 
TEST_F(UtilTest,DescriptorByteswap)366 TEST_F(UtilTest, DescriptorByteswap) {
367   AvbDescriptor h;
368   AvbDescriptor s;
369   uint64_t n64;
370 
371   n64 = 0x1122334455667788;
372 
373   h.num_bytes_following = htobe64(n64);
374   n64++;
375   h.tag = htobe64(n64);
376   n64++;
377 
378   EXPECT_NE(0, avb_descriptor_validate_and_byteswap(&h, &s));
379 
380   n64 = 0x1122334455667788;
381 
382   EXPECT_EQ(n64, s.num_bytes_following);
383   n64++;
384   EXPECT_EQ(n64, s.tag);
385   n64++;
386 
387   // Check that we catch if |num_bytes_following| isn't divisble by 8.
388   h.num_bytes_following = htobe64(7);
389   EXPECT_EQ(0, avb_descriptor_validate_and_byteswap(&h, &s));
390 }
391 
TEST_F(UtilTest,SafeAddition)392 TEST_F(UtilTest, SafeAddition) {
393   uint64_t value;
394   uint64_t pow2_60 = 1ULL << 60;
395 
396   value = 2;
397   EXPECT_NE(0, avb_safe_add_to(&value, 5));
398   EXPECT_EQ(7UL, value);
399 
400   /* These should not overflow */
401   value = 1 * pow2_60;
402   EXPECT_NE(0, avb_safe_add_to(&value, 2 * pow2_60));
403   EXPECT_EQ(3 * pow2_60, value);
404   value = 7 * pow2_60;
405   EXPECT_NE(0, avb_safe_add_to(&value, 8 * pow2_60));
406   EXPECT_EQ(15 * pow2_60, value);
407   value = 9 * pow2_60;
408   EXPECT_NE(0, avb_safe_add_to(&value, 3 * pow2_60));
409   EXPECT_EQ(12 * pow2_60, value);
410   value = 0xfffffffffffffffcUL;
411   EXPECT_NE(0, avb_safe_add_to(&value, 2));
412   EXPECT_EQ(0xfffffffffffffffeUL, value);
413 
414   /* These should overflow. */
415   value = 8 * pow2_60;
416   EXPECT_EQ(0, avb_safe_add_to(&value, 8 * pow2_60));
417   value = 0xfffffffffffffffcUL;
418   EXPECT_EQ(0, avb_safe_add_to(&value, 4));
419 }
420 
avb_validate_utf8z(const char * data)421 static int avb_validate_utf8z(const char* data) {
422   return avb_validate_utf8(reinterpret_cast<const uint8_t*>(data),
423                            strlen(data));
424 }
425 
TEST_F(UtilTest,UTF8Validation)426 TEST_F(UtilTest, UTF8Validation) {
427   // These should succeed.
428   EXPECT_NE(0, avb_validate_utf8z("foo bar"));
429   // Encoding of U+00E6 LATIN SMALL LETTER AE: æ
430   EXPECT_NE(0, avb_validate_utf8z("foo \xC3\xA6 bar"));
431   // Encoding of U+20AC EURO SIGN: €
432   EXPECT_NE(0, avb_validate_utf8z("foo \xE2\x82\xAC bar"));
433   // Encoding of U+1F466 BOY: ��
434   EXPECT_NE(0, avb_validate_utf8z("foo \xF0\x9F\x91\xA6 bar"));
435   // All three runes following each other.
436   EXPECT_NE(0, avb_validate_utf8z("\xC3\xA6\xE2\x82\xAC\xF0\x9F\x91\xA6"));
437 
438   // These should fail.
439   EXPECT_EQ(0, avb_validate_utf8z("foo \xF8 bar"));
440   EXPECT_EQ(0, avb_validate_utf8z("\xF8"));
441   // Stops in the middle of Unicode rune.
442   EXPECT_EQ(0, avb_validate_utf8z("foo \xC3"));
443 }
444 
TEST_F(UtilTest,StrConcat)445 TEST_F(UtilTest, StrConcat) {
446   char buf[8];
447 
448   // These should succeed.
449   EXPECT_NE(0, avb_str_concat(buf, sizeof buf, "foo", 3, "bar1", 4));
450 
451   // This should fail: Insufficient space.
452   EXPECT_EQ(0, avb_str_concat(buf, sizeof buf, "foo0", 4, "bar1", 4));
453 }
454 
TEST_F(UtilTest,StrStr)455 TEST_F(UtilTest, StrStr) {
456   const char* haystack = "abc def abcabc";
457 
458   EXPECT_EQ(nullptr, avb_strstr(haystack, "needle"));
459   EXPECT_EQ(haystack, avb_strstr(haystack, "abc"));
460   EXPECT_EQ(haystack + 4, avb_strstr(haystack, "def"));
461   EXPECT_EQ(haystack, avb_strstr(haystack, haystack));
462 }
463 
TEST_F(UtilTest,StrvFindStr)464 TEST_F(UtilTest, StrvFindStr) {
465   const char* strings[] = {"abcabc", "abc", "def", nullptr};
466 
467   EXPECT_EQ(nullptr, avb_strv_find_str(strings, "not there", 9));
468   EXPECT_EQ(strings[1], avb_strv_find_str(strings, "abc", 3));
469   EXPECT_EQ(strings[2], avb_strv_find_str(strings, "def", 3));
470   EXPECT_EQ(strings[0], avb_strv_find_str(strings, "abcabc", 6));
471 }
472 
TEST_F(UtilTest,StrReplace)473 TEST_F(UtilTest, StrReplace) {
474   char* str;
475 
476   str = avb_replace("$(FOO) blah bah $(FOO $(FOO) blah", "$(FOO)", "OK");
477   EXPECT_EQ("OK blah bah $(FOO OK blah", std::string(str));
478   avb_free(str);
479 
480   str = avb_replace("$(FOO)", "$(FOO)", "OK");
481   EXPECT_EQ("OK", std::string(str));
482   avb_free(str);
483 
484   str = avb_replace(" $(FOO)", "$(FOO)", "OK");
485   EXPECT_EQ(" OK", std::string(str));
486   avb_free(str);
487 
488   str = avb_replace("$(FOO) ", "$(FOO)", "OK");
489   EXPECT_EQ("OK ", std::string(str));
490   avb_free(str);
491 
492   str = avb_replace("$(FOO)$(FOO)", "$(FOO)", "LONGSTRING");
493   EXPECT_EQ("LONGSTRINGLONGSTRING", std::string(str));
494   avb_free(str);
495 }
496 
TEST_F(UtilTest,StrDupV)497 TEST_F(UtilTest, StrDupV) {
498   char* str;
499 
500   str = avb_strdupv("x", "y", "z", NULL);
501   EXPECT_EQ("xyz", std::string(str));
502   avb_free(str);
503 
504   str = avb_strdupv("Hello", "World", " XYZ", NULL);
505   EXPECT_EQ("HelloWorld XYZ", std::string(str));
506   avb_free(str);
507 }
508 
TEST_F(UtilTest,Crc32)509 TEST_F(UtilTest, Crc32) {
510   /* Compare with output of crc32(1):
511    *
512    *  $ (echo -n foobar > /tmp/crc32_input); crc32 /tmp/crc32_input
513    *  9ef61f95
514    */
515   EXPECT_EQ(uint32_t(0x9ef61f95), avb_crc32((const uint8_t*)"foobar", 6));
516 }
517 
TEST_F(UtilTest,htobe32)518 TEST_F(UtilTest, htobe32) {
519   EXPECT_EQ(avb_htobe32(0x12345678), htobe32(0x12345678));
520 }
521 
TEST_F(UtilTest,be32toh)522 TEST_F(UtilTest, be32toh) {
523   EXPECT_EQ(avb_be32toh(0x12345678), be32toh(0x12345678));
524 }
525 
TEST_F(UtilTest,htobe64)526 TEST_F(UtilTest, htobe64) {
527   EXPECT_EQ(avb_htobe64(0x123456789abcdef0), htobe64(0x123456789abcdef0));
528 }
529 
TEST_F(UtilTest,be64toh)530 TEST_F(UtilTest, be64toh) {
531   EXPECT_EQ(avb_be64toh(0x123456789abcdef0), be64toh(0x123456789abcdef0));
532 }
533 
TEST_F(UtilTest,Basename)534 TEST_F(UtilTest, Basename) {
535   EXPECT_EQ("foobar.c", std::string(avb_basename("foobar.c")));
536   EXPECT_EQ("foobar.c", std::string(avb_basename("/path/to/foobar.c")));
537   EXPECT_EQ("foobar.c", std::string(avb_basename("a/foobar.c")));
538   EXPECT_EQ("baz.c", std::string(avb_basename("/baz.c")));
539   EXPECT_EQ("some_dir/", std::string(avb_basename("some_dir/")));
540   EXPECT_EQ("some_dir/", std::string(avb_basename("/path/to/some_dir/")));
541   EXPECT_EQ("some_dir/", std::string(avb_basename("a/some_dir/")));
542   EXPECT_EQ("some_dir/", std::string(avb_basename("/some_dir/")));
543   EXPECT_EQ("/", std::string(avb_basename("/")));
544 }
545 
TEST_F(UtilTest,Sha256)546 TEST_F(UtilTest, Sha256) {
547   AvbSHA256Ctx ctx;
548 
549   /* Compare with
550    *
551    * $ echo -n foobar |sha256sum
552    * c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2 -
553    */
554   avb_sha256_init(&ctx);
555   avb_sha256_update(&ctx, (const uint8_t*)"foobar", 6);
556   EXPECT_EQ("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
557             mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
558 }
559 
560 // Disabled for now because it takes ~30 seconds to run.
TEST_F(UtilTest,DISABLED_Sha256Large)561 TEST_F(UtilTest, DISABLED_Sha256Large) {
562   AvbSHA256Ctx ctx;
563 
564   /* Also check we this works with greater than 4GiB input. Compare with
565    *
566    * $ dd if=/dev/zero bs=1048576 count=4097 |sha256sum
567    * 829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747 -
568    */
569   const size_t kMebibyte = 1048576;
570   uint8_t* megabuf;
571   megabuf = new uint8_t[kMebibyte];
572   memset((char*)megabuf, '\0', kMebibyte);
573   avb_sha256_init(&ctx);
574   for (size_t n = 0; n < 4097; n++) {
575     avb_sha256_update(&ctx, megabuf, kMebibyte);
576   }
577   EXPECT_EQ("829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747",
578             mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
579   delete[] megabuf;
580 }
581 
TEST_F(UtilTest,Sha512)582 TEST_F(UtilTest, Sha512) {
583   AvbSHA512Ctx ctx;
584 
585   /* Compare with
586    *
587    * $ echo -n foobar |sha512sum
588    * 0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b012587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425
589    * -
590    */
591   avb_sha512_init(&ctx);
592   avb_sha512_update(&ctx, (const uint8_t*)"foobar", 6);
593   EXPECT_EQ(
594       "0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b0"
595       "12587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425",
596       mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
597 }
598 
599 // Disabled for now because it takes ~30 seconds to run.
TEST_F(UtilTest,DISABLED_Sha512Large)600 TEST_F(UtilTest, DISABLED_Sha512Large) {
601   AvbSHA512Ctx ctx;
602 
603   /* Also check we this works with greater than 4GiB input. Compare with
604    *
605    * $ dd if=/dev/zero bs=1048576 count=4097 |sha512sum
606    * eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf291fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0
607    * -
608    */
609   const size_t kMebibyte = 1048576;
610   uint8_t* megabuf;
611   megabuf = new uint8_t[kMebibyte];
612   memset((char*)megabuf, '\0', kMebibyte);
613   avb_sha512_init(&ctx);
614   for (size_t n = 0; n < 4097; n++) {
615     avb_sha512_update(&ctx, megabuf, kMebibyte);
616   }
617   EXPECT_EQ(
618       "eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf2"
619       "91fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0",
620       mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
621   delete[] megabuf;
622 }
623 
624 }  // namespace avb
625