summaryrefslogtreecommitdiff
path: root/cpp/src/tests/amqp_0_10/serialize.cpp
blob: 975d6206ecb1b244ab356c5f462a8ff7a775401f (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "amqp_0_10/unit_test.h"
#include "amqp_0_10/allSegmentTypes.h"

#include "qpid/framing/AMQFrame.h"
#include "qpid/framing/Buffer.h"

#include "qpid/amqp_0_10/Packer.h"
#include "qpid/amqp_0_10/built_in_types.h"
#include "qpid/amqp_0_10/Codec.h"
#include "qpid/amqp_0_10/specification.h"
#include "qpid/amqp_0_10/ControlHolder.h"
#include "qpid/amqp_0_10/Struct32.h"
#include "qpid/amqp_0_10/FrameHeader.h"
#include "qpid/amqp_0_10/Map.h"
#include "qpid/amqp_0_10/Unit.h"
#include "allSegmentTypes.h"

#include <boost/test/test_case_template.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/optional.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/empty_sequence.hpp>
#include <boost/current_function.hpp>
#include <iterator>
#include <string>
#include <sstream>
#include <iostream>
#include <netinet/in.h>

// Missing operators needed for tests.
namespace boost {
template <class T, size_t N>
std::ostream& operator<<(std::ostream& out, const array<T,N>& a) {
    std::ostream_iterator<T> o(out, " ");
    std::copy(a.begin(), a.end(), o);
    return out;
}
} // boost

QPID_AUTO_TEST_SUITE(SerializeTestSuite)

using namespace std;
namespace mpl=boost::mpl;
using namespace qpid::amqp_0_10;
using qpid::framing::in_place;

template <class A, class B> struct concat2 { typedef typename mpl::copy<B, typename mpl::back_inserter<A> >::type type; };
template <class A, class B, class C> struct concat3 { typedef typename concat2<A, typename concat2<B, C>::type>::type type; };
template <class A, class B, class C, class D> struct concat4 { typedef typename concat2<A, typename concat3<B, C, D>::type>::type type; };

typedef mpl::vector<Boolean, Char, Int32, Int64, Int8, Uint16, CharUtf32, Uint32, Uint64, Bin8, Uint8>::type IntegralTypes;
typedef mpl::vector<Bin1024, Bin128, Bin16, Bin256, Bin32, Bin40, Bin512, Bin64, Bin72>::type BinTypes;
typedef mpl::vector<Double, Float>::type FloatTypes;
typedef mpl::vector<SequenceNo, Uuid, Datetime, Dec32, Dec64> FixedSizeClassTypes;
typedef mpl::vector<Map, Vbin8, Str8Latin, Str8, Str8Utf16, Vbin16, Str16Latin, Str16, Str16Utf16, Vbin32> VariableSizeTypes;

typedef concat4<IntegralTypes, BinTypes, FloatTypes, FixedSizeClassTypes>::type FixedSizeTypes;
typedef concat2<FixedSizeTypes, VariableSizeTypes>::type AllTypes;

// TODO aconway 2008-02-20: should test 64 bit integrals for order also.
QPID_AUTO_TEST_CASE(testNetworkByteOrder) {
    string data;

    uint32_t l = 0x11223344;
    Codec::encode(std::back_inserter(data))(l);
    uint32_t enc=reinterpret_cast<const uint32_t&>(*data.data());
    uint32_t l2 = ntohl(enc);
    BOOST_CHECK_EQUAL(l, l2);

    data.clear();
    uint16_t s = 0x1122;
    Codec::encode(std::back_inserter(data))(s);
    uint32_t s2 = ntohs(*reinterpret_cast<const uint32_t*>(data.data()));
    BOOST_CHECK_EQUAL(s, s2);
}

QPID_AUTO_TEST_CASE(testSetLimit) {
    typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
    string data;
    Encoder encode(back_inserter(data), 3);
    encode('1')('2')('3');
    try {
        encode('4');
        BOOST_FAIL("Expected exception");
    } catch (...) {}            // FIXME aconway 2008-04-03: catch proper exception
    BOOST_CHECK_EQUAL(data, "123");
}

QPID_AUTO_TEST_CASE(testScopedLimit) {
    typedef Codec::Encoder<back_insert_iterator<string> > Encoder;
    string data;
    Encoder encode(back_inserter(data), 10);
    encode(Str8("123"));        // 4 bytes
    {
        Encoder::ScopedLimit l(encode, 3);
        encode('a')('b')('c');
        try {
            encode('d');
            BOOST_FAIL("Expected exception");
        } catch(...) {}         // FIXME aconway 2008-04-03: catch proper exception
    }
    BOOST_CHECK_EQUAL(data, "\003123abc");
    encode('x')('y')('z');
    try {
        encode('!');
        BOOST_FAIL("Expected exception");
    } catch(...) {}         // FIXME aconway 2008-04-03: catch proper exception
    BOOST_CHECK_EQUAL(data.size(), 10u);
}

// Assign test values to the various types.
void testValue(bool& b) { b = true; }
void testValue(Bit&) { }
template <class T> typename boost::enable_if<boost::is_arithmetic<T> >::type testValue(T& n) { n=42; }
void testValue(CharUtf32& c) { c = 43; }
void testValue(long long& l) { l = 0x012345; }
void testValue(Datetime& dt) { dt = qpid::sys::now(); }
void testValue(Uuid& uuid) { uuid=Uuid(true); }
template <class E, class M> void testValue(Decimal<E,M>& d) { d.exponent=2; d.mantissa=0x1122; }
void testValue(SequenceNo& s) { s = 42; }
template <size_t N> void testValue(Bin<N>& a) { a.assign(42); }
template <class T, class S, int Unique> void testValue(SerializableString<T, S, Unique>& s) {
    char msg[]="foobar";
    s.assign(msg, msg+sizeof(msg));
}
void testValue(Str16& s) { s = "the quick brown fox jumped over the lazy dog"; }
void testValue(Str8& s) { s = "foobar"; }
void testValue(Map& m) { m["s"] = Str8("foobar"); m["b"] = true; m["c"] = uint16_t(42); }

//typedef mpl::vector<Str8, Str16>::type TestTypes;
/*BOOST_AUTO_TEST_CASE_TEMPLATE(testEncodeDecode, T, AllTypes)
{
    string data;
    T t;
    testValue(t);
    Codec::encode(std::back_inserter(data))(t);

    BOOST_CHECK_EQUAL(Codec::size(t), data.size());

    T t2;
    Codec::decode(data.begin())(t2);
    BOOST_CHECK_EQUAL(t,t2);
}
*/

struct TestMe {
    bool encoded, decoded;
    char value;
    TestMe(char v) : encoded(), decoded(), value(v) {}
    template <class S> void encode(S& s) const {
        const_cast<TestMe*>(this)->encoded=true; s(value);
    }
    template <class S> void decode(S& s) { decoded=true; s(value); }
    template <class S> void serialize(S& s) { s.split(*this); }
};

QPID_AUTO_TEST_CASE(testSplit) {
    string data;
    TestMe t1('x');
    Codec::encode(std::back_inserter(data))(t1);
    BOOST_CHECK(t1.encoded);
    BOOST_CHECK(!t1.decoded);
    BOOST_CHECK_EQUAL(data, "x");

    TestMe t2('y');
    Codec::decode(data.begin())(t2);
    BOOST_CHECK(!t2.encoded);
    BOOST_CHECK(t2.decoded);
    BOOST_CHECK_EQUAL(t2.value, 'x');
}

QPID_AUTO_TEST_CASE(testControlEncodeDecode) {
    string data;
    Control::Holder h(in_place<connection::Tune>(1,2,3,4));
    Codec::encode(std::back_inserter(data))(h);
    
    BOOST_CHECK_EQUAL(data.size(), Codec::size(h));

    Codec::Decoder<string::iterator> decode(data.begin());
    Control::Holder h2;
    decode(h2);

    BOOST_REQUIRE(h2.get());
    BOOST_CHECK_EQUAL(h2.get()->getClassCode(), connection::CODE);
    BOOST_CHECK_EQUAL(h2.get()->getCode(), uint8_t(connection::Tune::CODE));
    connection::Tune& tune=static_cast<connection::Tune&>(*h2.get());
    BOOST_CHECK_EQUAL(tune.channelMax, 1u);
    BOOST_CHECK_EQUAL(tune.maxFrameSize, 2u);
    BOOST_CHECK_EQUAL(tune.heartbeatMin, 3u);
    BOOST_CHECK_EQUAL(tune.heartbeatMax, 4u);
}

QPID_AUTO_TEST_CASE(testStruct32) {
    message::DeliveryProperties dp;
    dp.priority=message::MEDIUM;
    dp.routingKey="foo";
    Struct32 s(dp);
    string data;
    Codec::encode(back_inserter(data))(s);

    uint32_t structSize;        // Starts with size
    Codec::decode(data.begin())(structSize);
    BOOST_CHECK_EQUAL(structSize, Codec::size(dp) + 2);  // +2 for code
    BOOST_CHECK_EQUAL(structSize, data.size()-4);        // encoded body
    
    BOOST_CHECK_EQUAL(data.size(), Codec::size(s));
    Struct32 s2;
    Codec::decode(data.begin())(s2);
    message::DeliveryProperties* dp2 = s2.getIf<message::DeliveryProperties>();
    BOOST_REQUIRE(dp2);
    BOOST_CHECK_EQUAL(dp2->priority, message::MEDIUM);
    BOOST_CHECK_EQUAL(dp2->routingKey, "foo");
}

QPID_AUTO_TEST_CASE(testStruct32Unknown) {
    // Verify we can recode an unknown struct unchanged.
    Struct32 s;
    string data;
    Codec::encode(back_inserter(data))(uint32_t(10));
    data.append(10, 'X');
    Codec::decode(data.begin())(s);
    string data2;
    Codec::encode(back_inserter(data2))(s);
    BOOST_CHECK_EQUAL(data.size(), data2.size());
    BOOST_CHECK_EQUAL(data, data2);
}

struct DummyPacked {
    static const uint8_t PACK=1;
    boost::optional<char> i, j;
    char k;
    Bit l,m;
    DummyPacked(char a=0, char b=0, char c=0) : i(a), j(b), k(c), l(), m() {}
    template <class S> void serialize(S& s) { s(i)(j)(k)(l)(m); }
};

Packer<DummyPacked> serializable(DummyPacked& d) { return Packer<DummyPacked>(d); }

QPID_AUTO_TEST_CASE(testPackBits) {
    DummyPacked d('a','b','c');
    BOOST_CHECK_EQUAL(packBits(d), 7u);
    d.j = boost::none;
    BOOST_CHECK_EQUAL(packBits(d), 5u);
    d.m = true;
    BOOST_CHECK_EQUAL(packBits(d), 0x15u);
}


QPID_AUTO_TEST_CASE(testPacked) {
    string data;

    Codec::encode(back_inserter(data))('a')(boost::optional<char>('b'))(boost::optional<char>())('c');
    BOOST_CHECK_EQUAL(data, "abc");
    data.clear();
    
    DummyPacked dummy('a','b','c');

    Codec::encode(back_inserter(data))(dummy);
    BOOST_CHECK_EQUAL(data.size(), 4u);
    BOOST_CHECK_EQUAL(data, string("\007abc"));
    data.clear();

    dummy.i = boost::none;
    Codec::encode(back_inserter(data))(dummy);
    BOOST_CHECK_EQUAL(data, string("\6bc"));
    data.clear();

    const char* missing = "\5xy";
    Codec::decode(missing)(dummy);
    BOOST_CHECK(dummy.i);
    BOOST_CHECK_EQUAL(*dummy.i, 'x');
    BOOST_CHECK(!dummy.j);
    BOOST_CHECK_EQUAL(dummy.k, 'y');
}

QPID_AUTO_TEST_CASE(testUnitControl) {
    string data;
    Control::Holder h(in_place<connection::Tune>(1,2,3,4));
    Codec::encode(std::back_inserter(data))(h);

    Unit unit(FrameHeader(FIRST_FRAME|LAST_FRAME, CONTROL));
    Codec::decode(data.begin())(unit);

    BOOST_REQUIRE(unit.get<ControlHolder>());

    string data2;
    Codec::encode(back_inserter(data2))(unit);
    
    BOOST_CHECK_EQUAL(data, data2);
}

QPID_AUTO_TEST_CASE(testArray) {
    ArrayDomain<char> a;
    a.resize(3, 'x');
    string data;
    Codec::encode(back_inserter(data))(a);

    ArrayDomain<char> b;
    Codec::decode(data.begin())(b);
    BOOST_CHECK_EQUAL(b.size(), 3u);
    string data3;
    Codec::encode(back_inserter(data3))(a);
    BOOST_CHECK_EQUAL(data, data3);
    
    Array x;
    Codec::decode(data.begin())(x);
    BOOST_CHECK_EQUAL(x.size(), 3u);
    BOOST_CHECK_EQUAL(x[0].size(), 1u);
    BOOST_CHECK_EQUAL(*x[0].begin(), 'x');
    BOOST_CHECK_EQUAL(*x[2].begin(), 'x');

    string data2;
    Codec::encode(back_inserter(data2))(x);
    BOOST_CHECK_EQUAL(data,data2);
}

QPID_AUTO_TEST_CASE(testStruct) {
    string data;

    message::DeliveryProperties dp;
    BOOST_CHECK(!dp.discardUnroutable);
    dp.immediate = true;
    dp.redelivered = false;
    dp.priority = message::MEDIUM;
    dp.exchange = "foo";

    Codec::encode(back_inserter(data))(dp);
    // Skip 4 bytes size, little-endian decode for pack bits.
    uint16_t encodedBits=uint8_t(data[5]);
    encodedBits <<= 8;
    encodedBits += uint8_t(data[4]);
    BOOST_CHECK_EQUAL(encodedBits, packBits(dp));
        
    data.clear();
    Struct32 h(dp);
    Codec::encode(back_inserter(data))(h);    

    Struct32 h2;
    Codec::decode(data.begin())(h2);
    BOOST_CHECK_EQUAL(h2.getClassCode(), Uint8(message::DeliveryProperties::CLASS_CODE));
    BOOST_CHECK_EQUAL(h2.getCode(), Uint8(message::DeliveryProperties::CODE));
    message::DeliveryProperties* dp2 =
        dynamic_cast<message::DeliveryProperties*>(h2.get());
    BOOST_CHECK(dp2);
    BOOST_CHECK(!dp2->discardUnroutable);
    BOOST_CHECK(dp2->immediate);
    BOOST_CHECK(!dp2->redelivered);
    BOOST_CHECK_EQUAL(dp2->priority, message::MEDIUM);
    BOOST_CHECK_EQUAL(dp2->exchange, "foo");
}

struct RecodeUnit {
    template <class T>
    void operator() (const T& t) {
        BOOST_MESSAGE(BOOST_CURRENT_FUNCTION  << " called with: " << t);
        using qpid::framing::Buffer;
        using qpid::framing::AMQFrame;

        session::Header sh;
        BOOST_CHECK_EQUAL(Codec::size(sh), 2u);

        // Encode unit.
        Unit u(t);
        string data;
        Codec::encode(back_inserter(data))(u.getHeader())(u);
        data.push_back(char(0xCE)); // Preview end-of-frame

        // Decode AMQFrame
        Buffer buf(&data[0], data.size());
        AMQFrame f;
        f.decode(buf);
        BOOST_MESSAGE("AMQFrame decoded: " << f);
        // Encode AMQFrame
        string data2(f.size(), ' ');
        Buffer buf2(&data2[0], data.size());
        f.encode(buf2);

        // Verify encoded by unit == encoded by AMQFrame
        BOOST_CHECK_MESSAGE(data == data2, BOOST_CURRENT_FUNCTION);

        // Decode unit
        // FIXME aconway 2008-04-15: must set limit to decode a header.
        Codec::Decoder<string::iterator> decode(data2.begin(), data2.size()-1);

        FrameHeader h;
        decode(h);
        BOOST_CHECK_EQUAL(u.getHeader(), h);
        Unit u2(h);
        decode(u2);

        // Re-encode unit
        string data3;
        Codec::encode(back_inserter(data3))(u2.getHeader())(u2);        
        data3.push_back(char(0xCE)); // Preview end-of-frame

        BOOST_CHECK_MESSAGE(data3 == data2, BOOST_CURRENT_FUNCTION);
    }
};

QPID_AUTO_TEST_CASE(testSerializeAllSegmentTypes) {
    RecodeUnit recode;
    allSegmentTypes(recode);
}

QPID_AUTO_TEST_SUITE_END()