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