summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/crypto/test_getRandomValues.html
blob: bbaddc5a2eb7a70f4943b5531f5b8ce4974b8f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE HTML>
<html><head>
  <title>Test window.crypto.getRandomValues</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="onWindowLoad()">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();

var testData = [ { len: 32, type: "Int8", pass: true },
                 { len: 32, type: "Int16", pass: true },
                 { len: 32, type: "Int32", pass: true },
                 { len: 32, type: "Uint8", pass: true },
                 { len: 32, type: "Uint16", pass: true },
                 { len: 32, type: "Uint32", pass: true },
                 { len: 65536, type: "Uint8", pass: true },
                 { len: 32, type: "Uint8Clamped", pass: true },
                 { len: 65537, type: "Uint8", pass: false },
                 { len: 32, type: "Float32", pass: false },
                 { len: 32, type: "Float64", pass: false } ];


var testCount = 0;

function testNsCryptoGetRandomValues(aLength, aType)
{
  var arrayTypes = {
    Int8:         Int8Array,
    Int16:        Int16Array,
    Int32:        Int32Array,
    Uint8:        Uint8Array,
    Uint16:       Uint16Array,
    Uint32:       Uint32Array,
    Float32:      Float32Array,
    Float64:      Float64Array,
    Uint8Clamped: Uint8ClampedArray,
  };

  testCount++;

  var buf = new ArrayBuffer(aLength);
  var arBuf = new arrayTypes[aType](buf);

  var pass = false;
  var b = window.crypto.getRandomValues(arBuf);
  ok(b === arBuf, "ArrayBuffer result is argument buffer");

  for (var i = 0; i < arBuf.length; i++) {
    if (arBuf.length > 6) {
      // XXXddahl: THIS MIGHT FAIL EVERY FULL MOON, SORRY QA!!!
      if (arBuf[i] != 0) {
        pass = true;
        break;
      }
    } 
    else {
      pass = true;
    }
  }
  is(pass, true,  "Non-zero result: " + i  +  " found in the  " + aType + ": " + aLength + " ArrayBufferView");
}

function onWindowLoad()
{
  window.removeEventListener("load", onWindowLoad, false);
  var failedWithCorrectError = false;
  try {
    for (var i = 0; i < testData.length; i++) {
      if (testData[i].pass) {
        try {
          testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
        } 
        catch (ex) {
          ok(false, "testNsCryptoGetRandomValues failed, test should have passed: " + testData[i].type);
        }
      }
      else {
        // failing tests are dealt with here
        if (i == 8) {
          try {
            testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
          }
          catch (ex) {
            todo("QuotaExceededError" in window && ex instanceof QuotaExceededError,
                 "Exception was the correct type");
            failedWithCorrectError = ex.toString().search(/QUOTA_EXCEEDED_ERR/);
            ok(failedWithCorrectError, "Extended length array buffer fails, NS_ERROR_DOM_QUOTA_EXCEEDED_ERR thrown");
          }
        } // 8

        if (i == 9) {
          try {
            testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
          }
          catch (ex) {
            failedWithCorrectError = ex.toString().search(/TYPE_MISMATCH_ERR/);
            ok(failedWithCorrectError,
               "Expected TYPE_MISMATCH_ERR: Float32Array is not valid, got " + ex + ".");
          }
        } // 9

        if (i == 10) {
          try {
            testNsCryptoGetRandomValues(testData[i].len, testData[i].type, testData[i].pass);
          }
          catch (ex) {
            failedWithCorrectError = ex.toString().search(/TYPE_MISMATCH_ERR/);
            ok(failedWithCorrectError,
               "Expected TYPE_MISMATCH_ERR: Float64Array is not valid, got " + ex + ".");
          }
        }
      }
    } // end main for loop
  }
  catch (ex) {
    ok(false, "Unexpected Error: " + ex);
  }
  // Count the tests in the testData array
  ok(testCount == 11, "11 tests run via testData");

  // Test a null argument
  try {
    window.crypto.getRandomValues(null);
  }
  catch (ex) {
    var test = ex.toString().search(/1003|TypeError/);
    ok((test > -1), "Expected TYPE_ERR, got " + ex + ".");
  }

  // Test a zero-length buffer view
  try {
    var a = new Int8Array(0);
    window.crypto.getRandomValues(a);
    ok(a[0] === undefined, "The array buffer is unchanged, still 0 length");
  }
  catch (ex) {
    ok(false, "A zero-length array buffer view should not fail");
  }

  // Test a one-length buffer view
  var randomVal = 0;
  // The probability of getRandomValues generating a zero value is 1/256 so we
  // run this in a loop until it returns a non-zero value to guard against
  // false failures
  do {
    try {
      var a = new Uint8Array(1);
      var b = window.crypto.getRandomValues(a);
      randomVal = a[0];
      ok(a === b, "ArrayBuffer result is argument buffer");
    }
    catch (ex) {
      ok(false, "A one-length array buffer view should not fail");
    }
  }
  while (randomVal == 0);
  ok(randomVal !== 0, "The array buffer eventually had one random value");

  // Test a 16 byte length buffer
  var testConfig = { len: 16, type: "Int8", pass: true };
  testNsCryptoGetRandomValues(testConfig.len,
                              testConfig.type,
                              testConfig.pass);

  // Test a 31 byte length buffer
  testConfig = { len: 31, type: "Int8", pass: true };
  testNsCryptoGetRandomValues(testConfig.len,
                              testConfig.type,
                              testConfig.pass);

  // Test a 33 byte length buffer
  testConfig = { len: 33, type: "Int8", pass: true };
  testNsCryptoGetRandomValues(testConfig.len,
                              testConfig.type,
                              testConfig.pass);

  // Test a range of an array buffer view
  var buffer = new ArrayBuffer(32);
  var view = new Int8Array(buffer, 0, 16);
  var view2 = new Int8Array(buffer, 16, 16);
  for (var i = 0; i < view2.byteLength; i++) {
    view2[i] = 1;
  }
  var b = window.crypto.getRandomValues(view);
  ok(b === view, "ArrayBuffer result is argument buffer");
  for (var i = 0; i < view.byteLength; i++) {
    is(view2[i], 1, "view2 is unchanged");
  }

  // test an offset view
  var result = false;
  var b = window.crypto.getRandomValues(view2);
  for (var i = 0; i < view2.length; i++) {
    if (view2[i] != 1) {
      result = true;
      break;
    }
  }
  ok(result, "view2 has been updated correctly");
  ok(b === view2, "ArrayBuffer result is argument buffer");
  // test the return value
  buffer = new ArrayBuffer(32);
  view = new Int8Array(buffer, 0, 16);
  var retval = window.crypto.getRandomValues(view);
  ok(view === retval, "The view and return value are the same");

  SimpleTest.finish();
}
</script>
</body></html>