You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/test/session/utils/String_test.ts

122 lines
4.1 KiB
TypeScript

5 years ago
import chai from 'chai';
5 years ago
import ByteBuffer from 'bytebuffer';
5 years ago
5 years ago
// Can't import type as StringUtils.Encoding
import { Encoding } from '../../../session/utils/String';
import { StringUtils } from '../../../session/utils/';
5 years ago
// tslint:disable-next-line: no-require-imports no-var-requires
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const { expect } = chai;
describe('String Utils', () => {
5 years ago
5 years ago
describe('encode', () => {
5 years ago
it('can encode to base64', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'base64');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode to hex', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'hex');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('wont encode invalid hex', async () => {
const testString = 'ZZZZZZZZZZ';
const encoded = StringUtils.encode(testString, 'hex');
expect(encoded.byteLength).to.equal(0);
});
it('can encode to binary', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'binary');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode to utf8', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'binary');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode empty string', async () => {
const testString = '';
expect(testString).to.have.length(0);
const allEncodedings = ([
'base64',
'hex',
'binary',
'utf8',
] as Array<Encoding>).map(e => StringUtils.encode(testString, e));
allEncodedings.forEach(encoded => {
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.equal(0);
});
});
it('can encode huge string', async () => {
const testString = Array(Math.pow(2, 16)).fill('0').join('');
const allEncodedings = ([
'base64',
'hex',
'binary',
'utf8',
] as Array<Encoding>).map(e => StringUtils.encode(testString, e));
allEncodedings.forEach(encoded => {
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
});
it("won't encode illegal string length in hex", async () => {
const testString = 'A';
const encode = () => StringUtils.encode(testString, 'hex');
// Ensure string is odd length
expect(testString.length % 2).to.equal(1);
expect(encode).to.throw('Illegal str: Length not a multiple of 2');
});
it('can convert obscure string', async () => {
const testString = '↓←¶ᶑᵶ⅑⏕→⅓‎ᵹ⅙ᵰᶎ⅔⅗↔‌ᶈ⅞⸜ᶊᵴᶉ↉¥ᶖᶋᶃᶓ⏦ᵾᶂᶆ↕⸝ᶔᶐ⏔£⏙⅐⅒ᶌ⁁ᶘᶄᶒᶸ⅘‏⅚⅛ᶙᶇᶕᶀ↑ᵿ⏠ᶍᵯ⏖⏗⅜ᶚᶏ⁊‍ᶁᶗᵽᵼ⅝⏘⅖⅕⏡';
// Not valid hex format; try test the others
const encodings = ([
'base64',
'binary',
'utf8',
] as Array<Encoding>);
encodings.forEach(encoding => {
const encoded = StringUtils.encode(testString, encoding);
expect(encoded instanceof ArrayBuffer).to.equal(true, `a buffer was not returned using encoding: '${encoding}'`);
expect(encoded.byteLength).to.be.greaterThan(0);
});
5 years ago
});
});
describe('decode', () => {
it('', async () => {
//
});
});
});