1ffe3c632Sopenharmony_cigoog.module('proto.im.integration.ByteStringFieldsTest'); 2ffe3c632Sopenharmony_cigoog.setTestOnly(); 3ffe3c632Sopenharmony_ci 4ffe3c632Sopenharmony_ciconst ByteString = goog.require('protobuf.ByteString'); 5ffe3c632Sopenharmony_ciconst {arrayBufferSlice} = goog.require('protobuf.binary.typedArrays'); 6ffe3c632Sopenharmony_ci 7ffe3c632Sopenharmony_ciconst /** !ArrayBuffer */ TEST_BYTES = new Uint8Array([1, 2, 3, 4]).buffer; 8ffe3c632Sopenharmony_ciconst /** !ByteString */ TEST_STRING = ByteString.fromArrayBuffer(TEST_BYTES); 9ffe3c632Sopenharmony_ciconst /** !ArrayBuffer */ PREFIXED_TEST_BYTES = 10ffe3c632Sopenharmony_ci new Uint8Array([0, 1, 2, 3, 4]).buffer; 11ffe3c632Sopenharmony_ciconst /** string */ HALLO_IN_BASE64 = 'aGFsbG8='; 12ffe3c632Sopenharmony_ciconst /** string */ HALLO_IN_BASE64_WITH_SPACES = 'a G F s b G 8='; 13ffe3c632Sopenharmony_ciconst /** !ArrayBufferView */ BYTES_WITH_HALLO = new Uint8Array([ 14ffe3c632Sopenharmony_ci 'h'.charCodeAt(0), 15ffe3c632Sopenharmony_ci 'a'.charCodeAt(0), 16ffe3c632Sopenharmony_ci 'l'.charCodeAt(0), 17ffe3c632Sopenharmony_ci 'l'.charCodeAt(0), 18ffe3c632Sopenharmony_ci 'o'.charCodeAt(0), 19ffe3c632Sopenharmony_ci]); 20ffe3c632Sopenharmony_ci 21ffe3c632Sopenharmony_cidescribe('ByteString does', () => { 22ffe3c632Sopenharmony_ci it('create bytestring from buffer', () => { 23ffe3c632Sopenharmony_ci const byteString = 24ffe3c632Sopenharmony_ci ByteString.fromArrayBuffer(arrayBufferSlice(TEST_BYTES, 0)); 25ffe3c632Sopenharmony_ci expect(byteString.toArrayBuffer()).toEqual(TEST_BYTES); 26ffe3c632Sopenharmony_ci expect(byteString.toUint8ArrayUnsafe()).toEqual(new Uint8Array(TEST_BYTES)); 27ffe3c632Sopenharmony_ci }); 28ffe3c632Sopenharmony_ci 29ffe3c632Sopenharmony_ci it('create bytestring from ArrayBufferView', () => { 30ffe3c632Sopenharmony_ci const byteString = 31ffe3c632Sopenharmony_ci ByteString.fromArrayBufferView(new Uint8Array(TEST_BYTES)); 32ffe3c632Sopenharmony_ci expect(byteString.toArrayBuffer()).toEqual(TEST_BYTES); 33ffe3c632Sopenharmony_ci expect(byteString.toUint8ArrayUnsafe()).toEqual(new Uint8Array(TEST_BYTES)); 34ffe3c632Sopenharmony_ci }); 35ffe3c632Sopenharmony_ci 36ffe3c632Sopenharmony_ci it('create bytestring from subarray', () => { 37ffe3c632Sopenharmony_ci const byteString = ByteString.fromArrayBufferView( 38ffe3c632Sopenharmony_ci new Uint8Array(TEST_BYTES, /* offset */ 1, /* length */ 2)); 39ffe3c632Sopenharmony_ci const expected = new Uint8Array([2, 3]); 40ffe3c632Sopenharmony_ci expect(new Uint8Array(byteString.toArrayBuffer())).toEqual(expected); 41ffe3c632Sopenharmony_ci expect(byteString.toUint8ArrayUnsafe()).toEqual(expected); 42ffe3c632Sopenharmony_ci }); 43ffe3c632Sopenharmony_ci 44ffe3c632Sopenharmony_ci it('create bytestring from Uint8Array (unsafe)', () => { 45ffe3c632Sopenharmony_ci const array = new Uint8Array(TEST_BYTES); 46ffe3c632Sopenharmony_ci const byteString = ByteString.fromUint8ArrayUnsafe(array); 47ffe3c632Sopenharmony_ci expect(byteString.toArrayBuffer()).toEqual(array.buffer); 48ffe3c632Sopenharmony_ci expect(byteString.toUint8ArrayUnsafe()).toBe(array); 49ffe3c632Sopenharmony_ci }); 50ffe3c632Sopenharmony_ci 51ffe3c632Sopenharmony_ci it('create bytestring from base64 string', () => { 52ffe3c632Sopenharmony_ci const byteString = ByteString.fromBase64String(HALLO_IN_BASE64); 53ffe3c632Sopenharmony_ci expect(byteString.toBase64String()).toEqual(HALLO_IN_BASE64); 54ffe3c632Sopenharmony_ci expect(byteString.toArrayBuffer()).toEqual(BYTES_WITH_HALLO.buffer); 55ffe3c632Sopenharmony_ci expect(byteString.toUint8ArrayUnsafe()).toEqual(BYTES_WITH_HALLO); 56ffe3c632Sopenharmony_ci }); 57ffe3c632Sopenharmony_ci 58ffe3c632Sopenharmony_ci it('preserve immutability if underlying buffer changes: from buffer', () => { 59ffe3c632Sopenharmony_ci const buffer = new ArrayBuffer(4); 60ffe3c632Sopenharmony_ci const array = new Uint8Array(buffer); 61ffe3c632Sopenharmony_ci array[0] = 1; 62ffe3c632Sopenharmony_ci array[1] = 2; 63ffe3c632Sopenharmony_ci array[2] = 3; 64ffe3c632Sopenharmony_ci array[3] = 4; 65ffe3c632Sopenharmony_ci 66ffe3c632Sopenharmony_ci const byteString = ByteString.fromArrayBuffer(buffer); 67ffe3c632Sopenharmony_ci const otherBuffer = byteString.toArrayBuffer(); 68ffe3c632Sopenharmony_ci 69ffe3c632Sopenharmony_ci expect(otherBuffer).not.toBe(buffer); 70ffe3c632Sopenharmony_ci expect(new Uint8Array(otherBuffer)).toEqual(array); 71ffe3c632Sopenharmony_ci 72ffe3c632Sopenharmony_ci // modify the original buffer 73ffe3c632Sopenharmony_ci array[0] = 5; 74ffe3c632Sopenharmony_ci // Are we still returning the original bytes? 75ffe3c632Sopenharmony_ci expect(new Uint8Array(byteString.toArrayBuffer())).toEqual(new Uint8Array([ 76ffe3c632Sopenharmony_ci 1, 2, 3, 4 77ffe3c632Sopenharmony_ci ])); 78ffe3c632Sopenharmony_ci }); 79ffe3c632Sopenharmony_ci 80ffe3c632Sopenharmony_ci it('preserve immutability if underlying buffer changes: from ArrayBufferView', 81ffe3c632Sopenharmony_ci () => { 82ffe3c632Sopenharmony_ci const buffer = new ArrayBuffer(4); 83ffe3c632Sopenharmony_ci const array = new Uint8Array(buffer); 84ffe3c632Sopenharmony_ci array[0] = 1; 85ffe3c632Sopenharmony_ci array[1] = 2; 86ffe3c632Sopenharmony_ci array[2] = 3; 87ffe3c632Sopenharmony_ci array[3] = 4; 88ffe3c632Sopenharmony_ci 89ffe3c632Sopenharmony_ci const byteString = ByteString.fromArrayBufferView(array); 90ffe3c632Sopenharmony_ci const otherBuffer = byteString.toArrayBuffer(); 91ffe3c632Sopenharmony_ci 92ffe3c632Sopenharmony_ci expect(otherBuffer).not.toBe(buffer); 93ffe3c632Sopenharmony_ci expect(new Uint8Array(otherBuffer)).toEqual(array); 94ffe3c632Sopenharmony_ci 95ffe3c632Sopenharmony_ci // modify the original buffer 96ffe3c632Sopenharmony_ci array[0] = 5; 97ffe3c632Sopenharmony_ci // Are we still returning the original bytes? 98ffe3c632Sopenharmony_ci expect(new Uint8Array(byteString.toArrayBuffer())) 99ffe3c632Sopenharmony_ci .toEqual(new Uint8Array([1, 2, 3, 4])); 100ffe3c632Sopenharmony_ci }); 101ffe3c632Sopenharmony_ci 102ffe3c632Sopenharmony_ci it('mutate if underlying buffer changes: from Uint8Array (unsafe)', () => { 103ffe3c632Sopenharmony_ci const buffer = new ArrayBuffer(4); 104ffe3c632Sopenharmony_ci const array = new Uint8Array(buffer); 105ffe3c632Sopenharmony_ci array[0] = 1; 106ffe3c632Sopenharmony_ci array[1] = 2; 107ffe3c632Sopenharmony_ci array[2] = 3; 108ffe3c632Sopenharmony_ci array[3] = 4; 109ffe3c632Sopenharmony_ci 110ffe3c632Sopenharmony_ci const byteString = ByteString.fromUint8ArrayUnsafe(array); 111ffe3c632Sopenharmony_ci const otherBuffer = byteString.toArrayBuffer(); 112ffe3c632Sopenharmony_ci 113ffe3c632Sopenharmony_ci expect(otherBuffer).not.toBe(buffer); 114ffe3c632Sopenharmony_ci expect(new Uint8Array(otherBuffer)).toEqual(array); 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci // modify the original buffer 117ffe3c632Sopenharmony_ci array[0] = 5; 118ffe3c632Sopenharmony_ci // We are no longer returning the original bytes 119ffe3c632Sopenharmony_ci expect(new Uint8Array(byteString.toArrayBuffer())).toEqual(new Uint8Array([ 120ffe3c632Sopenharmony_ci 5, 2, 3, 4 121ffe3c632Sopenharmony_ci ])); 122ffe3c632Sopenharmony_ci }); 123ffe3c632Sopenharmony_ci 124ffe3c632Sopenharmony_ci it('preserve immutability for returned buffers: toArrayBuffer', () => { 125ffe3c632Sopenharmony_ci const byteString = ByteString.fromArrayBufferView(new Uint8Array(4)); 126ffe3c632Sopenharmony_ci const buffer1 = byteString.toArrayBuffer(); 127ffe3c632Sopenharmony_ci const buffer2 = byteString.toArrayBuffer(); 128ffe3c632Sopenharmony_ci 129ffe3c632Sopenharmony_ci expect(buffer1).toEqual(buffer2); 130ffe3c632Sopenharmony_ci 131ffe3c632Sopenharmony_ci const array1 = new Uint8Array(buffer1); 132ffe3c632Sopenharmony_ci array1[0] = 1; 133ffe3c632Sopenharmony_ci 134ffe3c632Sopenharmony_ci expect(buffer1).not.toEqual(buffer2); 135ffe3c632Sopenharmony_ci }); 136ffe3c632Sopenharmony_ci 137ffe3c632Sopenharmony_ci it('does not preserve immutability for returned buffers: toUint8ArrayUnsafe', 138ffe3c632Sopenharmony_ci () => { 139ffe3c632Sopenharmony_ci const byteString = ByteString.fromUint8ArrayUnsafe(new Uint8Array(4)); 140ffe3c632Sopenharmony_ci const array1 = byteString.toUint8ArrayUnsafe(); 141ffe3c632Sopenharmony_ci const array2 = byteString.toUint8ArrayUnsafe(); 142ffe3c632Sopenharmony_ci 143ffe3c632Sopenharmony_ci expect(array1).toEqual(array2); 144ffe3c632Sopenharmony_ci array1[0] = 1; 145ffe3c632Sopenharmony_ci 146ffe3c632Sopenharmony_ci expect(array1).toEqual(array2); 147ffe3c632Sopenharmony_ci }); 148ffe3c632Sopenharmony_ci 149ffe3c632Sopenharmony_ci it('throws when created with null ArrayBufferView', () => { 150ffe3c632Sopenharmony_ci expect( 151ffe3c632Sopenharmony_ci () => ByteString.fromArrayBufferView( 152ffe3c632Sopenharmony_ci /** @type {!ArrayBufferView} */ (/** @type{*} */ (null)))) 153ffe3c632Sopenharmony_ci .toThrow(); 154ffe3c632Sopenharmony_ci }); 155ffe3c632Sopenharmony_ci 156ffe3c632Sopenharmony_ci it('throws when created with null buffer', () => { 157ffe3c632Sopenharmony_ci expect( 158ffe3c632Sopenharmony_ci () => ByteString.fromBase64String( 159ffe3c632Sopenharmony_ci /** @type {string} */ (/** @type{*} */ (null)))) 160ffe3c632Sopenharmony_ci .toThrow(); 161ffe3c632Sopenharmony_ci }); 162ffe3c632Sopenharmony_ci 163ffe3c632Sopenharmony_ci it('convert base64 to ArrayBuffer', () => { 164ffe3c632Sopenharmony_ci const other = ByteString.fromBase64String(HALLO_IN_BASE64); 165ffe3c632Sopenharmony_ci expect(BYTES_WITH_HALLO).toEqual(new Uint8Array(other.toArrayBuffer())); 166ffe3c632Sopenharmony_ci }); 167ffe3c632Sopenharmony_ci 168ffe3c632Sopenharmony_ci it('convert base64 with spaces to ArrayBuffer', () => { 169ffe3c632Sopenharmony_ci const other = ByteString.fromBase64String(HALLO_IN_BASE64_WITH_SPACES); 170ffe3c632Sopenharmony_ci expect(new Uint8Array(other.toArrayBuffer())).toEqual(BYTES_WITH_HALLO); 171ffe3c632Sopenharmony_ci }); 172ffe3c632Sopenharmony_ci 173ffe3c632Sopenharmony_ci it('convert bytes to base64', () => { 174ffe3c632Sopenharmony_ci const other = ByteString.fromArrayBufferView(BYTES_WITH_HALLO); 175ffe3c632Sopenharmony_ci expect(HALLO_IN_BASE64).toEqual(other.toBase64String()); 176ffe3c632Sopenharmony_ci }); 177ffe3c632Sopenharmony_ci 178ffe3c632Sopenharmony_ci it('equal empty bytetring', () => { 179ffe3c632Sopenharmony_ci const empty = ByteString.fromArrayBuffer(new ArrayBuffer(0)); 180ffe3c632Sopenharmony_ci expect(ByteString.EMPTY.equals(empty)).toEqual(true); 181ffe3c632Sopenharmony_ci }); 182ffe3c632Sopenharmony_ci 183ffe3c632Sopenharmony_ci it('equal empty bytestring constructed from ArrayBufferView', () => { 184ffe3c632Sopenharmony_ci const empty = ByteString.fromArrayBufferView(new Uint8Array(0)); 185ffe3c632Sopenharmony_ci expect(ByteString.EMPTY.equals(empty)).toEqual(true); 186ffe3c632Sopenharmony_ci }); 187ffe3c632Sopenharmony_ci 188ffe3c632Sopenharmony_ci it('equal empty bytestring constructed from Uint8Array (unsafe)', () => { 189ffe3c632Sopenharmony_ci const empty = ByteString.fromUint8ArrayUnsafe(new Uint8Array(0)); 190ffe3c632Sopenharmony_ci expect(ByteString.EMPTY.equals(empty)).toEqual(true); 191ffe3c632Sopenharmony_ci }); 192ffe3c632Sopenharmony_ci 193ffe3c632Sopenharmony_ci it('equal empty bytestring constructed from base64', () => { 194ffe3c632Sopenharmony_ci const empty = ByteString.fromBase64String(''); 195ffe3c632Sopenharmony_ci expect(ByteString.EMPTY.equals(empty)).toEqual(true); 196ffe3c632Sopenharmony_ci }); 197ffe3c632Sopenharmony_ci 198ffe3c632Sopenharmony_ci it('equal other instance', () => { 199ffe3c632Sopenharmony_ci const other = ByteString.fromArrayBuffer(arrayBufferSlice(TEST_BYTES, 0)); 200ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(true); 201ffe3c632Sopenharmony_ci }); 202ffe3c632Sopenharmony_ci 203ffe3c632Sopenharmony_ci it('not equal different instance', () => { 204ffe3c632Sopenharmony_ci const other = 205ffe3c632Sopenharmony_ci ByteString.fromArrayBuffer(new Uint8Array([1, 2, 3, 4, 5]).buffer); 206ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(false); 207ffe3c632Sopenharmony_ci }); 208ffe3c632Sopenharmony_ci 209ffe3c632Sopenharmony_ci it('equal other instance constructed from ArrayBufferView', () => { 210ffe3c632Sopenharmony_ci const other = 211ffe3c632Sopenharmony_ci ByteString.fromArrayBufferView(new Uint8Array(PREFIXED_TEST_BYTES, 1)); 212ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(true); 213ffe3c632Sopenharmony_ci }); 214ffe3c632Sopenharmony_ci 215ffe3c632Sopenharmony_ci it('not equal different instance constructed from ArrayBufferView', () => { 216ffe3c632Sopenharmony_ci const other = 217ffe3c632Sopenharmony_ci ByteString.fromArrayBufferView(new Uint8Array([1, 2, 3, 4, 5])); 218ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(false); 219ffe3c632Sopenharmony_ci }); 220ffe3c632Sopenharmony_ci 221ffe3c632Sopenharmony_ci it('equal other instance constructed from Uint8Array (unsafe)', () => { 222ffe3c632Sopenharmony_ci const other = 223ffe3c632Sopenharmony_ci ByteString.fromUint8ArrayUnsafe(new Uint8Array(PREFIXED_TEST_BYTES, 1)); 224ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(true); 225ffe3c632Sopenharmony_ci }); 226ffe3c632Sopenharmony_ci 227ffe3c632Sopenharmony_ci it('not equal different instance constructed from Uint8Array (unsafe)', 228ffe3c632Sopenharmony_ci () => { 229ffe3c632Sopenharmony_ci const other = 230ffe3c632Sopenharmony_ci ByteString.fromUint8ArrayUnsafe(new Uint8Array([1, 2, 3, 4, 5])); 231ffe3c632Sopenharmony_ci expect(TEST_STRING.equals(other)).toEqual(false); 232ffe3c632Sopenharmony_ci }); 233ffe3c632Sopenharmony_ci 234ffe3c632Sopenharmony_ci it('have same hashcode for empty bytes', () => { 235ffe3c632Sopenharmony_ci const empty = ByteString.fromArrayBuffer(new ArrayBuffer(0)); 236ffe3c632Sopenharmony_ci expect(ByteString.EMPTY.hashCode()).toEqual(empty.hashCode()); 237ffe3c632Sopenharmony_ci }); 238ffe3c632Sopenharmony_ci 239ffe3c632Sopenharmony_ci it('have same hashcode for test bytes', () => { 240ffe3c632Sopenharmony_ci const other = ByteString.fromArrayBuffer(arrayBufferSlice(TEST_BYTES, 0)); 241ffe3c632Sopenharmony_ci expect(TEST_STRING.hashCode()).toEqual(other.hashCode()); 242ffe3c632Sopenharmony_ci }); 243ffe3c632Sopenharmony_ci 244ffe3c632Sopenharmony_ci it('have same hashcode for test bytes', () => { 245ffe3c632Sopenharmony_ci const other = ByteString.fromArrayBufferView( 246ffe3c632Sopenharmony_ci new Uint8Array(arrayBufferSlice(TEST_BYTES, 0))); 247ffe3c632Sopenharmony_ci expect(TEST_STRING.hashCode()).toEqual(other.hashCode()); 248ffe3c632Sopenharmony_ci }); 249ffe3c632Sopenharmony_ci 250ffe3c632Sopenharmony_ci it('have same hashcode for different instance constructed with base64', 251ffe3c632Sopenharmony_ci () => { 252ffe3c632Sopenharmony_ci const other = ByteString.fromBase64String(HALLO_IN_BASE64); 253ffe3c632Sopenharmony_ci expect(ByteString.fromArrayBufferView(BYTES_WITH_HALLO).hashCode()) 254ffe3c632Sopenharmony_ci .toEqual(other.hashCode()); 255ffe3c632Sopenharmony_ci }); 256ffe3c632Sopenharmony_ci 257ffe3c632Sopenharmony_ci it('preserves the length of a Uint8Array', () => { 258ffe3c632Sopenharmony_ci const original = new Uint8Array([105, 183, 51, 251, 253, 118, 247]); 259ffe3c632Sopenharmony_ci const afterByteString = new Uint8Array( 260ffe3c632Sopenharmony_ci ByteString.fromArrayBufferView(original).toArrayBuffer()); 261ffe3c632Sopenharmony_ci expect(afterByteString).toEqual(original); 262ffe3c632Sopenharmony_ci }); 263ffe3c632Sopenharmony_ci 264ffe3c632Sopenharmony_ci it('preserves the length of a base64 value', () => { 265ffe3c632Sopenharmony_ci const expected = new Uint8Array([105, 183, 51, 251, 253, 118, 247]); 266ffe3c632Sopenharmony_ci const afterByteString = new Uint8Array( 267ffe3c632Sopenharmony_ci ByteString.fromBase64String('abcz+/129w').toArrayBuffer()); 268ffe3c632Sopenharmony_ci expect(afterByteString).toEqual(expected); 269ffe3c632Sopenharmony_ci }); 270ffe3c632Sopenharmony_ci 271ffe3c632Sopenharmony_ci it('preserves the length of a base64 value with padding', () => { 272ffe3c632Sopenharmony_ci const expected = new Uint8Array([105, 183, 51, 251, 253, 118, 247]); 273ffe3c632Sopenharmony_ci const afterByteString = new Uint8Array( 274ffe3c632Sopenharmony_ci ByteString.fromBase64String('abcz+/129w==').toArrayBuffer()); 275ffe3c632Sopenharmony_ci expect(afterByteString).toEqual(expected); 276ffe3c632Sopenharmony_ci }); 277ffe3c632Sopenharmony_ci}); 278