/
petrel-test.html
169 lines (161 loc) · 5.45 KB
/
petrel-test.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<html>
<head>
<script src="./demo/sha256.js"></script>
<script src="petrel-dev.js"></script>
<script>
var testcount = 0;
var passcount = 0;
function testHeader(msg) {
console.log("-".repeat(72) + "\n" + msg + "\n" + "-".repeat(72));
}
function test(msg, a, b, invert) {
testcount++;
if (invert == true) {
if (a != b) {
console.log(msg + " - PASS");
passcount++;
} else {
console.log(msg + " - FAIL: '" + a + "', '" + b + "'");
throw '';
}
return;
}
if (a == b) {
console.log(msg + " - PASS");
passcount++;
} else {
console.log(msg + " - FAIL: '" + a + "', '" + b + "'");
throw '';
}
}
testHeader("1 - Instantiate empty Petrel obj");
var p = new PetrelClient();
test("1.1 p.err should initialize to null", p.err, null);
test("1.2 p.hmac should initialize to null", p.hmac, null);
test("1.3 p.timeout should initialize to 0", p.timeout, 0);
test("1.4 p.ws should initialize to undefined", p.ws, undefined);
test("1.5 p.seq should initialize to 0", p.seq, 0);
test("1.6 p.reqq should have zero elements", Object.keys(p.reqq).length, 0);
testHeader("2 - Instantiate non-default Petrel obj");
p = new PetrelClient(50, "foo");
test("2.1 p.err should initialize to null", p.err, null);
test("2.2 p.hmac should initialize to 'foo'", p.hmac, "foo");
test("2.3 p.timeout should initialize to 50", p.timeout, 50);
test("2.4 p.ws should initialize to undefined", p.ws, undefined);
test("2.5 p.seq should initialize to 0", p.seq, 0);
test("2.6 p.reqq should have zero elements", Object.keys(p.reqq).length, 0);
testHeader("3 - Message marshalling");
p = new PetrelClient();
msg = petrelMarshal(p, "hello, world");
test("3.1 p.err should be null", p.err, null);
test("3.2 msg should be defined", msg, undefined, true);
test("3.3 msg.size should be 21 (4+4+1+12)", msg.size, 21);
p.seq = 1;
msg = petrelMarshal(p, "hello, world");
x1 = msg.slice(0,4); // seq
r1 = new FileReader();
r1.onload = function(evt) {
x2 = evt.target.result;
x3 = new Uint32Array(x2);
test("3.4 seq should be 1", x3[0], 1);
};
r1.readAsArrayBuffer(x1);
x1 = msg.slice(4,8); // plen
r2 = new FileReader();
r2.onload = function(evt) {
x2 = evt.target.result;
x3 = new Uint32Array(x2);
test("3.5 plen should be 12", x3[0], 12);
};
r2.readAsArrayBuffer(x1);
x1 = msg.slice(8,9); // pver
r3 = new FileReader();
r3.onload = function(evt) {
x2 = evt.target.result;
x3 = new Uint8Array(x2);
test("3.6 pver should be 0", x3[0], 0);
};
r3.readAsArrayBuffer(x1);
x1 = msg.slice(9,21); // payloar
r4 = new FileReader();
r4.onload = function(evt) {
x2 = evt.target.result;
test("3.7 payload should be 'hello, world'", x2, "hello, world");
};
r4.readAsText(x1);
testHeader("4 - Message unmarshalling");
function test4a(m) {
test("4.1 msg1.seq is 1", m.seq, 1);
test("4.2 msg1.plen is 12", m.plen, 12);
test("4.3 msg1.pver is 0", m.pver, 0);
test("4.4 msg1.payload is 'hello, world'", m.payload, "hello, world");
}
function test4b(m) {
test("4.5 msg2.seq is 2", m.seq, 2);
test("4.6 msg2.plen is 3", m.plen, 3);
test("4.7 msg2.pver is 0", m.pver, 0);
test("4.8 msg2.payload is 'foo'", m.payload, "foo");
}
function test4c(m) {
test("4.9 msg3.seq is 3", m.seq, 3);
test("4.10 msg3.plen is 4", m.plen, 4);
test("4.11 msg3.pver is 0", m.pver, 0);
test("4.12 msg3.payload is 'quux'", m.payload, "quux");
}
p.seq++;
msg2 = petrelMarshal(p, "foo");
p.seq++;
msg3 = petrelMarshal(p, "quux");
p.reqq[1] = test4a; // fake requests and fire unmarshal calls
p.reqq[2] = test4b;
p.reqq[3] = test4c;
petrelUnmarshal(p, msg3);
petrelUnmarshal(p, msg2);
petrelUnmarshal(p, msg);
testHeader("5 - Message marshalling/unmarshalling + HMAC");
p = new PetrelClient(50, "testkey");
test("5.1 p.hmac should be 'testkey'", p.hmac, "testkey");
msg = petrelMarshal(p, "test message");
test("5.2 msg should have size 65 (4+4+1+44+12)", msg.size, 65);
x1 = msg.slice(9,53); // hmac
r1 = new FileReader();
r1.onload = function(evt) {
x2 = evt.target.result;
test("5.3 hmac should be 'htwE0CebBpUCKLCF68l2NJxSV+TT276Dt/4wWVvT/Ck='", x2, "htwE0CebBpUCKLCF68l2NJxSV+TT276Dt/4wWVvT/Ck=");
};
r1.readAsText(x1);
function test5a(m) {
test("5.4 msg.seq is 0", m.seq, 0);
test("5.5 msg.plen is 12", m.plen, 12);
test("5.6 msg.pver is 0", m.pver, 0);
test("5.7 msg.payload is 'test message'", m.payload, "test message");
test("5.8 p.err should be null", p.err, null);
}
p.reqq[0] = test5a;
petrelUnmarshal(p, msg);
p2 = new PetrelClient(50, "badkey"); // new instance with different key
p3 = new PetrelClient(50, "testkey"); // new instance with original key
msg2 = petrelMarshal(p2, "test message"); // same message, different hmac
function test5b() {
test("5.9 p3.err should be HMAC mismatch", p3.err, "HMAC mismatch in request 0");
test("5.10 p3.reqq[0] should be undefined", p3.reqq[0], undefined);
}
p3.reqq[0] = test5b;
petrelUnmarshal(p3, msg2);
testHeader("6 - Request dispatch");
function test6(msg) {
test("6.x 1 = 1", 1, 1);
}
p = new PetrelClient();
p.dispatch("foo", test6);
test("6.1 p.reqq should have 1 element", Object.keys(p.reqq).length, 1);
test("6.2 p.seq should be 1", p.seq, 1);
test("6.3 p.reqq[1] should be test6", p.reqq[1], test6);
testHeader("X - Async test completions follow");
// ---------------------------------------------------------- end tests
</script>
</head>
<body>
Test results are in the Javascript console.
</body>
</html>