aboutsummaryrefslogtreecommitdiff
path: root/javascript/TestUtil.js
blob: 5d65b510cf2b2c047e033d36497f14ac3b8df46f (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
/**
 * @license
 * Copyright 2017 Google Inc. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Some utilities for all Javascript tests.
 */
goog.provide('wycheproof.TestUtil');
goog.require('goog.crypt');
goog.require('goog.crypt.base64');
goog.require('goog.json');
goog.requireType('goog.testing.TestCase.Result');

/**
 * Reads test vectors in JSON format from a file.
 * @param {string} filename
 *
 * @return {?}
 */
wycheproof.TestUtil.readJsonTestVectorsFromFile = function(filename){
  var fileContent = goog.loadFileSync_(filename);
  assertTrue('Invalid file format (expected JSON)', goog.json.isValid(fileContent));
  return JSON.parse(fileContent);
};


/**
 * Analyzes the result from a goog.testing.TestCase execution.
 *
 * @param {!goog.testing.TestCase.Result} result
 */
wycheproof.TestUtil.checkTestCaseResult = function(result) {
  var failMsg = '';
  if (result.errors.length > 0) {
    for (var i = 0; i < result.errors.length; i++) {
      failMsg += result.errors[i].message + '\n';
    }
    fail(failMsg);
  }
};


/**
 * Checks whether the given string is in hex format.
 * @param {string} s
 *
 * @return {boolean}
 */
wycheproof.TestUtil.isHex = function(s) {
  return /(^[0-9A-F]*$)|(^[0-9a-f]*$)/.test(s);
};

/**
 * Converts a hex string to a ArrayBuffer.
 * @param {string} s
 *
 * @return {!ArrayBuffer}
 */
wycheproof.TestUtil.hexToArrayBuffer = function(s) {
  var bytes = goog.crypt.hexToByteArray(s);
  return new Uint8Array(bytes).buffer;
};

/**
 * Converts an ArrayBuffer to hex string
 * @param {!ArrayBuffer} ab
 *
 * @return {string}
 */
wycheproof.TestUtil.arrayBufferToHex = function(ab) {
  return goog.crypt.byteArrayToHex(new Uint8Array(ab));
};

/**
 * Converts a Base64URL string to hex format
 * @param {string} s
 *
 * @return {string}
 */
wycheproof.TestUtil.base64UrlToHex = function (s) {
  var b64str = wycheproof.TestUtil.base64UrlToBase64(s);
  return wycheproof.TestUtil.base64ToHex(b64str);
};

/**
 * Converts a hex string to Base64 format.
 * @param {string} s
 *
 * @return {string}
 */
wycheproof.TestUtil.hexToBase64 = function(s) {
  var bytes = goog.crypt.hexToByteArray(s);
  return goog.crypt.base64.encodeByteArray(bytes);
};

/**
 * Converts a base64 string to hex format.
 * @param {string} s
 *
 * @return {string}
 */
wycheproof.TestUtil.base64ToHex = function(s) {
  var bytes = goog.crypt.base64.decodeStringToByteArray(s);
  return goog.crypt.byteArrayToHex(bytes);
};

/**
 * Converts a Base64 string to Base64URL
 * @param {string} s
 *
 * @return {string}
 */
wycheproof.TestUtil.base64ToBase64Url = function(s) {
  return s.replace(/\+/g, '-')
      .replace(/\//g, '_')
      .replace(/=/g, '');
};

/**
 * Converts a Base64Url string to Base64
 * @param {string} s
 *
 * @return {string}
 */
wycheproof.TestUtil.base64UrlToBase64 = function(s) {
  return (s + '==='.slice((s.length + 3) % 4))
      .replace(/\-/g, '+')
      .replace(/_/g, '/');
};