-
Notifications
You must be signed in to change notification settings - Fork 11
/
block-013-put-invalid-size.js
190 lines (166 loc) · 7.28 KB
/
block-013-put-invalid-size.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
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
190
/*
Handle an out-of-order response to a confirmable, blockwise PUT request:
1. User sends a request with the `blockSize` option set to `128`:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0001
1 | CON | 0 bytes | PUT | 1
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Uri-Path : invalid-size
Content-Format: text/plain;charset=utf-8
------------------------------------------------------------------------------
Payload (128 bytes)
|-------------------------------------------------------------|
| BLOCK 1 |
|-------------------------------------------------------------|
| BLOCK 2 |
|-------------------------------------------------------------|
==============================================================================
2. Client recognizes that the payload of the #1 request is bigger than
the specified block size, so it constructs and sends the first Block1 request
instead:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0002
1 | CON | 0 bytes | PUT | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 0, M: true, SZX: 3 (128 bytes)
------------------------------------------------------------------------------
Payload (128 bytes)
|-------------------------------------------------------------|
| BLOCK 1 |
==============================================================================
3. Server confirms the first block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0002
1 | ACK | 0 bytes | 2.04 Changed | 2
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 0, M: true, SZX: 3 (128 bytes)
==============================================================================
4. Client receives the #3 confirmation. Request emits the `acknowledged` event
and the `block sent` event.
5. Client sends a request with the second block:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 0 0 | 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0x0003
1 | CON | 0 bytes | PUT | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Uri-Path : blocks
Uri-Path : put
Content-Format: text/plain;charset=utf-8
Block1 : NUM: 1, M: true, SZX: 3 (128 bytes)
------------------------------------------------------------------------------
Payload (128 bytes)
|-------------------------------------------------------------|
| BLOCK 2 |
==============================================================================
6. Servers confirms the second block and switches to a bigger block size:
==============================================================================
Version | Type | Token Length | Code | Message ID
0 1 | 1 0 | 0 0 0 0 | 0 1 0 0 0 1 0 0 | 0x0003
1 | ACK | 0 bytes | 2.04 Changed | 3
------------------------------------------------------------------------------
Remote Endpoint: 127.0.0.1
------------------------------------------------------------------------------
Block1: NUM: 1, M: true, SZX: 4 (256 bytes)
==============================================================================
7. Client receives the #6 confirmation, but recognizes that its block size
is bigger than the one the client expected and ignores it.
8. After `exchangeTimeout` ms have passed, the client emits
the `exchange timeout` event and the request emits the `timeout` event.
*/
'use strict';
require('should');
var sinon = require('sinon');
var helpers = require('../helpers');
var Message = require(helpers.LIB_DIR).Message;
helpers.test(__filename, function(ctx)
{
var request = {
type: Message.Type.CON,
code: Message.Code.PUT,
uri: '/blocks/put',
contentFormat: 'text/plain;charset=utf-8',
payload: new Buffer(
'|-------------------------------------------------------------|\n' +
'| BLOCK 1 |\n' +
'|-------------------------------------------------------------|\n' +
'| BLOCK 2 |\n' +
'|-------------------------------------------------------------|'
)
};
var reqWithBlock0 = {
type: request.type,
code: request.code,
id: 0x0002,
uri: request.uri,
block1: {num: 0, m: true, szx: 3},
contentFormat: request.contentFormat,
payload: request.payload.slice(0 * 128, 1 * 128)
};
var resToBlock0 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock0.id,
block1: {num: 0, m: true, szx: 3}
};
var reqWithBlock1 = {
type: request.type,
code: request.code,
id: 0x0003,
uri: request.uri,
block1: {num: 1, m: true, szx: 3},
contentFormat: request.contentFormat,
payload: request.payload.slice(1 * 128, 2 * 128)
};
var resToBlock1 = {
type: Message.Type.ACK,
code: Message.Code.CHANGED,
id: reqWithBlock1.id,
block1: {num: 1, m: true, szx: 4}
};
ctx.socket.expectRequest(reqWithBlock0);
ctx.socket.scheduleResponse(50, resToBlock0);
ctx.socket.expectRequest(50, reqWithBlock1);
ctx.socket.scheduleResponse(100, resToBlock1);
var req = ctx.client.request(Message.fromObject(request), {
blockSize: 128
});
var eventSpy = sinon.spy(req, 'emit');
ctx.clock.tick(3600000);
return function assert()
{
ctx.socket.assert();
sinon.assert.callCount(eventSpy, 3);
sinon.assert.calledWith(
eventSpy, 'acknowledged', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(
eventSpy, 'block sent', sinon.match.instanceOf(Message)
);
sinon.assert.calledWith(eventSpy, 'timeout');
eventSpy.args[0][0].should.be.equal('acknowledged');
sinon.assert.coapMessage(
eventSpy.args[0][1], resToBlock0, "Invalid ACK."
);
eventSpy.args[1][0].should.be.equal('block sent');
sinon.assert.coapMessage(
eventSpy.args[1][1], resToBlock0, "Invalid `block sent` (#1)."
);
eventSpy.args[2][0].should.be.equal('timeout');
};
});