Back to index

lightning-sunbird  0.9+nobinonly
TestCacheBlockFiles.cpp
Go to the documentation of this file.
00001 /*
00002     TestCacheBlockFiles.cpp
00003 */
00004 
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <utime.h>
00009 
00010 #include <Files.h>
00011 #include <Strings.h>
00012 #include <Errors.h>
00013 #include <Resources.h>
00014 #include <Aliases.h>
00015 
00016 #include "nsCOMPtr.h"
00017 #include "nsCRT.h"
00018 #include "nsDirectoryServiceDefs.h"
00019 #include "nsError.h"
00020 #include "nsIComponentManager.h"
00021 #include "nsIComponentRegistrar.h"
00022 #include "nsIFile.h"
00023 #include "nsILocalFile.h"
00024 #include "nsIFileStreams.h"
00025 #include "nsMemory.h"
00026 #include "nsIComponentRegistrar.h"
00027 #include "nsANSIFileStreams.h"
00028 #include "nsDiskCacheBlockFile.h"
00029 
00030 #include "prclist.h"
00031 
00032 #if defined(XP_MAC)
00033 #include <SIOUX.h>
00034 #include "macstdlibextras.h"
00035     // Set up the toolbox and (if DEBUG) the console.  Do this in a static initializer,
00036     // to make it as unlikely as possible that somebody calls printf() before we get initialized.
00037 static struct MacInitializer    {   MacInitializer()
00038                                     {
00039                                         InitializeMacToolbox();
00040                                         InitializeSIOUX(true);
00041                                     } } gInitializer;
00042 #endif // XP_MAC
00043 
00044 
00049 typedef struct Allocation {
00050     PRInt32     start;
00051     PRInt32     count;
00052 } Allocation;
00053 
00054 nsresult
00055 StressTest(nsILocalFile *  localFile, PRInt32  testNumber, PRBool readWrite)
00056 {
00057     nsresult  rv = NS_OK;
00058 
00059 #define ITERATIONS      1024
00060 #define MAX_ALLOCATIONS 256
00061     Allocation  block[MAX_ALLOCATIONS];
00062     PRInt32     currentAllocations = 0;
00063     PRInt32     i;
00064     PRUint32    a;
00065 
00066     char * writeBuf[4];
00067     char   readBuf[256 * 4];
00068 
00069 
00070     if (readWrite) {
00071         for (i = 0; i < 4; i++) {
00072             writeBuf[i] = new char[256 * i];
00073             if (!writeBuf[i]) {
00074                 printf("Test %d: failed - out of memory\n", testNumber);
00075                 rv = NS_ERROR_OUT_OF_MEMORY;
00076                 goto exit;
00077             }
00078 
00079             memset(writeBuf[i], i, 256 * i);
00080         }
00081     }
00082 
00083     nsDiskCacheBlockFile * blockFile = new nsDiskCacheBlockFile;
00084     if (!blockFile) {
00085         printf("Test %d failed (unable to allocate nsDiskCacheBlockFile", testNumber);
00086         rv = NS_ERROR_OUT_OF_MEMORY;
00087         goto exit;
00088     }
00089 
00090     rv = blockFile->Open(localFile, 256);
00091     if (NS_FAILED(rv)) {
00092         printf("Test %d: failed (Open returned: 0x%.8x)\n", testNumber, rv);
00093         goto exit;
00094     }
00095 
00096     i = ITERATIONS;
00097     while (i > 0) {
00098         if ((currentAllocations >= MAX_ALLOCATIONS) ||
00099             ((currentAllocations > 0) && (rand() % 4 == 0))) {
00100             // deallocate if we've reached the limit, or 25% of the time we have allocations
00101             a = rand() % currentAllocations;
00102 
00103             if (readWrite) {
00104                 // read verify deallocation
00105                 rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);
00106                 if (NS_FAILED(rv)) {
00107                     printf("Test %d: failed (ReadBlocks() returned 0x%.8x)\n", testNumber, rv);
00108                     goto exit;
00109                 }
00110 
00111                 // Verify buffer
00112                 for (i = 0; i < 256 * block[a].count; i++) {
00113                     if (readBuf[i] != block[a].count) {
00114                         printf("Test %d: failed (verifying buffer 1)\n", testNumber);
00115                         rv = NS_ERROR_FAILURE;
00116                         goto exit;
00117                     }
00118                 }
00119             }
00120 
00121             rv = blockFile->DeallocateBlocks(block[a].start, block[a].count);
00122             if (NS_FAILED(rv)) {
00123                 printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);
00124                 goto exit;
00125             }
00126 
00127             --currentAllocations;
00128             if (currentAllocations > 0)
00129                 block[a] = block[currentAllocations];
00130 
00131         } else {
00132             // allocate blocks
00133             --i;
00134             a = currentAllocations++;
00135             block[a].count = rand() % 4 + 1; // allocate 1 to 4 blocks
00136             block[a].start = blockFile->AllocateBlocks(block[a].count);
00137             if (block[a].start < 0) {
00138                 printf("Test %d: failed (AllocateBlocks() failed.)\n", testNumber);
00139                 goto exit;
00140             }
00141 
00142             if (readWrite) {
00143                 // write buffer
00144                 rv = blockFile->WriteBlocks(writeBuf[block[a].count], block[a].start, block[a].count);
00145                 if (NS_FAILED(rv)) {
00146                     printf("Test %d: failed (WriteBlocks() returned 0x%.8x)\n",testNumber, rv);
00147                     goto exit;
00148                 }
00149             }
00150         }
00151     }
00152 
00153     // now deallocate remaining allocations
00154     i = currentAllocations;
00155     while (i--) {
00156 
00157         if (readWrite) {
00158             // read verify deallocation
00159             rv = blockFile->ReadBlocks(readBuf, block[a].start, block[a].count);
00160             if (NS_FAILED(rv)) {
00161                 printf("Test %d: failed (ReadBlocks(1) returned 0x%.8x)\n", testNumber, rv);
00162                 goto exit;
00163             }
00164 
00165             // Verify buffer
00166             for (i = 0; i < 256 * block[a].count; i++) {
00167                 if (readBuf[i] != block[a].count) {
00168                     printf("Test %d: failed (verifying buffer 1)\n", testNumber);
00169                     rv = NS_ERROR_FAILURE;
00170                     goto exit;
00171                 }
00172             }
00173         }
00174 
00175         rv = blockFile->DeallocateBlocks(block[i].start, block[i].count);
00176         if (NS_FAILED(rv)) {
00177             printf("Test %d: failed (DeallocateBlocks() returned %d)\n", testNumber, rv);
00178             goto exit;
00179         }
00180     }
00181 
00182 
00183 
00184 exit:
00185     nsresult rv2 = blockFile->Close();
00186     if (NS_FAILED(rv2)) {
00187         printf("Test %d: failed (Close returned: 0x%.8x)\n", testNumber, rv2);
00188     }
00189 
00190     return rv ? rv : rv2;
00191 }    
00192 
00197 int
00198 main(void)
00199 {
00200 //     OSErr  err;
00201     printf("hello world\n");
00202 
00203 #if defined(XP_MAC)
00204     SIOUXSettings.autocloseonquit = false;
00205        SIOUXSettings.asktosaveonclose = false;
00206 #endif
00207 
00208     unsigned long now = time(0);
00209     srand(now);
00210 
00211     nsCOMPtr<nsIFile>       file;
00212     nsCOMPtr<nsILocalFile>  localFile;
00213     nsresult  rv = NS_OK;
00214     {
00215         // Start up XPCOM
00216         nsCOMPtr<nsIServiceManager> servMan;
00217         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00218         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00219         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00220         if (registrar)
00221             registrar->AutoRegister(nsnull);
00222 
00223         // Get default directory
00224         rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
00225                                     getter_AddRefs(file));
00226         if (NS_FAILED(rv)) {
00227             printf("NS_GetSpecialDirectory() failed : 0x%.8x\n", rv);
00228             goto exit;
00229         }
00230         char * currentDirPath;
00231         rv = file->GetPath(&currentDirPath);
00232         if (NS_FAILED(rv)) {
00233             printf("currentProcessDir->GetPath() failed : 0x%.8x\n", rv);
00234             goto exit;
00235         }
00236 
00237         printf("Current Process Directory: %s\n", currentDirPath);
00238 
00239 
00240         // Generate name for cache block file
00241        rv = file->Append("_CACHE_001_");
00242         if (NS_FAILED(rv)) goto exit;
00243 
00244         // Delete existing file
00245         rv = file->Delete(PR_FALSE);
00246         if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) goto exit;
00247 
00248         // Need nsILocalFile to open
00249        localFile = do_QueryInterface(file, &rv);
00250         if (NS_FAILED(rv)) {
00251             printf("do_QueryInterface(file) failed : 0x%.8x\n", rv);
00252             goto exit;
00253         }
00254 
00255         nsDiskCacheBlockFile * blockFile = new nsDiskCacheBlockFile;
00256         if (!blockFile) {
00257             rv = NS_ERROR_OUT_OF_MEMORY;
00258             goto exit;
00259         }
00260 
00261         //----------------------------------------------------------------
00262         //  local variables used in tests
00263         //----------------------------------------------------------------
00264         PRUint32 bytesWritten = 0;
00265         PRInt32  startBlock;
00266         PRInt32  i = 0;
00267 
00268 
00269         //----------------------------------------------------------------
00270         //  Test 1: Open non-existing file
00271         //----------------------------------------------------------------
00272         rv = blockFile->Open(localFile, 256);
00273         if (NS_FAILED(rv)) {
00274             printf("Test 1: failed (Open returned: 0x%.8x)\n", rv);
00275             goto exit;
00276         }
00277 
00278         rv = blockFile->Close();
00279         if (NS_FAILED(rv)) {
00280             printf("Test 1: failed (Close returned: 0x%.8x)\n", rv);
00281             goto exit;
00282         }
00283 
00284         printf("Test 1: passed\n");
00285 
00286 
00287         //----------------------------------------------------------------
00288         //  Test 2: Open existing file (with no allocation)
00289         //----------------------------------------------------------------
00290         rv = blockFile->Open(localFile, 256);
00291         if (NS_FAILED(rv)) {
00292             printf("Test 2: failed (Open returned: 0x%.8x)\n", rv);
00293             goto exit;
00294         }
00295 
00296         rv = blockFile->Close();
00297         if (NS_FAILED(rv)) {
00298             printf("Test 2: failed (Close returned: 0x%.8x)\n", rv);
00299             goto exit;
00300         }
00301 
00302         printf("Test 2: passed\n");
00303 
00304 
00305         //----------------------------------------------------------------
00306         //  Test 3: Open existing file (bad format) size < kBitMapBytes
00307         //----------------------------------------------------------------
00308 
00309         // Delete existing file
00310         rv = localFile->Delete(PR_FALSE);
00311         if (NS_FAILED(rv)) {
00312             printf("Test 3 failed (Delete returned: 0x%.8x)\n", rv);
00313             goto exit;
00314         }
00315 
00316         // write < kBitMapBytes to file
00317         nsANSIFileStream * stream = new nsANSIFileStream;
00318         if (!stream) {
00319             printf("Test 3 failed (unable to allocate stream\n", rv);
00320             goto exit;
00321         }
00322         NS_ADDREF(stream);
00323         rv = stream->Open(localFile);
00324         if (NS_FAILED(rv)) {
00325             NS_RELEASE(stream);
00326             printf("Test 3 failed (stream->Open returned: 0x%.8x)\n", rv);
00327             goto exit;
00328         }
00329 
00330         bytesWritten = 0;
00331         rv = stream->Write("Tell me something good.\n", 24, &bytesWritten);
00332         if (NS_FAILED(rv)) {
00333             NS_RELEASE(stream);
00334             printf("Test 3 failed (stream->Write returned: 0x%.8x)\n", rv);
00335             goto exit;
00336         }
00337 
00338         rv = stream->Close();
00339         if (NS_FAILED(rv)) {
00340             NS_RELEASE(stream);
00341             printf("Test 3 failed (stream->Close returned: 0x%.8x)\n", rv);
00342             goto exit;
00343         }
00344         NS_RELEASE(stream);
00345 
00346         rv = blockFile->Open(localFile, 256);
00347         if (NS_SUCCEEDED(rv)) {
00348             printf("Test 3: failed (Open erroneously succeeded)\n", rv);
00349 
00350             (void) blockFile->Close();
00351             goto exit;
00352         }
00353 
00354         printf("Test 3: passed\n");
00355 
00356 
00357         //----------------------------------------------------------------
00358         //  Test 4: Open non-existing file (again)
00359         //----------------------------------------------------------------
00360 
00361         // Delete existing file
00362         rv = localFile->Delete(PR_FALSE);
00363         if (NS_FAILED(rv)) {
00364             printf("Test 4 failed (Delete returned: 0x%.8x)\n", rv);
00365             goto exit;
00366         }
00367 
00368         rv = blockFile->Open(localFile, 256);
00369         if (NS_FAILED(rv)) {
00370             printf("Test 4: failed (Open returned: 0x%.8x)\n", rv);
00371             goto exit;
00372         }
00373 
00374         printf("Test 4: passed\n");
00375 
00376 
00377         //----------------------------------------------------------------
00378         //  Test 5: AllocateBlocks: invalid block count (0, 5)
00379         //----------------------------------------------------------------
00380 
00381 
00382         startBlock = blockFile->AllocateBlocks(0);
00383         if (startBlock > -1) {
00384             printf("Test 5: failed (AllocateBlocks(0) erroneously succeeded)\n");
00385             goto exit;
00386         }
00387 
00388         startBlock = blockFile->AllocateBlocks(5);
00389         if (startBlock > -1) {
00390             printf("Test 5: failed (AllocateBlocks(5) erroneously succeeded)\n");
00391             goto exit;
00392         }
00393         printf("Test 5: passed\n");
00394 
00395 
00396         //----------------------------------------------------------------
00397         //  Test 6: AllocateBlocks: valid block count (1, 2, 3, 4)
00398         //----------------------------------------------------------------
00399         startBlock = blockFile->AllocateBlocks(1);
00400         if (startBlock != 0) {
00401             printf("Test 6: failed (AllocateBlocks(1) failed)\n");
00402             goto exit;
00403         }
00404 
00405         startBlock = blockFile->AllocateBlocks(2);
00406         if (startBlock != 1) {
00407             printf("Test 6: failed (AllocateBlocks(2) failed)\n");
00408             goto exit;
00409         }
00410 
00411         startBlock = blockFile->AllocateBlocks(3);
00412         if (startBlock != 4) {
00413             printf("Test 6: failed (AllocateBlocks(3) failed)\n");
00414             goto exit;
00415         }
00416 
00417         startBlock = blockFile->AllocateBlocks(4);
00418         if (startBlock != 8) {
00419             printf("Test 6: failed (AllocateBlocks(4) failed)\n");
00420             goto exit;
00421         }
00422 
00423         // blocks allocated should be 1220 3330 4444
00424         printf("Test 6: passed\n");  // but bits could be mis-allocated
00425 
00426 
00427 
00428         //----------------------------------------------------------------
00429         //  Test 7: VerifyAllocation
00430         //----------------------------------------------------------------
00431         rv = blockFile->VerifyAllocation(0,1);
00432         if (NS_FAILED(rv)) {
00433             printf("Test 7: failed (VerifyAllocation(0,1) returned: 0x%.8x)\n", rv);
00434             goto exit;
00435         }
00436 
00437         rv = blockFile->VerifyAllocation(1,2);
00438         if (NS_FAILED(rv)) {
00439             printf("Test 7: failed (VerifyAllocation(1,2) returned: 0x%.8x)\n", rv);
00440             goto exit;
00441         }
00442 
00443         rv = blockFile->VerifyAllocation(4,3);
00444         if (NS_FAILED(rv)) {
00445             printf("Test 7: failed (VerifyAllocation(4,3) returned: 0x%.8x)\n", rv);
00446             goto exit;
00447         }
00448 
00449         rv = blockFile->VerifyAllocation(8,4);
00450         if (NS_FAILED(rv)) {
00451             printf("Test 7: failed (VerifyAllocation(8,4) returned: 0x%.8x)\n", rv);
00452             goto exit;
00453         }
00454         printf("Test 7: passed\n");
00455 
00456 
00457         //----------------------------------------------------------------
00458         //  Test 8: LastBlock
00459         //----------------------------------------------------------------
00460         PRInt32  lastBlock = blockFile->LastBlock();
00461         if (lastBlock != 11) {
00462             printf("Test 8: failed (LastBlock() returned: %d)\n", lastBlock);
00463             goto exit;
00464         }
00465         printf("Test 8: passed\n");
00466 
00467 
00468         //----------------------------------------------------------------
00469         //  Test 9: DeallocateBlocks: bad startBlock ( < 0)
00470         //----------------------------------------------------------------
00471         rv = blockFile->DeallocateBlocks(-1, 4);
00472         if (NS_SUCCEEDED(rv)) {
00473             printf("Test 9: failed (DeallocateBlocks(-1, 4) erroneously succeeded)\n");
00474             goto exit;
00475         }
00476         printf("Test 9: passed\n");
00477 
00478 
00479         //----------------------------------------------------------------
00480         //  Test 10: DeallocateBlocks: bad numBlocks (0, 5)
00481         //----------------------------------------------------------------
00482         rv = blockFile->DeallocateBlocks(0, 0);
00483         if (NS_SUCCEEDED(rv)) {
00484             printf("Test 10: failed (DeallocateBlocks(0, 0) erroneously succeeded)\n");
00485             goto exit;
00486         }
00487 
00488         rv = blockFile->DeallocateBlocks(0, 5);
00489         if (NS_SUCCEEDED(rv)) {
00490             printf("Test 10: failed (DeallocateBlocks(0, 5) erroneously succeeded)\n");
00491             goto exit;
00492         }
00493 
00494         printf("Test 10: passed\n");
00495 
00496 
00497         //----------------------------------------------------------------
00498         //  Test 11: DeallocateBlocks: unallocated blocks
00499         //----------------------------------------------------------------
00500         rv = blockFile->DeallocateBlocks(12, 1);
00501         if (NS_SUCCEEDED(rv)) {
00502             printf("Test 11: failed (DeallocateBlocks(12, 1) erroneously succeeded)\n");
00503             goto exit;
00504         }
00505 
00506         printf("Test 11: passed\n");
00507 
00508 
00509         //----------------------------------------------------------------
00510         //  Test 12: DeallocateBlocks: 1, 2, 3, 4 (allocated in Test 6)
00511         //----------------------------------------------------------------
00512         rv = blockFile->DeallocateBlocks(0, 1);
00513         if (NS_FAILED(rv)) {
00514             printf("Test 12: failed (DeallocateBlocks(12, 1) returned: 0x%.8x)\n", rv);
00515             goto exit;
00516         }
00517 
00518         rv = blockFile->DeallocateBlocks(1, 2);
00519         if (NS_FAILED(rv)) {
00520             printf("Test 12: failed (DeallocateBlocks(1, 2) returned: 0x%.8x)\n", rv);
00521             goto exit;
00522         }
00523 
00524         rv = blockFile->DeallocateBlocks(4, 3);
00525         if (NS_FAILED(rv)) {
00526             printf("Test 12: failed (DeallocateBlocks(4, 3) returned: 0x%.8x)\n", rv);
00527             goto exit;
00528         }
00529 
00530         rv = blockFile->DeallocateBlocks(8, 4);
00531         if (NS_FAILED(rv)) {
00532             printf("Test 12: failed (DeallocateBlocks(8, 4) returned: 0x%.8x)\n", rv);
00533             goto exit;
00534         }
00535 
00536         // zero blocks should be allocated
00537         rv = blockFile->Close();
00538         if (NS_FAILED(rv)) {
00539             printf("Test 12: failed (Close returned: 0x%.8x)\n", rv);
00540             goto exit;
00541         }
00542 
00543         printf("Test 12: passed\n");
00544 
00545 
00546         //----------------------------------------------------------------
00547         //  Test 13: Allocate/Deallocate boundary test
00548         //----------------------------------------------------------------
00549 
00550         rv = blockFile->Open(localFile, 256);
00551         if (NS_FAILED(rv)) {
00552             printf("Test 13: failed (Open returned: 0x%.8x)\n", rv);
00553             goto exit;
00554         }
00555 
00556         // fully allocate, 1 block at a time
00557         for (i=0; i< kBitMapBytes * 8; ++i) {
00558             startBlock = blockFile->AllocateBlocks(1);
00559             if (startBlock < 0) {
00560                 printf("Test 13: failed (AllocateBlocks(1) failed on i=%d)\n", i);
00561                 goto exit;
00562             }
00563         }
00564 
00565         // attempt allocation with full bit map
00566         startBlock = blockFile->AllocateBlocks(1);
00567         if (startBlock >= 0) {
00568             printf("Test 13: failed (AllocateBlocks(1) erroneously succeeded i=%d)\n", i);
00569             goto exit;
00570         }
00571 
00572         // deallocate all the bits
00573         for (i=0; i< kBitMapBytes * 8; ++i) {
00574             rv = blockFile->DeallocateBlocks(i,1);
00575             if (NS_FAILED(rv)) {
00576                 printf("Test 13: failed (DeallocateBlocks(%d,1) returned: 0x%.8x)\n", i,rv);
00577                 goto exit;
00578             }
00579         }
00580 
00581         // attempt deallocation beyond end of bit map
00582         rv = blockFile->DeallocateBlocks(i,1);
00583         if (NS_SUCCEEDED(rv)) {
00584             printf("Test 13: failed (DeallocateBlocks(%d,1) erroneously succeeded)\n", i);
00585             goto exit;
00586         }
00587 
00588         // bit map should be empty
00589 
00590         // fully allocate, 2 block at a time
00591         for (i=0; i< kBitMapBytes * 8; i+=2) {
00592             startBlock = blockFile->AllocateBlocks(2);
00593             if (startBlock < 0) {
00594                 printf("Test 13: failed (AllocateBlocks(2) failed on i=%d)\n", i);
00595                 goto exit;
00596             }
00597         }
00598 
00599         // attempt allocation with full bit map
00600         startBlock = blockFile->AllocateBlocks(2);
00601         if (startBlock >= 0) {
00602             printf("Test 13: failed (AllocateBlocks(2) erroneously succeeded i=%d)\n", i);
00603             goto exit;
00604         }
00605 
00606         // deallocate all the bits
00607         for (i=0; i< kBitMapBytes * 8; i+=2) {
00608             rv = blockFile->DeallocateBlocks(i,2);
00609             if (NS_FAILED(rv)) {
00610                 printf("Test 13: failed (DeallocateBlocks(%d,2) returned: 0x%.8x)\n", i,rv);
00611                 goto exit;
00612             }
00613         }
00614 
00615         // bit map should be empty
00616 
00617         // fully allocate, 4 block at a time
00618         for (i=0; i< kBitMapBytes * 8; i+=4) {
00619             startBlock = blockFile->AllocateBlocks(4);
00620             if (startBlock < 0) {
00621                 printf("Test 13: failed (AllocateBlocks(4) failed on i=%d)\n", i);
00622                 goto exit;
00623             }
00624         }
00625 
00626         // attempt allocation with full bit map
00627         startBlock = blockFile->AllocateBlocks(4);
00628         if (startBlock >= 0) {
00629             printf("Test 13: failed (AllocateBlocks(4) erroneously succeeded i=%d)\n", i);
00630             goto exit;
00631         }
00632 
00633         // deallocate all the bits
00634         for (i=0; i< kBitMapBytes * 8; i+=4) {
00635             rv = blockFile->DeallocateBlocks(i,4);
00636             if (NS_FAILED(rv)) {
00637                 printf("Test 13: failed (DeallocateBlocks(%d,4) returned: 0x%.8x)\n", i,rv);
00638                 goto exit;
00639             }
00640         }
00641 
00642         // bit map should be empty
00643 
00644         // allocate as many triple-blocks as possible
00645         for (i=0; i< kBitMapBytes * 8; i+=4) {
00646             startBlock = blockFile->AllocateBlocks(3);
00647             if (startBlock < 0) {
00648                 printf("Test 13: failed (AllocateBlocks(3) failed on i=%d)\n", i);
00649                 goto exit;
00650             }
00651         }
00652 
00653         // attempt allocation with "full" bit map
00654         startBlock = blockFile->AllocateBlocks(3);
00655         if (startBlock >= 0) {
00656             printf("Test 13: failed (AllocateBlocks(3) erroneously succeeded i=%d)\n", i);
00657             goto exit;
00658         }
00659 
00660         // leave some blocks allocated
00661 
00662         rv = blockFile->Close();
00663         if (NS_FAILED(rv)) {
00664             printf("Test 13: failed (Close returned: 0x%.8x)\n", rv);
00665             goto exit;
00666         }
00667 
00668         printf("Test 13: passed\n");
00669 
00670 
00671         //----------------------------------------------------------------
00672         //  Test 14: ValidateFile (open existing file w/size < allocated blocks
00673         //----------------------------------------------------------------
00674         rv = blockFile->Open(localFile, 256);
00675         if (NS_SUCCEEDED(rv)) {
00676             printf("Test 14: failed (Open erroneously succeeded)\n");
00677             goto exit;
00678         }
00679 
00680         // Delete existing file
00681         rv = localFile->Delete(PR_FALSE);
00682         if (NS_FAILED(rv)) {
00683             printf("Test 14 failed (Delete returned: 0x%.8x)\n", rv);
00684             goto exit;
00685         }
00686         printf("Test 14: passed\n");
00687 
00688 
00689         //----------------------------------------------------------------
00690         //  Test 15: Allocate/Deallocate stress test
00691         //----------------------------------------------------------------
00692 
00693         rv = StressTest(localFile, 15, PR_FALSE);
00694         if (NS_FAILED(rv))
00695             goto exit;
00696 
00697         printf("Test 15: passed\n");
00698 
00699 
00700         //----------------------------------------------------------------
00701         //  Test 16: WriteBlocks
00702         //----------------------------------------------------------------
00703 
00704         rv = blockFile->Open(localFile, 256);
00705         if (NS_FAILED(rv)) {
00706             printf("Test 16: failed (Open returned: 0x%.8x)\n", rv);
00707             goto exit;
00708         }
00709 
00710         char * one   = new char[256 * 1];
00711         char * two   = new char[256 * 2];
00712         char * three = new char[256 * 3];
00713         char * four  = new char[256 * 4];
00714         if (!one || !two || !three || !four) {
00715             printf("Test 16: failed - out of memory\n");
00716             rv = NS_ERROR_OUT_OF_MEMORY;
00717             goto exit;
00718         }
00719 
00720         memset(one,   1, 256);
00721         memset(two,   2, 256 * 2);
00722         memset(three, 3, 256 * 3);
00723         memset(four,  4, 256 * 4);
00724 
00725         startBlock = blockFile->AllocateBlocks(1);
00726         if (startBlock != 0) {
00727             printf("Test 16: failed (AllocateBlocks(1) failed)\n");
00728             goto exit;
00729         }
00730 
00731         rv = blockFile->WriteBlocks(one, startBlock, 1);
00732         if (NS_FAILED(rv)) {
00733             printf("Test 16: failed (WriteBlocks(1) returned 0x%.8x)\n", rv);
00734             goto exit;
00735         }
00736 
00737         startBlock = blockFile->AllocateBlocks(2);
00738         if (startBlock != 1) {  // starting with empy map, this allocation should begin at block 1
00739             printf("Test 16: failed (AllocateBlocks(2) failed)\n");
00740             goto exit;
00741         }
00742 
00743         rv = blockFile->WriteBlocks(two, startBlock, 2);
00744         if (NS_FAILED(rv)) {
00745             printf("Test 16: failed (WriteBlocks(2) returned 0x%.8x)\n", rv);
00746             goto exit;
00747         }
00748 
00749         startBlock = blockFile->AllocateBlocks(3);
00750         if (startBlock != 4) {  // starting with empy map, this allocation should begin at block 4
00751             printf("Test 16: failed (AllocateBlocks(3) failed)\n");
00752             goto exit;
00753         }
00754 
00755         rv = blockFile->WriteBlocks(three, startBlock, 3);
00756         if (NS_FAILED(rv)) {
00757             printf("Test 16: failed (WriteBlocks(3) returned 0x%.8x)\n", rv);
00758             goto exit;
00759         }
00760 
00761         startBlock = blockFile->AllocateBlocks(4);
00762         if (startBlock != 8) {  // starting with empy map, this allocation should begin at block 8
00763             printf("Test 16: failed (AllocateBlocks(4) failed)\n");
00764             goto exit;
00765         }
00766 
00767         rv = blockFile->WriteBlocks(four, startBlock, 4);
00768         if (NS_FAILED(rv)) {
00769             printf("Test 16: failed (WriteBlocks(4) returned 0x%.8x)\n", rv);
00770             goto exit;
00771         }
00772 
00773         printf("Test 16: passed\n");
00774 
00775 
00776         //----------------------------------------------------------------
00777         //  Test 17: ReadBlocks
00778         //----------------------------------------------------------------
00779 
00780         rv = blockFile->ReadBlocks(one, 0, 1);
00781         if (NS_FAILED(rv)) {
00782             printf("Test 17: failed (ReadBlocks(1) returned 0x%.8x)\n", rv);
00783             goto exit;
00784         }
00785 
00786         // Verify buffer
00787         for (i = 0; i < 256; i++) {
00788             if (one[i] != 1) {
00789                 printf("Test 17: failed (verifying buffer 1)\n");
00790                 rv = NS_ERROR_FAILURE;
00791                 goto exit;
00792             }
00793         }
00794 
00795         rv = blockFile->ReadBlocks(two, 1, 2);
00796         if (NS_FAILED(rv)) {
00797             printf("Test 17: failed (ReadBlocks(2) returned 0x%.8x)\n", rv);
00798             goto exit;
00799         }
00800 
00801         // Verify buffer
00802         for (i = 0; i < 256 * 2; i++) {
00803             if (two[i] != 2) {
00804                 printf("Test 17: failed (verifying buffer 2)\n");
00805                 rv = NS_ERROR_FAILURE;
00806                 goto exit;
00807             }
00808         }
00809 
00810         rv = blockFile->ReadBlocks(three, 4, 3);
00811         if (NS_FAILED(rv)) {
00812             printf("Test 17: failed (ReadBlocks(3) returned 0x%.8x)\n", rv);
00813             goto exit;
00814         }
00815 
00816         // Verify buffer
00817         for (i = 0; i < 256 * 3; i++) {
00818             if (three[i] != 3) {
00819                 printf("Test 17: failed (verifying buffer 3)\n");
00820                 rv = NS_ERROR_FAILURE;
00821                 goto exit;
00822             }
00823         }
00824 
00825         rv = blockFile->ReadBlocks(four, 8, 4);
00826         if (NS_FAILED(rv)) {
00827             printf("Test 17: failed (ReadBlocks(4) returned 0x%.8x)\n", rv);
00828             goto exit;
00829         }
00830 
00831         // Verify buffer
00832         for (i = 0; i < 256 * 4; i++) {
00833             if (four[i] != 4) {
00834                 printf("Test 17: failed (verifying buffer 4)\n");
00835                 rv = NS_ERROR_FAILURE;
00836                 goto exit;
00837             }
00838         }
00839 
00840         rv = blockFile->Close();
00841         if (NS_FAILED(rv)) {
00842             printf("Test 17: failed (Close returned: 0x%.8x)\n", rv);
00843             goto exit;
00844         }
00845 
00846         printf("Test 17: passed\n");
00847 
00848 
00849         //----------------------------------------------------------------
00850         //  Test 18: ValidateFile (open existing file with blocks allocated)
00851         //----------------------------------------------------------------
00852         rv = blockFile->Open(localFile, 256);
00853         if (NS_FAILED(rv)) {
00854             printf("Test 18: failed (Open returned: 0x%.8x)\n", rv);
00855             goto exit;
00856         }
00857 
00858         rv = blockFile->Close();
00859         if (NS_FAILED(rv)) {
00860             printf("Test 18: failed (Close returned: 0x%.8x)\n", rv);
00861             goto exit;
00862         }
00863 
00864         printf("Test 18: passed\n");
00865 
00866         //----------------------------------------------------------------
00867         //  Test 19: WriteBlocks/ReadBlocks stress
00868         //----------------------------------------------------------------
00869 
00870         rv = StressTest(localFile, 19, PR_FALSE);
00871         if (NS_FAILED(rv))
00872             goto exit;
00873 
00874         printf("Test 19: passed\n");
00875 
00876 
00877 exit:
00878 
00879         if (currentDirPath)
00880             nsMemory::Free(currentDirPath);
00881     } // this scopes the nsCOMPtrs
00882     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00883     if (NS_FAILED(rv))
00884         printf("Test failed: 0x%.8x\n", rv);
00885 
00886     rv = NS_ShutdownXPCOM(nsnull);
00887     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00888 
00889     printf("XPCOM shut down.\n\n");
00890     return 0;
00891 }
00892