test-buffer-fill.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 'use strict';
  2. var Buffer = require('../../').Buffer;
  3. var assert = require('assert');
  4. var os = require('os');
  5. var SIZE = 28;
  6. var buf1 = Buffer.allocUnsafe(SIZE);
  7. var buf2 = Buffer.allocUnsafe(SIZE);
  8. // Default encoding
  9. testBufs('abc');
  10. testBufs('\u0222aa');
  11. testBufs('a\u0234b\u0235c\u0236');
  12. testBufs('abc', 4);
  13. testBufs('abc', 5);
  14. testBufs('abc', SIZE);
  15. testBufs('\u0222aa', 2);
  16. testBufs('\u0222aa', 8);
  17. testBufs('a\u0234b\u0235c\u0236', 4);
  18. testBufs('a\u0234b\u0235c\u0236', 12);
  19. testBufs('abc', 4, -1);
  20. testBufs('abc', 4, 1);
  21. testBufs('abc', 5, 1);
  22. testBufs('\u0222aa', 2, -1);
  23. testBufs('\u0222aa', 8, 1);
  24. testBufs('a\u0234b\u0235c\u0236', 4, -1);
  25. testBufs('a\u0234b\u0235c\u0236', 4, 1);
  26. testBufs('a\u0234b\u0235c\u0236', 12, 1);
  27. // UTF8
  28. testBufs('abc', 'utf8');
  29. testBufs('\u0222aa', 'utf8');
  30. testBufs('a\u0234b\u0235c\u0236', 'utf8');
  31. testBufs('abc', 4, 'utf8');
  32. testBufs('abc', 5, 'utf8');
  33. testBufs('abc', SIZE, 'utf8');
  34. testBufs('\u0222aa', 2, 'utf8');
  35. testBufs('\u0222aa', 8, 'utf8');
  36. testBufs('a\u0234b\u0235c\u0236', 4, 'utf8');
  37. testBufs('a\u0234b\u0235c\u0236', 12, 'utf8');
  38. testBufs('abc', 4, -1, 'utf8');
  39. testBufs('abc', 4, 1, 'utf8');
  40. testBufs('abc', 5, 1, 'utf8');
  41. testBufs('\u0222aa', 2, -1, 'utf8');
  42. testBufs('\u0222aa', 8, 1, 'utf8');
  43. testBufs('a\u0234b\u0235c\u0236', 4, -1, 'utf8');
  44. testBufs('a\u0234b\u0235c\u0236', 4, 1, 'utf8');
  45. testBufs('a\u0234b\u0235c\u0236', 12, 1, 'utf8');
  46. assert.equal(Buffer.allocUnsafe(1).fill(0).fill('\u0222')[0], 0xc8);
  47. // BINARY
  48. testBufs('abc', 'binary');
  49. testBufs('\u0222aa', 'binary');
  50. testBufs('a\u0234b\u0235c\u0236', 'binary');
  51. testBufs('abc', 4, 'binary');
  52. testBufs('abc', 5, 'binary');
  53. testBufs('abc', SIZE, 'binary');
  54. testBufs('\u0222aa', 2, 'binary');
  55. testBufs('\u0222aa', 8, 'binary');
  56. testBufs('a\u0234b\u0235c\u0236', 4, 'binary');
  57. testBufs('a\u0234b\u0235c\u0236', 12, 'binary');
  58. testBufs('abc', 4, -1, 'binary');
  59. testBufs('abc', 4, 1, 'binary');
  60. testBufs('abc', 5, 1, 'binary');
  61. testBufs('\u0222aa', 2, -1, 'binary');
  62. testBufs('\u0222aa', 8, 1, 'binary');
  63. testBufs('a\u0234b\u0235c\u0236', 4, -1, 'binary');
  64. testBufs('a\u0234b\u0235c\u0236', 4, 1, 'binary');
  65. testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary');
  66. // LATIN1
  67. testBufs('abc', 'latin1');
  68. testBufs('\u0222aa', 'latin1');
  69. testBufs('a\u0234b\u0235c\u0236', 'latin1');
  70. testBufs('abc', 4, 'latin1');
  71. testBufs('abc', 5, 'latin1');
  72. testBufs('abc', SIZE, 'latin1');
  73. testBufs('\u0222aa', 2, 'latin1');
  74. testBufs('\u0222aa', 8, 'latin1');
  75. testBufs('a\u0234b\u0235c\u0236', 4, 'latin1');
  76. testBufs('a\u0234b\u0235c\u0236', 12, 'latin1');
  77. testBufs('abc', 4, -1, 'latin1');
  78. testBufs('abc', 4, 1, 'latin1');
  79. testBufs('abc', 5, 1, 'latin1');
  80. testBufs('\u0222aa', 2, -1, 'latin1');
  81. testBufs('\u0222aa', 8, 1, 'latin1');
  82. testBufs('a\u0234b\u0235c\u0236', 4, -1, 'latin1');
  83. testBufs('a\u0234b\u0235c\u0236', 4, 1, 'latin1');
  84. testBufs('a\u0234b\u0235c\u0236', 12, 1, 'latin1');
  85. // UCS2
  86. testBufs('abc', 'ucs2');
  87. testBufs('\u0222aa', 'ucs2');
  88. testBufs('a\u0234b\u0235c\u0236', 'ucs2');
  89. testBufs('abc', 4, 'ucs2');
  90. testBufs('abc', SIZE, 'ucs2');
  91. testBufs('\u0222aa', 2, 'ucs2');
  92. testBufs('\u0222aa', 8, 'ucs2');
  93. testBufs('a\u0234b\u0235c\u0236', 4, 'ucs2');
  94. testBufs('a\u0234b\u0235c\u0236', 12, 'ucs2');
  95. testBufs('abc', 4, -1, 'ucs2');
  96. testBufs('abc', 4, 1, 'ucs2');
  97. testBufs('abc', 5, 1, 'ucs2');
  98. testBufs('\u0222aa', 2, -1, 'ucs2');
  99. testBufs('\u0222aa', 8, 1, 'ucs2');
  100. testBufs('a\u0234b\u0235c\u0236', 4, -1, 'ucs2');
  101. testBufs('a\u0234b\u0235c\u0236', 4, 1, 'ucs2');
  102. testBufs('a\u0234b\u0235c\u0236', 12, 1, 'ucs2');
  103. assert.equal(Buffer.allocUnsafe(1).fill('\u0222', 'ucs2')[0],
  104. os.endianness() === 'LE' ? 0x22 : 0x02);
  105. // HEX
  106. testBufs('616263', 'hex');
  107. testBufs('c8a26161', 'hex');
  108. testBufs('61c8b462c8b563c8b6', 'hex');
  109. testBufs('616263', 4, 'hex');
  110. testBufs('616263', 5, 'hex');
  111. testBufs('616263', SIZE, 'hex');
  112. testBufs('c8a26161', 2, 'hex');
  113. testBufs('c8a26161', 8, 'hex');
  114. testBufs('61c8b462c8b563c8b6', 4, 'hex');
  115. testBufs('61c8b462c8b563c8b6', 12, 'hex');
  116. testBufs('616263', 4, -1, 'hex');
  117. testBufs('616263', 4, 1, 'hex');
  118. testBufs('616263', 5, 1, 'hex');
  119. testBufs('c8a26161', 2, -1, 'hex');
  120. testBufs('c8a26161', 8, 1, 'hex');
  121. testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
  122. testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
  123. testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
  124. // Make sure this operation doesn't go on forever
  125. buf1.fill('yKJh', 'hex');
  126. assert.throws(() => buf1.fill('\u0222', 'hex'));
  127. // BASE64
  128. testBufs('YWJj', 'ucs2');
  129. testBufs('yKJhYQ==', 'ucs2');
  130. testBufs('Yci0Ysi1Y8i2', 'ucs2');
  131. testBufs('YWJj', 4, 'ucs2');
  132. testBufs('YWJj', SIZE, 'ucs2');
  133. testBufs('yKJhYQ==', 2, 'ucs2');
  134. testBufs('yKJhYQ==', 8, 'ucs2');
  135. testBufs('Yci0Ysi1Y8i2', 4, 'ucs2');
  136. testBufs('Yci0Ysi1Y8i2', 12, 'ucs2');
  137. testBufs('YWJj', 4, -1, 'ucs2');
  138. testBufs('YWJj', 4, 1, 'ucs2');
  139. testBufs('YWJj', 5, 1, 'ucs2');
  140. testBufs('yKJhYQ==', 2, -1, 'ucs2');
  141. testBufs('yKJhYQ==', 8, 1, 'ucs2');
  142. testBufs('Yci0Ysi1Y8i2', 4, -1, 'ucs2');
  143. testBufs('Yci0Ysi1Y8i2', 4, 1, 'ucs2');
  144. testBufs('Yci0Ysi1Y8i2', 12, 1, 'ucs2');
  145. // Buffer
  146. function deepStrictEqualValues(buf, arr) {
  147. for (var [index, value] of buf.entries()) {
  148. assert.deepStrictEqual(value, arr[index]);
  149. }
  150. }
  151. var buf2Fill = Buffer.allocUnsafe(1).fill(2);
  152. deepStrictEqualValues(genBuffer(4, [buf2Fill]), [2, 2, 2, 2]);
  153. deepStrictEqualValues(genBuffer(4, [buf2Fill, 1]), [0, 2, 2, 2]);
  154. deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 3]), [0, 2, 2, 0]);
  155. deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 1]), [0, 0, 0, 0]);
  156. deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, -1]), [0, 0, 0, 0]);
  157. var hexBufFill = Buffer.allocUnsafe(2).fill(0).fill('0102', 'hex');
  158. deepStrictEqualValues(genBuffer(4, [hexBufFill]), [1, 2, 1, 2]);
  159. deepStrictEqualValues(genBuffer(4, [hexBufFill, 1]), [0, 1, 2, 1]);
  160. deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 3]), [0, 1, 2, 0]);
  161. deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 1]), [0, 0, 0, 0]);
  162. deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
  163. // Check exceptions
  164. assert.throws(() => buf1.fill(0, -1));
  165. assert.throws(() => buf1.fill(0, 0, buf1.length + 1));
  166. assert.throws(() => buf1.fill('', -1));
  167. assert.throws(() => buf1.fill('', 0, buf1.length + 1));
  168. assert.throws(() => buf1.fill('a', 0, buf1.length, 'node rocks!'));
  169. assert.throws(() => buf1.fill('a', 0, 0, NaN));
  170. assert.throws(() => buf1.fill('a', 0, 0, null));
  171. assert.throws(() => buf1.fill('a', 0, 0, 'foo'));
  172. function genBuffer(size, args) {
  173. var b = Buffer.allocUnsafe(size);
  174. return b.fill(0).fill.apply(b, args);
  175. }
  176. function bufReset() {
  177. buf1.fill(0);
  178. buf2.fill(0);
  179. }
  180. // This is mostly accurate. Except write() won't write partial bytes to the
  181. // string while fill() blindly copies bytes into memory. To account for that an
  182. // error will be thrown if not all the data can be written, and the SIZE has
  183. // been massaged to work with the input characters.
  184. function writeToFill(string, offset, end, encoding) {
  185. if (typeof offset === 'string') {
  186. encoding = offset;
  187. offset = 0;
  188. end = buf2.length;
  189. } else if (typeof end === 'string') {
  190. encoding = end;
  191. end = buf2.length;
  192. } else if (end === undefined) {
  193. end = buf2.length;
  194. }
  195. if (offset < 0 || end > buf2.length)
  196. throw new RangeError('Out of range index');
  197. if (end <= offset)
  198. return buf2;
  199. offset >>>= 0;
  200. end >>>= 0;
  201. assert(offset <= buf2.length);
  202. // Convert "end" to "length" (which write understands).
  203. var length = end - offset < 0 ? 0 : end - offset;
  204. var wasZero = false;
  205. do {
  206. var written = buf2.write(string, offset, length, encoding);
  207. offset += written;
  208. // Safety check in case write falls into infinite loop.
  209. if (written === 0) {
  210. if (wasZero)
  211. throw new Error('Could not write all data to Buffer');
  212. else
  213. wasZero = true;
  214. }
  215. } while (offset < buf2.length);
  216. // Correction for UCS2 operations.
  217. if (os.endianness() === 'BE' && encoding === 'ucs2') {
  218. for (var i = 0; i < buf2.length; i += 2) {
  219. var tmp = buf2[i];
  220. buf2[i] = buf2[i + 1];
  221. buf2[i + 1] = tmp;
  222. }
  223. }
  224. return buf2;
  225. }
  226. function testBufs(string, offset, length, encoding) {
  227. bufReset();
  228. buf1.fill.apply(buf1, arguments);
  229. // Swap bytes on BE archs for ucs2 encoding.
  230. assert.deepStrictEqual(buf1.fill.apply(buf1, arguments),
  231. writeToFill.apply(null, arguments));
  232. }