You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false
  2. var B = require('../').Buffer
  3. var test = require('tape')
  4. test('utf8 buffer to base64', function (t) {
  5. t.equal(
  6. new B('Ձאab', 'utf8').toString('base64'),
  7. '1YHXkGFi'
  8. )
  9. t.end()
  10. })
  11. test('utf8 buffer to hex', function (t) {
  12. t.equal(
  13. new B('Ձאab', 'utf8').toString('hex'),
  14. 'd581d7906162'
  15. )
  16. t.end()
  17. })
  18. test('utf8 to utf8', function (t) {
  19. t.equal(
  20. new B('öäüõÖÄÜÕ', 'utf8').toString('utf8'),
  21. 'öäüõÖÄÜÕ'
  22. )
  23. t.end()
  24. })
  25. test('utf16le to utf16', function (t) {
  26. t.equal(
  27. new B(new B('abcd', 'utf8').toString('utf16le'), 'utf16le').toString('utf8'),
  28. 'abcd'
  29. )
  30. t.end()
  31. })
  32. test('utf16le to hex', function (t) {
  33. t.equal(
  34. new B('abcd', 'utf16le').toString('hex'),
  35. '6100620063006400'
  36. )
  37. t.end()
  38. })
  39. test('ascii buffer to base64', function (t) {
  40. t.equal(
  41. new B('123456!@#$%^', 'ascii').toString('base64'),
  42. 'MTIzNDU2IUAjJCVe'
  43. )
  44. t.end()
  45. })
  46. test('ascii buffer to hex', function (t) {
  47. t.equal(
  48. new B('123456!@#$%^', 'ascii').toString('hex'),
  49. '31323334353621402324255e'
  50. )
  51. t.end()
  52. })
  53. test('base64 buffer to utf8', function (t) {
  54. t.equal(
  55. new B('1YHXkGFi', 'base64').toString('utf8'),
  56. 'Ձאab'
  57. )
  58. t.end()
  59. })
  60. test('hex buffer to utf8', function (t) {
  61. t.equal(
  62. new B('d581d7906162', 'hex').toString('utf8'),
  63. 'Ձאab'
  64. )
  65. t.end()
  66. })
  67. test('base64 buffer to ascii', function (t) {
  68. t.equal(
  69. new B('MTIzNDU2IUAjJCVe', 'base64').toString('ascii'),
  70. '123456!@#$%^'
  71. )
  72. t.end()
  73. })
  74. test('hex buffer to ascii', function (t) {
  75. t.equal(
  76. new B('31323334353621402324255e', 'hex').toString('ascii'),
  77. '123456!@#$%^'
  78. )
  79. t.end()
  80. })
  81. test('base64 buffer to binary', function (t) {
  82. t.equal(
  83. new B('MTIzNDU2IUAjJCVe', 'base64').toString('binary'),
  84. '123456!@#$%^'
  85. )
  86. t.end()
  87. })
  88. test('hex buffer to binary', function (t) {
  89. t.equal(
  90. new B('31323334353621402324255e', 'hex').toString('binary'),
  91. '123456!@#$%^'
  92. )
  93. t.end()
  94. })
  95. test('utf8 to binary', function (t) {
  96. /* jshint -W100 */
  97. t.equal(
  98. new B('öäüõÖÄÜÕ', 'utf8').toString('binary'),
  99. 'öäüõÖÄÜÕ'
  100. )
  101. /* jshint +W100 */
  102. t.end()
  103. })
  104. test('utf8 replacement chars (1 byte sequence)', function (t) {
  105. t.equal(
  106. new B([ 0x80 ]).toString(),
  107. '\uFFFD'
  108. )
  109. t.equal(
  110. new B([ 0x7F ]).toString(),
  111. '\u007F'
  112. )
  113. t.end()
  114. })
  115. test('utf8 replacement chars (2 byte sequences)', function (t) {
  116. t.equal(
  117. new B([ 0xC7 ]).toString(),
  118. '\uFFFD'
  119. )
  120. t.equal(
  121. new B([ 0xC7, 0xB1 ]).toString(),
  122. '\u01F1'
  123. )
  124. t.equal(
  125. new B([ 0xC0, 0xB1 ]).toString(),
  126. '\uFFFD\uFFFD'
  127. )
  128. t.equal(
  129. new B([ 0xC1, 0xB1 ]).toString(),
  130. '\uFFFD\uFFFD'
  131. )
  132. t.end()
  133. })
  134. test('utf8 replacement chars (3 byte sequences)', function (t) {
  135. t.equal(
  136. new B([ 0xE0 ]).toString(),
  137. '\uFFFD'
  138. )
  139. t.equal(
  140. new B([ 0xE0, 0xAC ]).toString(),
  141. '\uFFFD\uFFFD'
  142. )
  143. t.equal(
  144. new B([ 0xE0, 0xAC, 0xB9 ]).toString(),
  145. '\u0B39'
  146. )
  147. t.end()
  148. })
  149. test('utf8 replacement chars (4 byte sequences)', function (t) {
  150. t.equal(
  151. new B([ 0xF4 ]).toString(),
  152. '\uFFFD'
  153. )
  154. t.equal(
  155. new B([ 0xF4, 0x8F ]).toString(),
  156. '\uFFFD\uFFFD'
  157. )
  158. t.equal(
  159. new B([ 0xF4, 0x8F, 0x80 ]).toString(),
  160. '\uFFFD\uFFFD\uFFFD'
  161. )
  162. t.equal(
  163. new B([ 0xF4, 0x8F, 0x80, 0x84 ]).toString(),
  164. '\uDBFC\uDC04'
  165. )
  166. t.equal(
  167. new B([ 0xFF ]).toString(),
  168. '\uFFFD'
  169. )
  170. t.equal(
  171. new B([ 0xFF, 0x8F, 0x80, 0x84 ]).toString(),
  172. '\uFFFD\uFFFD\uFFFD\uFFFD'
  173. )
  174. t.end()
  175. })
  176. test('utf8 replacement chars on 256 random bytes', function (t) {
  177. t.equal(
  178. new B([ 152, 130, 206, 23, 243, 238, 197, 44, 27, 86, 208, 36, 163, 184, 164, 21, 94, 242, 178, 46, 25, 26, 253, 178, 72, 147, 207, 112, 236, 68, 179, 190, 29, 83, 239, 147, 125, 55, 143, 19, 157, 68, 157, 58, 212, 224, 150, 39, 128, 24, 94, 225, 120, 121, 75, 192, 112, 19, 184, 142, 203, 36, 43, 85, 26, 147, 227, 139, 242, 186, 57, 78, 11, 102, 136, 117, 180, 210, 241, 92, 3, 215, 54, 167, 249, 1, 44, 225, 146, 86, 2, 42, 68, 21, 47, 238, 204, 153, 216, 252, 183, 66, 222, 255, 15, 202, 16, 51, 134, 1, 17, 19, 209, 76, 238, 38, 76, 19, 7, 103, 249, 5, 107, 137, 64, 62, 170, 57, 16, 85, 179, 193, 97, 86, 166, 196, 36, 148, 138, 193, 210, 69, 187, 38, 242, 97, 195, 219, 252, 244, 38, 1, 197, 18, 31, 246, 53, 47, 134, 52, 105, 72, 43, 239, 128, 203, 73, 93, 199, 75, 222, 220, 166, 34, 63, 236, 11, 212, 76, 243, 171, 110, 78, 39, 205, 204, 6, 177, 233, 212, 243, 0, 33, 41, 122, 118, 92, 252, 0, 157, 108, 120, 70, 137, 100, 223, 243, 171, 232, 66, 126, 111, 142, 33, 3, 39, 117, 27, 107, 54, 1, 217, 227, 132, 13, 166, 3, 73, 53, 127, 225, 236, 134, 219, 98, 214, 125, 148, 24, 64, 142, 111, 231, 194, 42, 150, 185, 10, 182, 163, 244, 19, 4, 59, 135, 16 ]).toString(),
  179. '\uFFFD\uFFFD\uFFFD\u0017\uFFFD\uFFFD\uFFFD\u002C\u001B\u0056\uFFFD\u0024\uFFFD\uFFFD\uFFFD\u0015\u005E\uFFFD\uFFFD\u002E\u0019\u001A\uFFFD\uFFFD\u0048\uFFFD\uFFFD\u0070\uFFFD\u0044\uFFFD\uFFFD\u001D\u0053\uFFFD\uFFFD\u007D\u0037\uFFFD\u0013\uFFFD\u0044\uFFFD\u003A\uFFFD\uFFFD\uFFFD\u0027\uFFFD\u0018\u005E\uFFFD\u0078\u0079\u004B\uFFFD\u0070\u0013\uFFFD\uFFFD\uFFFD\u0024\u002B\u0055\u001A\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\u0039\u004E\u000B\u0066\uFFFD\u0075\uFFFD\uFFFD\uFFFD\u005C\u0003\uFFFD\u0036\uFFFD\uFFFD\u0001\u002C\uFFFD\uFFFD\u0056\u0002\u002A\u0044\u0015\u002F\uFFFD\u0319\uFFFD\uFFFD\uFFFD\u0042\uFFFD\uFFFD\u000F\uFFFD\u0010\u0033\uFFFD\u0001\u0011\u0013\uFFFD\u004C\uFFFD\u0026\u004C\u0013\u0007\u0067\uFFFD\u0005\u006B\uFFFD\u0040\u003E\uFFFD\u0039\u0010\u0055\uFFFD\uFFFD\u0061\u0056\uFFFD\uFFFD\u0024\uFFFD\uFFFD\uFFFD\uFFFD\u0045\uFFFD\u0026\uFFFD\u0061\uFFFD\uFFFD\uFFFD\uFFFD\u0026\u0001\uFFFD\u0012\u001F\uFFFD\u0035\u002F\uFFFD\u0034\u0069\u0048\u002B\uFFFD\uFFFD\uFFFD\u0049\u005D\uFFFD\u004B\uFFFD\u0726\u0022\u003F\uFFFD\u000B\uFFFD\u004C\uFFFD\uFFFD\u006E\u004E\u0027\uFFFD\uFFFD\u0006\uFFFD\uFFFD\uFFFD\uFFFD\u0000\u0021\u0029\u007A\u0076\u005C\uFFFD\u0000\uFFFD\u006C\u0078\u0046\uFFFD\u0064\uFFFD\uFFFD\uFFFD\uFFFD\u0042\u007E\u006F\uFFFD\u0021\u0003\u0027\u0075\u001B\u006B\u0036\u0001\uFFFD\uFFFD\uFFFD\u000D\uFFFD\u0003\u0049\u0035\u007F\uFFFD\uFFFD\uFFFD\uFFFD\u0062\uFFFD\u007D\uFFFD\u0018\u0040\uFFFD\u006F\uFFFD\uFFFD\u002A\uFFFD\uFFFD\u000A\uFFFD\uFFFD\uFFFD\u0013\u0004\u003B\uFFFD\u0010'
  180. )
  181. t.end()
  182. })
  183. test('utf8 replacement chars for anything in the surrogate pair range', function (t) {
  184. t.equal(
  185. new B([ 0xED, 0x9F, 0xBF ]).toString(),
  186. '\uD7FF'
  187. )
  188. t.equal(
  189. new B([ 0xED, 0xA0, 0x80 ]).toString(),
  190. '\uFFFD\uFFFD\uFFFD'
  191. )
  192. t.equal(
  193. new B([ 0xED, 0xBE, 0x8B ]).toString(),
  194. '\uFFFD\uFFFD\uFFFD'
  195. )
  196. t.equal(
  197. new B([ 0xED, 0xBF, 0xBF ]).toString(),
  198. '\uFFFD\uFFFD\uFFFD'
  199. )
  200. t.equal(
  201. new B([ 0xEE, 0x80, 0x80 ]).toString(),
  202. '\uE000'
  203. )
  204. t.end()
  205. })
  206. test('utf8 don\'t replace the replacement char', function (t) {
  207. t.equal(
  208. new B('\uFFFD').toString(),
  209. '\uFFFD'
  210. )
  211. t.end()
  212. })