TestReadOnlyClient.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  64. void startReadOnly() {
  65. char cmd[1024];
  66. sprintf(cmd, "%s startCleanReadOnly", ZKSERVER_CMD);
  67. CPPUNIT_ASSERT(system(cmd) == 0);
  68. }
  69. void stopPeer() {
  70. char cmd[1024];
  71. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  72. CPPUNIT_ASSERT(system(cmd) == 0);
  73. }
  74. zhandle_t* connectReadOnly(const char *address, watchctx_t *watch) {
  75. zhandle_t* zh = zookeeper_init(address, watcher, 10000, NULL, watch, ZOO_READONLY);
  76. watch->zh = zh;
  77. CPPUNIT_ASSERT(zh != 0);
  78. sleep(1);
  79. return zh;
  80. }
  81. void assertCanRead(zhandle_t* zh, const char *znode_path) {
  82. int len = 1024;
  83. char buf[len];
  84. int res = zoo_get(zh, znode_path, 0, buf, &len, 0);
  85. CPPUNIT_ASSERT_EQUAL((int)ZOK, res);
  86. }
  87. void assertCanNotWrite(zhandle_t* zh, const char *znode_path) {
  88. char path[1024];
  89. char buf[1024];
  90. int res = zoo_create(zh, znode_path, buf, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  91. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, res);
  92. }
  93. void testReadOnly()
  94. {
  95. startReadOnly();
  96. watchctx_t watch;
  97. zhandle_t* zh = connectReadOnly("localhost:22181", &watch);
  98. assertCanRead(zh, "/");
  99. assertCanNotWrite(zh, "/test");
  100. stopPeer();
  101. }
  102. #ifdef HAVE_OPENSSL_H
  103. zhandle_t* connectReadOnlySSL(const char *address, const char *certs, watchctx_t *watch) {
  104. zhandle_t* zh = zookeeper_init_ssl(address, certs, watcher, 10000, NULL, watch, ZOO_READONLY);
  105. watch->zh = zh;
  106. CPPUNIT_ASSERT(zh != 0);
  107. sleep(1);
  108. return zh;
  109. }
  110. void testReadOnlyWithSSL() {
  111. startReadOnly();
  112. watchctx_t watch;
  113. zhandle_t* zh = connectReadOnlySSL("localhost:22281",
  114. "/tmp/certs/server.crt,/tmp/certs/client.crt,/tmp/certs/clientkey.pem,password",
  115. &watch);
  116. assertCanRead(zh, "/");
  117. assertCanNotWrite(zh, "/testSSL");
  118. stopPeer();
  119. }
  120. #endif
  121. };
  122. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_readOnly);
  123. #endif