toolkit/components/places/tests/unit/test_sql_guid_functions.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 /**
     5  * This file tests that the guid function generates a guid of the proper length,
     6  * with no invalid characters.
     7  */
     9 /**
    10  * Checks all our invariants about our guids for a given result.
    11  *
    12  * @param aGuid
    13  *        The guid to check.
    14  */
    15 function check_invariants(aGuid)
    16 {
    17   do_print("Checking guid '" + aGuid + "'");
    19   do_check_valid_places_guid(aGuid);
    20 }
    22 ////////////////////////////////////////////////////////////////////////////////
    23 //// Test Functions
    25 function test_guid_invariants()
    26 {
    27   const kExpectedChars = 64;
    28   const kAllowedChars =
    29     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
    30   do_check_eq(kAllowedChars.length, kExpectedChars);
    31   const kGuidLength = 12;
    33   let checkedChars = [];
    34   for (let i = 0; i < kGuidLength; i++) {
    35     checkedChars[i] = {};
    36     for (let j = 0; j < kAllowedChars; j++) {
    37       checkedChars[i][kAllowedChars[j]] = false;
    38     }
    39   }
    41   // We run this until we've seen every character that we expect to see in every
    42   // position.
    43   let seenChars = 0;
    44   let stmt = DBConn().createStatement("SELECT GENERATE_GUID()");
    45   while (seenChars != (kExpectedChars * kGuidLength)) {
    46     do_check_true(stmt.executeStep());
    47     let guid = stmt.getString(0);
    48     check_invariants(guid);
    50     for (let i = 0; i < guid.length; i++) {
    51       let character = guid[i];
    52       if (!checkedChars[i][character]) {
    53         checkedChars[i][character] = true;
    54         seenChars++;
    55       }
    56     }
    57     stmt.reset();
    58   }
    59   stmt.finalize();
    61   // One last reality check - make sure all of our characters were seen.
    62   for (let i = 0; i < kGuidLength; i++) {
    63     for (let j = 0; j < kAllowedChars; j++) {
    64       do_check_true(checkedChars[i][kAllowedChars[j]]);
    65     }
    66   }
    68   run_next_test();
    69 }
    71 function test_guid_on_background()
    72 {
    73   // We should not assert if we execute this asynchronously.
    74   let stmt = DBConn().createAsyncStatement("SELECT GENERATE_GUID()");
    75   let checked = false;
    76   stmt.executeAsync({
    77     handleResult: function(aResult) {
    78       try {
    79         let row = aResult.getNextRow();
    80         check_invariants(row.getResultByIndex(0));
    81         do_check_eq(aResult.getNextRow(), null);
    82         checked = true;
    83       }
    84       catch (e) {
    85         do_throw(e);
    86       }
    87     },
    88     handleCompletion: function(aReason) {
    89       do_check_eq(aReason, Ci.mozIStorageStatementCallback.REASON_FINISHED);
    90       do_check_true(checked);
    91       run_next_test();
    92     }
    93   });
    94   stmt.finalize();
    95 }
    97 ////////////////////////////////////////////////////////////////////////////////
    98 //// Test Runner
   100 [
   101   test_guid_invariants,
   102   test_guid_on_background,
   103 ].forEach(add_test);
   105 function run_test()
   106 {
   107   run_next_test();
   108 }

mercurial