-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.js
212 lines (200 loc) · 6.85 KB
/
test.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict'
var test = require('tape')
var C = require('./constants')
var ipp = require('./')
test('encodingLength', function (t) {
var types = ['request', 'response']
types.forEach(function (type) {
t.test(type + ' minimal', function (t) {
var len = ipp[type].encodingLength({})
t.deepEqual(len, 9)
t.end()
})
t.test(type + ' groups', function (t) {
var date = new Date(2015, 11, 1, 1, 23, 45, 678)
var obj = { // version + statusCode + operationId/requestId: +8
groups: [
{ tag: 0, attributes: [ // +1 (9)
{ tag: C.KEYWORD, name: 'string', value: 'foo' }, // +1+2+6+2+3=14 (23)
{ tag: C.KEYWORD, name: 'array', value: ['foo', 'bar'] }, // +1+2+5+2+3+1+2+0+2+3=21 (44)
{ tag: C.BOOLEAN, name: 'bool', value: true }, // +1+2+4+2+1=10 (54)
{ tag: C.ENUM, name: 'enum', value: 1 } // +1+2+4+2+4=13 (67)
] },
{ tag: 1, attributes: [ // +1 (68)
{ tag: C.KEYWORD, name: 'string', value: ['foo'] }, // +1+2+6+2+3=14 (82)
{ tag: C.TEXT_WITH_LANG, name: 'text-with-language', value: { lang: 'fr-CA', value: 'fou' } }, // +1+2+18+2+2+5+2+3=35 (117)
{ tag: C.DATE_TIME, name: 'date-time', value: date } // +1+2+9+2+11=25 (142)
] }
]
} // end tag: +1 (143)
var len = ipp[type].encodingLength(obj)
t.deepEqual(len, 143)
t.end()
})
})
})
test('encode', function (t) {
t.test('request', function (t) {
t.test('minimal', function (t) {
var obj = {
operationId: C.PRINT_JOB,
requestId: 42
}
var encoded = ipp.request.encode(obj)
var expected = new Buffer('010100020000002a03', 'hex')
t.deepEqual(encoded, expected)
t.end()
})
})
t.test('response', function (t) {
t.test('minimal', function (t) {
var obj = {
statusCode: C.SERVER_ERROR_VERSION_NOT_SUPPORTED,
requestId: 42
}
var encoded = ipp.response.encode(obj)
var expected = new Buffer('010105030000002a03', 'hex')
t.deepEqual(encoded, expected)
t.end()
})
t.test('custom version', function (t) {
var obj = {
version: { major: 2, minor: 0 },
statusCode: C.SUCCESSFUL_OK,
requestId: 42
}
var encoded = ipp.response.encode(obj)
var expected = new Buffer('020000000000002a03', 'hex')
t.deepEqual(encoded, expected)
t.end()
})
t.test('groups', function (t) {
var date = new Date(2015, 11, 1, 1, 23, 45, 678)
var sign = date.getTimezoneOffset() > 0 ? '2d' : '2b'
var zone = new Buffer(2)
zone.writeInt8(date.getTimezoneOffset() / 60, 0)
zone.writeInt8(date.getTimezoneOffset() % 60, 1)
var dateHex = '07df0c0101172d06' + sign + zone.toString('hex')
var obj = {
statusCode: C.SUCCESSFUL_OK,
requestId: 42,
groups: [
{ tag: C.OPERATION_ATTRIBUTES_TAG, attributes: [
{ tag: C.KEYWORD, name: 'string', value: 'foo' },
{ tag: C.KEYWORD, name: 'array', value: ['foo', 'bar'] },
{ tag: C.BOOLEAN, name: 'bool', value: true },
{ tag: C.ENUM, name: 'enum', value: 42 }
] },
{ tag: C.JOB_ATTRIBUTES_TAG, attributes: [
{ tag: C.KEYWORD, name: 'string', value: ['foo'] },
{ tag: C.NAME_WITH_LANG, name: 'name-with-language', value: { lang: 'fr-CA', value: 'fou' } },
{ tag: C.DATE_TIME, name: 'date-time', value: date }
] }
]
}
var encoded = ipp.response.encode(obj)
var expected = new Buffer(
'0101' + // version
'0000' + // statusCode
'0000002a' + // requestId
'01' + // delimiter tag
'44' + // value tag
'0006' + // name length
'737472696e67' + // name
'0003' + // value length
'666f6f' + // value
'44' + // value tag
'0005' + // name length
'6172726179' + // name
'0003' + // value length
'666f6f' + // value
'44' + // value tag
'0000' + // name length
'' + // name
'0003' + // value length
'626172' + // value
'22' + // value tag
'0004' + // name length
'626f6f6c' + // name
'0001' + // value length
'01' + // value
'23' + // value tag
'0004' + // name length
'656e756d' + // name
'0004' + // value length
'0000002a' + // value
'02' + // delimiter tag
'44' + // value tag
'0006' + // name length
'737472696e67' + // name
'0003' + // value length
'666f6f' + // value
'36' + // value tag
'0012' + // name length
'6e616d652d776974682d6c616e6775616765' + // name
'000c' + // value length
'0005' + // sub-value length
'66722d4341' + // sub-value
'0003' + // sub-value length
'666f75' + // name
'31' + // value tag
'0009' + // name length
'646174652d74696d65' + // name
'000b' + // value length
dateHex + // value
'03', // end of attributes tag
'hex')
t.deepEqual(encoded, expected)
t.end()
})
})
})
test('decode', function (t) {
t.test('request', function (t) {
t.test('minimal', function (t) {
var data = new Buffer('0101000a0000002a03', 'hex')
var expected = {
version: { major: 1, minor: 1 },
operationId: 10,
requestId: 42,
groups: []
}
var decoded = ipp.request.decode(data)
t.deepEqual(decoded, expected)
t.end()
})
t.test('truncated', function (t) {
var data = new Buffer('0101000a0000002a', 'hex')
t.throws(function () {
ipp.request.decode(data)
})
t.end()
})
})
})
test('encode -> decode', function (t) {
var encodeDate = new Date(2015, 11, 1, 1, 23, 45, 678)
var decodeDate = new Date(2015, 11, 1, 1, 23, 45, 600)
var obj = {
version: { major: 1, minor: 0 },
statusCode: C.SUCCESSFUL_OK,
requestId: 42,
groups: [
{ tag: C.OPERATION_ATTRIBUTES_TAG, attributes: [
{ tag: C.KEYWORD, name: 'string', value: ['foo'] },
{ tag: C.KEYWORD, name: 'array', value: ['foo', 'bar'] },
{ tag: C.BOOLEAN, name: 'bool', value: [true] },
{ tag: C.ENUM, name: 'enum', value: [42] }
] },
{ tag: C.JOB_ATTRIBUTES_TAG, attributes: [
{ tag: C.KEYWORD, name: 'string', value: ['foo'] },
{ tag: C.DATE_TIME, name: 'date-time', value: [encodeDate] }
] }
]
}
var encoded = Buffer.concat([ipp.response.encode(obj), new Buffer('foo')])
obj.groups[1].attributes[1].value[0] = decodeDate
var decoded = ipp.response.decode(encoded)
t.deepEqual(decoded, obj)
t.end()
})