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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_sql_guid_functions.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/**
     1.8 + * This file tests that the guid function generates a guid of the proper length,
     1.9 + * with no invalid characters.
    1.10 + */
    1.11 +
    1.12 +/**
    1.13 + * Checks all our invariants about our guids for a given result.
    1.14 + *
    1.15 + * @param aGuid
    1.16 + *        The guid to check.
    1.17 + */
    1.18 +function check_invariants(aGuid)
    1.19 +{
    1.20 +  do_print("Checking guid '" + aGuid + "'");
    1.21 +
    1.22 +  do_check_valid_places_guid(aGuid);
    1.23 +}
    1.24 +
    1.25 +////////////////////////////////////////////////////////////////////////////////
    1.26 +//// Test Functions
    1.27 +
    1.28 +function test_guid_invariants()
    1.29 +{
    1.30 +  const kExpectedChars = 64;
    1.31 +  const kAllowedChars =
    1.32 +    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
    1.33 +  do_check_eq(kAllowedChars.length, kExpectedChars);
    1.34 +  const kGuidLength = 12;
    1.35 +
    1.36 +  let checkedChars = [];
    1.37 +  for (let i = 0; i < kGuidLength; i++) {
    1.38 +    checkedChars[i] = {};
    1.39 +    for (let j = 0; j < kAllowedChars; j++) {
    1.40 +      checkedChars[i][kAllowedChars[j]] = false;
    1.41 +    }
    1.42 +  }
    1.43 +
    1.44 +  // We run this until we've seen every character that we expect to see in every
    1.45 +  // position.
    1.46 +  let seenChars = 0;
    1.47 +  let stmt = DBConn().createStatement("SELECT GENERATE_GUID()");
    1.48 +  while (seenChars != (kExpectedChars * kGuidLength)) {
    1.49 +    do_check_true(stmt.executeStep());
    1.50 +    let guid = stmt.getString(0);
    1.51 +    check_invariants(guid);
    1.52 +
    1.53 +    for (let i = 0; i < guid.length; i++) {
    1.54 +      let character = guid[i];
    1.55 +      if (!checkedChars[i][character]) {
    1.56 +        checkedChars[i][character] = true;
    1.57 +        seenChars++;
    1.58 +      }
    1.59 +    }
    1.60 +    stmt.reset();
    1.61 +  }
    1.62 +  stmt.finalize();
    1.63 +
    1.64 +  // One last reality check - make sure all of our characters were seen.
    1.65 +  for (let i = 0; i < kGuidLength; i++) {
    1.66 +    for (let j = 0; j < kAllowedChars; j++) {
    1.67 +      do_check_true(checkedChars[i][kAllowedChars[j]]);
    1.68 +    }
    1.69 +  }
    1.70 +
    1.71 +  run_next_test();
    1.72 +}
    1.73 +
    1.74 +function test_guid_on_background()
    1.75 +{
    1.76 +  // We should not assert if we execute this asynchronously.
    1.77 +  let stmt = DBConn().createAsyncStatement("SELECT GENERATE_GUID()");
    1.78 +  let checked = false;
    1.79 +  stmt.executeAsync({
    1.80 +    handleResult: function(aResult) {
    1.81 +      try {
    1.82 +        let row = aResult.getNextRow();
    1.83 +        check_invariants(row.getResultByIndex(0));
    1.84 +        do_check_eq(aResult.getNextRow(), null);
    1.85 +        checked = true;
    1.86 +      }
    1.87 +      catch (e) {
    1.88 +        do_throw(e);
    1.89 +      }
    1.90 +    },
    1.91 +    handleCompletion: function(aReason) {
    1.92 +      do_check_eq(aReason, Ci.mozIStorageStatementCallback.REASON_FINISHED);
    1.93 +      do_check_true(checked);
    1.94 +      run_next_test();
    1.95 +    }
    1.96 +  });
    1.97 +  stmt.finalize();
    1.98 +}
    1.99 +
   1.100 +////////////////////////////////////////////////////////////////////////////////
   1.101 +//// Test Runner
   1.102 +
   1.103 +[
   1.104 +  test_guid_invariants,
   1.105 +  test_guid_on_background,
   1.106 +].forEach(add_test);
   1.107 +
   1.108 +function run_test()
   1.109 +{
   1.110 +  run_next_test();
   1.111 +}

mercurial