|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// tests contains MPL tests: https://github.com/aws/aws-cryptographic-material-providers-library/blob/da6812fa30315fda75d4277f814d1d0e36e22498/StandardLibrary/test/UUID.dfy |
| 5 | + |
| 6 | +import { v3, v5, v1, v4 } from 'uuid' |
| 7 | +import { uuidv4Factory } from '../src/uuidv4_factory' |
| 8 | +import { expect } from 'chai' |
| 9 | + |
| 10 | +const stringToHexBytes = (input: string): Uint8Array => |
| 11 | + new Uint8Array(Buffer.from(input, 'hex')) |
| 12 | + |
| 13 | +const hexBytesToString = (input: Uint8Array): string => |
| 14 | + Buffer.from(input).toString('hex') |
| 15 | + |
| 16 | +const { uuidv4ToCompressedBytes, decompressBytesToUuidv4 } = uuidv4Factory( |
| 17 | + stringToHexBytes, |
| 18 | + hexBytesToString |
| 19 | +) |
| 20 | + |
| 21 | +const uuidString = '92382658-b7a0-4d97-9c49-cee4e672a3b3' |
| 22 | +const byteUuid = new Uint8Array([ |
| 23 | + 146, 56, 38, 88, 183, 160, 77, 151, 156, 73, 206, 228, 230, 114, 163, 179, |
| 24 | +]) |
| 25 | +const wrongByteUuid = new Uint8Array([ |
| 26 | + 146, 56, 38, 88, 183, 160, 77, 151, 156, 73, 206, 228, 230, 114, 163, 178, |
| 27 | +]) |
| 28 | + |
| 29 | +describe('uuidv4Factory', () => { |
| 30 | + it('Test roundtrip string conversion', () => { |
| 31 | + const stringToBytes = uuidv4ToCompressedBytes(uuidString) |
| 32 | + expect(stringToBytes).has.lengthOf(16) |
| 33 | + const bytesToString = decompressBytesToUuidv4(stringToBytes) |
| 34 | + expect(bytesToString).to.equal(uuidString) |
| 35 | + }) |
| 36 | + |
| 37 | + it('Test roundtrip byte conversion', () => { |
| 38 | + const bytesToString = decompressBytesToUuidv4(byteUuid) |
| 39 | + const stringToBytes = uuidv4ToCompressedBytes(bytesToString) |
| 40 | + expect(stringToBytes).has.lengthOf(16) |
| 41 | + expect(stringToBytes).to.deep.equal(byteUuid) |
| 42 | + }) |
| 43 | + |
| 44 | + it('Test generate and conversion', () => { |
| 45 | + const uuid = v4() |
| 46 | + const uuidBytes = uuidv4ToCompressedBytes(uuid) |
| 47 | + const bytesToString = decompressBytesToUuidv4(uuidBytes) |
| 48 | + const stringToBytes = uuidv4ToCompressedBytes(bytesToString) |
| 49 | + |
| 50 | + expect(stringToBytes).has.lengthOf(16) |
| 51 | + expect(stringToBytes).to.deep.equal(uuidBytes) |
| 52 | + |
| 53 | + const uuidStringToBytes = uuidv4ToCompressedBytes(uuid) |
| 54 | + expect(uuidStringToBytes).has.lengthOf(16) |
| 55 | + const uuidBytesToString = decompressBytesToUuidv4(uuidStringToBytes) |
| 56 | + expect(uuidBytesToString).to.equal(uuid) |
| 57 | + }) |
| 58 | + |
| 59 | + describe('decompressBytesToUuidv4', () => { |
| 60 | + it('Precondition: Compressed bytes must have correct byte length', () => { |
| 61 | + expect(() => decompressBytesToUuidv4(new Uint8Array([0]))).to.throw( |
| 62 | + 'Compressed uuid has incorrect byte length' |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + it('Postcondition: Output string must be valid uuidv4', () => { |
| 67 | + expect(() => |
| 68 | + decompressBytesToUuidv4(new Uint8Array(Buffer.alloc(16))) |
| 69 | + ).to.throw('Input must represent a uuidv4') |
| 70 | + }) |
| 71 | + |
| 72 | + it('Test success', () => { |
| 73 | + const fromBytes = decompressBytesToUuidv4(byteUuid) |
| 74 | + expect(fromBytes).to.equal(uuidString) |
| 75 | + }) |
| 76 | + |
| 77 | + it('Test failure', () => { |
| 78 | + const fromBytes = decompressBytesToUuidv4(wrongByteUuid) |
| 79 | + expect(fromBytes).to.not.equals(uuidString) |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('uuidv4ToCompressedBytes', () => { |
| 84 | + it('Precondition: Input string must be valid uuidv4', () => { |
| 85 | + expect(() => uuidv4ToCompressedBytes(v1())).to.throw( |
| 86 | + 'Input must be valid uuidv4' |
| 87 | + ) |
| 88 | + |
| 89 | + const name = 'example.com' |
| 90 | + const namespace = uuidString |
| 91 | + expect(() => uuidv4ToCompressedBytes(v3(name, namespace))).to.throw( |
| 92 | + 'Input must be valid uuidv4' |
| 93 | + ) |
| 94 | + |
| 95 | + expect(() => uuidv4ToCompressedBytes(v5(name, namespace))).to.throw( |
| 96 | + 'Input must be valid uuidv4' |
| 97 | + ) |
| 98 | + }) |
| 99 | + |
| 100 | + it('Postcondition: Compressed bytes must have correct byte length', () => { |
| 101 | + expect(() => uuidv4ToCompressedBytes(uuidString)).to.not.throw() |
| 102 | + }) |
| 103 | + |
| 104 | + it('Test success', () => { |
| 105 | + const fromBytes = uuidv4ToCompressedBytes(uuidString) |
| 106 | + expect(fromBytes).to.deep.equal(byteUuid) |
| 107 | + }) |
| 108 | + |
| 109 | + it('Test failure', () => { |
| 110 | + const fromBytes = uuidv4ToCompressedBytes(uuidString) |
| 111 | + expect(fromBytes).to.not.deep.equals(wrongByteUuid) |
| 112 | + }) |
| 113 | + }) |
| 114 | +}) |
0 commit comments