forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnassh_buffer_test_util.js
More file actions
189 lines (165 loc) · 5.23 KB
/
nassh_buffer_test_util.js
File metadata and controls
189 lines (165 loc) · 5.23 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Common buffer API tests.
*/
import {newBuffer, setDefaultBackend} from './nassh_buffer.js';
import {BufferInterface} from './nassh_buffer_interface.js';
/**
* Helper class for inspecting buffer internals.
*
* @abstract
*/
export class BufferInspector {
/**
* @param {!BufferInterface} buffer The buffer to inspect.
*/
constructor(buffer) {
this.buffer = buffer;
}
/**
* How many bytes not yet acked.
*
* @abstract
* @return {number}
*/
getUnackedCount() {}
}
/**
* This is mocha.Context with extra stuff attached.
*
* @typedef {{
* skip: function(),
* backend: string,
* inspectClass: !typeof BufferInspector,
* }}
*/
let BufferContext;
/**
* Check behavior of empty buffers.
*
* @this {!BufferContext}
*/
function testBufferEmpty() {
setDefaultBackend(this.backend);
const buffer = newBuffer();
// No data available.
assert.equal(0, buffer.getUnreadCount());
// Read data that doesn't exist.
const data = buffer.read(100);
// The buffer should be empty.
assert.equal(0, data.length);
assert.deepStrictEqual(new Uint8Array(0), data);
// Internal length should be still be zero.
assert.equal(0, buffer.getUnreadCount());
// Acking data that doesn't exist shouldn't confuse it.
buffer.ack(10);
assert.equal(0, buffer.getUnreadCount());
}
/**
* Check autoacking behavior.
*
* @this {!BufferContext}
* @suppress {checkTypes} Closure can't figure out abstract class.
*/
function testBufferAutoack() {
setDefaultBackend(this.backend);
const buffer = newBuffer(/* autoack= */ true);
const inspector = new this.inspectClass(buffer);
// Write some data to the buffer.
buffer.write(new Uint8Array([1, 2]));
buffer.write(new Uint8Array([3]));
// Make sure our counters are correct.
assert.equal(3, buffer.getUnreadCount());
assert.equal(3, inspector.getUnackedCount());
// Read out a byte and check the counters.
let data = buffer.read(1);
assert.deepStrictEqual(new Uint8Array([1]), data);
assert.equal(2, buffer.getUnreadCount());
assert.equal(2, inspector.getUnackedCount());
// Read out the rest of the data and check the counters.
data = buffer.read(2);
assert.deepStrictEqual(new Uint8Array([2, 3]), data);
assert.equal(0, buffer.getUnreadCount());
assert.equal(0, inspector.getUnackedCount());
}
/**
* Check manual acking behavior.
*
* @this {!BufferContext}
* @suppress {checkTypes} Closure can't figure out abstract class.
*/
function testBufferManualAck() {
setDefaultBackend(this.backend);
const buffer = newBuffer();
const inspector = new this.inspectClass(buffer);
// Write some data to the buffer.
buffer.write(new Uint8Array([5, 6, 7]));
assert.equal(3, buffer.getUnreadCount());
// Read it out and verify the ack counts.
buffer.read(1);
assert.equal(2, buffer.getUnreadCount());
assert.equal(3, inspector.getUnackedCount());
// Read out the rest of the data and check the counters.
buffer.read(2);
assert.equal(0, buffer.getUnreadCount());
assert.equal(3, inspector.getUnackedCount());
// Check ack handling.
buffer.ack(1);
assert.equal(2, inspector.getUnackedCount());
buffer.ack(2);
assert.equal(0, inspector.getUnackedCount());
}
/**
* Check automatic buffer growing.
*
* @this {!BufferContext}
* @suppress {checkTypes} Closure can't figure out abstract class.
*/
function testBufferGrow() {
setDefaultBackend(this.backend);
const buffer = newBuffer();
const inspector = new this.inspectClass(buffer);
const basesize = 1024;
// Fill the buffer.
buffer.write(new Uint8Array(basesize).fill(10));
assert.equal(basesize, buffer.getUnreadCount());
// Add some more data and check the growth.
buffer.write(new Uint8Array([1, 2, 3]));
assert.equal(basesize + 3, buffer.getUnreadCount());
// Read out most data to verify buffer doesn't move.
assert.deepStrictEqual(new Uint8Array(basesize).fill(10),
buffer.read(basesize));
assert.equal(3, buffer.getUnreadCount());
// Write some more data to check more growth.
buffer.write(new Uint8Array(1024).fill(20));
assert.equal(1027, buffer.getUnreadCount());
// Read out all the data.
assert.deepStrictEqual(new Uint8Array([1, 2, 3]), buffer.read(3));
assert.deepStrictEqual(new Uint8Array(1024).fill(20), buffer.read(1024));
// Counters shouldn't change even as we ack.
assert.equal(0, buffer.getUnreadCount());
assert.equal(basesize + 1027, inspector.getUnackedCount());
buffer.ack(basesize + 1027);
assert.equal(0, inspector.getUnackedCount());
}
/**
* Testsuite for the generic buffer API.
*
* Each implementation should call this to verify functionality.
*
* @param {string} backend The name of the backend to instantiate.
* @param {!typeof BufferInspector} inspectClass Class for inspecting the
* buffer internals while testing.
*/
export function BufferApiTest(backend, inspectClass) {
before(function() {
this.backend = backend;
this.inspectClass = inspectClass;
});
it('buffer-empty', testBufferEmpty);
it('buffer-autoack', testBufferAutoack);
it('buffer-manual-ack', testBufferManualAck);
it('buffer-grow', testBufferGrow);
}