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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:4d9345883684
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /**
5 * This file tests that the guid function generates a guid of the proper length,
6 * with no invalid characters.
7 */
8
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 + "'");
18
19 do_check_valid_places_guid(aGuid);
20 }
21
22 ////////////////////////////////////////////////////////////////////////////////
23 //// Test Functions
24
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;
32
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 }
40
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);
49
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();
60
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 }
67
68 run_next_test();
69 }
70
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 }
96
97 ////////////////////////////////////////////////////////////////////////////////
98 //// Test Runner
99
100 [
101 test_guid_invariants,
102 test_guid_on_background,
103 ].forEach(add_test);
104
105 function run_test()
106 {
107 run_next_test();
108 }

mercurial