TestClient.cc 49 KB

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