testRandomUuid.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "base/RandomUuid.h"
11#include "compat/cppunit.h"
12#include "sbuf/SBuf.h"
13#include "sbuf/Stream.h"
14#include "unitTestMain.h"
15
16#include <map>
17#include <set>
18
19class TestRandomUuid: public CPPUNIT_NS::TestFixture
20{
27
28protected:
29 void testUniqueness();
30 void testSerialization();
32 void testInvalidIds();
33};
34
36
37typedef std::map<SBuf, RandomUuid::Serialized> RandomIds;
38
39// Generated by https://www.uuidgenerator.net/version4
40// binary representation of the generated UUID in network byte order
41static const RandomIds ExternalIds {
42 { SBuf("bd1b1c07-f7fa-428a-b019-7e390133b0e5"), {{ 0xbd, 0x1b, 0x1c, 0x07, 0xf7, 0xfa, 0x42, 0x8a, 0xb0, 0x19, 0x7e, 0x39, 0x01, 0x33, 0xb0, 0xe5 }} },
43 { SBuf("f63ccd5a-9d25-41a5-a36c-a7b0c6b5c678"), {{ 0xf6, 0x3c, 0xcd, 0x5a, 0x9d, 0x25, 0x41, 0xa5, 0xa3, 0x6c, 0xa7, 0xb0, 0xc6, 0xb5, 0xc6, 0x78 }} },
44 { SBuf("9c8363ac-9c62-44e9-941f-86b7edc25dc7"), {{ 0x9c, 0x83, 0x63, 0xac, 0x9c, 0x62, 0x44, 0xe9, 0x94, 0x1f, 0x86, 0xb7, 0xed, 0xc2, 0x5d, 0xc7 }} }
45};
46
47static const RandomIds InvalidIds {
48 { SBuf("00000000-0000-0000-0000-000000000000"), {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }} },
49 { SBuf("ffffffff-ffff-ffff-ffff-ffffffffffff"), {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }} }
50};
51
52void
54{
55 std::set<SBuf> uniqueIds;
56 while (uniqueIds.size() < 1000) {
57 const auto inserted = uniqueIds.insert(ToSBuf(RandomUuid())).second;
58 CPPUNIT_ASSERT_MESSAGE("few generated UUIDs are unique", inserted);
59 }
60}
61
62void
64{
65 RandomUuid uuid;
66 CPPUNIT_ASSERT_MESSAGE("original and deserialized UUIDs are equal", uuid == RandomUuid(uuid.serialize()));
67}
68
69void
71{
72 for (const auto &id: ExternalIds) {
73 CPPUNIT_ASSERT_MESSAGE("UUID text representation matches the expected one", ToSBuf(RandomUuid(id.second)) == id.first);
74 }
75}
76
77void
79{
80 for (const auto &id: InvalidIds) {
81 try {
82 RandomUuid uuid(id.second);
83 std::cerr << std::endl
84 << "FAIL: " << id.first
85 << Debug::Extra << "error: should be rejected" << std::endl;
86 } catch (const TextException &e) {
87 continue; // success, caught a malformed UUID
88 }
89 CPPUNIT_FAIL("failed to reject an invalid UUID");
90 }
91}
92
93int
94main(int argc, char *argv[])
95{
96 return TestProgram().run(argc, argv);
97}
98
static std::ostream & Extra(std::ostream &)
Definition: debug.cc:1313
Serialized serialize() const
exports UUID value; suitable for long-term storage
Definition: RandomUuid.cc:79
Definition: SBuf.h:94
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST(testSerialization)
void testSerialization()
CPPUNIT_TEST(testUniqueness)
CPPUNIT_TEST(testInvalidIds)
CPPUNIT_TEST(testTextRepresentation)
void testTextRepresentation()
CPPUNIT_TEST_SUITE(TestRandomUuid)
an std::runtime_error with thrower location info
Definition: TextException.h:21
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:63
int main(int argc, char *argv[])
static const RandomIds InvalidIds
CPPUNIT_TEST_SUITE_REGISTRATION(TestRandomUuid)
static const RandomIds ExternalIds
std::map< SBuf, RandomUuid::Serialized > RandomIds

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors