TestClient.cc 54 KB

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