]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testUriScheme.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / tests / testUriScheme.cc
1 /*
2 * Copyright (C) 1996-2020 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 #if 0
21 /*
22 * We should be able to make an HttpRequestMethod straight from a string.
23 */
24 void
25 testHttpRequestMethod::testConstructCharStart()
26 {
27 /* parse an empty string -> METHOD_NONE */
28 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL));
29 /* parsing a literal should work */
30 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
31 }
32
33 /*
34 * We can also parse precise ranges of characters
35 */
36 void
37 testHttpRequestMethod::testConstructCharStartEnd()
38 {
39 char const * buffer;
40 /* parse an empty string -> METHOD_NONE */
41 CPPUNIT_ASSERT(METHOD_NONE == HttpRequestMethod(NULL, NULL));
42 /* parsing a literal should work */
43 CPPUNIT_ASSERT(METHOD_GET == HttpRequestMethod("GET", NULL));
44 /* parsing with an explicit end should work */
45 buffer = "POSTPLUS";
46 CPPUNIT_ASSERT(METHOD_POST == HttpRequestMethod(buffer, buffer + 4));
47 }
48
49 #endif
50
51 /*
52 * we should be able to assign a protocol_t to a AnyP::UriScheme for ease
53 * of code conversion
54 */
55 void
56 testUriScheme::testAssignFromprotocol_t()
57 {
58 AnyP::UriScheme empty_scheme;
59 AnyP::UriScheme scheme;
60 scheme = AnyP::PROTO_NONE;
61 CPPUNIT_ASSERT_EQUAL(empty_scheme, scheme);
62
63 AnyP::UriScheme https_scheme(AnyP::PROTO_HTTPS);
64 scheme = AnyP::PROTO_HTTPS;
65 CPPUNIT_ASSERT_EQUAL(https_scheme, scheme);
66 }
67
68 /*
69 * We should be able to get a protocol_t from a AnyP::UriScheme for ease
70 * of migration
71 */
72 void
73 testUriScheme::testCastToprotocol_t()
74 {
75 /* explicit cast */
76 AnyP::ProtocolType protocol = static_cast<AnyP::ProtocolType>(AnyP::UriScheme());
77 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_NONE, protocol);
78 /* and implicit */
79 protocol = AnyP::UriScheme(AnyP::PROTO_HTTP);
80 CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, protocol);
81 }
82
83 /*
84 * a default constructed AnyP::UriScheme is == AnyP::PROTO_NONE
85 */
86 void
87 testUriScheme::testDefaultConstructor()
88 {
89 AnyP::UriScheme lhs;
90 AnyP::UriScheme rhs(AnyP::PROTO_NONE);
91 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
92 }
93
94 /*
95 * we should be able to construct a AnyP::UriScheme from the old 'protocol_t' enum.
96 */
97 void
98 testUriScheme::testConstructprotocol_t()
99 {
100 AnyP::UriScheme lhs_none(AnyP::PROTO_NONE), rhs_none(AnyP::PROTO_NONE);
101 CPPUNIT_ASSERT_EQUAL(lhs_none, rhs_none);
102
103 AnyP::UriScheme lhs_cacheobj(AnyP::PROTO_CACHE_OBJECT), rhs_cacheobj(AnyP::PROTO_CACHE_OBJECT);
104 CPPUNIT_ASSERT_EQUAL(lhs_cacheobj, rhs_cacheobj);
105 CPPUNIT_ASSERT(lhs_none != rhs_cacheobj);
106 }
107
108 /*
109 * we should be able to get a char const * version of the method.
110 */
111 void
112 testUriScheme::testC_str()
113 {
114 SBuf lhs("wais");
115 AnyP::UriScheme wais(AnyP::PROTO_WAIS);
116 SBuf rhs(wais.image());
117 CPPUNIT_ASSERT_EQUAL(lhs, rhs);
118 }
119
120 /*
121 * a AnyP::UriScheme replaces protocol_t, so we should be able to test for equality on
122 * either the left or right hand side seamlessly.
123 */
124 void
125 testUriScheme::testEqualprotocol_t()
126 {
127 CPPUNIT_ASSERT(AnyP::UriScheme() == AnyP::PROTO_NONE);
128 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_WAIS) == AnyP::PROTO_HTTP));
129 CPPUNIT_ASSERT(AnyP::PROTO_HTTP == AnyP::UriScheme(AnyP::PROTO_HTTP));
130 CPPUNIT_ASSERT(not (AnyP::PROTO_CACHE_OBJECT == AnyP::UriScheme(AnyP::PROTO_HTTP)));
131 }
132
133 /*
134 * a AnyP::UriScheme should testable for inequality with a protocol_t.
135 */
136 void
137 testUriScheme::testNotEqualprotocol_t()
138 {
139 CPPUNIT_ASSERT(AnyP::UriScheme(AnyP::PROTO_NONE) != AnyP::PROTO_HTTP);
140 CPPUNIT_ASSERT(not (AnyP::UriScheme(AnyP::PROTO_HTTP) != AnyP::PROTO_HTTP));
141 CPPUNIT_ASSERT(AnyP::PROTO_NONE != AnyP::UriScheme(AnyP::PROTO_HTTP));
142 CPPUNIT_ASSERT(not (AnyP::PROTO_WAIS != AnyP::UriScheme(AnyP::PROTO_WAIS)));
143 }
144
145 /*
146 * we should be able to send it to a stream and get the normalised version
147 */
148 void
149 testUriScheme::testStream()
150 {
151 std::ostringstream buffer;
152 buffer << AnyP::UriScheme(AnyP::PROTO_HTTP);
153 SBuf http_str("http");
154 SBuf from_buf(buffer.str());
155 CPPUNIT_ASSERT_EQUAL(http_str, from_buf);
156 }
157