base64_spec.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local exec_lua = n.exec_lua
  5. local eq = t.eq
  6. local pcall_err = t.pcall_err
  7. local matches = t.matches
  8. describe('vim.base64', function()
  9. before_each(clear)
  10. local function encode(s)
  11. return exec_lua([[return vim.base64.encode(...)]], s)
  12. end
  13. local function decode(s)
  14. return exec_lua([[return vim.base64.decode(...)]], s)
  15. end
  16. it('works', function()
  17. local values = {
  18. '',
  19. 'Many hands make light work.',
  20. [[
  21. Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in
  22. my purse, and nothing particular to interest me on shore, I thought I would sail about a
  23. little and see the watery part of the world.
  24. ]],
  25. [[
  26. It is a truth universally acknowledged, that a single man in possession of a good fortune,
  27. must be in want of a wife.
  28. ]],
  29. 'Happy families are all alike; every unhappy family is unhappy in its own way.',
  30. 'ЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя',
  31. 'ÅÍÎÏ˝ÓÔÒÚÆ☃',
  32. '𐐜 𐐔𐐇𐐝𐐀𐐡𐐇𐐓 𐐙𐐊𐐡𐐝𐐓/𐐝𐐇𐐗𐐊𐐤𐐔 𐐒𐐋𐐗 𐐒𐐌 𐐜 𐐡𐐀𐐖𐐇𐐤𐐓𐐝 𐐱𐑂 𐑄 𐐔𐐇𐐝𐐀𐐡𐐇𐐓 𐐏𐐆𐐅𐐤𐐆𐐚𐐊𐐡𐐝𐐆𐐓𐐆',
  33. '👨‍👩‍👦 👨‍👩‍👧‍👦 👨‍👨‍👦 👩‍👩‍👧 👨‍👦 👨‍👧‍👦 👩‍👦 👩‍👧‍👦',
  34. 'مُنَاقَشَةُ سُبُلِ اِسْتِخْدَامِ اللُّغَةِ فِي النُّظُمِ الْقَائِمَةِ وَفِيم يَخُصَّ التَّطْبِيقَاتُ الْحاسُوبِيَّةُ،',
  35. [[
  36. Ṱ̺̺̕o͞ ̷i̲̬͇̪͙n̝̗͕v̟̜̘̦͟o̶̙̰̠kè͚̮̺̪̹̱̤ ̖t̝͕̳̣̻̪͞h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳ ̞̥̱̳̭r̛̗̘e͙p͠r̼̞̻̭̗e̺̠̣͟s̘͇̳͍̝͉e͉̥̯̞̲͚̬͜ǹ̬͎͎̟̖͇̤t͍̬̤͓̼̭͘ͅi̪̱n͠g̴͉ ͏͉ͅc̬̟h͡a̫̻̯͘o̫̟̖͍̙̝͉s̗̦̲.̨̹͈̣
  37. ̡͓̞ͅI̗̘̦͝n͇͇͙v̮̫ok̲̫̙͈i̖͙̭̹̠̞n̡̻̮̣̺g̲͈͙̭͙̬͎ ̰t͔̦h̞̲e̢̤ ͍̬̲͖f̴̘͕̣è͖ẹ̥̩l͖͔͚i͓͚̦͠n͖͍̗͓̳̮g͍ ̨o͚̪͡f̘̣̬ ̖̘͖̟͙̮c҉͔̫͖͓͇͖ͅh̵̤̣͚͔á̗̼͕ͅo̼̣̥s̱͈̺̖̦̻͢.̛̖̞̠̫̰
  38. ̗̺͖̹̯͓Ṯ̤͍̥͇͈h̲́e͏͓̼̗̙̼̣͔ ͇̜̱̠͓͍ͅN͕͠e̗̱z̘̝̜̺͙p̤̺̹͍̯͚e̠̻̠͜r̨̤͍̺̖͔̖̖d̠̟̭̬̝͟i̦͖̩͓͔̤a̠̗̬͉̙n͚͜ ̻̞̰͚ͅh̵͉i̳̞v̢͇ḙ͎͟-҉̭̩̼͔m̤̭̫i͕͇̝̦n̗͙ḍ̟ ̯̲͕͞ǫ̟̯̰̲͙̻̝f ̪̰̰̗̖̭̘͘c̦͍̲̞͍̩̙ḥ͚a̮͎̟̙͜ơ̩̹͎s̤.̝̝ ҉Z̡̖̜͖̰̣͉̜a͖̰͙̬͡l̲̫̳͍̩g̡̟̼̱͚̞̬ͅo̗͜.̟
  39. ̦H̬̤̗̤͝e͜ ̜̥̝̻͍̟́w̕h̖̯͓o̝͙̖͎̱̮ ҉̺̙̞̟͈W̷̼̭a̺̪͍į͈͕̭͙̯̜t̶̼̮s̘͙͖̕ ̠̫̠B̻͍͙͉̳ͅe̵h̵̬͇̫͙i̹͓̳̳̮͎̫̕n͟d̴̪̜̖ ̰͉̩͇͙̲͞ͅT͖̼͓̪͢h͏͓̮̻e̬̝̟ͅ ̤̹̝W͙̞̝͔͇͝ͅa͏͓͔̹̼̣l̴͔̰̤̟͔ḽ̫.͕
  40. Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮
  41. ]],
  42. 'Hello\0world',
  43. }
  44. for _, v in ipairs(values) do
  45. eq(v, decode(encode(v)))
  46. end
  47. -- Explicitly check encoded output
  48. eq(
  49. 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZwo=',
  50. encode('The quick brown fox jumps over the lazy dog\n')
  51. )
  52. -- Test vectors from rfc4648
  53. local rfc4648 = {
  54. { '', '' },
  55. { 'f', 'Zg==' },
  56. { 'fo', 'Zm8=' },
  57. { 'foo', 'Zm9v' },
  58. { 'foob', 'Zm9vYg==' },
  59. { 'fooba', 'Zm9vYmE=' },
  60. { 'foobar', 'Zm9vYmFy' },
  61. }
  62. for _, v in ipairs(rfc4648) do
  63. local input = v[1]
  64. local output = v[2]
  65. eq(output, encode(input))
  66. eq(input, decode(output))
  67. end
  68. end)
  69. it('detects invalid input', function()
  70. local invalid = {
  71. 'A',
  72. 'AA',
  73. 'AAA',
  74. 'A..A',
  75. 'AA=A',
  76. 'AA/=',
  77. 'A/==',
  78. 'A===',
  79. '====',
  80. 'Zm9vYmFyZm9vYmFyA..A',
  81. 'Zm9vYmFyZm9vYmFyAA=A',
  82. 'Zm9vYmFyZm9vYmFyAA/=',
  83. 'Zm9vYmFyZm9vYmFyA/==',
  84. 'Zm9vYmFyZm9vYmFyA===',
  85. 'A..AZm9vYmFyZm9vYmFy',
  86. 'Zm9vYmFyZm9vAA=A',
  87. 'Zm9vYmFyZm9vAA/=',
  88. 'Zm9vYmFyZm9vA/==',
  89. 'Zm9vYmFyZm9vA===',
  90. }
  91. for _, v in ipairs(invalid) do
  92. eq('Invalid input', pcall_err(decode, v))
  93. end
  94. eq('Expected 1 argument', pcall_err(encode))
  95. eq('Expected 1 argument', pcall_err(decode))
  96. matches('expected string', pcall_err(encode, 42))
  97. matches('expected string', pcall_err(decode, 42))
  98. end)
  99. end)