|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <cppunit/extensions/HelperMacros.h> |
| 20 | +#include "CppAssertHelper.h" |
| 21 | + |
| 22 | +#include <sys/socket.h> |
| 23 | +#include <unistd.h> |
| 24 | + |
| 25 | +#include <zookeeper.h> |
| 26 | + |
| 27 | +#include "Util.h" |
| 28 | +#include "WatchUtil.h" |
| 29 | + |
| 30 | +ZOOAPI int zoo_create2(zhandle_t *zh, const char *path, const char *value, |
| 31 | + int valuelen, const struct ACL_vector *acl, int mode, |
| 32 | + char *path_buffer, int path_buffer_len, struct Stat *stat); |
| 33 | + |
| 34 | +class Zookeeper_serverRequireClientSASL : public CPPUNIT_NS::TestFixture { |
| 35 | + CPPUNIT_TEST_SUITE(Zookeeper_serverRequireClientSASL); |
| 36 | +#ifdef THREADED |
| 37 | + CPPUNIT_TEST(testServerRequireClientSASL); |
| 38 | +#endif |
| 39 | + CPPUNIT_TEST_SUITE_END(); |
| 40 | + FILE *logfile; |
| 41 | + static const char hostPorts[]; |
| 42 | + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ |
| 43 | + watchctx_t *ctx = (watchctx_t*)v; |
| 44 | + |
| 45 | + if (state == ZOO_CONNECTED_STATE) { |
| 46 | + ctx->connected = true; |
| 47 | + } else { |
| 48 | + ctx->connected = false; |
| 49 | + } |
| 50 | + if (type != ZOO_SESSION_EVENT) { |
| 51 | + evt_t evt; |
| 52 | + evt.path = path; |
| 53 | + evt.type = type; |
| 54 | + ctx->putEvent(evt); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +public: |
| 59 | + Zookeeper_serverRequireClientSASL() { |
| 60 | + logfile = openlogfile("Zookeeper_serverRequireClientSASL"); |
| 61 | + } |
| 62 | + |
| 63 | + ~Zookeeper_serverRequireClientSASL() { |
| 64 | + if (logfile) { |
| 65 | + fflush(logfile); |
| 66 | + fclose(logfile); |
| 67 | + logfile = 0; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void setUp() { |
| 72 | + zoo_set_log_stream(logfile); |
| 73 | + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); |
| 74 | + stopServer(); |
| 75 | + } |
| 76 | + |
| 77 | + void tearDown() { |
| 78 | + startServer(); |
| 79 | + } |
| 80 | + |
| 81 | + void startServer() { |
| 82 | + char cmd[1024]; |
| 83 | + sprintf(cmd, "%s start", ZKSERVER_CMD); |
| 84 | + CPPUNIT_ASSERT(system(cmd) == 0); |
| 85 | + } |
| 86 | + |
| 87 | + void startServerRequireSASLAuth() { |
| 88 | + char cmd[1024]; |
| 89 | + sprintf(cmd, "%s startRequireSASLAuth", ZKSERVER_CMD); |
| 90 | + CPPUNIT_ASSERT(system(cmd) == 0); |
| 91 | + } |
| 92 | + |
| 93 | + void stopServer() { |
| 94 | + char cmd[1024]; |
| 95 | + sprintf(cmd, "%s stop", ZKSERVER_CMD); |
| 96 | + CPPUNIT_ASSERT(system(cmd) == 0); |
| 97 | + } |
| 98 | + |
| 99 | + void testServerRequireClientSASL() { |
| 100 | + startServerRequireSASLAuth(); |
| 101 | + |
| 102 | + watchctx_t ctx; |
| 103 | + int rc = 0; |
| 104 | + zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0); |
| 105 | + ctx.zh = zk; |
| 106 | + CPPUNIT_ASSERT(zk); |
| 107 | + |
| 108 | + char pathbuf[80]; |
| 109 | + struct Stat stat_a = {0}; |
| 110 | + |
| 111 | + rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0, |
| 112 | + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a); |
| 113 | + CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc); |
| 114 | + |
| 115 | + stopServer(); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +const char Zookeeper_serverRequireClientSASL::hostPorts[] = "127.0.0.1:22181"; |
| 120 | + |
| 121 | +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_serverRequireClientSASL); |
0 commit comments