TestReadOnlyClient.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <cppunit/extensions/HelperMacros.h>
  19. #include "CppAssertHelper.h"
  20. #include <sys/socket.h>
  21. #include <unistd.h>
  22. #include <zookeeper.h>
  23. #include "Util.h"
  24. #include "WatchUtil.h"
  25. #ifdef THREADED
  26. class Zookeeper_readOnly : public CPPUNIT_NS::TestFixture {
  27. CPPUNIT_TEST_SUITE(Zookeeper_readOnly);
  28. CPPUNIT_TEST(testReadOnly);
  29. #ifdef HAVE_OPENSSL_H
  30. CPPUNIT_TEST(testReadOnlyWithSSL);
  31. #endif
  32. CPPUNIT_TEST_SUITE_END();
  33. static void watcher(zhandle_t* zh, int type, int state,
  34. const char* path, void* v) {
  35. watchctx_t *ctx = (watchctx_t*)v;
  36. if (state==ZOO_CONNECTED_STATE || state==ZOO_READONLY_STATE) {
  37. ctx->connected = true;
  38. } else {
  39. ctx->connected = false;
  40. }
  41. if (type != ZOO_SESSION_EVENT) {
  42. evt_t evt;
  43. evt.path = path;
  44. evt.type = type;
  45. ctx->putEvent(evt);
  46. }
  47. }
  48. FILE *logfile;
  49. public:
  50. Zookeeper_readOnly() {
  51. logfile = openlogfile("Zookeeper_readOnly");
  52. }
  53. ~Zookeeper_readOnly() {
  54. if (logfile) {
  55. fflush(logfile);
  56. fclose(logfile);
  57. logfile = 0;
  58. }
  59. }
  60. void setUp() {
  61. zoo_set_log_stream(logfile);
  62. zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);
  63. stopServer();
  64. }
  65. void tearDown()
  66. {
  67. startServer();
  68. }
  69. void startServer() {
  70. char cmd[1024];
  71. sprintf(cmd, "%s start %s", ZKSERVER_CMD, "127.0.0.1:22181");
  72. CPPUNIT_ASSERT(system(cmd) == 0);
  73. }
  74. void stopServer() {
  75. char cmd[1024];
  76. sprintf(cmd, "%s stop %s", ZKSERVER_CMD, "127.0.0.1:22181");
  77. CPPUNIT_ASSERT(system(cmd) == 0);
  78. }
  79. void startReadOnly() {
  80. char cmd[1024];
  81. sprintf(cmd, "%s startCleanReadOnly", ZKSERVER_CMD);
  82. CPPUNIT_ASSERT(system(cmd) == 0);
  83. }
  84. void stopPeer() {
  85. char cmd[1024];
  86. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  87. CPPUNIT_ASSERT(system(cmd) == 0);
  88. }
  89. zhandle_t* connectReadOnly(const char *address, watchctx_t *watch) {
  90. zhandle_t* zh = zookeeper_init(address, watcher, 10000, NULL, watch, ZOO_READONLY);
  91. watch->zh = zh;
  92. CPPUNIT_ASSERT(zh != 0);
  93. sleep(1);
  94. return zh;
  95. }
  96. void assertCanRead(zhandle_t* zh, const char *znode_path) {
  97. int len = 1024;
  98. char buf[len];
  99. int res = zoo_get(zh, znode_path, 0, buf, &len, 0);
  100. CPPUNIT_ASSERT_EQUAL((int)ZOK, res);
  101. }
  102. void assertCanNotWrite(zhandle_t* zh, const char *znode_path) {
  103. char path[1024];
  104. char buf[1024];
  105. int res = zoo_create(zh, znode_path, buf, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  106. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, res);
  107. }
  108. void testReadOnly()
  109. {
  110. startReadOnly();
  111. watchctx_t watch;
  112. zhandle_t* zh = connectReadOnly("localhost:22181", &watch);
  113. assertCanRead(zh, "/");
  114. assertCanNotWrite(zh, "/test");
  115. stopPeer();
  116. }
  117. #ifdef HAVE_OPENSSL_H
  118. zhandle_t* connectReadOnlySSL(const char *address, const char *certs, watchctx_t *watch) {
  119. zhandle_t* zh = zookeeper_init_ssl(address, certs, watcher, 10000, NULL, watch, ZOO_READONLY);
  120. watch->zh = zh;
  121. CPPUNIT_ASSERT(zh != 0);
  122. sleep(1);
  123. return zh;
  124. }
  125. void testReadOnlyWithSSL() {
  126. startReadOnly();
  127. watchctx_t watch;
  128. zhandle_t* zh = connectReadOnlySSL("localhost:22281",
  129. "/tmp/certs/server.crt,/tmp/certs/client.crt,/tmp/certs/clientkey.pem,password",
  130. &watch);
  131. assertCanRead(zh, "/");
  132. assertCanNotWrite(zh, "/testSSL");
  133. stopPeer();
  134. }
  135. #endif
  136. };
  137. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_readOnly);
  138. #endif