TestClient.cc 50 KB

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