]> git.proxmox.com Git - mirror_novnc.git/blob - tests/test.deflator.js
12e8a46bfa58e1b071e90ade57e0f26b13ddd7c8
[mirror_novnc.git] / tests / test.deflator.js
1 /* eslint-disable no-console */
2 const expect = chai.expect;
3
4 import { inflateInit, inflate } from "../vendor/pako/lib/zlib/inflate.js";
5 import ZStream from "../vendor/pako/lib/zlib/zstream.js";
6 import Deflator from "../core/deflator.js";
7
8 function _inflator(compText, expected) {
9 let strm = new ZStream();
10 let chunkSize = 1024 * 10 * 10;
11 strm.output = new Uint8Array(chunkSize);
12
13 inflateInit(strm, 5);
14
15 if (expected > chunkSize) {
16 chunkSize = expected;
17 strm.output = new Uint8Array(chunkSize);
18 }
19
20 /* eslint-disable camelcase */
21 strm.input = compText;
22 strm.avail_in = strm.input.length;
23 strm.next_in = 0;
24
25 strm.next_out = 0;
26 strm.avail_out = expected.length;
27 /* eslint-enable camelcase */
28
29 let ret = inflate(strm, 0);
30
31 // Check that return code is not an error
32 expect(ret).to.be.greaterThan(-1);
33
34 return new Uint8Array(strm.output.buffer, 0, strm.next_out);
35 }
36
37 describe('Deflate data', function () {
38
39 it('should be able to deflate messages', function () {
40 let deflator = new Deflator();
41
42 let text = "123asdf";
43 let preText = new Uint8Array(text.length);
44 for (let i = 0; i < preText.length; i++) {
45 preText[i] = text.charCodeAt(i);
46 }
47
48 let compText = deflator.deflate(preText);
49
50 let inflatedText = _inflator(compText, text.length);
51 expect(inflatedText).to.array.equal(preText);
52
53 });
54
55 it('should be able to deflate large messages', function () {
56 let deflator = new Deflator();
57
58 /* Generate a big string with random characters. Used because
59 repetition of letters might be deflated more effectively than
60 random ones. */
61 let text = "";
62 let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
63 for (let i = 0; i < 300000; i++) {
64 text += characters.charAt(Math.floor(Math.random() * characters.length));
65 }
66
67 let preText = new Uint8Array(text.length);
68 for (let i = 0; i < preText.length; i++) {
69 preText[i] = text.charCodeAt(i);
70 }
71
72 let compText = deflator.deflate(preText);
73
74 //Check that the compressed size is expected size
75 expect(compText.length).to.be.greaterThan((1024 * 10 * 10) * 2);
76
77 let inflatedText = _inflator(compText, text.length);
78
79 expect(inflatedText).to.array.equal(preText);
80
81 });
82 });