TestServerRequireClientSASLAuth.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ZOOAPI int zoo_create2(zhandle_t *zh, const char *path, const char *value,
  26. int valuelen, const struct ACL_vector *acl, int mode,
  27. char *path_buffer, int path_buffer_len, struct Stat *stat);
  28. class Zookeeper_serverRequireClientSASL : public CPPUNIT_NS::TestFixture {
  29. CPPUNIT_TEST_SUITE(Zookeeper_serverRequireClientSASL);
  30. #ifdef THREADED
  31. CPPUNIT_TEST(testServerRequireClientSASL);
  32. #endif
  33. CPPUNIT_TEST_SUITE_END();
  34. FILE *logfile;
  35. static const char hostPorts[];
  36. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  37. watchctx_t *ctx = (watchctx_t*)v;
  38. if (state == ZOO_CONNECTED_STATE) {
  39. ctx->connected = true;
  40. } else {
  41. ctx->connected = false;
  42. }
  43. if (type != ZOO_SESSION_EVENT) {
  44. evt_t evt;
  45. evt.path = path;
  46. evt.type = type;
  47. ctx->putEvent(evt);
  48. }
  49. }
  50. public:
  51. Zookeeper_serverRequireClientSASL() {
  52. logfile = openlogfile("Zookeeper_serverRequireClientSASL");
  53. }
  54. ~Zookeeper_serverRequireClientSASL() {
  55. if (logfile) {
  56. fflush(logfile);
  57. fclose(logfile);
  58. logfile = 0;
  59. }
  60. }
  61. void setUp() {
  62. zoo_set_log_stream(logfile);
  63. }
  64. void startServer() {
  65. char cmd[1024];
  66. sprintf(cmd, "%s startRequireSASLAuth", ZKSERVER_CMD);
  67. CPPUNIT_ASSERT(system(cmd) == 0);
  68. }
  69. void stopServer() {
  70. char cmd[1024];
  71. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  72. CPPUNIT_ASSERT(system(cmd) == 0);
  73. }
  74. void testServerRequireClientSASL() {
  75. startServer();
  76. watchctx_t ctx;
  77. int rc = 0;
  78. zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0);
  79. ctx.zh = zk;
  80. CPPUNIT_ASSERT(zk);
  81. char pathbuf[80];
  82. struct Stat stat_a = {0};
  83. rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0,
  84. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  85. CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc);
  86. stopServer();
  87. }
  88. };
  89. const char Zookeeper_serverRequireClientSASL::hostPorts[] = "127.0.0.1:22181";
  90. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_serverRequireClientSASL);