TestClient.cc 50 KB

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