Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (C) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (C) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by the GPL v2 license that can | 2 // Use of this source code is governed by the GPL v2 license that can |
| 3 // be found in the LICENSE file. | 3 // be found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Basic unittesting of dm-bht using google-gtest. | 5 // Basic unittesting of dm-bht using google-gtest. |
| 6 | 6 |
| 7 #include <base/basictypes.h> | 7 #include <base/basictypes.h> |
| 8 #include <base/logging.h> | 8 #include <base/logging.h> |
| 9 #include <base/scoped_ptr.h> | 9 #include <base/scoped_ptr.h> |
| 10 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #endif | 22 #endif |
| 23 } | 23 } |
| 24 | 24 |
| 25 void *my_memalign(size_t boundary, size_t size) { | 25 void *my_memalign(size_t boundary, size_t size) { |
| 26 void * memptr; | 26 void * memptr; |
| 27 if (posix_memalign(&memptr, boundary, size)) | 27 if (posix_memalign(&memptr, boundary, size)) |
| 28 return NULL; | 28 return NULL; |
| 29 return memptr; | 29 return memptr; |
| 30 } | 30 } |
| 31 | 31 |
| 32 | |
| 33 | |
| 34 TEST(DmBht, CreateFailOnOverflow) { | 32 TEST(DmBht, CreateFailOnOverflow) { |
| 35 struct dm_bht bht; | 33 struct dm_bht bht; |
| 36 // This should fail. | 34 // This should fail. |
| 37 EXPECT_EQ(-EINVAL, dm_bht_create(&bht, 32, 1, "sha256")); | 35 EXPECT_EQ(-EINVAL, dm_bht_create(&bht, UINT_MAX, "sha1")); |
| 38 } | 36 } |
| 39 | 37 |
| 40 // Simple test to help valgrind/tcmalloc catch bad mem management | 38 // Simple test to help valgrind/tcmalloc catch bad mem management |
| 41 TEST(DmBht, CreateZeroPopulateDestroy) { | 39 TEST(DmBht, CreateZeroPopulateDestroy) { |
| 42 struct dm_bht bht; | 40 struct dm_bht bht; |
| 43 // This should fail. | 41 // This should fail. |
| 44 unsigned int blocks = 16384; | 42 unsigned int blocks = 16384; |
| 45 u8 *data = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 43 u8 *data = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 46 | 44 |
| 47 // Store all the block hashes of blocks of 0. | 45 // Store all the block hashes of blocks of 0. |
| 48 memset(reinterpret_cast<void *>(data), 0, sizeof(data)); | 46 memset(reinterpret_cast<void *>(data), 0, sizeof(data)); |
| 49 EXPECT_EQ(0, dm_bht_create(&bht, 2, blocks, "sha256")); | 47 EXPECT_EQ(0, dm_bht_create(&bht, blocks, "sha256")); |
| 50 dm_bht_set_read_cb(&bht, dm_bht_zeroread_callback); | 48 dm_bht_set_read_cb(&bht, dm_bht_zeroread_callback); |
| 51 do { | 49 do { |
| 52 EXPECT_EQ(dm_bht_store_block(&bht, blocks - 1, data), 0); | 50 EXPECT_EQ(dm_bht_store_block(&bht, blocks - 1, data), 0); |
| 53 } while (--blocks > 0); | 51 } while (--blocks > 0); |
| 54 EXPECT_EQ(0, dm_bht_compute(&bht, NULL)); | 52 EXPECT_EQ(0, dm_bht_compute(&bht, NULL)); |
| 55 EXPECT_EQ(0, dm_bht_destroy(&bht)); | 53 EXPECT_EQ(0, dm_bht_destroy(&bht)); |
| 56 free(data); | 54 free(data); |
| 57 } | 55 } |
| 58 | 56 |
| 59 class MemoryBhtTest : public ::testing::Test { | 57 class MemoryBhtTest : public ::testing::Test { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 sector_t count, | 92 sector_t count, |
| 95 struct dm_bht_entry *entry) { | 93 struct dm_bht_entry *entry) { |
| 96 MemoryBhtTest *mbht = reinterpret_cast<MemoryBhtTest *>(mbht_instance); | 94 MemoryBhtTest *mbht = reinterpret_cast<MemoryBhtTest *>(mbht_instance); |
| 97 mbht->Read(start, dst, count); | 95 mbht->Read(start, dst, count); |
| 98 dm_bht_read_completed(entry, 0); | 96 dm_bht_read_completed(entry, 0); |
| 99 return 0; | 97 return 0; |
| 100 } | 98 } |
| 101 | 99 |
| 102 protected: | 100 protected: |
| 103 // Creates a new dm_bht and sets it in the existing MemoryBht. | 101 // Creates a new dm_bht and sets it in the existing MemoryBht. |
| 104 void NewBht(const unsigned int depth, | 102 void NewBht(const unsigned int total_blocks, |
| 105 const unsigned int total_blocks, | |
| 106 const char *digest_algorithm) { | 103 const char *digest_algorithm) { |
| 107 bht_.reset(new dm_bht()); | 104 bht_.reset(new dm_bht()); |
| 108 EXPECT_EQ(0, dm_bht_create(bht_.get(),depth, total_blocks, | 105 EXPECT_EQ(0, dm_bht_create(bht_.get(), total_blocks, |
| 109 digest_algorithm)); | 106 digest_algorithm)); |
| 110 if (hash_data_.get() == NULL) { | 107 if (hash_data_.get() == NULL) { |
| 111 sectors_ = dm_bht_sectors(bht_.get()); | 108 sectors_ = dm_bht_sectors(bht_.get()); |
| 112 hash_data_.reset(new u8[to_bytes(sectors_)]); | 109 hash_data_.reset(new u8[to_bytes(sectors_)]); |
| 113 } | 110 } |
| 114 dm_bht_set_write_cb(bht_.get(), MemoryBhtTest::WriteCallback); | 111 dm_bht_set_write_cb(bht_.get(), MemoryBhtTest::WriteCallback); |
| 115 dm_bht_set_read_cb(bht_.get(), MemoryBhtTest::ReadCallback); | 112 dm_bht_set_read_cb(bht_.get(), MemoryBhtTest::ReadCallback); |
| 116 } | 113 } |
| 117 void SetupBht(const unsigned int depth, | 114 void SetupBht(const unsigned int total_blocks, |
| 118 const unsigned int total_blocks, | |
| 119 const char *digest_algorithm) { | 115 const char *digest_algorithm) { |
| 120 NewBht(depth, total_blocks, digest_algorithm); | 116 NewBht(total_blocks, digest_algorithm); |
| 121 | 117 |
| 122 u8 *data = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 118 u8 *data = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 123 | 119 |
| 124 memset(data, 0, PAGE_SIZE); | 120 memset(data, 0, PAGE_SIZE); |
| 125 | 121 |
| 126 unsigned int blocks = total_blocks; | 122 unsigned int blocks = total_blocks; |
| 127 do { | 123 do { |
| 128 EXPECT_EQ(dm_bht_store_block(bht_.get(), blocks - 1, data), 0); | 124 EXPECT_EQ(dm_bht_store_block(bht_.get(), blocks - 1, data), 0); |
| 129 } while (--blocks > 0); | 125 } while (--blocks > 0); |
| 130 | 126 |
| 131 dm_bht_set_read_cb(bht_.get(), dm_bht_zeroread_callback); | 127 dm_bht_set_read_cb(bht_.get(), dm_bht_zeroread_callback); |
| 132 EXPECT_EQ(0, dm_bht_compute(bht_.get(), NULL)); | 128 EXPECT_EQ(0, dm_bht_compute(bht_.get(), NULL)); |
| 133 EXPECT_EQ(0, dm_bht_sync(bht_.get(), reinterpret_cast<void *>(this))); | 129 EXPECT_EQ(0, dm_bht_sync(bht_.get(), reinterpret_cast<void *>(this))); |
| 134 u8 digest[1024]; | 130 u8 digest[1024]; |
| 135 dm_bht_root_hexdigest(bht_.get(), digest, sizeof(digest)); | 131 dm_bht_root_hexdigest(bht_.get(), digest, sizeof(digest)); |
| 136 LOG(INFO) << "MemoryBhtTest root is " << digest; | 132 LOG(INFO) << "MemoryBhtTest root is " << digest; |
| 137 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 133 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 138 // bht is now dead and mbht_ is a prepared hash image | 134 // bht is now dead and mbht_ is a prepared hash image |
| 139 | 135 |
| 140 NewBht(depth, total_blocks, digest_algorithm); | 136 NewBht(total_blocks, digest_algorithm); |
| 141 | 137 |
| 142 // Load the tree from the pre-populated hash data | 138 // Load the tree from the pre-populated hash data |
| 143 for (blocks = 0; blocks < total_blocks; blocks += bht_->node_count) { | 139 for (blocks = 0; blocks < total_blocks; blocks += bht_->node_count) { |
| 144 EXPECT_GE(dm_bht_populate(bht_.get(), | 140 EXPECT_GE(dm_bht_populate(bht_.get(), |
| 145 reinterpret_cast<void *>(this), | 141 reinterpret_cast<void *>(this), |
| 146 blocks), | 142 blocks), |
| 147 DM_BHT_ENTRY_REQUESTED); | 143 DM_BHT_ENTRY_REQUESTED); |
| 148 // Since we're testing synchronously, a second run through should yield | 144 // Since we're testing synchronously, a second run through should yield |
| 149 // READY. | 145 // READY. |
| 150 EXPECT_GE(dm_bht_populate(bht_.get(), | 146 EXPECT_GE(dm_bht_populate(bht_.get(), |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 163 TEST_F(MemoryBhtTest, CreateThenVerifyOk) { | 159 TEST_F(MemoryBhtTest, CreateThenVerifyOk) { |
| 164 static const unsigned int total_blocks = 16384; | 160 static const unsigned int total_blocks = 16384; |
| 165 // Set the root hash for a 0-filled image | 161 // Set the root hash for a 0-filled image |
| 166 static const char kRootDigest[] = | 162 static const char kRootDigest[] = |
| 167 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; | 163 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; |
| 168 // A page of all zeros | 164 // A page of all zeros |
| 169 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 165 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 170 | 166 |
| 171 memset(zero_page, 0, PAGE_SIZE); | 167 memset(zero_page, 0, PAGE_SIZE); |
| 172 | 168 |
| 173 SetupBht(2, total_blocks, "sha256"); | 169 SetupBht(total_blocks, "sha256"); |
| 174 dm_bht_set_root_hexdigest(bht_.get(), | 170 dm_bht_set_root_hexdigest(bht_.get(), |
| 175 reinterpret_cast<const u8 *>(kRootDigest)); | 171 reinterpret_cast<const u8 *>(kRootDigest)); |
| 176 | 172 |
| 177 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | |
| 178 DLOG(INFO) << "verifying block: " << blocks; | |
| 179 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | |
| 180 } | |
| 181 | |
| 182 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | |
| 183 free(zero_page); | |
| 184 } | |
| 185 | |
| 186 TEST_F(MemoryBhtTest, CreateThenVerifyMultipleLevels) { | |
| 187 static const unsigned int total_blocks = 16384; | |
| 188 // Set the root hash for a 0-filled image | |
| 189 static const char kRootDigest[] = | |
| 190 "c86619624d3456f711dbb94d4ad79a4b029f6fd3b5a4a90b155c856bf5b3409b"; | |
| 191 // A page of all zeros | |
| 192 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | |
| 193 | |
| 194 memset(zero_page, 0, PAGE_SIZE); | |
| 195 | |
| 196 SetupBht(4, total_blocks, "sha256"); | |
| 197 dm_bht_set_root_hexdigest(bht_.get(), | |
| 198 reinterpret_cast<const u8 *>(kRootDigest)); | |
| 199 | |
| 200 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | 173 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { |
| 201 DLOG(INFO) << "verifying block: " << blocks; | 174 DLOG(INFO) << "verifying block: " << blocks; |
| 202 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | 175 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); |
| 203 } | 176 } |
| 204 | 177 |
| 205 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 178 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 206 free(zero_page); | 179 free(zero_page); |
| 207 } | 180 } |
| 208 | 181 |
| 209 TEST_F(MemoryBhtTest, CreateThenVerifyZeroDepth) { | 182 TEST_F(MemoryBhtTest, CreateThenVerifySingleLevel) { |
|
Will Drewry
2011/04/12 03:12:32
Nit: SingleBlock :) 32 blocks will all end up hash
| |
| 210 static const unsigned int total_blocks = 16384; | 183 static const unsigned int total_blocks = 32; |
| 211 // Set the root hash for a 0-filled image | 184 // Set the root hash for a 0-filled image |
| 212 static const char kRootDigest[] = | 185 static const char kRootDigest[] = |
| 213 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; | 186 "2d3a43008286f56536fa24dcdbf14d342f0548827e374210415c7be0b610d2ba"; |
|
Will Drewry
2011/04/12 03:12:32
FWIW, I did manually cross check this hash:
$ rm /
| |
| 214 // A page of all zeros | 187 // A page of all zeros |
| 215 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 188 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 216 | 189 |
| 217 memset(zero_page, 0, PAGE_SIZE); | 190 memset(zero_page, 0, PAGE_SIZE); |
| 218 | 191 |
| 219 SetupBht(0, total_blocks, "sha256"); | 192 SetupBht(total_blocks, "sha256"); |
| 220 dm_bht_set_root_hexdigest(bht_.get(), | 193 dm_bht_set_root_hexdigest(bht_.get(), |
| 221 reinterpret_cast<const u8 *>(kRootDigest)); | 194 reinterpret_cast<const u8 *>(kRootDigest)); |
| 222 | 195 |
| 223 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | 196 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { |
| 224 DLOG(INFO) << "verifying block: " << blocks; | 197 DLOG(INFO) << "verifying block: " << blocks; |
| 225 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | 198 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); |
| 226 } | 199 } |
| 227 | 200 |
| 228 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 201 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 229 free(zero_page); | 202 free(zero_page); |
| 230 } | 203 } |
| 231 | 204 |
| 232 TEST_F(MemoryBhtTest, CreateThenVerifyRealParameters) { | 205 TEST_F(MemoryBhtTest, CreateThenVerifyRealParameters) { |
| 233 static const unsigned int total_blocks = 217600; | 206 static const unsigned int total_blocks = 217600; |
| 234 // Set the root hash for a 0-filled image | 207 // Set the root hash for a 0-filled image |
| 235 static const char kRootDigest[] = | 208 static const char kRootDigest[] = |
| 236 "15d5a180b5080a1d43e3fbd1f2cd021d0fc3ea91a8e330bad468b980c2fd4d8b"; | 209 "15d5a180b5080a1d43e3fbd1f2cd021d0fc3ea91a8e330bad468b980c2fd4d8b"; |
| 237 // A page of all zeros | 210 // A page of all zeros |
| 238 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 211 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 239 | 212 |
| 240 memset(zero_page, 0, PAGE_SIZE); | 213 memset(zero_page, 0, PAGE_SIZE); |
| 241 | 214 |
| 242 SetupBht(3, total_blocks, "sha256"); | 215 SetupBht(total_blocks, "sha256"); |
| 243 dm_bht_set_root_hexdigest(bht_.get(), | 216 dm_bht_set_root_hexdigest(bht_.get(), |
| 244 reinterpret_cast<const u8 *>(kRootDigest)); | 217 reinterpret_cast<const u8 *>(kRootDigest)); |
| 245 | 218 |
| 246 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | 219 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { |
| 247 DLOG(INFO) << "verifying block: " << blocks; | 220 DLOG(INFO) << "verifying block: " << blocks; |
| 248 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | 221 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); |
| 249 } | 222 } |
| 250 | 223 |
| 251 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 224 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 252 free(zero_page); | 225 free(zero_page); |
| 253 } | 226 } |
| 254 | 227 |
| 255 TEST_F(MemoryBhtTest, CreateThenVerifyOddLeafCount) { | 228 TEST_F(MemoryBhtTest, CreateThenVerifyOddLeafCount) { |
| 256 static const unsigned int total_blocks = 16383; | 229 static const unsigned int total_blocks = 16383; |
| 257 // Set the root hash for a 0-filled image | 230 // Set the root hash for a 0-filled image |
| 258 static const char kRootDigest[] = | 231 static const char kRootDigest[] = |
| 259 "c78d187c430465bd7831fe4908247b6ab5107e3a826d933b71e85aa9a932e03c"; | 232 "dc8cec4220d388b05ba75c853f858bb8cc25edfb1d5d2f3be6bdf9edfa66dc6a"; |
| 260 // A page of all zeros | 233 // A page of all zeros |
| 261 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 234 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 262 | 235 |
| 263 memset(zero_page, 0, PAGE_SIZE); | 236 memset(zero_page, 0, PAGE_SIZE); |
| 264 | 237 |
| 265 SetupBht(4, total_blocks, "sha256"); | 238 SetupBht(total_blocks, "sha256"); |
| 266 dm_bht_set_root_hexdigest(bht_.get(), | 239 dm_bht_set_root_hexdigest(bht_.get(), |
| 267 reinterpret_cast<const u8 *>(kRootDigest)); | 240 reinterpret_cast<const u8 *>(kRootDigest)); |
| 268 | 241 |
| 269 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | 242 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { |
| 270 DLOG(INFO) << "verifying block: " << blocks; | 243 DLOG(INFO) << "verifying block: " << blocks; |
| 271 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | 244 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); |
| 272 } | 245 } |
| 273 | 246 |
| 274 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 247 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 275 free(zero_page); | 248 free(zero_page); |
| 276 } | 249 } |
| 277 | 250 |
| 278 TEST_F(MemoryBhtTest, CreateThenVerifyOddNodeCount) { | 251 TEST_F(MemoryBhtTest, CreateThenVerifyOddNodeCount) { |
| 279 static const unsigned int total_blocks = 16000; | 252 static const unsigned int total_blocks = 16000; |
| 280 // Set the root hash for a 0-filled image | 253 // Set the root hash for a 0-filled image |
| 281 static const char kRootDigest[] = | 254 static const char kRootDigest[] = |
| 282 "13e04b6aa410187b900834aa23e45f3e5240b0c4d2fadb2d8836a357c33499f0"; | 255 "10832dd62c427bcf68c56c8de0d1f9c32b61d9e5ddf43c77c56a97b372ad4b07"; |
| 283 // A page of all zeros | 256 // A page of all zeros |
| 284 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 257 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 285 | 258 |
| 286 memset(zero_page, 0, PAGE_SIZE); | 259 memset(zero_page, 0, PAGE_SIZE); |
| 287 | 260 |
| 288 SetupBht(4, total_blocks, "sha256"); | 261 SetupBht(total_blocks, "sha256"); |
| 289 dm_bht_set_root_hexdigest(bht_.get(), | 262 dm_bht_set_root_hexdigest(bht_.get(), |
| 290 reinterpret_cast<const u8 *>(kRootDigest)); | 263 reinterpret_cast<const u8 *>(kRootDigest)); |
| 291 | 264 |
| 292 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { | 265 for (unsigned int blocks = 0; blocks < total_blocks; ++blocks) { |
| 293 DLOG(INFO) << "verifying block: " << blocks; | 266 DLOG(INFO) << "verifying block: " << blocks; |
| 294 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); | 267 EXPECT_EQ(0, dm_bht_verify_block(bht_.get(), blocks, zero_page)); |
| 295 } | 268 } |
| 296 | 269 |
| 297 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 270 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 298 free(zero_page); | 271 free(zero_page); |
| 299 } | 272 } |
| 300 | 273 |
| 301 TEST_F(MemoryBhtTest, CreateThenVerifyBadHashBlock) { | 274 TEST_F(MemoryBhtTest, CreateThenVerifyBadHashBlock) { |
| 302 static const unsigned int total_blocks = 16384; | 275 static const unsigned int total_blocks = 16384; |
| 303 // Set the root hash for a 0-filled image | 276 // Set the root hash for a 0-filled image |
| 304 static const char kRootDigest[] = | 277 static const char kRootDigest[] = |
| 305 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; | 278 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; |
| 306 // A page of all zeros | 279 // A page of all zeros |
| 307 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 280 u8 *zero_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 308 | 281 |
| 309 memset(zero_page, 0, PAGE_SIZE); | 282 memset(zero_page, 0, PAGE_SIZE); |
| 310 | 283 |
| 311 SetupBht(2, total_blocks, "sha256"); | 284 SetupBht(total_blocks, "sha256"); |
| 312 | 285 |
| 313 dm_bht_set_root_hexdigest(bht_.get(), | 286 dm_bht_set_root_hexdigest(bht_.get(), |
| 314 reinterpret_cast<const u8 *>(kRootDigest)); | 287 reinterpret_cast<const u8 *>(kRootDigest)); |
| 315 | 288 |
| 316 // TODO(wad) add tests for partial tree validity/verification | 289 // TODO(wad) add tests for partial tree validity/verification |
| 317 | 290 |
| 318 // Corrupt one has hblock | 291 // Corrupt one has hblock |
| 319 static const unsigned int kBadBlock = 256; | 292 static const unsigned int kBadBlock = 256; |
| 320 u8 *bad_hash_block= (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 293 u8 *bad_hash_block= (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 321 memset(bad_hash_block, 'A', PAGE_SIZE); | 294 memset(bad_hash_block, 'A', PAGE_SIZE); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 340 zero_page), | 313 zero_page), |
| 341 0); | 314 0); |
| 342 | 315 |
| 343 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 316 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 344 free(bad_hash_block); | 317 free(bad_hash_block); |
| 345 free(zero_page); | 318 free(zero_page); |
| 346 } | 319 } |
| 347 | 320 |
| 348 TEST_F(MemoryBhtTest, CreateThenVerifyBadDataBlock) { | 321 TEST_F(MemoryBhtTest, CreateThenVerifyBadDataBlock) { |
| 349 static const unsigned int total_blocks = 384; | 322 static const unsigned int total_blocks = 384; |
| 350 SetupBht(2, total_blocks, "sha256"); | 323 SetupBht(total_blocks, "sha256"); |
| 351 // Set the root hash for a 0-filled image | 324 // Set the root hash for a 0-filled image |
| 352 static const char kRootDigest[] = | 325 static const char kRootDigest[] = |
| 353 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; | 326 "45d65d6f9e5a962f4d80b5f1bd7a918152251c27bdad8c5f52b590c129833372"; |
| 354 dm_bht_set_root_hexdigest(bht_.get(), | 327 dm_bht_set_root_hexdigest(bht_.get(), |
| 355 reinterpret_cast<const u8 *>(kRootDigest)); | 328 reinterpret_cast<const u8 *>(kRootDigest)); |
| 356 // A corrupt page | 329 // A corrupt page |
| 357 u8 *bad_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); | 330 u8 *bad_page = (u8 *)my_memalign(PAGE_SIZE, PAGE_SIZE); |
| 358 | 331 |
| 359 memset(bad_page, 'A', PAGE_SIZE); | 332 memset(bad_page, 'A', PAGE_SIZE); |
| 360 | 333 |
| 361 | 334 |
| 362 EXPECT_LT(dm_bht_verify_block(bht_.get(), 0, bad_page), 0); | 335 EXPECT_LT(dm_bht_verify_block(bht_.get(), 0, bad_page), 0); |
| 363 EXPECT_LT(dm_bht_verify_block(bht_.get(), 127, bad_page), 0); | 336 EXPECT_LT(dm_bht_verify_block(bht_.get(), 127, bad_page), 0); |
| 364 EXPECT_LT(dm_bht_verify_block(bht_.get(), 128, bad_page), 0); | 337 EXPECT_LT(dm_bht_verify_block(bht_.get(), 128, bad_page), 0); |
| 365 EXPECT_LT(dm_bht_verify_block(bht_.get(), 255, bad_page), 0); | 338 EXPECT_LT(dm_bht_verify_block(bht_.get(), 255, bad_page), 0); |
| 366 EXPECT_LT(dm_bht_verify_block(bht_.get(), 256, bad_page), 0); | 339 EXPECT_LT(dm_bht_verify_block(bht_.get(), 256, bad_page), 0); |
| 367 EXPECT_LT(dm_bht_verify_block(bht_.get(), 383, bad_page), 0); | 340 EXPECT_LT(dm_bht_verify_block(bht_.get(), 383, bad_page), 0); |
| 368 | 341 |
| 369 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); | 342 EXPECT_EQ(0, dm_bht_destroy(bht_.get())); |
| 370 free(bad_page); | 343 free(bad_page); |
| 371 } | 344 } |
| OLD | NEW |