From f4fdd42ef3940b54a7513782fc914b805c895d82 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 26 Jun 2020 12:48:06 +1000 Subject: [PATCH] string-tests --- ts/session/utils/String.ts | 4 +- ts/test/session/utils/Promise_test.ts | 4 +- ts/test/session/utils/String_test.ts | 109 ++++++++++++++++++++++++-- ts/test/test-utils/testUtils.ts | 4 +- 4 files changed, 106 insertions(+), 15 deletions(-) diff --git a/ts/session/utils/String.ts b/ts/session/utils/String.ts index 0c2b16c37..820021757 100644 --- a/ts/session/utils/String.ts +++ b/ts/session/utils/String.ts @@ -1,7 +1,7 @@ import ByteBuffer from 'bytebuffer'; -type Encoding = 'base64' | 'hex' | 'binary' | 'utf8'; -type BufferType = ByteBuffer | Buffer | ArrayBuffer | Uint8Array; +export type Encoding = 'base64' | 'hex' | 'binary' | 'utf8'; +export type BufferType = ByteBuffer | Buffer | ArrayBuffer | Uint8Array; /** * Take a string value with the given encoding and converts it to an `ArrayBuffer`. diff --git a/ts/test/session/utils/Promise_test.ts b/ts/test/session/utils/Promise_test.ts index 2ea801a80..1aee7091c 100644 --- a/ts/test/session/utils/Promise_test.ts +++ b/ts/test/session/utils/Promise_test.ts @@ -48,7 +48,7 @@ describe('Promise Utils', () => { const completionSpy = sandbox.spy(); const task = (_done: any) => undefined; - const promise = PromiseUtils.poll(task, {timeout: 1}); + const promise = PromiseUtils.poll(task, { timeout: 1 }); await expect(promise).to.be.rejectedWith('Periodic check timeout'); expect(pollSpy.callCount).to.equal(1); @@ -70,7 +70,7 @@ describe('Promise Utils', () => { } }; - const promise = PromiseUtils.poll(task, {timeout, interval}); + const promise = PromiseUtils.poll(task, { timeout, interval }); await expect(promise).to.be.fulfilled; expect(pollSpy.callCount).to.equal(1); diff --git a/ts/test/session/utils/String_test.ts b/ts/test/session/utils/String_test.ts index d06987dce..2b60efa39 100644 --- a/ts/test/session/utils/String_test.ts +++ b/ts/test/session/utils/String_test.ts @@ -1,11 +1,9 @@ import chai from 'chai'; -import * as sinon from 'sinon'; +import ByteBuffer from 'bytebuffer'; -import { PubKey } from '../../../session/types/'; -import { SyncMessageUtils } from '../../../session/utils/'; -import { SyncMessage } from '../../../session/messages/outgoing'; -import { TestUtils } from '../../test-utils'; -import { UserUtil } from '../../../util'; +// Can't import type as StringUtils.Encoding +import { Encoding } from '../../../session/utils/String'; +import { StringUtils } from '../../../session/utils/'; // tslint:disable-next-line: no-require-imports no-var-requires const chaiAsPromised = require('chai-as-promised'); @@ -14,9 +12,104 @@ chai.use(chaiAsPromised); const { expect } = chai; describe('String Utils', () => { + describe('encode', () => { - it('', async () => { - // + 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).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).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); + + 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); + }); }); }); diff --git a/ts/test/test-utils/testUtils.ts b/ts/test/test-utils/testUtils.ts index 457fa5ca6..58eec6fdc 100644 --- a/ts/test/test-utils/testUtils.ts +++ b/ts/test/test-utils/testUtils.ts @@ -10,9 +10,7 @@ import { ClosedGroupChatMessage, OpenGroupMessage, } from '../../session/messages/outgoing'; -import { - ConversationAttributes, -} from '../../../js/models/conversation'; +import { ConversationAttributes } from '../../../js/models/conversation'; import { TestUtils } from '.'; const globalAny: any = global;