ci: sr.ht: fix git ref
[m6w6/libmemcached] / test / fixtures / parser.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include <cstring>
19 #include <functional>
20 #include <utility>
21
22 using namespace std;
23
24 struct c_string {
25 const char *c_str;
26 size_t size;
27
28 c_string(const char *s, size_t l) noexcept
29 : c_str{s}
30 , size{l} {}
31 bool operator==(const c_string &cs) const noexcept { return !strcmp(c_str, cs.c_str); }
32 bool operator==(const char *p) const noexcept { return !strcmp(c_str, p); }
33 };
34
35 static c_string null_c_string{nullptr, 0};
36
37 struct test_case {
38 using setting_key_t = variant<memcached_flag_t, memcached_behavior_t>;
39 using setting_t = pair<setting_key_t, uint64_t>;
40 using result_t = variant<c_string, setting_t>;
41 c_string option;
42 result_t result;
43
44 test_case(c_string o, c_string r) noexcept
45 : option{o}
46 , result{r} {}
47 test_case(const char *os, size_t ol, const char *rs, size_t rl) noexcept
48 : option{os, ol}
49 , result{c_string{rs, rl}} {}
50 test_case(const char *os, size_t ol, memcached_behavior_t b, uint64_t v) noexcept
51 : option{os, ol}
52 , result{setting_t{b, v}} {}
53 test_case(const char *os, size_t ol, memcached_flag_t f, uint64_t v) noexcept
54 : option{os, ol}
55 , result{setting_t{f, v}} {}
56 };
57
58 #define test_count(tca) (sizeof(tca) / sizeof(tca[0]))
59
60 struct test_group {
61 using check_func = function<void(memcached_st *, const test_case::result_t &)>;
62 const char *name;
63 check_func check;
64 test_case *tests;
65 size_t ntests;
66
67 test_group(const char *name_, check_func check_, test_case *tests_, size_t ntests_) noexcept
68 : name{name_}
69 , check{move(check_)}
70 , tests{tests_}
71 , ntests{ntests_} {}
72 };
73
74 static test_case host_string_tests[] = {
75 {S("--server=localhost"), S("localhost")},
76 {S("--server=10.0.2.1"), S("10.0.2.1")},
77 {S("--server=example.com"), S("example.com")},
78 {S("--server=localhost:30"), S("localhost")},
79 {S("--server=10.0.2.1:20"), S("10.0.2.1")},
80 {S("--server=example.com:1024"), S("example.com")},
81 {S("--server=10.0.2.1:30/?40"), S("10.0.2.1")},
82 {S("--server=example.com:1024/?30"), S("example.com")},
83 {S("--server=10.0.2.1/?20"), S("10.0.2.1")},
84 {S("--server=example.com/?10"), S("example.com")},
85 };
86
87 static test_group test_host_strings{
88 "host strings",
89 [](memcached_st *memc, const test_case::result_t &result) {
90 auto instance = memcached_server_instance_by_position(memc, 0);
91 REQUIRE(instance);
92 REQUIRE(get<c_string>(result) == memcached_server_name(instance));
93 },
94 host_string_tests, test_count(host_string_tests)};
95
96 static test_case bad_host_string_tests[] = {
97 {{S("-servers=localhost:11221,localhost:11222,localhost:11223,localhost:11224,localhost:"
98 "11225")},
99 null_c_string},
100 {{S("-- servers=a.example.com:81,localhost:82,b.example.com")}, null_c_string},
101 {{S("--servers=localhost:+80")}, null_c_string},
102 // all of the following should not fail IMO
103 {{S("--servers=localhost.com.")}, null_c_string},
104 {{S("--server=localhost.com.")}, null_c_string},
105 {{S("--server=localhost.com.:80")}, null_c_string},
106 };
107
108 static test_group test_bad_host_strings{
109 "bad host strings",
110 [](memcached_st *memc, const test_case::result_t &) { REQUIRE_FALSE(memc); },
111 bad_host_string_tests, test_count(bad_host_string_tests)};
112
113 static test_case behavior_tests[] = {
114 {S("--CONNECT-TIMEOUT=456"), MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT, 456},
115 {S("--IO-BYTES-WATERMARK=456"), MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK, 456},
116 {S("--IO-KEY-PREFETCH=456"), MEMCACHED_BEHAVIOR_IO_KEY_PREFETCH, 456},
117 {S("--IO-MSG-WATERMARK=456"), MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 456},
118 {S("--NUMBER-OF-REPLICAS=456"), MEMCACHED_BEHAVIOR_NUMBER_OF_REPLICAS, 456},
119 {S("--POLL-TIMEOUT=456"), MEMCACHED_BEHAVIOR_POLL_TIMEOUT, 456},
120 {S("--RCV-TIMEOUT=456"), MEMCACHED_BEHAVIOR_RCV_TIMEOUT, 456},
121 {S("--REMOVE-FAILED-SERVERS=3"), MEMCACHED_BEHAVIOR_REMOVE_FAILED_SERVERS, 1},
122 {S("--RETRY-TIMEOUT=456"), MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 456},
123 {S("--SND-TIMEOUT=456"), MEMCACHED_BEHAVIOR_SND_TIMEOUT, 456},
124 {S("--SOCKET-RECV-SIZE=456"), MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE, 456},
125 {S("--SOCKET-SEND-SIZE=456"), MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, 456},
126 {S("--BINARY-PROTOCOL"), MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1},
127 {S("--BUFFER-REQUESTS"), MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1},
128 {S("--NOREPLY"), MEMCACHED_BEHAVIOR_NOREPLY, 1},
129 {S("--RANDOMIZE-REPLICA-READ"), MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ, 1},
130 {S("--SORT-HOSTS"), MEMCACHED_BEHAVIOR_SORT_HOSTS, 1},
131 {S("--SUPPORT-CAS"), MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1},
132 {S("--TCP-NODELAY"), MEMCACHED_BEHAVIOR_TCP_NODELAY, 1},
133 {S("--TCP-KEEPALIVE"), MEMCACHED_BEHAVIOR_TCP_KEEPALIVE, 1},
134 {S("--TCP-KEEPIDLE"), MEMCACHED_BEHAVIOR_TCP_KEEPIDLE, 1},
135 {S("--USE-UDP"), MEMCACHED_BEHAVIOR_USE_UDP, 1},
136 {S("--VERIFY-KEY"), MEMCACHED_BEHAVIOR_VERIFY_KEY, 1},
137 {S("--DISTRIBUTION=consistent"), MEMCACHED_BEHAVIOR_DISTRIBUTION,
138 MEMCACHED_DISTRIBUTION_CONSISTENT},
139 {S("--DISTRIBUTION=consistent,CRC"), MEMCACHED_BEHAVIOR_DISTRIBUTION,
140 MEMCACHED_DISTRIBUTION_CONSISTENT},
141 {S("--DISTRIBUTION=consistent,MD5"), MEMCACHED_BEHAVIOR_DISTRIBUTION,
142 MEMCACHED_DISTRIBUTION_CONSISTENT},
143 {S("--DISTRIBUTION=consistent,JENKINS"), MEMCACHED_BEHAVIOR_KETAMA_HASH,
144 MEMCACHED_HASH_JENKINS},
145 {S("--DISTRIBUTION=random"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_RANDOM},
146 {S("--DISTRIBUTION=modula"), MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_MODULA},
147 {S("--HASH=CRC"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_CRC},
148 {S("--HASH=FNV1A_32"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1A_32},
149 {S("--HASH=FNV1_32"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_FNV1_32},
150 {S("--HASH=JENKINS"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_JENKINS},
151 {S("--HASH=MD5"), MEMCACHED_BEHAVIOR_HASH, MEMCACHED_HASH_MD5},
152
153 };
154
155 static test_group test_behaviors{
156 "behaviors",
157 [](memcached_st *memc, const test_case::result_t &result) {
158 auto setting = get<test_case::setting_t>(result);
159 REQUIRE(memc);
160 REQUIRE(setting.second
161 == memcached_behavior_get(memc, get<memcached_behavior_t>(setting.first)));
162 },
163 behavior_tests, test_count(behavior_tests)};
164
165 static test_case flag_tests[] = {
166 {S("--FETCH-VERSION"), MEMCACHED_FLAG_IS_FETCHING_VERSION, 1},
167 {S("--HASH-WITH-NAMESPACE"), MEMCACHED_FLAG_HASH_WITH_NAMESPACE, 1},
168 };
169
170 static test_group test_flags{
171 "flags",
172 [](memcached_st *memc, const test_case::result_t &result) {
173 auto setting = get<test_case::setting_t>(result);
174 REQUIRE(memc);
175 REQUIRE(setting.second == memcached_flag(*memc, get<memcached_flag_t>(setting.first)));
176 },
177 flag_tests, test_count(flag_tests)};
178
179 static test_case namespace_tests[] = {
180 {S("--NAMESPACE=foo"), S("foo")},
181 {S("--NAMESPACE=\"foo\""), S("foo")},
182 {S("--NAMESPACE=\"The quick brown fox jumps over the lazy dog\""),
183 S("The quick brown fox jumps over the lazy dog")},
184 };
185
186 static test_group test_namespace{"namespace",
187 [](memcached_st *memc, const test_case::result_t &result) {
188 auto ns = get<c_string>(result);
189 REQUIRE(memc);
190 REQUIRE(ns == memcached_get_namespace(*memc));
191 },
192 namespace_tests, test_count(namespace_tests)};
193
194 static test_group tests[] = {
195 test_host_strings,
196 test_bad_host_strings,
197 test_behaviors,
198 test_flags,
199 };