From 13db2d666f141419b281d3be349c41492e5396d6 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Tue, 4 Dec 2018 17:53:00 +1100 Subject: [PATCH] Fixed test again after rebase, reworked some logic in the conversation validateNumber function to correctly reflect what libsignal thinks is valid --- js/models/conversations.js | 25 +++++++++---------------- js/signal_protocol_store.js | 7 ------- test/index.html | 11 +++++++++++ test/models/conversations_test.js | 29 +++++++++++++++-------------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 6fe724ead..636ed3b2f 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -857,25 +857,18 @@ validateNumber() { if (!this.id) return 'Invalid ID'; + if (!this.isPrivate()) return null; - if (this.isPrivate()) { - // Check if it's hex - const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); - if (!isHex) return 'Invalid Hex ID'; - - // Check if it has a valid length - if (this.id.length !== 33 * 2) { - // 33 bytes in hex - this.set({ id: this.id }); - return 'Invalid ID Length'; - } + // Check if it's hex + const isHex = this.id.replace(/[\s]*/g, '').match(/^[0-9a-fA-F]+$/); + if (!isHex) return 'Invalid Hex ID'; - // Check if the id is prefixed by 05 - if (!/^05/.test(this.id)) { - return 'Invalid Pubkey Format'; - } - } + // Check if the pubkey length is 33 and leading with 05 or of length 32 + const len = this.id.length; + if ((len !== 33 * 2 || !/^05/.test(this.id)) && len !== 32 * 2) + return 'Invalid Pubkey Format'; + this.set({ id: this.id }); return null; }, diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index 8859af50a..b13fc3ed5 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -167,13 +167,6 @@ } return 1; - - // const item = await window.Signal.Data.getItemById('registrationId'); - // if (item) { - // return item.value; - // } - - // return undefined; }, /* Returns a prekeypair object or undefined */ diff --git a/test/index.html b/test/index.html index 2502fe2a9..b03374a61 100644 --- a/test/index.html +++ b/test/index.html @@ -329,6 +329,16 @@ {{/action }} + + diff --git a/test/models/conversations_test.js b/test/models/conversations_test.js index b11b2a4bc..62190fec7 100644 --- a/test/models/conversations_test.js +++ b/test/models/conversations_test.js @@ -29,7 +29,7 @@ describe('ConversationCollection', () => { }); describe('Conversation', () => { - const attributes = { type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }; + const attributes = { type: 'private', id: '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }; before(async () => { const convo = new Whisper.ConversationCollection().add(attributes); await window.Signal.Data.saveConversation(convo.attributes, { @@ -50,7 +50,7 @@ describe('Conversation', () => { after(clearDatabase); it('sorts its contacts in an intl-friendly way', () => { - const convo = new Whisper.Conversation({ id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); + const convo = new Whisper.Conversation({ id: '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); convo.contactCollection.add( new Whisper.Conversation({ name: 'C', @@ -74,7 +74,7 @@ describe('Conversation', () => { it('contains its own messages', async () => { const convo = new Whisper.ConversationCollection().add({ - id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab', + id: '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab', }); await convo.fetchMessages(); assert.notEqual(convo.messageCollection.length, 0); @@ -82,7 +82,7 @@ describe('Conversation', () => { it('contains only its own messages', async () => { const convo = new Whisper.ConversationCollection().add({ - id: '6eb56f06737d0966239e70d431d4dfd9e57c1e7dddacaf61907fcbc14295e424fd', + id: '052d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab', }); await convo.fetchMessages(); assert.strictEqual(convo.messageCollection.length, 0); @@ -100,7 +100,7 @@ describe('Conversation', () => { it('has a title', () => { const convos = new Whisper.ConversationCollection(); let convo = convos.add(attributes); - assert.equal(convo.getTitle(), '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab'); + assert.equal(convo.getTitle(), '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab'); convo = convos.add({ type: '' }); assert.equal(convo.getTitle(), 'Unknown group'); @@ -112,7 +112,7 @@ describe('Conversation', () => { it('returns the number', () => { const convos = new Whisper.ConversationCollection(); let convo = convos.add(attributes); - assert.equal(convo.getNumber(), '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab'); + assert.equal(convo.getNumber(), '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab'); convo = convos.add({ type: '' }); assert.equal(convo.getNumber(), ''); @@ -127,17 +127,19 @@ describe('Conversation', () => { describe('when set to private', () => { it('correctly validates hex numbers', () => { - const regularId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); + const regularId = new Whisper.Conversation({ type: 'private', id: '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); const invalidId = new Whisper.Conversation({ type: 'private', id: 'j71d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); assert.ok(regularId.isValid()); assert.notOk(invalidId.isValid()); }); it('correctly validates length', () => { - const regularId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); + const regularId33 = new Whisper.Conversation({ type: 'private', id: '051d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); + const regularId32 = new Whisper.Conversation({ type: 'private', id: '1d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab' }); const shortId = new Whisper.Conversation({ type: 'private', id: '771d11d' }); const longId = new Whisper.Conversation({ type: 'private', id: '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94abaa' }); - assert.ok(regularId.isValid()); + assert.ok(regularId33.isValid()); + assert.ok(regularId32.isValid()); assert.notOk(shortId.isValid()); assert.notOk(longId.isValid()); }); @@ -174,11 +176,10 @@ describe('Conversation', () => { it('matches by partial keys', () => { return testSearch([ '1', - '771', - '1e', - '56d9bfc3d74115c3322', - '6d9bfc3d74115c33225a632321b509ac17a13fdeac71165d', - '771d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab', + 'd11', + 'fc3d74115c33225', + 'd01e56d9bfc3d74115c33225a632321b509ac17a13fde', + '1d11d01e56d9bfc3d74115c33225a632321b509ac17a13fdeac71165d09b94ab', ]); }); // TODO: Re-enable once we have nickanme functionality