]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testUriScheme.cc
cb2464b3df2eecb0e0ddac533ad4e8ee13017cd6
[thirdparty/squid.git] / src / tests / testUriScheme.cc
1 /*
2 * Copyright (C) 1996-2022 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
11 #include <cppunit/TestAssert.h>
12
13 #include "anyp/UriScheme.h"
14 #include "tests/testUriScheme.h"
15
16 #include <sstream>
17
18 CPPUNIT_TEST_SUITE_REGISTRATION( testUriScheme );
19
20 /*
21 * we should be able to assign a protocol_t to a AnyP::UriScheme for ease
22 * of code conversion
23 */
24 void
25 testUriScheme::testAssignFromprotocol_t()
26 {
27 AnyP::UriScheme empty_scheme;
28 AnyP::UriScheme scheme;
29 scheme = AnyP::PROTO_NONE;
30 CPPUNIT_ASSERT_EQUAL(empty_scheme, scheme);
31
32 AnyP::UriScheme https_scheme(AnyP::PROTO_HTTPS);
33 scheme = AnyP::PROTO_HTTPS;
34 CPPUNIT_ASSERT_EQUAL(https_scheme, scheme);
35 }
36
37 /*
38 * We should be able to get a protocol_t from a AnyP::UriScheme for ease
39 * of migration
40 */
41 void
42 testUriScheme::testCastToprotocol_t()
43 {
44 /* explicit cast */
45 AnyP::ProtocolType protocol = static_cast<AnyP::ProtocolType>(AnyP::UriScheme());
46 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, protocol);
47 /* and implicit */
48 protocol = AnyP::UriScheme(AnyP::PROTO_HTTP);
49 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, protocol);
50 }
51
52 /*
53 * a default constructed AnyP::UriScheme is == AnyP::PROTO_NONE
54 */
55 void
56 testUriScheme::testDefaultConstructor()
57 {
58 AnyP::UriScheme lhs;
59 AnyP::UriScheme rhs(AnyP::PROTO_NONE);
60 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
61 }
62
63 /*
64 * we should be able to construct a AnyP::UriScheme from the old 'protocol_t' enum.
65 */
66 void
67 testUriScheme::testConstructprotocol_t()
68 {
69 AnyP::UriScheme lhs_none(AnyP::PROTO_NONE), rhs_none(AnyP::PROTO_NONE);
70 CPPUNIT_ASSERT_EQUAL(lhs_none, rhs_none);
71
72 AnyP::UriScheme lhs_cacheobj(AnyP::PROTO_CACHE_OBJECT), rhs_cacheobj(AnyP::PROTO_CACHE_OBJECT);
73 CPPUNIT_ASSERT_EQUAL(lhs_cacheobj, rhs_cacheobj);
74 CPPUNIT_ASSERT(lhs_none != rhs_cacheobj);
75 }
76
77 /*
78 * we should be able to get a char const * version of the method.
79 */
80 void
81 testUriScheme::testC_str()
82 {
83 SBuf lhs("wais");
84 AnyP::UriScheme wais(AnyP::PROTO_WAIS);
85 SBuf rhs(wais.image());
86 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
87 }
88
89 /*
90 * a AnyP::UriScheme replaces protocol_t, so we should be able to test for equality on
91 * either the left or right hand side seamlessly.
92 */
93 void
94 testUriScheme::testEqualprotocol_t()
95 {
96 CPPUNIT_ASSERT(AnyP::UriScheme() == AnyP::PROTO_NONE);
97 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_WAIS) == AnyP::PROTO_HTTP));
98 CPPUNIT_ASSERT(AnyP::PROTO_HTTP == AnyP::UriScheme(AnyP::PROTO_HTTP));
99 CPPUNIT_ASSERT(not (AnyP::PROTO_CACHE_OBJECT == AnyP::UriScheme(AnyP::PROTO_HTTP)));
100 }
101
102 /*
103 * a AnyP::UriScheme should testable for inequality with a protocol_t.
104 */
105 void
106 testUriScheme::testNotEqualprotocol_t()
107 {
108 CPPUNIT_ASSERT(AnyP::UriScheme(AnyP::PROTO_NONE) != AnyP::PROTO_HTTP);
109 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_HTTP) != AnyP::PROTO_HTTP));
110 CPPUNIT_ASSERT(AnyP::PROTO_NONE != AnyP::UriScheme(AnyP::PROTO_HTTP));
111 CPPUNIT_ASSERT(not (AnyP::PROTO_WAIS != AnyP::UriScheme(AnyP::PROTO_WAIS)));
112 }
113
114 /*
115 * we should be able to send it to a stream and get the normalised version
116 */
117 void
118 testUriScheme::testStream()
119 {
120 std::ostringstream buffer;
121 buffer << AnyP::UriScheme(AnyP::PROTO_HTTP);
122 SBuf http_str("http");
123 SBuf from_buf(buffer.str());
124 CPPUNIT_ASSERT_EQUAL(http_str, from_buf);
125 }
126
127 void
128 testUriScheme::setUp()
129 {
130 Mem::Init();
131 AnyP::UriScheme::Init();
132 }
133