-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_agent_message_tests.js
139 lines (121 loc) · 4.71 KB
/
nassh_agent_message_tests.js
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview Test suite for the SSH agent message handling primitives in
* nassh.agent.Message.
*/
describe('nassh_agent_message_tests.js', () => {
it('rawMessage', () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS,
new Uint8Array([2, 3, 4]));
const rawMsg = msg.rawMessage();
assert.deepStrictEqual(Array.from(rawMsg), [0, 0, 0, 4, 6, 2, 3, 4]);
});
it('eom', () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS);
assert.isTrue(msg.eom(), 'empty message');
msg.writeUint32(2);
assert.isTrue(!msg.eom(), 'non-empty message');
msg.readUint32();
assert.isTrue(msg.eom(), 'end of non-empty message');
});
it('readUint32', () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS,
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]));
assert.equal(msg.readUint32(), 0x01020304, 'big endian');
assert.equal(msg.readUint32(), 0x05060708, 'correct offset');
// Check end reached.
assert.throws(() => msg.readUint32());
});
it('writeUint32', /** @suppress {visibility} msg.data_ */ () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS);
// Check unsafe integer.
assert.throws(() => msg.writeUint32(0.5));
msg.writeUint32(0x01020304);
msg.writeUint32(0x05060708);
assert.deepStrictEqual(Array.from(msg.data_), [1, 2, 3, 4, 5, 6, 7, 8]);
});
it('readString', () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS,
new Uint8Array(
[0, 0, 0, 3, 1, 2, 3, 0, 0, 0, 4, 4, 5, 6, 7, 0, 0, 0, 2, 8]));
assert.deepStrictEqual(Array.from(msg.readString()), [1, 2, 3],
'valid length');
assert.deepStrictEqual(Array.from(msg.readString()), [4, 5, 6, 7],
'correct offset');
// Check end reached.
assert.throws(() => msg.readString());
});
it('writeString', /** @suppress {visibility} msg.data_ */ () => {
const msg = new nassh.agent.Message(
nassh.agent.messages.Numbers.AGENT_SUCCESS);
msg.writeString(new Uint8Array([1, 2, 3]));
msg.writeString(new Uint8Array([4, 5, 6, 7]));
assert.deepStrictEqual(
Array.from(msg.data_), [0, 0, 0, 3, 1, 2, 3, 0, 0, 0, 4, 4, 5, 6, 7]);
});
it('fromRawMessage', () => {
assert.strictEqual(
nassh.agent.Message.fromRawMessage(new Uint8Array([1, 2, 3, 4])), null,
'raw message too short');
assert.strictEqual(
nassh.agent.Message.fromRawMessage(new Uint8Array([0, 0, 0, 4, 1, 2, 3])),
null, 'length field invalid (too small)');
assert.strictEqual(
nassh.agent.Message.fromRawMessage(
new Uint8Array([0, 0, 0, 4, 1, 2, 3, 4, 5])),
null, 'length field invalid (too large)');
});
// clang-format off
it('fromRawMessage_requestIdentities', () => {
assert.deepStrictEqual(
nassh.agent.Message.fromRawMessage(
new Uint8Array([0, 0, 0, 5, 11, 1, 2, 3, 4])),
null, 'invalid message (SSH_AGENTC_REQUEST_IDENTITIES)');
const requestIdentitiesMsg =
nassh.agent.Message.fromRawMessage(new Uint8Array([0, 0, 0, 1, 11]));
assert.isTrue(
requestIdentitiesMsg.eom(), 'eom (SSH_AGENTC_REQUEST_IDENTITIES)');
assert.equal(
requestIdentitiesMsg.type, 11,
'valid type (SSH_AGENTC_REQUEST_IDENTITIES)');
assert.equal(
Object.keys(requestIdentitiesMsg.fields).length, 0,
'no fields (SSH_AGENTC_REQUEST_IDENTITIES)');
});
// clang-format on
// clang-format off
it('fromRawMessage_signRequest', () => {
assert.deepStrictEqual(
nassh.agent.Message.fromRawMessage(new Uint8Array([
0, 0, 0, 18, 13, 0, 0, 0, 2, 1, 2, 6, 7, 8, 9, 0, 0, 0, 3, 3, 4, 5,
])),
null, 'invalid message (SSH_AGENTC_REQUEST_IDENTITIES)');
const signRequestMsg = nassh.agent.Message.fromRawMessage(new Uint8Array([
0, 0, 0, 18, 13, 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 3, 4, 5, 6, 7, 8, 9,
]));
assert.isTrue(signRequestMsg.eom(), 'eom (SSH_AGENTC_SIGN_REQUEST)');
assert.equal(
signRequestMsg.type, 13, 'valid type (SSH_AGENTC_SIGN_REQUEST)');
assert.equal(
Object.keys(signRequestMsg.fields).length, 3,
'three fields (SSH_AGENTC_SIGN_REQUEST)');
assert.deepStrictEqual(
Array.from(signRequestMsg.fields.keyBlob), [1, 2],
'key blob (SSH_AGENTC_SIGN_REQUEST)');
assert.deepStrictEqual(
Array.from(signRequestMsg.fields.data), [3, 4, 5],
'data (SSH_AGENTC_SIGN_REQUEST)');
assert.equal(
signRequestMsg.fields.flags, 0x06070809,
'flags (SSH_AGENTC_SIGN_REQUEST)');
});
// clang-format on
});