-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.test.ts
88 lines (77 loc) · 2.96 KB
/
types.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
import { Principal } from "@dfinity/principal";
import { isClientIncomingMessage, isGatewayHandshakeMessage } from "./types";
describe("isClientIncomingMessage", () => {
it("should return false for wrong type", () => {
const incomingMessageWrongType = "not a ClientIncomingMessage";
expect(isClientIncomingMessage(incomingMessageWrongType)).toBe(false);
})
it("should return false for wrong properties", () => {
const incomingMessageWrongKey = {
key: {},
content: new Uint8Array(),
cert: new Uint8Array(),
tree: new Uint8Array()
};
const incomingMessageWrongContent = {
key: "key",
content: "not a Uint8Array",
cert: new Uint8Array(),
tree: new Uint8Array()
};
const incomingMessageWrongCert = {
key: "key",
content: new Uint8Array(),
cert: "not a Uint8Array",
tree: new Uint8Array()
};
const incomingMessageWrongTree = {
key: "key",
content: new Uint8Array(),
cert: new Uint8Array(),
tree: "not a Uint8Array"
}
expect(isClientIncomingMessage(incomingMessageWrongKey)).toBe(false);
expect(isClientIncomingMessage(incomingMessageWrongContent)).toBe(false);
expect(isClientIncomingMessage(incomingMessageWrongCert)).toBe(false);
expect(isClientIncomingMessage(incomingMessageWrongTree)).toBe(false);
})
it("should return true for valid client incoming message", () => {
const incomingMessage = {
key: "key",
content: new Uint8Array(),
cert: new Uint8Array(),
tree: new Uint8Array()
};
expect(isClientIncomingMessage(incomingMessage)).toBe(true);
});
});
describe("isGatewayHandshakeMessage", () => {
it("should return false for wrong type", () => {
const handshakeMessageWrongType = "not a HandshakeMessage";
expect(isGatewayHandshakeMessage(handshakeMessageWrongType)).toBe(false);
});
it("should return false for wrong properties", () => {
const handshakeMessageWrongGatewayPrincipal1 = {
gateway_principal: {},
};
const handshakeMessageWrongGatewayPrincipal2 = {
gateway_principal: "",
};
const handshakeMessageWrongGatewayPrincipal3 = {
gateway_principal: null,
};
expect(isGatewayHandshakeMessage(handshakeMessageWrongGatewayPrincipal1)).toBe(false);
expect(isGatewayHandshakeMessage(handshakeMessageWrongGatewayPrincipal2)).toBe(false);
expect(isGatewayHandshakeMessage(handshakeMessageWrongGatewayPrincipal3)).toBe(false);
});
it("should return true for valid handshake message", () => {
const message = {
gateway_principal: Principal.fromText("pmisz-prtlk-b6oe6-bj4fl-6l5fy-h7c2h-so6i7-jiz2h-bgto7-piqfr-7ae"), // a random but valid principal
};
const message2 = {
gateway_principal: Principal.fromText("pmisz-prtlk-b6oe6-bj4fl-6l5fy-h7c2h-so6i7-jiz2h-bgto7-piqfr-7ae").toUint8Array(),
};
expect(isGatewayHandshakeMessage(message)).toBe(true);
expect(isGatewayHandshakeMessage(message2)).toBe(true);
});
});