forked from oxen-io/session-desktop
/
Padding_test.ts
126 lines (111 loc) · 4.76 KB
/
Padding_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression
import chai from 'chai';
import { describe } from 'mocha';
import chaiAsPromised from 'chai-as-promised';
import {
addAttachmentPadding,
addMessagePadding,
getUnpaddedAttachment,
removeMessagePadding,
} from '../../../../session/crypto/BufferPadding';
import { MAX_ATTACHMENT_FILESIZE_BYTES } from '../../../../session/constants';
chai.use(chaiAsPromised as any);
chai.should();
const { expect } = chai;
// tslint:disable-next-line: max-func-body-length
describe('Padding', () => {
describe('Attachment padding', () => {
it('add padding', () => {
const bufferIn = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const paddedBuffer = addAttachmentPadding(bufferIn);
expect(paddedBuffer.byteLength).to.equal(541);
expect(new Uint8Array(paddedBuffer.slice(0, bufferIn.length))).to.equalBytes(bufferIn);
// this makes sure that the padding is just the 0 bytes
expect(new Uint8Array(paddedBuffer.slice(bufferIn.length))).to.equalBytes(
new Uint8Array(541 - bufferIn.length)
);
});
it('no padding if attachment has the max size', () => {
//if the attachment is already of the max size, we do not pad it more
const bufferIn = new Uint8Array(MAX_ATTACHMENT_FILESIZE_BYTES);
const paddedBuffer = addAttachmentPadding(bufferIn);
expect(paddedBuffer.byteLength).to.equal(MAX_ATTACHMENT_FILESIZE_BYTES);
expect(new Uint8Array(paddedBuffer)).to.equalBytes(bufferIn);
});
it('add padding is limited to max attachment size', () => {
// there is only enough room to add one byte as padding.
const bufferIn = new Uint8Array(MAX_ATTACHMENT_FILESIZE_BYTES - 1);
const paddedBuffer = addAttachmentPadding(bufferIn);
expect(paddedBuffer.byteLength).to.equal(MAX_ATTACHMENT_FILESIZE_BYTES);
expect(new Uint8Array(paddedBuffer.slice(0, bufferIn.length))).to.equalBytes(bufferIn);
// this makes sure that the padding is just the 0 bytes
expect(paddedBuffer.slice(bufferIn.length).byteLength).to.eq(1);
expect(new Uint8Array(paddedBuffer.slice(bufferIn.length))).to.equalBytes(
new Uint8Array([0])
);
});
it('add padding if the attachment is already too big', () => {
// we just want to make sure we do not override attachment data. The file upload will fail, but at least make sure to keep the user data.
const bufferIn = new Uint8Array(MAX_ATTACHMENT_FILESIZE_BYTES + 1);
const paddedBuffer = addAttachmentPadding(bufferIn);
const expectedPaddedSize = Math.floor(
Math.pow(1.05, Math.ceil(Math.log(bufferIn.length) / Math.log(1.05)))
);
expect(new Uint8Array(paddedBuffer.slice(0, bufferIn.length))).to.equalBytes(bufferIn);
// this makes sure that the padding is just the 0 bytes
expect(new Uint8Array(paddedBuffer.slice(bufferIn.length))).to.equalBytes(
new Uint8Array(expectedPaddedSize - bufferIn.length)
);
});
it('remove padding', () => {
// padding can be anything after the expected size
const expectedSize = 10;
const paddedBuffer = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 5]);
const paddingRemoveBuffer = getUnpaddedAttachment(paddedBuffer, expectedSize);
expect(paddingRemoveBuffer?.byteLength).to.equal(expectedSize);
expect(paddingRemoveBuffer).to.equalBytes(paddedBuffer.slice(0, expectedSize));
});
});
describe('Message padding', () => {
it('add padding', () => {
const bufferIn = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const paddedMessage = addMessagePadding(bufferIn);
expect(paddedMessage.byteLength).to.equal(159);
// for message padding, we have [bufferIn, 0x80, 0x00, 0x00, 0x00, ...]
expect(new Uint8Array(paddedMessage.slice(0, bufferIn.length))).to.equalBytes(bufferIn);
expect(paddedMessage[bufferIn.length]).to.equal(0x80);
// this makes sure that the padding is just the 0 bytes
expect(new Uint8Array(paddedMessage.slice(bufferIn.length + 1))).to.equalBytes(
new Uint8Array(159 - bufferIn.length - 1)
);
});
it('remove padding', () => {
const expectedSize = 10;
const paddedBuffer = new Uint8Array([
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
128,
0,
0,
0,
0,
0,
0,
0,
0,
]);
const unpaddedMessage = removeMessagePadding(paddedBuffer);
// for message padding, we have [paddedBuffer, 0x80, 0x00, 0x00, 0x00, ...]
expect(unpaddedMessage?.byteLength).to.equal(expectedSize);
expect(new Uint8Array(unpaddedMessage)).to.equalBytes(paddedBuffer.slice(0, expectedSize));
});
});
});