buffer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. var B = require('../').Buffer
  2. var test = require('tape')
  3. test('utf8 buffer to base64', function (t) {
  4. t.equal(
  5. new B('Ձאab', 'utf8').toString('base64'),
  6. '1YHXkGFi'
  7. )
  8. t.end()
  9. })
  10. test('utf8 buffer to hex', function (t) {
  11. t.equal(
  12. new B('Ձאab', 'utf8').toString('hex'),
  13. 'd581d7906162'
  14. )
  15. t.end()
  16. })
  17. test('utf8 to utf8', function (t) {
  18. t.equal(
  19. new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'),
  20. 'öäüõÖÄÜÕ'
  21. )
  22. t.end()
  23. })
  24. test('utf16le to utf16', function (t) {
  25. t.equal(
  26. new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
  27. 'abcd'
  28. )
  29. t.end()
  30. })
  31. test('utf16le to hex', function (t) {
  32. t.equal(
  33. new B('abcd', 'utf16le').toString('hex'),
  34. '6100620063006400'
  35. )
  36. t.end()
  37. })
  38. test('ascii buffer to base64', function (t) {
  39. t.equal(
  40. new B('123456!@#$%^', 'ascii').toString('base64'),
  41. 'MTIzNDU2IUAjJCVe'
  42. )
  43. t.end()
  44. })
  45. test('ascii buffer to hex', function (t) {
  46. t.equal(
  47. new B('123456!@#$%^', 'ascii').toString('hex'),
  48. '31323334353621402324255e'
  49. )
  50. t.end()
  51. })
  52. test('base64 buffer to utf8', function (t) {
  53. t.equal(
  54. new B('1YHXkGFi', 'base64').toString('utf8'),
  55. 'Ձאab'
  56. )
  57. t.end()
  58. })
  59. test('hex buffer to utf8', function (t) {
  60. t.equal(
  61. new B('d581d7906162', 'hex').toString('utf8'),
  62. 'Ձאab'
  63. )
  64. t.end()
  65. })
  66. test('base64 buffer to ascii', function (t) {
  67. t.equal(
  68. new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'),
  69. '123456!@#$%^'
  70. )
  71. t.end()
  72. })
  73. test('hex buffer to ascii', function (t) {
  74. t.equal(
  75. new B('31323334353621402324255e', 'hex').toString('ascii'),
  76. '123456!@#$%^'
  77. )
  78. t.end()
  79. })
  80. test('base64 buffer to binary', function (t) {
  81. t.equal(
  82. new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'),
  83. '123456!@#$%^'
  84. )
  85. t.end()
  86. })
  87. test('hex buffer to binary', function (t) {
  88. t.equal(
  89. new B('31323334353621402324255e', 'hex').toString('binary'),
  90. '123456!@#$%^'
  91. )
  92. t.end()
  93. })
  94. test('utf8 to binary', function (t) {
  95. t.equal(
  96. new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
  97. 'öäüõÖÄÜÕ'
  98. )
  99. t.end()
  100. })
  101. test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {
  102. t.plan(2*(2*2*2+2))
  103. var hex = [
  104. '03', '0300', '0003', '03000000', '00000003',
  105. 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'
  106. ]
  107. var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]
  108. var xs = ['UInt','Int']
  109. var ys = [8,16,32]
  110. for (var i = 0; i < xs.length; i++) {
  111. var x = xs[i]
  112. for (var j = 0; j < ys.length; j++) {
  113. var y = ys[j]
  114. var endianesses = (y === 8) ? [''] : ['LE','BE']
  115. for (var k = 0; k < endianesses.length; k++) {
  116. var z = endianesses[k]
  117. var v1 = new B(y / 8)
  118. var writefn = 'write' + x + y + z
  119. var val = (x === 'Int') ? -3 : 3
  120. v1[writefn](val, 0)
  121. t.equal(
  122. v1.toString('hex'),
  123. hex.shift()
  124. )
  125. var readfn = 'read' + x + y + z
  126. t.equal(
  127. v1[readfn](0),
  128. reads.shift()
  129. )
  130. }
  131. }
  132. }
  133. t.end()
  134. })
  135. test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {
  136. t.plan(3*(2*2*2+2))
  137. var hex = [
  138. '', '03', '00', '030000', '000000',
  139. '', 'fd', 'ff', 'fdffff', 'ffffff'
  140. ]
  141. var reads = [
  142. undefined, 3, 0, 3, 0,
  143. undefined, 253, -256, 16777213, -256
  144. ]
  145. var xs = ['UInt','Int']
  146. var ys = [8,16,32]
  147. for (var i = 0; i < xs.length; i++) {
  148. var x = xs[i]
  149. for (var j = 0; j < ys.length; j++) {
  150. var y = ys[j]
  151. var endianesses = (y === 8) ? [''] : ['LE','BE']
  152. for (var k = 0; k < endianesses.length; k++) {
  153. var z = endianesses[k]
  154. var v1 = new B(y / 8 - 1)
  155. var next = new B(4)
  156. next.writeUInt32BE(0, 0)
  157. var writefn = 'write' + x + y + z
  158. var val = (x === 'Int') ? -3 : 3
  159. v1[writefn](val, 0, true)
  160. t.equal(
  161. v1.toString('hex'),
  162. hex.shift()
  163. )
  164. // check that nothing leaked to next buffer.
  165. t.equal(next.readUInt32BE(0), 0)
  166. // check that no bytes are read from next buffer.
  167. next.writeInt32BE(~0, 0)
  168. var readfn = 'read' + x + y + z
  169. t.equal(
  170. v1[readfn](0, true),
  171. reads.shift()
  172. )
  173. }
  174. }
  175. }
  176. t.end()
  177. })
  178. test('concat() a varying number of buffers', function (t) {
  179. var zero = []
  180. var one = [ new B('asdf') ]
  181. var long = []
  182. for (var i = 0; i < 10; i++) long.push(new B('asdf'))
  183. var flatZero = B.concat(zero)
  184. var flatOne = B.concat(one)
  185. var flatLong = B.concat(long)
  186. var flatLongLen = B.concat(long, 40)
  187. t.equal(flatZero.length, 0)
  188. t.equal(flatOne.toString(), 'asdf')
  189. t.equal(flatOne, one[0])
  190. t.equal(flatLong.toString(), (new Array(10+1).join('asdf')))
  191. t.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')))
  192. t.end()
  193. })
  194. test('fill', function(t) {
  195. var b = new B(10)
  196. b.fill(2)
  197. t.equal(b.toString('hex'), '02020202020202020202')
  198. t.end()
  199. })
  200. test('copy() empty buffer with sourceEnd=0', function (t) {
  201. var source = new B([42])
  202. var destination = new B([43])
  203. source.copy(destination, 0, 0, 0)
  204. t.equal(destination.readUInt8(0), 43)
  205. t.end()
  206. })
  207. test('copy() after slice()', function(t) {
  208. var source = new B(200)
  209. var dest = new B(200)
  210. var expected = new B(200)
  211. for (var i = 0; i < 200; i++) {
  212. source[i] = i
  213. dest[i] = 0
  214. }
  215. source.slice(2).copy(dest)
  216. source.copy(expected, 0, 2)
  217. t.deepEqual(dest, expected)
  218. t.end()
  219. })
  220. test('base64 ignore whitespace', function(t) {
  221. var text = '\n YW9ldQ== '
  222. var buf = new B(text, 'base64')
  223. t.equal(buf.toString(), 'aoeu')
  224. t.end()
  225. })
  226. test('buffer.slice sets indexes', function (t) {
  227. t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')
  228. t.end()
  229. })
  230. test('buffer.slice out of range', function (t) {
  231. t.plan(2)
  232. t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo')
  233. t.equal((new B('hallo')).slice(10, 2).toString(), '')
  234. t.end()
  235. })
  236. test('base64 strings without padding', function (t) {
  237. t.equal((new B('YW9ldQ', 'base64').toString()), 'aoeu')
  238. t.end()
  239. })