1 | var codec = require('../lib/codec');
|
2 | var defs = require('../lib/defs');
|
3 | var assert = require('assert');
|
4 |
|
5 | var C = require('claire');
|
6 | var forAll = C.forAll;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var testCases = [
|
12 |
|
13 | ['byte', {byte: 127}, [4,98,121,116,101,98,127]],
|
14 | ['byte max value', {byte: 255}, [4,98,121,116,101,98,255]],
|
15 | ['byte min value', {byte: 0}, [4,98,121,116,101,98,0]],
|
16 | ['< 0 promoted to signed short', {short: -1}, [5,115,104,111,114,116,115,255,255]],
|
17 | ['> 255 promoted to short', {short: 256}, [5,115,104,111,114,116,115,1,0]],
|
18 | ['< 2^15 still a short', {short: 0x7fff}, [5,115,104,111,114,116,115,127,255]],
|
19 | ['-2^15 still a short', {short: -0x8000}, [5,115,104,111,114,116,115,128,0]],
|
20 | ['>= 2^15 promoted to int', {int: 0x8000}, [3,105,110,116,73,0,0,128,0]],
|
21 | ['< -2^15 promoted to int', {int: -0x8001}, [3,105,110,116,73,255,255,127,255]],
|
22 | ['< 2^31 still an int', {int: 0x7fffffff}, [3,105,110,116,73,127,255,255,255]],
|
23 | ['>= -2^31 still an int', {int: -0x80000000}, [3,105,110,116,73,128,0,0,0]],
|
24 | ['>= 2^31 promoted to long', {long: 0x80000000}, [4,108,111,110,103,108,0,0,0,0,128,0,0,0]],
|
25 | ['< -2^31 promoted to long', {long: -0x80000001}, [4,108,111,110,103,108,255,255,255,255,127,255,255,255]],
|
26 |
|
27 |
|
28 | ['float value', {double: 0.5}, [6,100,111,117,98,108,101,100,63,224,0,0,0,0,0,0]],
|
29 | ['negative float value', {double: -0.5}, [6,100,111,117,98,108,101,100,191,224,0,0,0,0,0,0]],
|
30 |
|
31 |
|
32 |
|
33 | ['string', {string: "boop"}, [6,115,116,114,105,110,103,83,0,0,0,4,98,111,111,112]],
|
34 |
|
35 |
|
36 | ['byte array from buffer', {bytes: new Buffer([1,2,3,4])},
|
37 | [5,98,121,116,101,115,120,0,0,0,4,1,2,3,4]],
|
38 |
|
39 |
|
40 | ['true', {bool: true}, [4,98,111,111,108,116,1]],
|
41 | ['false', {bool: false}, [4,98,111,111,108,116,0]],
|
42 | ['null', {'void': null}, [4,118,111,105,100,86]],
|
43 |
|
44 |
|
45 | ['array', {array: [6, true, "foo"]},
|
46 | [5,97,114,114,97,121,65,0,0,0,12,98,6,116,1,83,0,0,0,3,102,111,111]],
|
47 | ['object', {object: {foo: "bar", baz: 12}},
|
48 | [6,111,98,106,101,99,116,70,0,0,0,18,3,102,111,111,83,0,
|
49 | 0,0,3,98,97,114,3,98,97,122,98,12]],
|
50 |
|
51 |
|
52 | ['timestamp', {timestamp: {'!': 'timestamp', value: 1357212277527}},
|
53 | [9,116,105,109,101,115,116,97,109,112,84,0,0,1,60,0,39,219,23]],
|
54 | ['decimal', {decimal: {'!': 'decimal', value: {digits: 2345, places: 2}}},
|
55 | [7,100,101,99,105,109,97,108,68,2,0,0,9,41]],
|
56 | ['float', {float: {'!': 'float', value: 0.1}},
|
57 | [5,102,108,111,97,116,102,61,204,204,205]],,
|
58 | ];
|
59 |
|
60 | function bufferToArray(b) {
|
61 | return Array.prototype.slice.call(b);
|
62 | }
|
63 |
|
64 | suite("Explicit encodings", function() {
|
65 |
|
66 | testCases.forEach(function(tc) {
|
67 | var name = tc[0], val = tc[1], expect = tc[2];
|
68 | test(name, function() {
|
69 | var buffer = new Buffer(1000);
|
70 | var size = codec.encodeTable(buffer, val, 0);
|
71 | var result = buffer.slice(4, size);
|
72 | assert.deepEqual(expect, bufferToArray(result));
|
73 | });
|
74 | });
|
75 | });
|
76 |
|
77 |
|
78 |
|
79 | var amqp = require('./data');
|
80 |
|
81 | function roundtrip_table(t) {
|
82 | var buf = new Buffer(4096);
|
83 | var size = codec.encodeTable(buf, t, 0);
|
84 | var decoded = codec.decodeFields(buf.slice(4, size));
|
85 | try {
|
86 | assert.deepEqual(t, decoded);
|
87 | }
|
88 | catch (e) { return false; }
|
89 | return true;
|
90 | }
|
91 |
|
92 | function roundtrips(T) {
|
93 | return forAll(T).satisfy(function(v) { return roundtrip_table({value: v}); });
|
94 | }
|
95 |
|
96 | suite("Roundtrip values", function() {
|
97 | [
|
98 | amqp.Octet,
|
99 | amqp.ShortStr,
|
100 | amqp.LongStr,
|
101 | amqp.UShort,
|
102 | amqp.ULong,
|
103 | amqp.ULongLong,
|
104 | amqp.UShort,
|
105 | amqp.Short,
|
106 | amqp.Long,
|
107 | amqp.Bit,
|
108 | amqp.Decimal,
|
109 | amqp.Timestamp,
|
110 | amqp.Double,
|
111 | amqp.Float,
|
112 | amqp.FieldArray,
|
113 | amqp.FieldTable
|
114 | ].forEach(function(T) {
|
115 | test(T.toString() + ' roundtrip', roundtrips(T).asTest());
|
116 | });
|
117 | });
|
118 |
|
119 | function roundtripMethod(Method) {
|
120 | return forAll(Method).satisfy(function(method) {
|
121 | var buf = defs.encodeMethod(method.id, 0, method.fields);
|
122 |
|
123 | var fs1 = defs.decode(method.id, buf.slice(11, buf.length));
|
124 | assert.deepEqual(method.fields, fs1);
|
125 | return true;
|
126 | });
|
127 | }
|
128 |
|
129 | function roundtripProperties(Properties) {
|
130 | return forAll(Properties).satisfy(function(properties) {
|
131 | var buf = defs.encodeProperties(properties.id, 0, properties.size,
|
132 | properties.fields);
|
133 |
|
134 | var fs1 = defs.decode(properties.id, buf.slice(19, buf.length));
|
135 | assert.equal(properties.size, buf.readUInt64BE(11));
|
136 | assert.deepEqual(properties.fields, fs1);
|
137 | return true;
|
138 | });
|
139 | }
|
140 |
|
141 | suite("Roundtrip methods", function() {
|
142 | amqp.methods.forEach(function(Method) {
|
143 | test(Method.toString() + ' roundtrip',
|
144 | roundtripMethod(Method).asTest());
|
145 | });
|
146 | });
|
147 |
|
148 | suite("Roundtrip properties", function() {
|
149 | amqp.properties.forEach(function(Properties) {
|
150 | test(Properties.toString() + ' roundtrip',
|
151 | roundtripProperties(Properties).asTest());
|
152 | });
|
153 | });
|