TestClient.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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 <signal.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <sys/select.h>
  24. #include "CollectionUtil.h"
  25. #include "ThreadingUtil.h"
  26. using namespace Util;
  27. #include "Vector.h"
  28. using namespace std;
  29. #include <cstring>
  30. #include <list>
  31. #include <zookeeper.h>
  32. #include <errno.h>
  33. #include <recordio.h>
  34. #include "Util.h"
  35. #include "ZKMocks.h"
  36. struct buff_struct_2 {
  37. int32_t len;
  38. int32_t off;
  39. char *buffer;
  40. };
  41. // TODO(br33d): the vast majority of this test is not usable with single threaded.
  42. // it needs a overhaul to work properly with both threaded and single
  43. // threaded (ZOOKEEPER-2640)
  44. #ifdef THREADED
  45. // For testing LogMessage Callback functionality
  46. list<string> logMessages;
  47. void logMessageHandler(const char* message) {
  48. cout << "Log Message Received: [" << message << "]" << endl;
  49. logMessages.push_back(message);
  50. }
  51. static int Stat_eq(struct Stat* a, struct Stat* b)
  52. {
  53. if (a->czxid != b->czxid) return 0;
  54. if (a->mzxid != b->mzxid) return 0;
  55. if (a->ctime != b->ctime) return 0;
  56. if (a->mtime != b->mtime) return 0;
  57. if (a->version != b->version) return 0;
  58. if (a->cversion != b->cversion) return 0;
  59. if (a->aversion != b->aversion) return 0;
  60. if (a->ephemeralOwner != b->ephemeralOwner) return 0;
  61. if (a->dataLength != b->dataLength) return 0;
  62. if (a->numChildren != b->numChildren) return 0;
  63. if (a->pzxid != b->pzxid) return 0;
  64. return 1;
  65. }
  66. #ifdef THREADED
  67. static void yield(zhandle_t *zh, int i)
  68. {
  69. sleep(i);
  70. }
  71. #else
  72. static void yield(zhandle_t *zh, int seconds)
  73. {
  74. int fd;
  75. int interest;
  76. int events;
  77. struct timeval tv;
  78. int rc;
  79. time_t expires = time(0) + seconds;
  80. time_t timeLeft = seconds;
  81. fd_set rfds, wfds, efds;
  82. FD_ZERO(&rfds);
  83. FD_ZERO(&wfds);
  84. FD_ZERO(&efds);
  85. while(timeLeft >= 0) {
  86. zookeeper_interest(zh, &fd, &interest, &tv);
  87. if (fd != -1) {
  88. if (interest&ZOOKEEPER_READ) {
  89. FD_SET(fd, &rfds);
  90. } else {
  91. FD_CLR(fd, &rfds);
  92. }
  93. if (interest&ZOOKEEPER_WRITE) {
  94. FD_SET(fd, &wfds);
  95. } else {
  96. FD_CLR(fd, &wfds);
  97. }
  98. } else {
  99. fd = 0;
  100. }
  101. FD_SET(0, &rfds);
  102. if (tv.tv_sec > timeLeft) {
  103. tv.tv_sec = timeLeft;
  104. }
  105. rc = select(fd+1, &rfds, &wfds, &efds, &tv);
  106. timeLeft = expires - time(0);
  107. events = 0;
  108. if (FD_ISSET(fd, &rfds)) {
  109. events |= ZOOKEEPER_READ;
  110. }
  111. if (FD_ISSET(fd, &wfds)) {
  112. events |= ZOOKEEPER_WRITE;
  113. }
  114. zookeeper_process(zh, events);
  115. }
  116. }
  117. #endif
  118. typedef struct evt {
  119. string path;
  120. int type;
  121. } evt_t;
  122. typedef struct watchCtx {
  123. private:
  124. list<evt_t> events;
  125. watchCtx(const watchCtx&);
  126. watchCtx& operator=(const watchCtx&);
  127. public:
  128. bool connected;
  129. zhandle_t *zh;
  130. Mutex mutex;
  131. watchCtx() {
  132. connected = false;
  133. zh = 0;
  134. }
  135. ~watchCtx() {
  136. if (zh) {
  137. zookeeper_close(zh);
  138. zh = 0;
  139. }
  140. }
  141. evt_t getEvent() {
  142. evt_t evt;
  143. mutex.acquire();
  144. CPPUNIT_ASSERT( events.size() > 0);
  145. evt = events.front();
  146. events.pop_front();
  147. mutex.release();
  148. return evt;
  149. }
  150. int countEvents() {
  151. int count;
  152. mutex.acquire();
  153. count = events.size();
  154. mutex.release();
  155. return count;
  156. }
  157. void putEvent(evt_t evt) {
  158. mutex.acquire();
  159. events.push_back(evt);
  160. mutex.release();
  161. }
  162. bool waitForConnected(zhandle_t *zh) {
  163. time_t expires = time(0) + 10;
  164. while(!connected && time(0) < expires) {
  165. yield(zh, 1);
  166. }
  167. return connected;
  168. }
  169. bool waitForDisconnected(zhandle_t *zh) {
  170. time_t expires = time(0) + 15;
  171. while(connected && time(0) < expires) {
  172. yield(zh, 1);
  173. }
  174. return !connected;
  175. }
  176. } watchctx_t;
  177. class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture
  178. {
  179. CPPUNIT_TEST_SUITE(Zookeeper_simpleSystem);
  180. CPPUNIT_TEST(testLogCallbackSet);
  181. CPPUNIT_TEST(testLogCallbackInit);
  182. CPPUNIT_TEST(testLogCallbackClear);
  183. CPPUNIT_TEST(testAsyncWatcherAutoReset);
  184. CPPUNIT_TEST(testDeserializeString);
  185. CPPUNIT_TEST(testFirstServerDown);
  186. CPPUNIT_TEST(testNonexistentHost);
  187. #ifdef THREADED
  188. CPPUNIT_TEST(testNullData);
  189. #ifdef ZOO_IPV6_ENABLED
  190. CPPUNIT_TEST(testIPV6);
  191. #endif
  192. CPPUNIT_TEST(testCreate);
  193. CPPUNIT_TEST(testCreateContainer);
  194. CPPUNIT_TEST(testCreateTtl);
  195. CPPUNIT_TEST(testPath);
  196. CPPUNIT_TEST(testPathValidation);
  197. CPPUNIT_TEST(testPing);
  198. CPPUNIT_TEST(testAcl);
  199. CPPUNIT_TEST(testChroot);
  200. CPPUNIT_TEST(testAuth);
  201. CPPUNIT_TEST(testHangingClient);
  202. CPPUNIT_TEST(testWatcherAutoResetWithGlobal);
  203. CPPUNIT_TEST(testWatcherAutoResetWithLocal);
  204. CPPUNIT_TEST(testGetChildren2);
  205. CPPUNIT_TEST(testLastZxid);
  206. CPPUNIT_TEST(testRemoveWatchers);
  207. #endif
  208. CPPUNIT_TEST_SUITE_END();
  209. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  210. watchctx_t *ctx = (watchctx_t*)v;
  211. if (state == ZOO_CONNECTED_STATE) {
  212. ctx->connected = true;
  213. } else {
  214. ctx->connected = false;
  215. }
  216. if (type != ZOO_SESSION_EVENT) {
  217. evt_t evt;
  218. evt.path = path;
  219. evt.type = type;
  220. ctx->putEvent(evt);
  221. }
  222. }
  223. static const char hostPorts[];
  224. const char *getHostPorts() {
  225. return hostPorts;
  226. }
  227. zhandle_t *createClient(watchctx_t *ctx) {
  228. return createClient(hostPorts, ctx);
  229. }
  230. zhandle_t *createClient(watchctx_t *ctx, log_callback_fn logCallback) {
  231. zhandle_t *zk = zookeeper_init2(hostPorts, watcher, 10000, 0, ctx, 0, logCallback);
  232. ctx->zh = zk;
  233. sleep(1);
  234. return zk;
  235. }
  236. zhandle_t *createClient(const char *hp, watchctx_t *ctx) {
  237. zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0);
  238. ctx->zh = zk;
  239. sleep(1);
  240. return zk;
  241. }
  242. zhandle_t *createchClient(watchctx_t *ctx, const char* chroot) {
  243. zhandle_t *zk = zookeeper_init(chroot, watcher, 10000, 0, ctx, 0);
  244. ctx->zh = zk;
  245. sleep(1);
  246. return zk;
  247. }
  248. FILE *logfile;
  249. public:
  250. Zookeeper_simpleSystem() {
  251. logfile = openlogfile("Zookeeper_simpleSystem");
  252. }
  253. ~Zookeeper_simpleSystem() {
  254. if (logfile) {
  255. fflush(logfile);
  256. fclose(logfile);
  257. logfile = 0;
  258. }
  259. }
  260. void setUp()
  261. {
  262. zoo_set_log_stream(logfile);
  263. }
  264. void startServer() {
  265. char cmd[1024];
  266. sprintf(cmd, "%s start %s", ZKSERVER_CMD, getHostPorts());
  267. CPPUNIT_ASSERT(system(cmd) == 0);
  268. }
  269. void stopServer() {
  270. char cmd[1024];
  271. sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts());
  272. CPPUNIT_ASSERT(system(cmd) == 0);
  273. }
  274. void tearDown()
  275. {
  276. }
  277. /** have a callback in the default watcher **/
  278. static void default_zoo_watcher(zhandle_t *zzh, int type, int state, const char *path, void *context){
  279. int zrc = 0;
  280. struct String_vector str_vec = {0, NULL};
  281. zrc = zoo_wget_children(zzh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  282. }
  283. /** ZOOKEEPER-1057 This checks that the client connects to the second server when the first is not reachable **/
  284. void testFirstServerDown() {
  285. watchctx_t ctx;
  286. zoo_deterministic_conn_order(true);
  287. zhandle_t* zk = createClient("127.0.0.1:22182,127.0.0.1:22181", &ctx);
  288. CPPUNIT_ASSERT(zk != 0);
  289. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  290. }
  291. /* Checks that a non-existent host will not block the connection*/
  292. void testNonexistentHost() {
  293. char hosts[] = "jimmy:5555,127.0.0.1:22181";
  294. watchctx_t ctx;
  295. zoo_deterministic_conn_order(true /* disable permute */);
  296. zhandle_t *zh = createClient(hosts, &ctx);
  297. CPPUNIT_ASSERT(ctx.waitForConnected(zh));
  298. zoo_deterministic_conn_order(false /* enable permute */);
  299. }
  300. /** this checks for a deadlock in calling zookeeper_close and calls from a default watcher that might get triggered just when zookeeper_close() is in progress **/
  301. void testHangingClient() {
  302. int zrc = 0;
  303. char buff[10] = "testall";
  304. char path[512];
  305. watchctx_t *ctx;
  306. struct String_vector str_vec = {0, NULL};
  307. zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0);
  308. sleep(1);
  309. zrc = zoo_create(zh, "/mytest", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  310. zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  311. zrc = zoo_create(zh, "/mytest/test1", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512);
  312. zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec);
  313. zrc = zoo_delete(zh, "/mytest/test1", -1);
  314. zookeeper_close(zh);
  315. }
  316. void testBadDescriptor() {
  317. int zrc = 0;
  318. watchctx_t *ctx;
  319. zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0);
  320. sleep(1);
  321. zh->io_count = 0;
  322. //close socket
  323. close(zh->fd);
  324. sleep(1);
  325. //Check that doIo isn't spinning
  326. CPPUNIT_ASSERT(zh->io_count < 2);
  327. zookeeper_close(zh);
  328. }
  329. void testPing()
  330. {
  331. watchctx_t ctxIdle;
  332. watchctx_t ctxWC;
  333. zhandle_t *zkIdle = createClient(&ctxIdle);
  334. zhandle_t *zkWatchCreator = createClient(&ctxWC);
  335. CPPUNIT_ASSERT(zkIdle);
  336. CPPUNIT_ASSERT(zkWatchCreator);
  337. char path[80];
  338. sprintf(path, "/testping");
  339. int rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  340. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  341. for(int i = 0; i < 30; i++) {
  342. sprintf(path, "/testping/%i", i);
  343. rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  344. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  345. }
  346. for(int i = 0; i < 30; i++) {
  347. sprintf(path, "/testping/%i", i);
  348. struct Stat stat;
  349. rc = zoo_exists(zkIdle, path, 1, &stat);
  350. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  351. }
  352. for(int i = 0; i < 30; i++) {
  353. sprintf(path, "/testping/%i", i);
  354. usleep(500000);
  355. rc = zoo_delete(zkWatchCreator, path, -1);
  356. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  357. }
  358. struct Stat stat;
  359. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, zoo_exists(zkIdle, "/testping/0", 0, &stat));
  360. }
  361. bool waitForEvent(zhandle_t *zh, watchctx_t *ctx, int seconds) {
  362. time_t expires = time(0) + seconds;
  363. while(ctx->countEvents() == 0 && time(0) < expires) {
  364. yield(zh, 1);
  365. }
  366. return ctx->countEvents() > 0;
  367. }
  368. #define COUNT 100
  369. static zhandle_t *async_zk;
  370. static volatile int count;
  371. static const char* hp_chroot;
  372. static void statCompletion(int rc, const struct Stat *stat, const void *data) {
  373. int tmp = (int) (long) data;
  374. CPPUNIT_ASSERT_EQUAL(tmp, rc);
  375. }
  376. static void stringCompletion(int rc, const char *value, const void *data) {
  377. char *path = (char*)data;
  378. if (rc == ZCONNECTIONLOSS && path) {
  379. // Try again
  380. rc = zoo_acreate(async_zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, 0);
  381. } else if (rc != ZOK) {
  382. // fprintf(stderr, "rc = %d with path = %s\n", rc, (path ? path : "null"));
  383. }
  384. if (path) {
  385. free(path);
  386. }
  387. }
  388. static void stringStatCompletion(int rc, const char *value, const struct Stat *stat,
  389. const void *data) {
  390. stringCompletion(rc, value, data);
  391. CPPUNIT_ASSERT(stat != 0);
  392. }
  393. static void create_completion_fn(int rc, const char* value, const void *data) {
  394. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  395. count++;
  396. }
  397. static void waitForCreateCompletion(int seconds) {
  398. time_t expires = time(0) + seconds;
  399. while(count == 0 && time(0) < expires) {
  400. sleep(1);
  401. }
  402. count--;
  403. }
  404. static void watcher_chroot_fn(zhandle_t *zh, int type,
  405. int state, const char *path,void *watcherCtx) {
  406. // check for path
  407. char *client_path = (char *) watcherCtx;
  408. CPPUNIT_ASSERT(strcmp(client_path, path) == 0);
  409. count ++;
  410. }
  411. static void waitForChrootWatch(int seconds) {
  412. time_t expires = time(0) + seconds;
  413. while (count == 0 && time(0) < expires) {
  414. sleep(1);
  415. }
  416. count--;
  417. }
  418. static void waitForVoidCompletion(int seconds) {
  419. time_t expires = time(0) + seconds;
  420. while(count == 0 && time(0) < expires) {
  421. sleep(1);
  422. }
  423. count--;
  424. }
  425. static void voidCompletion(int rc, const void *data) {
  426. int tmp = (int) (long) data;
  427. CPPUNIT_ASSERT_EQUAL(tmp, rc);
  428. count++;
  429. }
  430. static void verifyCreateFails(const char *path, zhandle_t *zk) {
  431. CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk,
  432. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0));
  433. }
  434. static void verifyCreateOk(const char *path, zhandle_t *zk) {
  435. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk,
  436. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0));
  437. }
  438. static void verifyCreateFailsSeq(const char *path, zhandle_t *zk) {
  439. CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk,
  440. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0));
  441. }
  442. static void verifyCreateOkSeq(const char *path, zhandle_t *zk) {
  443. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk,
  444. path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0));
  445. }
  446. /**
  447. returns false if the vectors dont match
  448. **/
  449. bool compareAcl(struct ACL_vector acl1, struct ACL_vector acl2) {
  450. if (acl1.count != acl2.count) {
  451. return false;
  452. }
  453. struct ACL *aclval1 = acl1.data;
  454. struct ACL *aclval2 = acl2.data;
  455. if (aclval1->perms != aclval2->perms) {
  456. return false;
  457. }
  458. struct Id id1 = aclval1->id;
  459. struct Id id2 = aclval2->id;
  460. if (strcmp(id1.scheme, id2.scheme) != 0) {
  461. return false;
  462. }
  463. if (strcmp(id1.id, id2.id) != 0) {
  464. return false;
  465. }
  466. return true;
  467. }
  468. void testDeserializeString() {
  469. char *val_str;
  470. int rc = 0;
  471. int val = -1;
  472. struct iarchive *ia;
  473. struct buff_struct_2 *b;
  474. struct oarchive *oa = create_buffer_oarchive();
  475. oa->serialize_Int(oa, "int", &val);
  476. b = (struct buff_struct_2 *) oa->priv;
  477. ia = create_buffer_iarchive(b->buffer, b->len);
  478. rc = ia->deserialize_String(ia, "string", &val_str);
  479. CPPUNIT_ASSERT_EQUAL(-EINVAL, rc);
  480. }
  481. void testAcl() {
  482. int rc;
  483. struct ACL_vector aclvec;
  484. struct Stat stat;
  485. watchctx_t ctx;
  486. zhandle_t *zk = createClient(&ctx);
  487. rc = zoo_create(zk, "/acl", "", 0,
  488. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  489. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  490. rc = zoo_get_acl(zk, "/acl", &aclvec, &stat );
  491. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  492. bool cmp = compareAcl(ZOO_OPEN_ACL_UNSAFE, aclvec);
  493. CPPUNIT_ASSERT_EQUAL(true, cmp);
  494. rc = zoo_set_acl(zk, "/acl", -1, &ZOO_READ_ACL_UNSAFE);
  495. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  496. rc = zoo_get_acl(zk, "/acl", &aclvec, &stat);
  497. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  498. cmp = compareAcl(ZOO_READ_ACL_UNSAFE, aclvec);
  499. CPPUNIT_ASSERT_EQUAL(true, cmp);
  500. }
  501. void testAuth() {
  502. int rc;
  503. count = 0;
  504. watchctx_t ctx1, ctx2, ctx3, ctx4, ctx5;
  505. zhandle_t *zk = createClient(&ctx1);
  506. struct ACL_vector nodeAcl;
  507. struct ACL acl_val;
  508. rc = zoo_add_auth(0, "", 0, 0, voidCompletion, (void*)-1);
  509. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  510. rc = zoo_add_auth(zk, 0, 0, 0, voidCompletion, (void*)-1);
  511. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  512. // auth as pat, create /tauth1, close session
  513. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
  514. (void*)ZOK);
  515. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  516. waitForVoidCompletion(3);
  517. CPPUNIT_ASSERT(count == 0);
  518. rc = zoo_create(zk, "/tauth1", "", 0, &ZOO_CREATOR_ALL_ACL, 0, 0, 0);
  519. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  520. {
  521. //create a new client
  522. zk = createClient(&ctx4);
  523. rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK);
  524. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  525. waitForVoidCompletion(3);
  526. CPPUNIT_ASSERT(count == 0);
  527. rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK);
  528. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  529. waitForVoidCompletion(3);
  530. CPPUNIT_ASSERT(count == 0);
  531. }
  532. //create a new client
  533. zk = createClient(&ctx2);
  534. rc = zoo_add_auth(zk, "digest", "pat:passwd2", 11, voidCompletion,
  535. (void*)ZOK);
  536. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  537. waitForVoidCompletion(3);
  538. CPPUNIT_ASSERT(count == 0);
  539. char buf[1024];
  540. int blen = sizeof(buf);
  541. struct Stat stat;
  542. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  543. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc);
  544. // add auth pat w/correct pass verify success
  545. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion,
  546. (void*)ZOK);
  547. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  548. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  549. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  550. waitForVoidCompletion(3);
  551. CPPUNIT_ASSERT(count == 0);
  552. //create a new client
  553. zk = createClient(&ctx3);
  554. rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion, (void*) ZOK);
  555. waitForVoidCompletion(3);
  556. CPPUNIT_ASSERT(count == 0);
  557. rc = zoo_add_auth(zk, "ip", "none", 4, voidCompletion, (void*)ZOK);
  558. //make the server forget the auths
  559. waitForVoidCompletion(3);
  560. CPPUNIT_ASSERT(count == 0);
  561. stopServer();
  562. CPPUNIT_ASSERT(ctx3.waitForDisconnected(zk));
  563. startServer();
  564. CPPUNIT_ASSERT(ctx3.waitForConnected(zk));
  565. // now try getting the data
  566. rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat);
  567. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  568. // also check for get
  569. rc = zoo_get_acl(zk, "/", &nodeAcl, &stat);
  570. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  571. // check if the acl has all the perms
  572. CPPUNIT_ASSERT_EQUAL((int)1, (int)nodeAcl.count);
  573. acl_val = *(nodeAcl.data);
  574. CPPUNIT_ASSERT_EQUAL((int) acl_val.perms, ZOO_PERM_ALL);
  575. // verify on root node
  576. rc = zoo_set_acl(zk, "/", -1, &ZOO_CREATOR_ALL_ACL);
  577. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  578. rc = zoo_set_acl(zk, "/", -1, &ZOO_OPEN_ACL_UNSAFE);
  579. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  580. //[ZOOKEEPER-1108], test that auth info is sent to server, if client is not
  581. //connected to server when zoo_add_auth was called.
  582. zhandle_t *zk_auth = zookeeper_init(hostPorts, NULL, 10000, 0, NULL, 0);
  583. rc = zoo_add_auth(zk_auth, "digest", "pat:passwd", 10, voidCompletion, (void*)ZOK);
  584. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  585. sleep(2);
  586. CPPUNIT_ASSERT(count == 1);
  587. count = 0;
  588. CPPUNIT_ASSERT_EQUAL((int) ZOK, zookeeper_close(zk_auth));
  589. struct sockaddr addr;
  590. socklen_t addr_len = sizeof(addr);
  591. zk = createClient(&ctx5);
  592. stopServer();
  593. CPPUNIT_ASSERT(ctx5.waitForDisconnected(zk));
  594. CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) == NULL);
  595. addr_len = sizeof(addr);
  596. startServer();
  597. CPPUNIT_ASSERT(ctx5.waitForConnected(zk));
  598. CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) != NULL);
  599. }
  600. void testCreate() {
  601. watchctx_t ctx;
  602. int rc = 0;
  603. zhandle_t *zk = createClient(&ctx);
  604. CPPUNIT_ASSERT(zk);
  605. char pathbuf[80];
  606. struct Stat stat_a = {0};
  607. struct Stat stat_b = {0};
  608. rc = zoo_create2(zk, "/testcreateA", "", 0,
  609. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  610. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  611. CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateA") == 0);
  612. CPPUNIT_ASSERT(stat_a.czxid > 0);
  613. CPPUNIT_ASSERT(stat_a.mtime > 0);
  614. rc = zoo_create2(zk, "/testcreateB", "", 0,
  615. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_b);
  616. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  617. CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateB") == 0);
  618. CPPUNIT_ASSERT(stat_b.czxid > 0);
  619. CPPUNIT_ASSERT(stat_b.mtime > 0);
  620. // Should get different Stats back from different creates
  621. CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b) != 1);
  622. }
  623. void testCreateContainer() {
  624. watchctx_t ctx;
  625. int rc = 0;
  626. zhandle_t *zk = createClient(&ctx);
  627. CPPUNIT_ASSERT(zk);
  628. char pathbuf[80];
  629. struct Stat stat = {0};
  630. rc = zoo_create2(zk, "/testContainer", "", 0, &ZOO_OPEN_ACL_UNSAFE,
  631. ZOO_CONTAINER, pathbuf, sizeof(pathbuf), &stat);
  632. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  633. }
  634. void testCreateTtl() {
  635. watchctx_t ctx;
  636. int rc = 0;
  637. zhandle_t *zk = createClient(&ctx);
  638. CPPUNIT_ASSERT(zk);
  639. char pathbuf[80];
  640. struct Stat stat = {0};
  641. rc = zoo_create2_ttl(zk, "/testTtl", "", 0, &ZOO_OPEN_ACL_UNSAFE,
  642. ZOO_PERSISTENT_WITH_TTL, 1, pathbuf, sizeof(pathbuf), &stat);
  643. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  644. sleep(1);
  645. rc = zoo_exists(zk, "/testTtl", 1, &stat);
  646. CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc);
  647. }
  648. void testGetChildren2() {
  649. int rc;
  650. watchctx_t ctx;
  651. zhandle_t *zk = createClient(&ctx);
  652. rc = zoo_create(zk, "/parent", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  653. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  654. rc = zoo_create(zk, "/parent/child_a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  655. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  656. rc = zoo_create(zk, "/parent/child_b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  657. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  658. rc = zoo_create(zk, "/parent/child_c", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  659. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  660. rc = zoo_create(zk, "/parent/child_d", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  661. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  662. struct String_vector strings;
  663. struct Stat stat_a, stat_b;
  664. rc = zoo_get_children2(zk, "/parent", 0, &strings, &stat_a);
  665. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  666. rc = zoo_exists(zk, "/parent", 0, &stat_b);
  667. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  668. CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b));
  669. CPPUNIT_ASSERT(stat_a.numChildren == 4);
  670. }
  671. void testIPV6() {
  672. watchctx_t ctx;
  673. zhandle_t *zk = createClient("::1:22181", &ctx);
  674. CPPUNIT_ASSERT(zk);
  675. int rc = 0;
  676. rc = zoo_create(zk, "/ipv6", NULL, -1,
  677. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  678. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  679. }
  680. void testNullData() {
  681. watchctx_t ctx;
  682. zhandle_t *zk = createClient(&ctx);
  683. CPPUNIT_ASSERT(zk);
  684. int rc = 0;
  685. rc = zoo_create(zk, "/mahadev", NULL, -1,
  686. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  687. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  688. char buffer[512];
  689. struct Stat stat;
  690. int len = 512;
  691. rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat);
  692. CPPUNIT_ASSERT_EQUAL( -1, len);
  693. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  694. rc = zoo_set(zk, "/mahadev", NULL, -1, -1);
  695. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  696. rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat);
  697. CPPUNIT_ASSERT_EQUAL( -1, len);
  698. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  699. }
  700. void testPath() {
  701. watchctx_t ctx;
  702. char pathbuf[20];
  703. zhandle_t *zk = createClient(&ctx);
  704. CPPUNIT_ASSERT(zk);
  705. int rc = 0;
  706. memset(pathbuf, 'X', 20);
  707. rc = zoo_create(zk, "/testpathpath0", "", 0,
  708. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 0);
  709. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  710. CPPUNIT_ASSERT_EQUAL('X', pathbuf[0]);
  711. rc = zoo_create(zk, "/testpathpath1", "", 0,
  712. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 1);
  713. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  714. CPPUNIT_ASSERT(strlen(pathbuf) == 0);
  715. rc = zoo_create(zk, "/testpathpath2", "", 0,
  716. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 2);
  717. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  718. CPPUNIT_ASSERT(strcmp(pathbuf, "/") == 0);
  719. rc = zoo_create(zk, "/testpathpath3", "", 0,
  720. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 3);
  721. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  722. CPPUNIT_ASSERT(strcmp(pathbuf, "/t") == 0);
  723. rc = zoo_create(zk, "/testpathpath7", "", 0,
  724. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 15);
  725. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  726. CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath7") == 0);
  727. rc = zoo_create(zk, "/testpathpath8", "", 0,
  728. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 16);
  729. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  730. CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath8") == 0);
  731. }
  732. void testPathValidation() {
  733. watchctx_t ctx;
  734. zhandle_t *zk = createClient(&ctx);
  735. CPPUNIT_ASSERT(zk);
  736. verifyCreateFails(0, zk);
  737. verifyCreateFails("", zk);
  738. verifyCreateFails("//", zk);
  739. verifyCreateFails("///", zk);
  740. verifyCreateFails("////", zk);
  741. verifyCreateFails("/.", zk);
  742. verifyCreateFails("/..", zk);
  743. verifyCreateFails("/./", zk);
  744. verifyCreateFails("/../", zk);
  745. verifyCreateFails("/foo/./", zk);
  746. verifyCreateFails("/foo/../", zk);
  747. verifyCreateFails("/foo/.", zk);
  748. verifyCreateFails("/foo/..", zk);
  749. verifyCreateFails("/./.", zk);
  750. verifyCreateFails("/../..", zk);
  751. verifyCreateFails("/foo/bar/", zk);
  752. verifyCreateFails("/foo//bar", zk);
  753. verifyCreateFails("/foo/bar//", zk);
  754. verifyCreateFails("foo", zk);
  755. verifyCreateFails("a", zk);
  756. // verify that trailing fails, except for seq which adds suffix
  757. verifyCreateOk("/createseq", zk);
  758. verifyCreateFails("/createseq/", zk);
  759. verifyCreateOkSeq("/createseq/", zk);
  760. verifyCreateOkSeq("/createseq/.", zk);
  761. verifyCreateOkSeq("/createseq/..", zk);
  762. verifyCreateFailsSeq("/createseq//", zk);
  763. verifyCreateFailsSeq("/createseq/./", zk);
  764. verifyCreateFailsSeq("/createseq/../", zk);
  765. verifyCreateOk("/.foo", zk);
  766. verifyCreateOk("/.f.", zk);
  767. verifyCreateOk("/..f", zk);
  768. verifyCreateOk("/..f..", zk);
  769. verifyCreateOk("/f.c", zk);
  770. verifyCreateOk("/f", zk);
  771. verifyCreateOk("/f/.f", zk);
  772. verifyCreateOk("/f/f.", zk);
  773. verifyCreateOk("/f/..f", zk);
  774. verifyCreateOk("/f/f..", zk);
  775. verifyCreateOk("/f/.f/f", zk);
  776. verifyCreateOk("/f/f./f", zk);
  777. }
  778. void testChroot() {
  779. // the c client async callbacks do
  780. // not callback with the path, so
  781. // we dont need to test taht for now
  782. // we should fix that though soon!
  783. watchctx_t ctx, ctx_ch;
  784. zhandle_t *zk, *zk_ch;
  785. char buf[60];
  786. int rc, len;
  787. struct Stat stat;
  788. const char* data = "garbage";
  789. const char* retStr = "/chroot";
  790. const char* root= "/";
  791. zk_ch = createchClient(&ctx_ch, "127.0.0.1:22181/testch1/mahadev");
  792. CPPUNIT_ASSERT(zk_ch != NULL);
  793. zk = createClient(&ctx);
  794. // first test with a NULL zk handle, make sure client library does not
  795. // dereference a null pointer, but instead returns ZBADARGUMENTS
  796. rc = zoo_create(NULL, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  797. CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc);
  798. rc = zoo_create(zk, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  799. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  800. rc = zoo_create(zk, "/testch1/mahadev", data, 7, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  801. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  802. // try an exists with /
  803. len = 60;
  804. rc = zoo_get(zk_ch, "/", 0, buf, &len, &stat);
  805. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  806. //check if the data is the same
  807. CPPUNIT_ASSERT(strncmp(buf, data, 7) == 0);
  808. //check for watches
  809. rc = zoo_wexists(zk_ch, "/chroot", watcher_chroot_fn, (void *) retStr, &stat);
  810. //now check if we can do create/delete/get/sets/acls/getChildren and others
  811. //check create
  812. rc = zoo_create(zk_ch, "/chroot", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0,0);
  813. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  814. waitForChrootWatch(3);
  815. CPPUNIT_ASSERT(count == 0);
  816. rc = zoo_create(zk_ch, "/chroot/child", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  817. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  818. rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat);
  819. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  820. rc = zoo_delete(zk_ch, "/chroot/child", -1);
  821. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  822. rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat);
  823. CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc);
  824. rc = zoo_wget(zk_ch, "/chroot", watcher_chroot_fn, (char*) retStr,
  825. buf, &len, &stat);
  826. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  827. rc = zoo_set(zk_ch, "/chroot",buf, 3, -1);
  828. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  829. waitForChrootWatch(3);
  830. CPPUNIT_ASSERT(count == 0);
  831. // check for getchildren
  832. struct String_vector children;
  833. rc = zoo_get_children(zk_ch, "/", 0, &children);
  834. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  835. CPPUNIT_ASSERT_EQUAL((int)1, (int)children.count);
  836. //check if te child if chroot
  837. CPPUNIT_ASSERT(strcmp((retStr+1), children.data[0]) == 0);
  838. // check for get/set acl
  839. struct ACL_vector acl;
  840. rc = zoo_get_acl(zk_ch, "/", &acl, &stat);
  841. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  842. CPPUNIT_ASSERT_EQUAL((int)1, (int)acl.count);
  843. CPPUNIT_ASSERT_EQUAL((int)ZOO_PERM_ALL, (int)acl.data->perms);
  844. // set acl
  845. rc = zoo_set_acl(zk_ch, "/chroot", -1, &ZOO_READ_ACL_UNSAFE);
  846. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  847. // see if you add children
  848. rc = zoo_create(zk_ch, "/chroot/child1", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  849. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc);
  850. //add wget children test
  851. rc = zoo_wget_children(zk_ch, "/", watcher_chroot_fn, (char*) root, &children);
  852. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  853. //now create a node
  854. rc = zoo_create(zk_ch, "/child2", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  855. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  856. waitForChrootWatch(3);
  857. CPPUNIT_ASSERT(count == 0);
  858. //check for one async call just to make sure
  859. rc = zoo_acreate(zk_ch, "/child3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  860. create_completion_fn, 0);
  861. waitForCreateCompletion(3);
  862. CPPUNIT_ASSERT(count == 0);
  863. //ZOOKEEPER-1027 correctly return path_buffer without prefixed chroot
  864. const char* path = "/zookeeper1027";
  865. char path_buffer[1024];
  866. int path_buffer_len=sizeof(path_buffer);
  867. rc = zoo_create(zk_ch, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, path_buffer, path_buffer_len);
  868. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  869. CPPUNIT_ASSERT_EQUAL(string(path), string(path_buffer));
  870. }
  871. // Test creating normal handle via zookeeper_init then explicitly setting callback
  872. void testLogCallbackSet()
  873. {
  874. watchctx_t ctx;
  875. CPPUNIT_ASSERT(logMessages.empty());
  876. zhandle_t *zk = createClient(&ctx);
  877. zoo_set_log_callback(zk, &logMessageHandler);
  878. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  879. // Log 10 messages and ensure all go to callback
  880. int expected = 10;
  881. for (int i = 0; i < expected; i++)
  882. {
  883. LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i);
  884. }
  885. CPPUNIT_ASSERT(expected == logMessages.size());
  886. }
  887. // Test creating handle via zookeeper_init2 to ensure all connection messages go to callback
  888. void testLogCallbackInit()
  889. {
  890. logMessages.clear();
  891. watchctx_t ctx;
  892. zhandle_t *zk = createClient(&ctx, &logMessageHandler);
  893. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  894. // All the connection messages should have gone to the callback -- don't
  895. // want this to be a maintenance issue so we're not asserting exact count
  896. int numBefore = logMessages.size();
  897. CPPUNIT_ASSERT(numBefore != 0);
  898. // Log 10 messages and ensure all go to callback
  899. int expected = 10;
  900. for (int i = 0; i < expected; i++)
  901. {
  902. LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i);
  903. }
  904. CPPUNIT_ASSERT(logMessages.size() == numBefore + expected);
  905. }
  906. // Test clearing log callback -- logging should resume going to logstream
  907. void testLogCallbackClear()
  908. {
  909. logMessages.clear();
  910. watchctx_t ctx;
  911. zhandle_t *zk = createClient(&ctx, &logMessageHandler);
  912. CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler);
  913. // All the connection messages should have gone to the callback -- again, we don't
  914. // want this to be a maintenance issue so we're not asserting exact count
  915. int numBefore = logMessages.size();
  916. CPPUNIT_ASSERT(numBefore > 0);
  917. // Clear log_callback
  918. zoo_set_log_callback(zk, NULL);
  919. // Future log messages should go to logstream not callback
  920. LOG_INFO(LOGCALLBACK(zk), __FUNCTION__);
  921. int numAfter = logMessages.size();
  922. CPPUNIT_ASSERT_EQUAL(numBefore, numAfter);
  923. }
  924. void testAsyncWatcherAutoReset()
  925. {
  926. watchctx_t ctx;
  927. zhandle_t *zk = createClient(&ctx);
  928. watchctx_t lctx[COUNT];
  929. int i;
  930. char path[80];
  931. int rc;
  932. evt_t evt;
  933. async_zk = zk;
  934. for(i = 0; i < COUNT; i++) {
  935. sprintf(path, "/awar%d", i);
  936. rc = zoo_awexists(zk, path, watcher, &lctx[i], statCompletion, (void*)ZNONODE);
  937. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  938. }
  939. yield(zk, 0);
  940. for(i = 0; i < COUNT/4; i++) {
  941. sprintf(path, "/awar%d", i);
  942. rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  943. stringCompletion, strdup(path));
  944. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  945. }
  946. for(i = COUNT/4; i < COUNT/2; i++) {
  947. sprintf(path, "/awar%d", i);
  948. rc = zoo_acreate2(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0,
  949. stringStatCompletion, strdup(path));
  950. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  951. }
  952. yield(zk, 3);
  953. for(i = 0; i < COUNT/2; i++) {
  954. sprintf(path, "/awar%d", i);
  955. CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5));
  956. evt = lctx[i].getEvent();
  957. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path.c_str(), ZOO_CREATED_EVENT, evt.type);
  958. CPPUNIT_ASSERT_EQUAL(string(path), evt.path);
  959. }
  960. for(i = COUNT/2 + 1; i < COUNT*10; i++) {
  961. sprintf(path, "/awar%d", i);
  962. rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, strdup(path));
  963. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  964. }
  965. yield(zk, 1);
  966. stopServer();
  967. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  968. startServer();
  969. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  970. yield(zk, 3);
  971. for(i = COUNT/2+1; i < COUNT; i++) {
  972. sprintf(path, "/awar%d", i);
  973. CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5));
  974. evt = lctx[i].getEvent();
  975. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type);
  976. CPPUNIT_ASSERT_EQUAL(string(path), evt.path);
  977. }
  978. }
  979. void testWatcherAutoReset(zhandle_t *zk, watchctx_t *ctxGlobal,
  980. watchctx_t *ctxLocal)
  981. {
  982. bool isGlobal = (ctxGlobal == ctxLocal);
  983. int rc;
  984. struct Stat stat;
  985. char buf[1024];
  986. int blen;
  987. struct String_vector strings;
  988. const char *testName;
  989. rc = zoo_create(zk, "/watchtest", "", 0,
  990. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  991. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  992. rc = zoo_create(zk, "/watchtest/child", "", 0,
  993. &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, 0, 0);
  994. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  995. if (isGlobal) {
  996. testName = "GlobalTest";
  997. rc = zoo_get_children(zk, "/watchtest", 1, &strings);
  998. deallocate_String_vector(&strings);
  999. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1000. blen = sizeof(buf);
  1001. rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat);
  1002. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1003. rc = zoo_exists(zk, "/watchtest/child2", 1, &stat);
  1004. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  1005. } else {
  1006. testName = "LocalTest";
  1007. rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal,
  1008. &strings);
  1009. deallocate_String_vector(&strings);
  1010. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1011. blen = sizeof(buf);
  1012. rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal,
  1013. buf, &blen, &stat);
  1014. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1015. rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal,
  1016. &stat);
  1017. CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc);
  1018. }
  1019. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1020. stopServer();
  1021. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1022. startServer();
  1023. CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk));
  1024. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1025. rc = zoo_set(zk, "/watchtest/child", "1", 1, -1);
  1026. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1027. struct Stat stat1, stat2;
  1028. rc = zoo_set2(zk, "/watchtest/child", "1", 1, -1, &stat1);
  1029. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1030. CPPUNIT_ASSERT(stat1.version >= 0);
  1031. rc = zoo_set2(zk, "/watchtest/child", "1", 1, stat1.version, &stat2);
  1032. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1033. rc = zoo_set(zk, "/watchtest/child", "1", 1, stat2.version);
  1034. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1035. rc = zoo_create(zk, "/watchtest/child2", "", 0,
  1036. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1037. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1038. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1039. evt_t evt = ctxLocal->getEvent();
  1040. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHANGED_EVENT, evt.type);
  1041. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path);
  1042. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1043. // The create will trigget the get children and the
  1044. // exists watches
  1045. evt = ctxLocal->getEvent();
  1046. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type);
  1047. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path);
  1048. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1049. evt = ctxLocal->getEvent();
  1050. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type);
  1051. CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path);
  1052. // Make sure Pings are giving us problems
  1053. sleep(5);
  1054. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1055. stopServer();
  1056. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1057. startServer();
  1058. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForConnected(zk));
  1059. if (isGlobal) {
  1060. testName = "GlobalTest";
  1061. rc = zoo_get_children(zk, "/watchtest", 1, &strings);
  1062. deallocate_String_vector(&strings);
  1063. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1064. blen = sizeof(buf);
  1065. rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat);
  1066. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1067. rc = zoo_exists(zk, "/watchtest/child2", 1, &stat);
  1068. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1069. } else {
  1070. testName = "LocalTest";
  1071. rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal,
  1072. &strings);
  1073. deallocate_String_vector(&strings);
  1074. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1075. blen = sizeof(buf);
  1076. rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal,
  1077. buf, &blen, &stat);
  1078. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1079. rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal,
  1080. &stat);
  1081. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1082. }
  1083. zoo_delete(zk, "/watchtest/child2", -1);
  1084. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1085. evt = ctxLocal->getEvent();
  1086. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type);
  1087. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path);
  1088. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1089. evt = ctxLocal->getEvent();
  1090. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type);
  1091. CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path);
  1092. stopServer();
  1093. CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk));
  1094. startServer();
  1095. CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk));
  1096. zoo_delete(zk, "/watchtest/child", -1);
  1097. zoo_delete(zk, "/watchtest", -1);
  1098. CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5));
  1099. evt = ctxLocal->getEvent();
  1100. CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type);
  1101. CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path);
  1102. // Make sure nothing is straggling
  1103. sleep(1);
  1104. CPPUNIT_ASSERT(ctxLocal->countEvents() == 0);
  1105. }
  1106. void testWatcherAutoResetWithGlobal()
  1107. {
  1108. {
  1109. watchctx_t ctx;
  1110. zhandle_t *zk = createClient(&ctx);
  1111. int rc = zoo_create(zk, "/testarwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1112. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1113. rc = zoo_create(zk, "/testarwg/arwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1114. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1115. }
  1116. {
  1117. watchctx_t ctx;
  1118. zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwg/arwg");
  1119. testWatcherAutoReset(zk, &ctx, &ctx);
  1120. }
  1121. }
  1122. void testWatcherAutoResetWithLocal()
  1123. {
  1124. {
  1125. watchctx_t ctx;
  1126. zhandle_t *zk = createClient(&ctx);
  1127. int rc = zoo_create(zk, "/testarwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1128. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1129. rc = zoo_create(zk, "/testarwl/arwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1130. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1131. }
  1132. {
  1133. watchctx_t ctx;
  1134. watchctx_t lctx;
  1135. zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwl/arwl");
  1136. testWatcherAutoReset(zk, &ctx, &lctx);
  1137. }
  1138. }
  1139. void testLastZxid() {
  1140. // ZOOKEEPER-1417: Test that c-client only update last zxid upon
  1141. // receiving request response only.
  1142. const int timeout = 5000;
  1143. int rc;
  1144. watchctx_t ctx1, ctx2;
  1145. zhandle_t *zk1 = createClient(&ctx1);
  1146. zhandle_t *zk2 = createClient(&ctx2);
  1147. CPPUNIT_ASSERT(zk1);
  1148. CPPUNIT_ASSERT(zk2);
  1149. int64_t original = zk2->last_zxid;
  1150. // Create txn to increase system zxid
  1151. rc = zoo_create(zk1, "/lastzxid", "", 0,
  1152. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1153. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1154. // This should be enough time for zk2 to receive ping request
  1155. usleep(timeout/2 * 1000);
  1156. // check that zk1's last zxid is updated
  1157. struct Stat stat;
  1158. rc = zoo_exists(zk1, "/lastzxid", 0, &stat);
  1159. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1160. CPPUNIT_ASSERT_EQUAL((int64_t) zk1->last_zxid, stat.czxid);
  1161. // zk2's last zxid should remain the same
  1162. CPPUNIT_ASSERT_EQUAL(original, (int64_t) zk2->last_zxid);
  1163. // Perform read and also register a watch
  1164. rc = zoo_wexists(zk2, "/lastzxid", watcher, &ctx2, &stat);
  1165. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1166. int64_t updated = zk2->last_zxid;
  1167. // check that zk2's last zxid is updated
  1168. CPPUNIT_ASSERT_EQUAL(updated, stat.czxid);
  1169. // Increment system zxid again
  1170. rc = zoo_set(zk1, "/lastzxid", NULL, -1, -1);
  1171. CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
  1172. // Wait for zk2 to get watch event
  1173. CPPUNIT_ASSERT(waitForEvent(zk2, &ctx2, 5));
  1174. // zk2's last zxid should remain the same
  1175. CPPUNIT_ASSERT_EQUAL(updated, (int64_t) zk2->last_zxid);
  1176. }
  1177. static void watcher_rw(zhandle_t *zh,
  1178. int type,
  1179. int state,
  1180. const char *path,
  1181. void *ctx) {
  1182. count++;
  1183. }
  1184. static void watcher_rw2(zhandle_t *zh,
  1185. int type,
  1186. int state,
  1187. const char *path,
  1188. void *ctx) {
  1189. count++;
  1190. }
  1191. void testRemoveWatchers() {
  1192. char *path = "/something";
  1193. char buf[1024];
  1194. int blen = sizeof(buf);
  1195. int rc;
  1196. watchctx_t ctx;
  1197. zhandle_t *zk;
  1198. /* setup path */
  1199. zk = createClient(&ctx);
  1200. CPPUNIT_ASSERT(zk);
  1201. rc = zoo_create(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1202. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1203. rc = zoo_create(zk, "/something2", "", 0,
  1204. &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0);
  1205. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1206. /* remove all watchers */
  1207. count = 0;
  1208. rc = zoo_wget(zk, path, watcher_rw, NULL, buf, &blen, NULL);
  1209. rc = zoo_wget(zk, path, watcher_rw2, NULL, buf, &blen, NULL);
  1210. rc = zoo_remove_all_watches(zk, path, ZWATCHTYPE_ANY, 0);
  1211. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1212. rc = zoo_set(zk, path, "nowatch", 7, -1);
  1213. CPPUNIT_ASSERT(count == 0);
  1214. /* remove a specific watcher before it's added (should fail) */
  1215. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1216. watcher_rw, NULL, 0);
  1217. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1218. /* now add a specific watcher and then remove it */
  1219. rc = zoo_wget(zk, path, watcher_rw, NULL,
  1220. buf, &blen, NULL);
  1221. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1222. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1223. watcher_rw, NULL, 0);
  1224. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1225. /* ditto for children watcher */
  1226. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD,
  1227. watcher_rw, NULL, 0);
  1228. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1229. struct String_vector str_vec = {0, NULL};
  1230. rc = zoo_wget_children(zk, path, watcher_rw, NULL,
  1231. &str_vec);
  1232. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1233. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD,
  1234. watcher_rw, NULL, 0);
  1235. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1236. /* add a watch, stop the server, and have remove fail */
  1237. rc = zoo_wget(zk, path, watcher_rw, NULL,
  1238. buf, &blen, NULL);
  1239. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1240. stopServer();
  1241. ctx.waitForDisconnected(zk);
  1242. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1243. watcher_rw, NULL, 0);
  1244. CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS, rc);
  1245. zookeeper_close(zk);
  1246. /* bring the server back */
  1247. startServer();
  1248. zk = createClient(&ctx);
  1249. /* add a watch, stop the server, and remove it locally */
  1250. void* ctx1=(void*)0x1;
  1251. void* ctx2=(void*)0x2;
  1252. rc = zoo_wget(zk, path, watcher_rw, ctx1,
  1253. buf, &blen, NULL);
  1254. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1255. rc = zoo_wget(zk, "/something2", watcher_rw, ctx2,
  1256. buf, &blen, NULL);
  1257. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1258. stopServer();
  1259. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1260. watcher_rw, ctx1, 1);
  1261. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  1262. rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA,
  1263. watcher_rw, ctx1, 1);
  1264. CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc);
  1265. rc = zoo_remove_watches(zk, "/something2", ZWATCHTYPE_DATA,
  1266. watcher_rw, ctx2, 1);
  1267. CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
  1268. }
  1269. };
  1270. volatile int Zookeeper_simpleSystem::count;
  1271. zhandle_t *Zookeeper_simpleSystem::async_zk;
  1272. const char Zookeeper_simpleSystem::hostPorts[] = "127.0.0.1:22181";
  1273. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_simpleSystem);
  1274. #endif