David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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: |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 11 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 14 | * |
David Zeuthen | c612e2e | 2016-09-16 16:44:08 -0400 | [diff] [blame] | 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. |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | #include "libavb.h" |
| 30 | |
| 31 | TEST(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 | |
| 56 | TEST(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 | |
| 105 | TEST(UtilTest, KernelCmdlineDescriptorByteswap) { |
| 106 | AvbKernelCmdlineDescriptor h; |
| 107 | AvbKernelCmdlineDescriptor s; |
| 108 | AvbKernelCmdlineDescriptor bad; |
| 109 | uint64_t nbf; |
| 110 | |
| 111 | // Specify 44 bytes of data past the end of the descriptor struct. |
| 112 | nbf = 44 + sizeof(AvbKernelCmdlineDescriptor) - sizeof(AvbDescriptor); |
| 113 | h.parent_descriptor.num_bytes_following = htobe64(nbf); |
| 114 | h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE); |
| 115 | h.kernel_cmdline_length = htobe32(44); |
| 116 | |
| 117 | EXPECT_NE(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&h, &s)); |
| 118 | |
| 119 | EXPECT_EQ(AVB_DESCRIPTOR_TAG_KERNEL_CMDLINE, s.parent_descriptor.tag); |
| 120 | EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following); |
| 121 | EXPECT_EQ(44UL, s.kernel_cmdline_length); |
| 122 | |
| 123 | // Check for bad tag. |
| 124 | bad = h; |
| 125 | bad.parent_descriptor.tag = htobe64(0xf00dd00d); |
| 126 | EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s)); |
| 127 | |
| 128 | // Doesn't fit in 45 bytes. |
| 129 | bad = h; |
| 130 | bad.kernel_cmdline_length = htobe32(45); |
| 131 | EXPECT_EQ(0, avb_kernel_cmdline_descriptor_validate_and_byteswap(&bad, &s)); |
| 132 | } |
| 133 | |
| 134 | TEST(UtilTest, HashtreeDescriptorByteswap) { |
| 135 | AvbHashtreeDescriptor h; |
| 136 | AvbHashtreeDescriptor s; |
| 137 | AvbHashtreeDescriptor bad; |
| 138 | uint64_t nbf; |
| 139 | uint32_t n32; |
| 140 | uint64_t n64; |
| 141 | |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 142 | // Specify 44 bytes of data past the end of the descriptor struct. |
| 143 | nbf = 44 + sizeof(AvbHashtreeDescriptor) - sizeof(AvbDescriptor); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 144 | h.parent_descriptor.num_bytes_following = htobe64(nbf); |
| 145 | h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASHTREE); |
| 146 | h.partition_name_len = htobe32(10); |
| 147 | h.salt_len = htobe32(10); |
| 148 | h.root_digest_len = htobe32(10); |
| 149 | |
| 150 | n32 = 0x11223344; |
| 151 | n64 = 0x1122334455667788; |
| 152 | |
| 153 | h.dm_verity_version = htobe32(n32); |
| 154 | n32++; |
| 155 | h.image_size = htobe64(n64); |
| 156 | n64++; |
| 157 | h.tree_offset = htobe64(n64); |
| 158 | n64++; |
| 159 | h.tree_size = htobe64(n64); |
| 160 | n64++; |
| 161 | h.data_block_size = htobe32(n32); |
| 162 | n32++; |
| 163 | h.hash_block_size = htobe32(n32); |
| 164 | n32++; |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 165 | h.fec_num_roots = htobe32(n32); |
| 166 | n32++; |
| 167 | h.fec_offset = htobe64(n64); |
| 168 | n64++; |
| 169 | h.fec_size = htobe64(n64); |
| 170 | n64++; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 171 | |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 172 | EXPECT_TRUE(avb_hashtree_descriptor_validate_and_byteswap(&h, &s)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 173 | |
| 174 | n32 = 0x11223344; |
| 175 | n64 = 0x1122334455667788; |
| 176 | |
| 177 | EXPECT_EQ(n32, s.dm_verity_version); |
| 178 | n32++; |
| 179 | EXPECT_EQ(n64, s.image_size); |
| 180 | n64++; |
| 181 | EXPECT_EQ(n64, s.tree_offset); |
| 182 | n64++; |
| 183 | EXPECT_EQ(n64, s.tree_size); |
| 184 | n64++; |
| 185 | EXPECT_EQ(n32, s.data_block_size); |
| 186 | n32++; |
| 187 | EXPECT_EQ(n32, s.hash_block_size); |
| 188 | n32++; |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 189 | EXPECT_EQ(n32, s.fec_num_roots); |
| 190 | n32++; |
| 191 | EXPECT_EQ(n64, s.fec_offset); |
| 192 | n64++; |
| 193 | EXPECT_EQ(n64, s.fec_size); |
| 194 | n64++; |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 195 | |
| 196 | EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASHTREE, s.parent_descriptor.tag); |
| 197 | EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following); |
| 198 | EXPECT_EQ(10UL, s.partition_name_len); |
| 199 | EXPECT_EQ(10UL, s.salt_len); |
| 200 | EXPECT_EQ(10UL, s.root_digest_len); |
| 201 | |
| 202 | // Check for bad tag. |
| 203 | bad = h; |
| 204 | bad.parent_descriptor.tag = htobe64(0xf00dd00d); |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 205 | EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 206 | |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 207 | // Doesn't fit in 44 bytes (30 + 10 + 10 = 50). |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 208 | bad = h; |
| 209 | bad.partition_name_len = htobe32(30); |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 210 | EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 211 | |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 212 | // Doesn't fit in 44 bytes (10 + 30 + 10 = 50). |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 213 | bad = h; |
| 214 | bad.salt_len = htobe32(30); |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 215 | EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 216 | |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 217 | // Doesn't fit in 44 bytes (10 + 10 + 30 = 50). |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 218 | bad = h; |
| 219 | bad.root_digest_len = htobe32(30); |
David Zeuthen | 0b7f1d3 | 2016-10-25 17:53:49 -0400 | [diff] [blame] | 220 | EXPECT_FALSE(avb_hashtree_descriptor_validate_and_byteswap(&bad, &s)); |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | TEST(UtilTest, HashDescriptorByteswap) { |
| 224 | AvbHashDescriptor h; |
| 225 | AvbHashDescriptor s; |
| 226 | AvbHashDescriptor bad; |
| 227 | uint64_t nbf; |
| 228 | |
| 229 | // Specify 44 bytes of data past the end of the descriptor struct. |
| 230 | nbf = 44 + sizeof(AvbHashDescriptor) - sizeof(AvbDescriptor); |
| 231 | h.parent_descriptor.num_bytes_following = htobe64(nbf); |
| 232 | h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_HASH); |
| 233 | h.partition_name_len = htobe32(10); |
| 234 | h.salt_len = htobe32(10); |
| 235 | h.digest_len = htobe32(10); |
| 236 | |
| 237 | EXPECT_NE(0, avb_hash_descriptor_validate_and_byteswap(&h, &s)); |
| 238 | |
| 239 | EXPECT_EQ(AVB_DESCRIPTOR_TAG_HASH, s.parent_descriptor.tag); |
| 240 | EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following); |
| 241 | EXPECT_EQ(10UL, s.partition_name_len); |
| 242 | EXPECT_EQ(10UL, s.salt_len); |
| 243 | EXPECT_EQ(10UL, s.digest_len); |
| 244 | |
| 245 | // Check for bad tag. |
| 246 | bad = h; |
| 247 | bad.parent_descriptor.tag = htobe64(0xf00dd00d); |
| 248 | EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s)); |
| 249 | |
| 250 | // Doesn't fit in 44 bytes (30 + 10 + 10 = 50). |
| 251 | bad = h; |
| 252 | bad.partition_name_len = htobe32(30); |
| 253 | EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s)); |
| 254 | |
| 255 | // Doesn't fit in 44 bytes (10 + 30 + 10 = 50). |
| 256 | bad = h; |
| 257 | bad.salt_len = htobe32(30); |
| 258 | EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s)); |
| 259 | |
| 260 | // Doesn't fit in 44 bytes (10 + 10 + 30 = 50). |
| 261 | bad = h; |
| 262 | bad.digest_len = htobe32(30); |
| 263 | EXPECT_EQ(0, avb_hash_descriptor_validate_and_byteswap(&bad, &s)); |
| 264 | } |
| 265 | |
| 266 | TEST(UtilTest, ChainPartitionDescriptorByteswap) { |
| 267 | AvbChainPartitionDescriptor h; |
| 268 | AvbChainPartitionDescriptor s; |
| 269 | AvbChainPartitionDescriptor bad; |
| 270 | uint64_t nbf; |
| 271 | |
| 272 | // Specify 36 bytes of data past the end of the descriptor struct. |
| 273 | nbf = 36 + sizeof(AvbChainPartitionDescriptor) - sizeof(AvbDescriptor); |
| 274 | h.parent_descriptor.num_bytes_following = htobe64(nbf); |
| 275 | h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION); |
| 276 | h.rollback_index_slot = htobe32(42); |
| 277 | h.partition_name_len = htobe32(16); |
| 278 | h.public_key_len = htobe32(17); |
| 279 | |
| 280 | EXPECT_NE(0, avb_chain_partition_descriptor_validate_and_byteswap(&h, &s)); |
| 281 | |
| 282 | EXPECT_EQ(AVB_DESCRIPTOR_TAG_CHAIN_PARTITION, s.parent_descriptor.tag); |
| 283 | EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following); |
| 284 | EXPECT_EQ(42UL, s.rollback_index_slot); |
| 285 | EXPECT_EQ(16UL, s.partition_name_len); |
| 286 | EXPECT_EQ(17UL, s.public_key_len); |
| 287 | |
| 288 | // Check for bad tag. |
| 289 | bad = h; |
| 290 | bad.parent_descriptor.tag = htobe64(0xf00dd00d); |
| 291 | EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s)); |
| 292 | |
| 293 | // Check for bad rollback index slot (must be at least 1). |
| 294 | bad = h; |
| 295 | bad.rollback_index_slot = htobe32(0); |
| 296 | EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s)); |
| 297 | |
| 298 | // Doesn't fit in 40 bytes (24 + 17 = 41). |
| 299 | bad = h; |
| 300 | bad.partition_name_len = htobe32(24); |
| 301 | EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s)); |
| 302 | |
| 303 | // Doesn't fit in 40 bytes (16 + 25 = 41). |
| 304 | bad = h; |
| 305 | bad.public_key_len = htobe32(25); |
| 306 | EXPECT_EQ(0, avb_chain_partition_descriptor_validate_and_byteswap(&bad, &s)); |
| 307 | } |
| 308 | |
| 309 | TEST(UtilTest, PropertyDescriptorByteswap) { |
| 310 | AvbPropertyDescriptor h; |
| 311 | AvbPropertyDescriptor s; |
| 312 | AvbPropertyDescriptor bad; |
| 313 | uint64_t nbf; |
| 314 | |
| 315 | // Specify 40 bytes of data past the end of the descriptor struct. |
| 316 | nbf = 40 + sizeof(AvbPropertyDescriptor) - sizeof(AvbDescriptor); |
| 317 | h.parent_descriptor.num_bytes_following = htobe64(nbf); |
| 318 | h.parent_descriptor.tag = htobe64(AVB_DESCRIPTOR_TAG_PROPERTY); |
| 319 | h.key_num_bytes = htobe64(16); |
| 320 | h.value_num_bytes = htobe64(17); |
| 321 | |
| 322 | EXPECT_NE(0, avb_property_descriptor_validate_and_byteswap(&h, &s)); |
| 323 | |
| 324 | EXPECT_EQ(AVB_DESCRIPTOR_TAG_PROPERTY, s.parent_descriptor.tag); |
| 325 | EXPECT_EQ(nbf, s.parent_descriptor.num_bytes_following); |
| 326 | EXPECT_EQ(16UL, s.key_num_bytes); |
| 327 | EXPECT_EQ(17UL, s.value_num_bytes); |
| 328 | |
| 329 | // Check for bad tag. |
| 330 | bad = h; |
| 331 | bad.parent_descriptor.tag = htobe64(0xf00dd00d); |
| 332 | EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s)); |
| 333 | |
| 334 | // Doesn't fit in 40 bytes (22 + 17 + 2 = 41). |
| 335 | bad = h; |
| 336 | bad.key_num_bytes = htobe64(22); |
| 337 | EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s)); |
| 338 | |
| 339 | // Doesn't fit in 40 bytes (16 + 23 + 2 = 41). |
| 340 | bad = h; |
| 341 | bad.value_num_bytes = htobe64(23); |
| 342 | EXPECT_EQ(0, avb_property_descriptor_validate_and_byteswap(&bad, &s)); |
| 343 | } |
| 344 | |
| 345 | TEST(UtilTest, DescriptorByteswap) { |
| 346 | AvbDescriptor h; |
| 347 | AvbDescriptor s; |
| 348 | uint64_t n64; |
| 349 | |
| 350 | n64 = 0x1122334455667788; |
| 351 | |
| 352 | h.num_bytes_following = htobe64(n64); |
| 353 | n64++; |
| 354 | h.tag = htobe64(n64); |
| 355 | n64++; |
| 356 | |
| 357 | EXPECT_NE(0, avb_descriptor_validate_and_byteswap(&h, &s)); |
| 358 | |
| 359 | n64 = 0x1122334455667788; |
| 360 | |
| 361 | EXPECT_EQ(n64, s.num_bytes_following); |
| 362 | n64++; |
| 363 | EXPECT_EQ(n64, s.tag); |
| 364 | n64++; |
| 365 | |
| 366 | // Check that we catch if |num_bytes_following| isn't divisble by 8. |
| 367 | h.num_bytes_following = htobe64(7); |
| 368 | EXPECT_EQ(0, avb_descriptor_validate_and_byteswap(&h, &s)); |
| 369 | } |
| 370 | |
| 371 | TEST(UtilTest, SafeAddition) { |
| 372 | uint64_t value; |
| 373 | uint64_t pow2_60 = 1ULL << 60; |
| 374 | |
| 375 | value = 2; |
| 376 | EXPECT_NE(0, avb_safe_add_to(&value, 5)); |
| 377 | EXPECT_EQ(7UL, value); |
| 378 | |
| 379 | /* These should not overflow */ |
| 380 | value = 1 * pow2_60; |
| 381 | EXPECT_NE(0, avb_safe_add_to(&value, 2 * pow2_60)); |
| 382 | EXPECT_EQ(3 * pow2_60, value); |
| 383 | value = 7 * pow2_60; |
| 384 | EXPECT_NE(0, avb_safe_add_to(&value, 8 * pow2_60)); |
| 385 | EXPECT_EQ(15 * pow2_60, value); |
| 386 | value = 9 * pow2_60; |
| 387 | EXPECT_NE(0, avb_safe_add_to(&value, 3 * pow2_60)); |
| 388 | EXPECT_EQ(12 * pow2_60, value); |
| 389 | value = 0xfffffffffffffffcUL; |
| 390 | EXPECT_NE(0, avb_safe_add_to(&value, 2)); |
| 391 | EXPECT_EQ(0xfffffffffffffffeUL, value); |
| 392 | |
| 393 | /* These should overflow. */ |
| 394 | value = 8 * pow2_60; |
| 395 | EXPECT_EQ(0, avb_safe_add_to(&value, 8 * pow2_60)); |
| 396 | value = 0xfffffffffffffffcUL; |
| 397 | EXPECT_EQ(0, avb_safe_add_to(&value, 4)); |
| 398 | } |
| 399 | |
| 400 | static int avb_validate_utf8z(const char* data) { |
| 401 | return avb_validate_utf8(reinterpret_cast<const uint8_t*>(data), |
| 402 | strlen(data)); |
| 403 | } |
| 404 | |
| 405 | TEST(UtilTest, UTF8Validation) { |
| 406 | // These should succeed. |
| 407 | EXPECT_NE(0, avb_validate_utf8z("foo bar")); |
| 408 | // Encoding of U+00E6 LATIN SMALL LETTER AE: æ |
| 409 | EXPECT_NE(0, avb_validate_utf8z("foo \xC3\xA6 bar")); |
| 410 | // Encoding of U+20AC EURO SIGN: € |
| 411 | EXPECT_NE(0, avb_validate_utf8z("foo \xE2\x82\xAC bar")); |
| 412 | // Encoding of U+1F466 BOY: 👦 |
| 413 | EXPECT_NE(0, avb_validate_utf8z("foo \xF0\x9F\x91\xA6 bar")); |
| 414 | // All three runes following each other. |
| 415 | EXPECT_NE(0, avb_validate_utf8z("\xC3\xA6\xE2\x82\xAC\xF0\x9F\x91\xA6")); |
| 416 | |
| 417 | // These should fail. |
| 418 | EXPECT_EQ(0, avb_validate_utf8z("foo \xF8 bar")); |
| 419 | EXPECT_EQ(0, avb_validate_utf8z("\xF8")); |
| 420 | // Stops in the middle of Unicode rune. |
| 421 | EXPECT_EQ(0, avb_validate_utf8z("foo \xC3")); |
| 422 | } |
| 423 | |
| 424 | TEST(UtilTest, StrConcat) { |
| 425 | char buf[8]; |
| 426 | |
| 427 | // These should succeed. |
| 428 | EXPECT_NE(0, avb_str_concat(buf, sizeof buf, "foo", 3, "bar1", 4)); |
| 429 | |
| 430 | // This should fail: Insufficient space. |
| 431 | EXPECT_EQ(0, avb_str_concat(buf, sizeof buf, "foo0", 4, "bar1", 4)); |
| 432 | } |
| 433 | |
| 434 | TEST(UtilTest, StrStr) { |
| 435 | const char* haystack = "abc def abcabc"; |
| 436 | |
| 437 | EXPECT_EQ(nullptr, avb_strstr(haystack, "needle")); |
| 438 | EXPECT_EQ(haystack, avb_strstr(haystack, "abc")); |
| 439 | EXPECT_EQ(haystack + 4, avb_strstr(haystack, "def")); |
| 440 | EXPECT_EQ(haystack, avb_strstr(haystack, haystack)); |
| 441 | } |
| 442 | |
David Zeuthen | a8bb9a0 | 2016-10-28 14:36:55 -0400 | [diff] [blame^] | 443 | TEST(UtilTest, StrvFindStr) { |
| 444 | const char* strings[] = {"abcabc", "abc", "def", nullptr}; |
| 445 | |
| 446 | EXPECT_EQ(nullptr, avb_strv_find_str(strings, "not there", 9)); |
| 447 | EXPECT_EQ(strings[1], avb_strv_find_str(strings, "abc", 3)); |
| 448 | EXPECT_EQ(strings[2], avb_strv_find_str(strings, "def", 3)); |
| 449 | EXPECT_EQ(strings[0], avb_strv_find_str(strings, "abcabc", 6)); |
| 450 | } |
| 451 | |
David Zeuthen | 21e9526 | 2016-07-27 17:58:40 -0400 | [diff] [blame] | 452 | TEST(UtilTest, StrReplace) { |
| 453 | // We don't care about leaking strings from avb_replace(). |
| 454 | EXPECT_EQ("OK blah bah $(FOO OK blah", |
| 455 | std::string(avb_replace("$(FOO) blah bah $(FOO $(FOO) blah", |
| 456 | "$(FOO)", "OK"))); |
| 457 | EXPECT_EQ("OK", std::string(avb_replace("$(FOO)", "$(FOO)", "OK"))); |
| 458 | EXPECT_EQ(" OK", std::string(avb_replace(" $(FOO)", "$(FOO)", "OK"))); |
| 459 | EXPECT_EQ("OK ", std::string(avb_replace("$(FOO) ", "$(FOO)", "OK"))); |
| 460 | EXPECT_EQ("LONGSTRINGLONGSTRING", |
| 461 | std::string(avb_replace("$(FOO)$(FOO)", "$(FOO)", "LONGSTRING"))); |
| 462 | } |
David Zeuthen | 8b6973b | 2016-09-20 12:39:49 -0400 | [diff] [blame] | 463 | |
| 464 | TEST(UtilTest, Crc32) { |
| 465 | /* Compare with output of crc32(1): |
| 466 | * |
| 467 | * $ (echo -n foobar > /tmp/crc32_input); crc32 /tmp/crc32_input |
| 468 | * 9ef61f95 |
| 469 | */ |
| 470 | EXPECT_EQ(uint32_t(0x9ef61f95), avb_crc32((const uint8_t*)"foobar", 6)); |
| 471 | } |
| 472 | |
| 473 | TEST(UtilTest, htobe32) { |
| 474 | EXPECT_EQ(avb_htobe32(0x12345678), htobe32(0x12345678)); |
| 475 | } |
| 476 | |
| 477 | TEST(UtilTest, be32toh) { |
| 478 | EXPECT_EQ(avb_be32toh(0x12345678), be32toh(0x12345678)); |
| 479 | } |
| 480 | |
| 481 | TEST(UtilTest, htobe64) { |
| 482 | EXPECT_EQ(avb_htobe64(0x123456789abcdef0), htobe64(0x123456789abcdef0)); |
| 483 | } |
| 484 | |
| 485 | TEST(UtilTest, be64toh) { |
| 486 | EXPECT_EQ(avb_be64toh(0x123456789abcdef0), be64toh(0x123456789abcdef0)); |
| 487 | } |