TestClient.cc 52 KB

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