summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/Uuid.cpp
blob: 6d6c07169feea105ff7de755d78e3bd8ad7670e8 (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
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed 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 "qpid/framing/Uuid.h"
#include "qpid/framing/Buffer.h"
#include "qpid/messaging/Uuid.h"
#include "qpid/sys/alloca.h"

#include "unit_test.h"

#include <set>

namespace qpid {
namespace tests {

QPID_AUTO_TEST_SUITE(UuidTestSuite)

using namespace std;
using namespace qpid::framing;

struct UniqueSet : public std::set<Uuid> {
    void operator()(const Uuid& uuid) {
        BOOST_REQUIRE(find(uuid) == end());
        insert(uuid);
    }
};

QPID_AUTO_TEST_CASE(testUuidCtor) {
    // Uniqueness
    boost::array<Uuid,1000> uuids;
    for_each(uuids.begin(), uuids.end(), mem_fun_ref(&Uuid::generate));
    UniqueSet unique;
    for_each(uuids.begin(), uuids.end(), unique);
}

boost::array<uint8_t, 16>  sample =  {{'\x1b', '\x4e', '\x28', '\xba', '\x2f', '\xa1', '\x11', '\xd2', '\x88', '\x3f', '\xb9', '\xa7', '\x61', '\xbd', '\xe3', '\xfb'}};
const string sampleStr("1b4e28ba-2fa1-11d2-883f-b9a761bde3fb");

QPID_AUTO_TEST_CASE(testUuidIstream) {
    Uuid uuid;
    istringstream in(sampleStr);
    in >> uuid;
    BOOST_CHECK(!in.fail());
    BOOST_CHECK(uuid == sample);
}

QPID_AUTO_TEST_CASE(testUuidOstream) {
    Uuid uuid(sample.c_array());
    ostringstream out;
    out << uuid;
    BOOST_CHECK(out.good());
    BOOST_CHECK_EQUAL(out.str(), sampleStr);
}

QPID_AUTO_TEST_CASE(testUuidEncodeDecode) {
    char* buff = static_cast<char*>(::alloca(Uuid::size()));
    Buffer wbuf(buff, Uuid::size());
    Uuid uuid(sample.c_array());
    uuid.encode(wbuf);

    Buffer rbuf(buff, Uuid::size());
    Uuid decoded;
    decoded.decode(rbuf);
    BOOST_CHECK_EQUAL(string(sample.begin(), sample.end()),
                      string(decoded.begin(), decoded.end()));
}

QPID_AUTO_TEST_CASE(testMessagingUuid)
{
    //tests for the Uuid class in the messaging namespace (introduced
    //to avoid pulling in dependencies from framing)
    messaging::Uuid a;
    messaging::Uuid b(true);
    messaging::Uuid c(true);
    messaging::Uuid d(b);
    messaging::Uuid e;
    e = c;

    BOOST_CHECK(!a);
    BOOST_CHECK(b);

    BOOST_CHECK(a != b);
    BOOST_CHECK(b != c);

    BOOST_CHECK_EQUAL(b, d);
    BOOST_CHECK_EQUAL(c, e);

    ostringstream out;
    out << b;    
    istringstream in(out.str());
    in >> a;
    BOOST_CHECK(!in.fail());
    BOOST_CHECK_EQUAL(a, b);
}

QPID_AUTO_TEST_SUITE_END()

}} // namespace qpid::tests