|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { arrayToBase64, base64ToArray } from './array-base64-translator'; |
| 19 | +import { expect } from 'chai'; |
| 20 | +import '../testing/setup'; |
| 21 | + |
| 22 | +// prettier-ignore |
| 23 | +const TEST_P256_ARRAY = new Uint8Array([ |
| 24 | + 4, 181, 98, 240, 48, 62, 75, 119, 193, 227, 154, 69, 250, 216, 53, 110, |
| 25 | + 157, 120, 62, 76, 213, 249, 11, 62, 12, 19, 149, 36, 5, 82, 140, 37, 141, |
| 26 | + 134, 132, 98, 87, 152, 175, 98, 53, 83, 196, 242, 202, 155, 19, 173, 157, |
| 27 | + 216, 45, 147, 20, 12, 151, 160, 147, 159, 205, 219, 75, 133, 156, 129, 152 |
| 28 | +]); |
| 29 | +const TEST_P256_BASE64 = 'BLVi8DA-S3fB45pF-tg1bp14PkzV-Qs-DBOVJAVSjCWNhoRi' + |
| 30 | + 'V5ivYjVTxPLKmxOtndgtkxQMl6CTn83bS4WcgZg'; |
| 31 | + |
| 32 | +// prettier-ignore |
| 33 | +const TEST_AUTH_ARRAY = new Uint8Array([ |
| 34 | + 255, 237, 107, 177, 171, 78, 84, 131, 221, 231, 87, 188, 22, 232, 71, 15 |
| 35 | +]); |
| 36 | +const TEST_AUTH_BASE64 = '_-1rsatOVIPd51e8FuhHDw'; |
| 37 | + |
| 38 | +// prettier-ignore |
| 39 | +const TEST_VAPID_ARRAY = new Uint8Array([4, 48, 191, 217, 11, 218, 74, 124, 103, 143, 63, 182, 203, |
| 40 | + 91, 0, 68, 221, 68, 172, 74, 89, 133, 198, 252, 145, 164, 136, 243, 186, 75, 198, 32, 45, 64, 240, |
| 41 | + 120, 141, 173, 240, 131, 253, 83, 209, 193, 129, 50, 155, 126, 189, 23, 127, 232, 109, 75, 101, |
| 42 | + 229, 92, 85, 137, 80, 121, 35, 229, 118, 207]); |
| 43 | +const TEST_VAPID_BASE64 = 'BDC_2QvaSnxnjz-2y1sARN1ErEpZhcb8kaSI87pLxiAtQPB4ja3wg_1T0cGBMpt' + |
| 44 | + '-vRd_6G1LZeVcVYlQeSPlds8'; |
| 45 | + |
| 46 | + |
| 47 | +describe('arrayToBase64', () => { |
| 48 | + it('array to base64 translation succeed', () => { |
| 49 | + expect(arrayToBase64(TEST_P256_ARRAY)).to.equal(TEST_P256_BASE64); |
| 50 | + expect(arrayToBase64(TEST_AUTH_ARRAY)).to.equal(TEST_AUTH_BASE64); |
| 51 | + expect(arrayToBase64(TEST_VAPID_ARRAY)).to.equal(TEST_VAPID_BASE64); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('base64ToArray', () => { |
| 56 | + it('base64 to array translation succeed', () => { |
| 57 | + expect(isEqual(base64ToArray(TEST_P256_BASE64), TEST_P256_ARRAY)).to.equal(true); |
| 58 | + expect(isEqual(base64ToArray(TEST_AUTH_BASE64), TEST_AUTH_ARRAY)).to.equal(true); |
| 59 | + expect(isEqual(base64ToArray(TEST_VAPID_BASE64), TEST_VAPID_ARRAY)).to.equal(true); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +function isEqual(a: Uint8Array, b: Uint8Array): boolean { |
| 64 | + if (a.length !== b.length) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + for (let i = 0; i < a.length; i++) { |
| 69 | + if (a[i] !== b[i]) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | +} |
0 commit comments