netwerk/test/TestCacheBlockFiles.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /*
michael@0 2 TestCacheBlockFiles.cpp
michael@0 3 */
michael@0 4
michael@0 5
michael@0 6 #include <stdio.h>
michael@0 7 #include <stdlib.h>
michael@0 8 #include <utime.h>
michael@0 9
michael@0 10 #include <Files.h>
michael@0 11 #include <Strings.h>
michael@0 12 #include <Errors.h>
michael@0 13 #include <Resources.h>
michael@0 14 #include <Aliases.h>
michael@0 15
michael@0 16 #include "nsCOMPtr.h"
michael@0 17 #include "nsCRT.h"
michael@0 18 #include "nsDirectoryServiceDefs.h"
michael@0 19 #include "nsError.h"
michael@0 20 #include "nsIComponentManager.h"
michael@0 21 #include "nsIComponentRegistrar.h"
michael@0 22 #include "nsIFile.h"
michael@0 23 #include "nsIFileStreams.h"
michael@0 24 #include "nsMemory.h"
michael@0 25 #include "nsIComponentRegistrar.h"
michael@0 26 #include "nsANSIFileStreams.h"
michael@0 27 #include "nsDiskCacheBlockFile.h"
michael@0 28
michael@0 29 #include "prclist.h"
michael@0 30
michael@0 31 /**
michael@0 32 * StressTest()
michael@0 33 */
michael@0 34
michael@0 35 typedef struct Allocation {
michael@0 36 int32_t start;
michael@0 37 int32_t count;
michael@0 38 } Allocation;
michael@0 39
michael@0 40 nsresult
michael@0 41 StressTest(nsIFile * localFile, int32_t testNumber, bool readWrite)
michael@0 42 {
michael@0 43 nsresult rv = NS_OK;
michael@0 44
michael@0 45 #define ITERATIONS 1024
michael@0 46 #define MAX_ALLOCATIONS 256
michael@0 47 Allocation block[MAX_ALLOCATIONS];
michael@0 48 int32_t currentAllocations = 0;
michael@0 49 int32_t i;
michael@0 50 uint32_t a;
michael@0 51
michael@0 52 char * writeBuf[4];
michael@0 53 char readBuf[256 * 4];
michael@0 54
michael@0 55
michael@0 56 if (readWrite) {
michael@0 57 for (i = 0; i < 4; i++) {
michael@0 58 writeBuf[i] = new char[256 * i];
michael@0 59 if (!writeBuf[i]) {
michael@0 60 printf("Test %d: failed - out of memory\n", testNumber);
michael@0 61 rv = NS_ERROR_OUT_OF_MEMORY;
michael@0 62 goto exit;
michael@0 63 }
michael@0 64
michael@0 65 memset(writeBuf[i], i, 256 * i);
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 nsDiskCacheBlockFile * blockFile = new nsDiskCacheBlockFile;
michael@0 70 if (!blockFile) {
michael@0 71 printf("Test %d failed (unable to allocate nsDiskCacheBlockFile", testNumber);
michael@0 72 rv = NS_ERROR_OUT_OF_MEMORY;
michael@0 73 goto exit;
michael@0 74 }
michael@0 75
michael@0 76 rv = blockFile->Open(localFile, 256);
michael@0 77 if (NS_FAILED(rv)) {
michael@0 78 printf("Test %d: failed (Open returned: 0x%.8x)\n", testNumber, rv);
michael@0 79 goto exit;
michael@0 80 }
michael@0 81
michael@0 82 i = ITERATIONS;
michael@0 83 while (i > 0) {
michael@0 84 if ((currentAllocations >= MAX_ALLOCATIONS) ||
michael@0 85 ((currentAllocations > 0) && (rand() % 4 == 0))) {
michael@0 86 // deallocate if we've reached the limit, or 25% of the time we have allocations
michael@0 87 a = rand() % currentAllocations;
michael@0 88
michael@0 89 if (readWrite) {
michael@0 90 // read verify deallocation
michael@0 91 rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);
michael@0 92 if (NS_FAILED(rv)) {
michael@0 93 printf("Test %d: failed (ReadBlocks() returned 0x%.8x)\n", testNumber, rv);
michael@0 94 goto exit;
michael@0 95 }
michael@0 96
michael@0 97 // Verify buffer
michael@0 98 for (i = 0; i < 256 * block[a].count; i++) {
michael@0 99 if (readBuf[i] != block[a].count) {
michael@0 100 printf("Test %d: failed (verifying buffer 1)\n", testNumber);
michael@0 101 rv = NS_ERROR_FAILURE;
michael@0 102 goto exit;
michael@0 103 }
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 rv = blockFile->DeallocateBlocks(block[a].start, block[a].count);
michael@0 108 if (NS_FAILED(rv)) {
michael@0 109 printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);
michael@0 110 goto exit;
michael@0 111 }
michael@0 112
michael@0 113 --currentAllocations;
michael@0 114 if (currentAllocations > 0)
michael@0 115 block[a] = block[currentAllocations];
michael@0 116
michael@0 117 } else {
michael@0 118 // allocate blocks
michael@0 119 --i;
michael@0 120 a = currentAllocations++;
michael@0 121 block[a].count = rand() % 4 + 1; // allocate 1 to 4 blocks
michael@0 122 block[a].start = blockFile->AllocateBlocks(block[a].count);
michael@0 123 if (block[a].start < 0) {
michael@0 124 printf("Test %d: failed (AllocateBlocks() failed.)\n", testNumber);
michael@0 125 goto exit;
michael@0 126 }
michael@0 127
michael@0 128 if (readWrite) {
michael@0 129 // write buffer
michael@0 130 rv = blockFile->WriteBlocks(writeBuf[block[a].count], block[a].start, block[a].count);
michael@0 131 if (NS_FAILED(rv)) {
michael@0 132 printf("Test %d: failed (WriteBlocks() returned 0x%.8x)\n",testNumber, rv);
michael@0 133 goto exit;
michael@0 134 }
michael@0 135 }
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 // now deallocate remaining allocations
michael@0 140 i = currentAllocations;
michael@0 141 while (i--) {
michael@0 142
michael@0 143 if (readWrite) {
michael@0 144 // read verify deallocation
michael@0 145 rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);
michael@0 146 if (NS_FAILED(rv)) {
michael@0 147 printf("Test %d: failed (ReadBlocks(1) returned 0x%.8x)\n", testNumber, rv);
michael@0 148 goto exit;
michael@0 149 }
michael@0 150
michael@0 151 // Verify buffer
michael@0 152 for (i = 0; i < 256 * block[a].count; i++) {
michael@0 153 if (readBuf[i] != block[a].count) {
michael@0 154 printf("Test %d: failed (verifying buffer 1)\n", testNumber);
michael@0 155 rv = NS_ERROR_FAILURE;
michael@0 156 goto exit;
michael@0 157 }
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 rv = blockFile->DeallocateBlocks(block[i].start, block[i].count);
michael@0 162 if (NS_FAILED(rv)) {
michael@0 163 printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);
michael@0 164 goto exit;
michael@0 165 }
michael@0 166 }
michael@0 167
michael@0 168
michael@0 169
michael@0 170 exit:
michael@0 171 nsresult rv2 = blockFile->Close();
michael@0 172 if (NS_FAILED(rv2)) {
michael@0 173 printf("Test %d: failed (Close returned: 0x%.8x)\n", testNumber, rv2);
michael@0 174 }
michael@0 175
michael@0 176 return rv ? rv : rv2;
michael@0 177 }
michael@0 178
michael@0 179 /**
michael@0 180 * main()
michael@0 181 */
michael@0 182
michael@0 183 int
michael@0 184 main(void)
michael@0 185 {
michael@0 186 // OSErr err;
michael@0 187 printf("hello world\n");
michael@0 188
michael@0 189 unsigned long now = time(0);
michael@0 190 srand(now);
michael@0 191
michael@0 192 nsCOMPtr<nsIFile> file;
michael@0 193 nsCOMPtr<nsIFile> localFile;
michael@0 194 nsresult rv = NS_OK;
michael@0 195 {
michael@0 196 // Start up XPCOM
michael@0 197 nsCOMPtr<nsIServiceManager> servMan;
michael@0 198 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 199 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
michael@0 200 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
michael@0 201 if (registrar)
michael@0 202 registrar->AutoRegister(nullptr);
michael@0 203
michael@0 204 // Get default directory
michael@0 205 rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
michael@0 206 getter_AddRefs(file));
michael@0 207 if (NS_FAILED(rv)) {
michael@0 208 printf("NS_GetSpecialDirectory() failed : 0x%.8x\n", rv);
michael@0 209 goto exit;
michael@0 210 }
michael@0 211 char * currentDirPath;
michael@0 212 rv = file->GetPath(&currentDirPath);
michael@0 213 if (NS_FAILED(rv)) {
michael@0 214 printf("currentProcessDir->GetPath() failed : 0x%.8x\n", rv);
michael@0 215 goto exit;
michael@0 216 }
michael@0 217
michael@0 218 printf("Current Process Directory: %s\n", currentDirPath);
michael@0 219
michael@0 220
michael@0 221 // Generate name for cache block file
michael@0 222 rv = file->Append("_CACHE_001_");
michael@0 223 if (NS_FAILED(rv)) goto exit;
michael@0 224
michael@0 225 // Delete existing file
michael@0 226 rv = file->Delete(false);
michael@0 227 if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) goto exit;
michael@0 228
michael@0 229 // Need nsIFile to open
michael@0 230 localFile = do_QueryInterface(file, &rv);
michael@0 231 if (NS_FAILED(rv)) {
michael@0 232 printf("do_QueryInterface(file) failed : 0x%.8x\n", rv);
michael@0 233 goto exit;
michael@0 234 }
michael@0 235
michael@0 236 nsDiskCacheBlockFile * blockFile = new nsDiskCacheBlockFile;
michael@0 237 if (!blockFile) {
michael@0 238 rv = NS_ERROR_OUT_OF_MEMORY;
michael@0 239 goto exit;
michael@0 240 }
michael@0 241
michael@0 242 //----------------------------------------------------------------
michael@0 243 // local variables used in tests
michael@0 244 //----------------------------------------------------------------
michael@0 245 uint32_t bytesWritten = 0;
michael@0 246 int32_t startBlock;
michael@0 247 int32_t i = 0;
michael@0 248
michael@0 249
michael@0 250 //----------------------------------------------------------------
michael@0 251 // Test 1: Open nonexistent file
michael@0 252 //----------------------------------------------------------------
michael@0 253 rv = blockFile->Open(localFile, 256);
michael@0 254 if (NS_FAILED(rv)) {
michael@0 255 printf("Test 1: failed (Open returned: 0x%.8x)\n", rv);
michael@0 256 goto exit;
michael@0 257 }
michael@0 258
michael@0 259 rv = blockFile->Close();
michael@0 260 if (NS_FAILED(rv)) {
michael@0 261 printf("Test 1: failed (Close returned: 0x%.8x)\n", rv);
michael@0 262 goto exit;
michael@0 263 }
michael@0 264
michael@0 265 printf("Test 1: passed\n");
michael@0 266
michael@0 267
michael@0 268 //----------------------------------------------------------------
michael@0 269 // Test 2: Open existing file (with no allocation)
michael@0 270 //----------------------------------------------------------------
michael@0 271 rv = blockFile->Open(localFile, 256);
michael@0 272 if (NS_FAILED(rv)) {
michael@0 273 printf("Test 2: failed (Open returned: 0x%.8x)\n", rv);
michael@0 274 goto exit;
michael@0 275 }
michael@0 276
michael@0 277 rv = blockFile->Close();
michael@0 278 if (NS_FAILED(rv)) {
michael@0 279 printf("Test 2: failed (Close returned: 0x%.8x)\n", rv);
michael@0 280 goto exit;
michael@0 281 }
michael@0 282
michael@0 283 printf("Test 2: passed\n");
michael@0 284
michael@0 285
michael@0 286 //----------------------------------------------------------------
michael@0 287 // Test 3: Open existing file (bad format) size < kBitMapBytes
michael@0 288 //----------------------------------------------------------------
michael@0 289
michael@0 290 // Delete existing file
michael@0 291 rv = localFile->Delete(false);
michael@0 292 if (NS_FAILED(rv)) {
michael@0 293 printf("Test 3 failed (Delete returned: 0x%.8x)\n", rv);
michael@0 294 goto exit;
michael@0 295 }
michael@0 296
michael@0 297 // write < kBitMapBytes to file
michael@0 298 nsANSIFileStream * stream = new nsANSIFileStream;
michael@0 299 if (!stream) {
michael@0 300 printf("Test 3 failed (unable to allocate stream\n", rv);
michael@0 301 goto exit;
michael@0 302 }
michael@0 303 NS_ADDREF(stream);
michael@0 304 rv = stream->Open(localFile);
michael@0 305 if (NS_FAILED(rv)) {
michael@0 306 NS_RELEASE(stream);
michael@0 307 printf("Test 3 failed (stream->Open returned: 0x%.8x)\n", rv);
michael@0 308 goto exit;
michael@0 309 }
michael@0 310
michael@0 311 bytesWritten = 0;
michael@0 312 rv = stream->Write("Tell me something good.\n", 24, &bytesWritten);
michael@0 313 if (NS_FAILED(rv)) {
michael@0 314 NS_RELEASE(stream);
michael@0 315 printf("Test 3 failed (stream->Write returned: 0x%.8x)\n", rv);
michael@0 316 goto exit;
michael@0 317 }
michael@0 318
michael@0 319 rv = stream->Close();
michael@0 320 if (NS_FAILED(rv)) {
michael@0 321 NS_RELEASE(stream);
michael@0 322 printf("Test 3 failed (stream->Close returned: 0x%.8x)\n", rv);
michael@0 323 goto exit;
michael@0 324 }
michael@0 325 NS_RELEASE(stream);
michael@0 326
michael@0 327 rv = blockFile->Open(localFile, 256);
michael@0 328 if (NS_SUCCEEDED(rv)) {
michael@0 329 printf("Test 3: failed (Open erroneously succeeded)\n", rv);
michael@0 330
michael@0 331 (void) blockFile->Close();
michael@0 332 goto exit;
michael@0 333 }
michael@0 334
michael@0 335 printf("Test 3: passed\n");
michael@0 336
michael@0 337
michael@0 338 //----------------------------------------------------------------
michael@0 339 // Test 4: Open nonexistent file (again)
michael@0 340 //----------------------------------------------------------------
michael@0 341
michael@0 342 // Delete existing file
michael@0 343 rv = localFile->Delete(false);
michael@0 344 if (NS_FAILED(rv)) {
michael@0 345 printf("Test 4 failed (Delete returned: 0x%.8x)\n", rv);
michael@0 346 goto exit;
michael@0 347 }
michael@0 348
michael@0 349 rv = blockFile->Open(localFile, 256);
michael@0 350 if (NS_FAILED(rv)) {
michael@0 351 printf("Test 4: failed (Open returned: 0x%.8x)\n", rv);
michael@0 352 goto exit;
michael@0 353 }
michael@0 354
michael@0 355 printf("Test 4: passed\n");
michael@0 356
michael@0 357
michael@0 358 //----------------------------------------------------------------
michael@0 359 // Test 5: AllocateBlocks: invalid block count (0, 5)
michael@0 360 //----------------------------------------------------------------
michael@0 361
michael@0 362
michael@0 363 startBlock = blockFile->AllocateBlocks(0);
michael@0 364 if (startBlock > -1) {
michael@0 365 printf("Test 5: failed (AllocateBlocks(0) erroneously succeeded)\n");
michael@0 366 goto exit;
michael@0 367 }
michael@0 368
michael@0 369 startBlock = blockFile->AllocateBlocks(5);
michael@0 370 if (startBlock > -1) {
michael@0 371 printf("Test 5: failed (AllocateBlocks(5) erroneously succeeded)\n");
michael@0 372 goto exit;
michael@0 373 }
michael@0 374 printf("Test 5: passed\n");
michael@0 375
michael@0 376
michael@0 377 //----------------------------------------------------------------
michael@0 378 // Test 6: AllocateBlocks: valid block count (1, 2, 3, 4)
michael@0 379 //----------------------------------------------------------------
michael@0 380 startBlock = blockFile->AllocateBlocks(1);
michael@0 381 if (startBlock != 0) {
michael@0 382 printf("Test 6: failed (AllocateBlocks(1) failed)\n");
michael@0 383 goto exit;
michael@0 384 }
michael@0 385
michael@0 386 startBlock = blockFile->AllocateBlocks(2);
michael@0 387 if (startBlock != 1) {
michael@0 388 printf("Test 6: failed (AllocateBlocks(2) failed)\n");
michael@0 389 goto exit;
michael@0 390 }
michael@0 391
michael@0 392 startBlock = blockFile->AllocateBlocks(3);
michael@0 393 if (startBlock != 4) {
michael@0 394 printf("Test 6: failed (AllocateBlocks(3) failed)\n");
michael@0 395 goto exit;
michael@0 396 }
michael@0 397
michael@0 398 startBlock = blockFile->AllocateBlocks(4);
michael@0 399 if (startBlock != 8) {
michael@0 400 printf("Test 6: failed (AllocateBlocks(4) failed)\n");
michael@0 401 goto exit;
michael@0 402 }
michael@0 403
michael@0 404 // blocks allocated should be 1220 3330 4444
michael@0 405 printf("Test 6: passed\n"); // but bits could be mis-allocated
michael@0 406
michael@0 407
michael@0 408
michael@0 409 //----------------------------------------------------------------
michael@0 410 // Test 7: VerifyAllocation
michael@0 411 //----------------------------------------------------------------
michael@0 412 rv = blockFile->VerifyAllocation(0,1);
michael@0 413 if (NS_FAILED(rv)) {
michael@0 414 printf("Test 7: failed (VerifyAllocation(0,1) returned: 0x%.8x)\n", rv);
michael@0 415 goto exit;
michael@0 416 }
michael@0 417
michael@0 418 rv = blockFile->VerifyAllocation(1,2);
michael@0 419 if (NS_FAILED(rv)) {
michael@0 420 printf("Test 7: failed (VerifyAllocation(1,2) returned: 0x%.8x)\n", rv);
michael@0 421 goto exit;
michael@0 422 }
michael@0 423
michael@0 424 rv = blockFile->VerifyAllocation(4,3);
michael@0 425 if (NS_FAILED(rv)) {
michael@0 426 printf("Test 7: failed (VerifyAllocation(4,3) returned: 0x%.8x)\n", rv);
michael@0 427 goto exit;
michael@0 428 }
michael@0 429
michael@0 430 rv = blockFile->VerifyAllocation(8,4);
michael@0 431 if (NS_FAILED(rv)) {
michael@0 432 printf("Test 7: failed (VerifyAllocation(8,4) returned: 0x%.8x)\n", rv);
michael@0 433 goto exit;
michael@0 434 }
michael@0 435 printf("Test 7: passed\n");
michael@0 436
michael@0 437
michael@0 438 //----------------------------------------------------------------
michael@0 439 // Test 8: LastBlock
michael@0 440 //----------------------------------------------------------------
michael@0 441 int32_t lastBlock = blockFile->LastBlock();
michael@0 442 if (lastBlock != 11) {
michael@0 443 printf("Test 8: failed (LastBlock() returned: %d)\n", lastBlock);
michael@0 444 goto exit;
michael@0 445 }
michael@0 446 printf("Test 8: passed\n");
michael@0 447
michael@0 448
michael@0 449 //----------------------------------------------------------------
michael@0 450 // Test 9: DeallocateBlocks: bad startBlock ( < 0)
michael@0 451 //----------------------------------------------------------------
michael@0 452 rv = blockFile->DeallocateBlocks(-1, 4);
michael@0 453 if (NS_SUCCEEDED(rv)) {
michael@0 454 printf("Test 9: failed (DeallocateBlocks(-1, 4) erroneously succeeded)\n");
michael@0 455 goto exit;
michael@0 456 }
michael@0 457 printf("Test 9: passed\n");
michael@0 458
michael@0 459
michael@0 460 //----------------------------------------------------------------
michael@0 461 // Test 10: DeallocateBlocks: bad numBlocks (0, 5)
michael@0 462 //----------------------------------------------------------------
michael@0 463 rv = blockFile->DeallocateBlocks(0, 0);
michael@0 464 if (NS_SUCCEEDED(rv)) {
michael@0 465 printf("Test 10: failed (DeallocateBlocks(0, 0) erroneously succeeded)\n");
michael@0 466 goto exit;
michael@0 467 }
michael@0 468
michael@0 469 rv = blockFile->DeallocateBlocks(0, 5);
michael@0 470 if (NS_SUCCEEDED(rv)) {
michael@0 471 printf("Test 10: failed (DeallocateBlocks(0, 5) erroneously succeeded)\n");
michael@0 472 goto exit;
michael@0 473 }
michael@0 474
michael@0 475 printf("Test 10: passed\n");
michael@0 476
michael@0 477
michael@0 478 //----------------------------------------------------------------
michael@0 479 // Test 11: DeallocateBlocks: unallocated blocks
michael@0 480 //----------------------------------------------------------------
michael@0 481 rv = blockFile->DeallocateBlocks(12, 1);
michael@0 482 if (NS_SUCCEEDED(rv)) {
michael@0 483 printf("Test 11: failed (DeallocateBlocks(12, 1) erroneously succeeded)\n");
michael@0 484 goto exit;
michael@0 485 }
michael@0 486
michael@0 487 printf("Test 11: passed\n");
michael@0 488
michael@0 489
michael@0 490 //----------------------------------------------------------------
michael@0 491 // Test 12: DeallocateBlocks: 1, 2, 3, 4 (allocated in Test 6)
michael@0 492 //----------------------------------------------------------------
michael@0 493 rv = blockFile->DeallocateBlocks(0, 1);
michael@0 494 if (NS_FAILED(rv)) {
michael@0 495 printf("Test 12: failed (DeallocateBlocks(12, 1) returned: 0x%.8x)\n", rv);
michael@0 496 goto exit;
michael@0 497 }
michael@0 498
michael@0 499 rv = blockFile->DeallocateBlocks(1, 2);
michael@0 500 if (NS_FAILED(rv)) {
michael@0 501 printf("Test 12: failed (DeallocateBlocks(1, 2) returned: 0x%.8x)\n", rv);
michael@0 502 goto exit;
michael@0 503 }
michael@0 504
michael@0 505 rv = blockFile->DeallocateBlocks(4, 3);
michael@0 506 if (NS_FAILED(rv)) {
michael@0 507 printf("Test 12: failed (DeallocateBlocks(4, 3) returned: 0x%.8x)\n", rv);
michael@0 508 goto exit;
michael@0 509 }
michael@0 510
michael@0 511 rv = blockFile->DeallocateBlocks(8, 4);
michael@0 512 if (NS_FAILED(rv)) {
michael@0 513 printf("Test 12: failed (DeallocateBlocks(8, 4) returned: 0x%.8x)\n", rv);
michael@0 514 goto exit;
michael@0 515 }
michael@0 516
michael@0 517 // zero blocks should be allocated
michael@0 518 rv = blockFile->Close();
michael@0 519 if (NS_FAILED(rv)) {
michael@0 520 printf("Test 12: failed (Close returned: 0x%.8x)\n", rv);
michael@0 521 goto exit;
michael@0 522 }
michael@0 523
michael@0 524 printf("Test 12: passed\n");
michael@0 525
michael@0 526
michael@0 527 //----------------------------------------------------------------
michael@0 528 // Test 13: Allocate/Deallocate boundary test
michael@0 529 //----------------------------------------------------------------
michael@0 530
michael@0 531 rv = blockFile->Open(localFile, 256);
michael@0 532 if (NS_FAILED(rv)) {
michael@0 533 printf("Test 13: failed (Open returned: 0x%.8x)\n", rv);
michael@0 534 goto exit;
michael@0 535 }
michael@0 536
michael@0 537 // fully allocate, 1 block at a time
michael@0 538 for (i=0; i< kBitMapBytes * 8; ++i) {
michael@0 539 startBlock = blockFile->AllocateBlocks(1);
michael@0 540 if (startBlock < 0) {
michael@0 541 printf("Test 13: failed (AllocateBlocks(1) failed on i=%d)\n", i);
michael@0 542 goto exit;
michael@0 543 }
michael@0 544 }
michael@0 545
michael@0 546 // attempt allocation with full bit map
michael@0 547 startBlock = blockFile->AllocateBlocks(1);
michael@0 548 if (startBlock >= 0) {
michael@0 549 printf("Test 13: failed (AllocateBlocks(1) erroneously succeeded i=%d)\n", i);
michael@0 550 goto exit;
michael@0 551 }
michael@0 552
michael@0 553 // deallocate all the bits
michael@0 554 for (i=0; i< kBitMapBytes * 8; ++i) {
michael@0 555 rv = blockFile->DeallocateBlocks(i,1);
michael@0 556 if (NS_FAILED(rv)) {
michael@0 557 printf("Test 13: failed (DeallocateBlocks(%d,1) returned: 0x%.8x)\n", i,rv);
michael@0 558 goto exit;
michael@0 559 }
michael@0 560 }
michael@0 561
michael@0 562 // attempt deallocation beyond end of bit map
michael@0 563 rv = blockFile->DeallocateBlocks(i,1);
michael@0 564 if (NS_SUCCEEDED(rv)) {
michael@0 565 printf("Test 13: failed (DeallocateBlocks(%d,1) erroneously succeeded)\n", i);
michael@0 566 goto exit;
michael@0 567 }
michael@0 568
michael@0 569 // bit map should be empty
michael@0 570
michael@0 571 // fully allocate, 2 block at a time
michael@0 572 for (i=0; i< kBitMapBytes * 8; i+=2) {
michael@0 573 startBlock = blockFile->AllocateBlocks(2);
michael@0 574 if (startBlock < 0) {
michael@0 575 printf("Test 13: failed (AllocateBlocks(2) failed on i=%d)\n", i);
michael@0 576 goto exit;
michael@0 577 }
michael@0 578 }
michael@0 579
michael@0 580 // attempt allocation with full bit map
michael@0 581 startBlock = blockFile->AllocateBlocks(2);
michael@0 582 if (startBlock >= 0) {
michael@0 583 printf("Test 13: failed (AllocateBlocks(2) erroneously succeeded i=%d)\n", i);
michael@0 584 goto exit;
michael@0 585 }
michael@0 586
michael@0 587 // deallocate all the bits
michael@0 588 for (i=0; i< kBitMapBytes * 8; i+=2) {
michael@0 589 rv = blockFile->DeallocateBlocks(i,2);
michael@0 590 if (NS_FAILED(rv)) {
michael@0 591 printf("Test 13: failed (DeallocateBlocks(%d,2) returned: 0x%.8x)\n", i,rv);
michael@0 592 goto exit;
michael@0 593 }
michael@0 594 }
michael@0 595
michael@0 596 // bit map should be empty
michael@0 597
michael@0 598 // fully allocate, 4 block at a time
michael@0 599 for (i=0; i< kBitMapBytes * 8; i+=4) {
michael@0 600 startBlock = blockFile->AllocateBlocks(4);
michael@0 601 if (startBlock < 0) {
michael@0 602 printf("Test 13: failed (AllocateBlocks(4) failed on i=%d)\n", i);
michael@0 603 goto exit;
michael@0 604 }
michael@0 605 }
michael@0 606
michael@0 607 // attempt allocation with full bit map
michael@0 608 startBlock = blockFile->AllocateBlocks(4);
michael@0 609 if (startBlock >= 0) {
michael@0 610 printf("Test 13: failed (AllocateBlocks(4) erroneously succeeded i=%d)\n", i);
michael@0 611 goto exit;
michael@0 612 }
michael@0 613
michael@0 614 // deallocate all the bits
michael@0 615 for (i=0; i< kBitMapBytes * 8; i+=4) {
michael@0 616 rv = blockFile->DeallocateBlocks(i,4);
michael@0 617 if (NS_FAILED(rv)) {
michael@0 618 printf("Test 13: failed (DeallocateBlocks(%d,4) returned: 0x%.8x)\n", i,rv);
michael@0 619 goto exit;
michael@0 620 }
michael@0 621 }
michael@0 622
michael@0 623 // bit map should be empty
michael@0 624
michael@0 625 // allocate as many triple-blocks as possible
michael@0 626 for (i=0; i< kBitMapBytes * 8; i+=4) {
michael@0 627 startBlock = blockFile->AllocateBlocks(3);
michael@0 628 if (startBlock < 0) {
michael@0 629 printf("Test 13: failed (AllocateBlocks(3) failed on i=%d)\n", i);
michael@0 630 goto exit;
michael@0 631 }
michael@0 632 }
michael@0 633
michael@0 634 // attempt allocation with "full" bit map
michael@0 635 startBlock = blockFile->AllocateBlocks(3);
michael@0 636 if (startBlock >= 0) {
michael@0 637 printf("Test 13: failed (AllocateBlocks(3) erroneously succeeded i=%d)\n", i);
michael@0 638 goto exit;
michael@0 639 }
michael@0 640
michael@0 641 // leave some blocks allocated
michael@0 642
michael@0 643 rv = blockFile->Close();
michael@0 644 if (NS_FAILED(rv)) {
michael@0 645 printf("Test 13: failed (Close returned: 0x%.8x)\n", rv);
michael@0 646 goto exit;
michael@0 647 }
michael@0 648
michael@0 649 printf("Test 13: passed\n");
michael@0 650
michael@0 651
michael@0 652 //----------------------------------------------------------------
michael@0 653 // Test 14: ValidateFile (open existing file w/size < allocated blocks
michael@0 654 //----------------------------------------------------------------
michael@0 655 rv = blockFile->Open(localFile, 256);
michael@0 656 if (NS_SUCCEEDED(rv)) {
michael@0 657 printf("Test 14: failed (Open erroneously succeeded)\n");
michael@0 658 goto exit;
michael@0 659 }
michael@0 660
michael@0 661 // Delete existing file
michael@0 662 rv = localFile->Delete(false);
michael@0 663 if (NS_FAILED(rv)) {
michael@0 664 printf("Test 14 failed (Delete returned: 0x%.8x)\n", rv);
michael@0 665 goto exit;
michael@0 666 }
michael@0 667 printf("Test 14: passed\n");
michael@0 668
michael@0 669
michael@0 670 //----------------------------------------------------------------
michael@0 671 // Test 15: Allocate/Deallocate stress test
michael@0 672 //----------------------------------------------------------------
michael@0 673
michael@0 674 rv = StressTest(localFile, 15, false);
michael@0 675 if (NS_FAILED(rv))
michael@0 676 goto exit;
michael@0 677
michael@0 678 printf("Test 15: passed\n");
michael@0 679
michael@0 680
michael@0 681 //----------------------------------------------------------------
michael@0 682 // Test 16: WriteBlocks
michael@0 683 //----------------------------------------------------------------
michael@0 684
michael@0 685 rv = blockFile->Open(localFile, 256);
michael@0 686 if (NS_FAILED(rv)) {
michael@0 687 printf("Test 16: failed (Open returned: 0x%.8x)\n", rv);
michael@0 688 goto exit;
michael@0 689 }
michael@0 690
michael@0 691 char * one = new char[256 * 1];
michael@0 692 char * two = new char[256 * 2];
michael@0 693 char * three = new char[256 * 3];
michael@0 694 char * four = new char[256 * 4];
michael@0 695 if (!one || !two || !three || !four) {
michael@0 696 printf("Test 16: failed - out of memory\n");
michael@0 697 rv = NS_ERROR_OUT_OF_MEMORY;
michael@0 698 goto exit;
michael@0 699 }
michael@0 700
michael@0 701 memset(one, 1, 256);
michael@0 702 memset(two, 2, 256 * 2);
michael@0 703 memset(three, 3, 256 * 3);
michael@0 704 memset(four, 4, 256 * 4);
michael@0 705
michael@0 706 startBlock = blockFile->AllocateBlocks(1);
michael@0 707 if (startBlock != 0) {
michael@0 708 printf("Test 16: failed (AllocateBlocks(1) failed)\n");
michael@0 709 goto exit;
michael@0 710 }
michael@0 711
michael@0 712 rv = blockFile->WriteBlocks(one, startBlock, 1);
michael@0 713 if (NS_FAILED(rv)) {
michael@0 714 printf("Test 16: failed (WriteBlocks(1) returned 0x%.8x)\n", rv);
michael@0 715 goto exit;
michael@0 716 }
michael@0 717
michael@0 718 startBlock = blockFile->AllocateBlocks(2);
michael@0 719 if (startBlock != 1) { // starting with empy map, this allocation should begin at block 1
michael@0 720 printf("Test 16: failed (AllocateBlocks(2) failed)\n");
michael@0 721 goto exit;
michael@0 722 }
michael@0 723
michael@0 724 rv = blockFile->WriteBlocks(two, startBlock, 2);
michael@0 725 if (NS_FAILED(rv)) {
michael@0 726 printf("Test 16: failed (WriteBlocks(2) returned 0x%.8x)\n", rv);
michael@0 727 goto exit;
michael@0 728 }
michael@0 729
michael@0 730 startBlock = blockFile->AllocateBlocks(3);
michael@0 731 if (startBlock != 4) { // starting with empy map, this allocation should begin at block 4
michael@0 732 printf("Test 16: failed (AllocateBlocks(3) failed)\n");
michael@0 733 goto exit;
michael@0 734 }
michael@0 735
michael@0 736 rv = blockFile->WriteBlocks(three, startBlock, 3);
michael@0 737 if (NS_FAILED(rv)) {
michael@0 738 printf("Test 16: failed (WriteBlocks(3) returned 0x%.8x)\n", rv);
michael@0 739 goto exit;
michael@0 740 }
michael@0 741
michael@0 742 startBlock = blockFile->AllocateBlocks(4);
michael@0 743 if (startBlock != 8) { // starting with empy map, this allocation should begin at block 8
michael@0 744 printf("Test 16: failed (AllocateBlocks(4) failed)\n");
michael@0 745 goto exit;
michael@0 746 }
michael@0 747
michael@0 748 rv = blockFile->WriteBlocks(four, startBlock, 4);
michael@0 749 if (NS_FAILED(rv)) {
michael@0 750 printf("Test 16: failed (WriteBlocks(4) returned 0x%.8x)\n", rv);
michael@0 751 goto exit;
michael@0 752 }
michael@0 753
michael@0 754 printf("Test 16: passed\n");
michael@0 755
michael@0 756
michael@0 757 //----------------------------------------------------------------
michael@0 758 // Test 17: ReadBlocks
michael@0 759 //----------------------------------------------------------------
michael@0 760
michael@0 761 rv = blockFile->ReadBlocks(one, 0, 1);
michael@0 762 if (NS_FAILED(rv)) {
michael@0 763 printf("Test 17: failed (ReadBlocks(1) returned 0x%.8x)\n", rv);
michael@0 764 goto exit;
michael@0 765 }
michael@0 766
michael@0 767 // Verify buffer
michael@0 768 for (i = 0; i < 256; i++) {
michael@0 769 if (one[i] != 1) {
michael@0 770 printf("Test 17: failed (verifying buffer 1)\n");
michael@0 771 rv = NS_ERROR_FAILURE;
michael@0 772 goto exit;
michael@0 773 }
michael@0 774 }
michael@0 775
michael@0 776 rv = blockFile->ReadBlocks(two, 1, 2);
michael@0 777 if (NS_FAILED(rv)) {
michael@0 778 printf("Test 17: failed (ReadBlocks(2) returned 0x%.8x)\n", rv);
michael@0 779 goto exit;
michael@0 780 }
michael@0 781
michael@0 782 // Verify buffer
michael@0 783 for (i = 0; i < 256 * 2; i++) {
michael@0 784 if (two[i] != 2) {
michael@0 785 printf("Test 17: failed (verifying buffer 2)\n");
michael@0 786 rv = NS_ERROR_FAILURE;
michael@0 787 goto exit;
michael@0 788 }
michael@0 789 }
michael@0 790
michael@0 791 rv = blockFile->ReadBlocks(three, 4, 3);
michael@0 792 if (NS_FAILED(rv)) {
michael@0 793 printf("Test 17: failed (ReadBlocks(3) returned 0x%.8x)\n", rv);
michael@0 794 goto exit;
michael@0 795 }
michael@0 796
michael@0 797 // Verify buffer
michael@0 798 for (i = 0; i < 256 * 3; i++) {
michael@0 799 if (three[i] != 3) {
michael@0 800 printf("Test 17: failed (verifying buffer 3)\n");
michael@0 801 rv = NS_ERROR_FAILURE;
michael@0 802 goto exit;
michael@0 803 }
michael@0 804 }
michael@0 805
michael@0 806 rv = blockFile->ReadBlocks(four, 8, 4);
michael@0 807 if (NS_FAILED(rv)) {
michael@0 808 printf("Test 17: failed (ReadBlocks(4) returned 0x%.8x)\n", rv);
michael@0 809 goto exit;
michael@0 810 }
michael@0 811
michael@0 812 // Verify buffer
michael@0 813 for (i = 0; i < 256 * 4; i++) {
michael@0 814 if (four[i] != 4) {
michael@0 815 printf("Test 17: failed (verifying buffer 4)\n");
michael@0 816 rv = NS_ERROR_FAILURE;
michael@0 817 goto exit;
michael@0 818 }
michael@0 819 }
michael@0 820
michael@0 821 rv = blockFile->Close();
michael@0 822 if (NS_FAILED(rv)) {
michael@0 823 printf("Test 17: failed (Close returned: 0x%.8x)\n", rv);
michael@0 824 goto exit;
michael@0 825 }
michael@0 826
michael@0 827 printf("Test 17: passed\n");
michael@0 828
michael@0 829
michael@0 830 //----------------------------------------------------------------
michael@0 831 // Test 18: ValidateFile (open existing file with blocks allocated)
michael@0 832 //----------------------------------------------------------------
michael@0 833 rv = blockFile->Open(localFile, 256);
michael@0 834 if (NS_FAILED(rv)) {
michael@0 835 printf("Test 18: failed (Open returned: 0x%.8x)\n", rv);
michael@0 836 goto exit;
michael@0 837 }
michael@0 838
michael@0 839 rv = blockFile->Close();
michael@0 840 if (NS_FAILED(rv)) {
michael@0 841 printf("Test 18: failed (Close returned: 0x%.8x)\n", rv);
michael@0 842 goto exit;
michael@0 843 }
michael@0 844
michael@0 845 printf("Test 18: passed\n");
michael@0 846
michael@0 847 //----------------------------------------------------------------
michael@0 848 // Test 19: WriteBlocks/ReadBlocks stress
michael@0 849 //----------------------------------------------------------------
michael@0 850
michael@0 851 rv = StressTest(localFile, 19, false);
michael@0 852 if (NS_FAILED(rv))
michael@0 853 goto exit;
michael@0 854
michael@0 855 printf("Test 19: passed\n");
michael@0 856
michael@0 857
michael@0 858 exit:
michael@0 859
michael@0 860 if (currentDirPath)
michael@0 861 nsMemory::Free(currentDirPath);
michael@0 862 } // this scopes the nsCOMPtrs
michael@0 863 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
michael@0 864 if (NS_FAILED(rv))
michael@0 865 printf("Test failed: 0x%.8x\n", rv);
michael@0 866
michael@0 867 rv = NS_ShutdownXPCOM(nullptr);
michael@0 868 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
michael@0 869
michael@0 870 printf("XPCOM shut down.\n\n");
michael@0 871 return 0;
michael@0 872 }
michael@0 873

mercurial