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.

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

mercurial