Base64.test.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const { expect } = require('chai');
  2. const Base64 = artifacts.require('$Base64');
  3. const Base64Dirty = artifacts.require('$Base64Dirty');
  4. contract('Base64', function () {
  5. beforeEach(async function () {
  6. this.base64 = await Base64.new();
  7. });
  8. describe('from bytes - base64', function () {
  9. it('converts to base64 encoded string with double padding', async function () {
  10. const TEST_MESSAGE = 'test';
  11. const input = web3.utils.asciiToHex(TEST_MESSAGE);
  12. expect(await this.base64.$encode(input)).to.equal('dGVzdA==');
  13. });
  14. it('converts to base64 encoded string with single padding', async function () {
  15. const TEST_MESSAGE = 'test1';
  16. const input = web3.utils.asciiToHex(TEST_MESSAGE);
  17. expect(await this.base64.$encode(input)).to.equal('dGVzdDE=');
  18. });
  19. it('converts to base64 encoded string without padding', async function () {
  20. const TEST_MESSAGE = 'test12';
  21. const input = web3.utils.asciiToHex(TEST_MESSAGE);
  22. expect(await this.base64.$encode(input)).to.equal('dGVzdDEy');
  23. });
  24. it('empty bytes', async function () {
  25. expect(await this.base64.$encode([])).to.equal('');
  26. });
  27. });
  28. it('Encode reads beyond the input buffer into dirty memory', async function () {
  29. const mock = await Base64Dirty.new();
  30. const buffer32 = Buffer.from(web3.utils.soliditySha3('example').replace(/0x/, ''), 'hex');
  31. const buffer31 = buffer32.slice(0, -2);
  32. expect(await mock.encode(buffer31)).to.equal(buffer31.toString('base64'));
  33. expect(await mock.encode(buffer32)).to.equal(buffer32.toString('base64'));
  34. });
  35. });