bytestring_test.js 9.8 KB

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