test: added unit tests for pubkeys constructor checks

pull/2873/head
Audric Ackermann 2 years ago
parent e220aeea91
commit 1dc5224c26

@ -46,14 +46,10 @@ export class PubKey {
/**
* If you want to update this regex. Be sure that those are matches ;
* __textsecure_group__!05010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!05010203040506070809a0b0c0d0e0f0ff
* __textsecure_group__!010203040506070809a0b0c0d0e0f0ff
* 05010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* 03010203040506070809a0b0c0d0e0f0ff010203040506070809a0b0c0d0e0f0ff
* 010203040506070809a0b0c0d0e0f0ff010203040506070809a0B0c0d0e0f0FF
* 05010203040506070809a0b0c0d0e0f0ff
* 010203040506070809a0b0c0d0e0f0ff
* 030203040506070809a0b0c0d0e0f0ff
*/
@ -67,7 +63,7 @@ export class PubKey {
*/
constructor(pubkeyString: string) {
if (!PubKey.validate(pubkeyString)) {
throw new Error(`Invalid pubkey string passed: ${pubkeyString}`);
throw new Error('Invalid pubkey string passed');
}
this.key = pubkeyString.toLowerCase();
}

@ -0,0 +1,73 @@
import { expect } from 'chai';
import { PubKey } from '../../../../session/types';
const defaultErr = 'Invalid pubkey string passed';
describe('PubKey constructor', () => {
it('does not throw with 05 prefix, right length and only hex chars', () => {
expect(() => new PubKey(`05${'0'.repeat(64)}`)).to.not.throw();
});
it('does not throw with 15 prefix, right length and only hex chars', () => {
expect(() => new PubKey(`15${'0'.repeat(64)}`)).to.not.throw();
});
it('does not throw with 03 prefix, right length and only hex chars', () => {
expect(() => new PubKey(`03${'0'.repeat(64)}`)).to.not.throw();
});
it('does not throw with 25 prefix, right length and only hex chars', () => {
expect(() => new PubKey(`25${'0'.repeat(64)}`)).to.not.throw();
});
it('does not throw with 05 and textsecure prefix, right length and only hex chars', () => {
expect(() => new PubKey(`__textsecure_group__!05${'0'.repeat(64)}`)).to.not.throw();
});
it('throws with null', () => {
expect(() => new PubKey(null as any)).to.throw(defaultErr);
});
it('throws with undefined', () => {
expect(() => new PubKey(undefined as any)).to.throw(defaultErr);
});
it('throws with empty string', () => {
expect(() => new PubKey('')).to.throw(defaultErr);
});
it('throws with incorrect prefix', () => {
expect(() => new PubKey(`95${'0'.repeat(64)}`)).to.throw(defaultErr);
});
describe('05 prefix', () => {
it('throws with non-hex chars', () => {
expect(() => new PubKey(`05${'0'.repeat(63)}(`)).to.throw(defaultErr);
});
it('throws with incorrect length', () => {
expect(() => new PubKey(`05${'0'.repeat(63)}`)).to.throw(defaultErr);
});
// Currently we allow pubkeys of length 52 if they have a length of
// it('throws with incorrect length -2', () => {
// expect(() => new PubKey(`05${'0'.repeat(62)}`)).to.throw(defaultErr);
// });
});
describe('25 prefix', () => {
it('throws with non-hex chars', () => {
expect(() => new PubKey(`25${'0'.repeat(63)}(`)).to.throw(defaultErr);
});
it('throws with incorrect length -1', () => {
expect(() => new PubKey(`25${'0'.repeat(63)}`)).to.throw(defaultErr);
});
});
describe('03 prefix', () => {
it('throws with non-hex chars', () => {
expect(() => new PubKey(`03${'0'.repeat(63)}(`)).to.throw(defaultErr);
});
it('throws with incorrect length -1', () => {
expect(() => new PubKey(`03${'0'.repeat(63)}`)).to.throw(defaultErr);
});
});
});
Loading…
Cancel
Save