tests: enable sasl
[awesomized/libmemcached] / libtest / memcached.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "libtest/yatlcon.h"
38
39 #include "libtest/common.h"
40
41 #include <cassert>
42 #include <cerrno>
43 #include <cstdio>
44 #include <cstdlib>
45 #include <cstring>
46 #include <iostream>
47 #include <signal.h>
48 #include <sys/types.h>
49 #include <sys/wait.h>
50 #include <unistd.h>
51
52 #include <libtest/server.h>
53 #include <libtest/wait.h>
54
55 #include <libtest/memcached.h>
56
57 #ifndef __INTEL_COMPILER
58 #pragma GCC diagnostic ignored "-Wold-style-cast"
59 #endif
60
61 namespace libtest {
62
63 class Memcached : public libtest::Server
64 {
65 std::string _username;
66 std::string _password;
67
68 public:
69 Memcached(const std::string& host_arg,
70 const in_port_t port_arg,
71 const bool is_socket_arg,
72 const std::string& username_arg,
73 const std::string& password_arg) :
74 libtest::Server(host_arg, port_arg,
75 memcached_binary(), false, is_socket_arg),
76 _username(username_arg),
77 _password(password_arg)
78 { }
79
80 Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) :
81 libtest::Server(host_arg, port_arg,
82 memcached_binary(), false, is_socket_arg)
83 {
84 set_pid_file();
85 }
86
87 virtual const char *sasl() const
88 {
89 return "-S";
90 }
91
92 bool is_sasl() const
93 {
94 return _username.size() && _password.size();
95 }
96
97 const std::string& password() const
98 {
99 return _password;
100 }
101
102 const std::string& username() const
103 {
104 return _username;
105 }
106
107 bool wait_for_pidfile() const
108 {
109 Wait wait(pid(), 4);
110
111 return wait.successful();
112 }
113
114 bool ping()
115 {
116 if (out_of_ban_killed())
117 {
118 return false;
119 }
120
121 if (is_socket() or is_sasl())
122 {
123 return _app.check();
124 }
125
126 SimpleClient client(_hostname, _port);
127
128 std::string response;
129 return client.send_message("version", response);
130 }
131
132 const char *name()
133 {
134 return "memcached";
135 };
136
137 const char *executable()
138 {
139 return memcached_binary();
140 }
141
142 bool is_libtool()
143 {
144 return false;
145 }
146
147 virtual void pid_file_option(Application& app, const std::string& arg)
148 {
149 if (arg.empty() == false)
150 {
151 app.add_option("-P", arg);
152 }
153 }
154
155 const char *socket_file_option() const
156 {
157 return "-s ";
158 }
159
160 virtual void port_option(Application& app, in_port_t arg)
161 {
162 char buffer[30];
163 snprintf(buffer, sizeof(buffer), "%d", int(arg));
164 app.add_option("-p", buffer);
165
166 if(!is_sasl())
167 {
168 app.add_option("-U", buffer);
169 }
170 }
171
172 bool has_port_option() const
173 {
174 return true;
175 }
176
177 bool has_socket_file_option() const
178 {
179 return has_socket();
180 }
181
182 void socket_file_option(Application& app, const std::string& socket_arg)
183 {
184 if (socket_arg.empty() == false)
185 {
186 app.add_option("-s", socket_arg);
187 }
188 }
189
190 bool broken_socket_cleanup()
191 {
192 return true;
193 }
194
195 // Memcached's pidfile is broken
196 bool broken_pid_file()
197 {
198 return true;
199 }
200
201 bool build();
202 };
203
204
205 #include <sstream>
206
207 bool Memcached::build()
208 {
209 if (getuid() == 0 or geteuid() == 0)
210 {
211 add_option("-u", "root");
212 }
213
214 add_option("-l", "localhost");
215 #ifdef __APPLE__
216 #else
217 add_option("-m", "128");
218 add_option("-M");
219 #endif
220
221 if (is_sasl())
222 {
223 add_option(sasl());
224 }
225
226 //add_option("-vvv");
227
228 return true;
229 }
230
231 libtest::Server *build_memcached(const std::string& hostname, const in_port_t try_port)
232 {
233 if (has_memcached())
234 {
235 return new Memcached(hostname, try_port, false);
236 }
237
238 return NULL;
239 }
240
241 libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port)
242 {
243 if (has_memcached())
244 {
245 return new Memcached(socket_file, try_port, true);
246 }
247
248 return NULL;
249 }
250
251 libtest::Server *build_memcached_sasl(const std::string &hostname, const in_port_t try_port, const std::string &username, const std::string &password)
252 {
253 if (has_memcached())
254 {
255 return new Memcached(hostname, try_port, false, username, password);
256 }
257
258 return NULL;
259 }
260
261 } // namespace libtest