michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * This file tests that the guid function generates a guid of the proper length, michael@0: * with no invalid characters. michael@0: */ michael@0: michael@0: /** michael@0: * Checks all our invariants about our guids for a given result. michael@0: * michael@0: * @param aGuid michael@0: * The guid to check. michael@0: */ michael@0: function check_invariants(aGuid) michael@0: { michael@0: do_print("Checking guid '" + aGuid + "'"); michael@0: michael@0: do_check_valid_places_guid(aGuid); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Functions michael@0: michael@0: function test_guid_invariants() michael@0: { michael@0: const kExpectedChars = 64; michael@0: const kAllowedChars = michael@0: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_" michael@0: do_check_eq(kAllowedChars.length, kExpectedChars); michael@0: const kGuidLength = 12; michael@0: michael@0: let checkedChars = []; michael@0: for (let i = 0; i < kGuidLength; i++) { michael@0: checkedChars[i] = {}; michael@0: for (let j = 0; j < kAllowedChars; j++) { michael@0: checkedChars[i][kAllowedChars[j]] = false; michael@0: } michael@0: } michael@0: michael@0: // We run this until we've seen every character that we expect to see in every michael@0: // position. michael@0: let seenChars = 0; michael@0: let stmt = DBConn().createStatement("SELECT GENERATE_GUID()"); michael@0: while (seenChars != (kExpectedChars * kGuidLength)) { michael@0: do_check_true(stmt.executeStep()); michael@0: let guid = stmt.getString(0); michael@0: check_invariants(guid); michael@0: michael@0: for (let i = 0; i < guid.length; i++) { michael@0: let character = guid[i]; michael@0: if (!checkedChars[i][character]) { michael@0: checkedChars[i][character] = true; michael@0: seenChars++; michael@0: } michael@0: } michael@0: stmt.reset(); michael@0: } michael@0: stmt.finalize(); michael@0: michael@0: // One last reality check - make sure all of our characters were seen. michael@0: for (let i = 0; i < kGuidLength; i++) { michael@0: for (let j = 0; j < kAllowedChars; j++) { michael@0: do_check_true(checkedChars[i][kAllowedChars[j]]); michael@0: } michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: function test_guid_on_background() michael@0: { michael@0: // We should not assert if we execute this asynchronously. michael@0: let stmt = DBConn().createAsyncStatement("SELECT GENERATE_GUID()"); michael@0: let checked = false; michael@0: stmt.executeAsync({ michael@0: handleResult: function(aResult) { michael@0: try { michael@0: let row = aResult.getNextRow(); michael@0: check_invariants(row.getResultByIndex(0)); michael@0: do_check_eq(aResult.getNextRow(), null); michael@0: checked = true; michael@0: } michael@0: catch (e) { michael@0: do_throw(e); michael@0: } michael@0: }, michael@0: handleCompletion: function(aReason) { michael@0: do_check_eq(aReason, Ci.mozIStorageStatementCallback.REASON_FINISHED); michael@0: do_check_true(checked); michael@0: run_next_test(); michael@0: } michael@0: }); michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Runner michael@0: michael@0: [ michael@0: test_guid_invariants, michael@0: test_guid_on_background, michael@0: ].forEach(add_test); michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: }