TestZookeeperInit.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <sys/types.h>
  20. #include <netinet/in.h>
  21. #include <errno.h>
  22. #include "Util.h"
  23. #include "LibCMocks.h"
  24. #include "ZKMocks.h"
  25. #ifdef THREADED
  26. #include "PthreadMocks.h"
  27. #else
  28. class MockPthreadsNull;
  29. #endif
  30. using namespace std;
  31. class Zookeeper_init : public CPPUNIT_NS::TestFixture
  32. {
  33. CPPUNIT_TEST_SUITE(Zookeeper_init);
  34. CPPUNIT_TEST(testVersion);
  35. CPPUNIT_TEST(testBasic);
  36. CPPUNIT_TEST(testAddressResolution);
  37. CPPUNIT_TEST(testMultipleAddressResolution);
  38. CPPUNIT_TEST(testNullAddressString);
  39. CPPUNIT_TEST(testEmptyAddressString);
  40. CPPUNIT_TEST(testOneSpaceAddressString);
  41. CPPUNIT_TEST(testTwoSpacesAddressString);
  42. CPPUNIT_TEST(testInvalidAddressString1);
  43. CPPUNIT_TEST(testInvalidAddressString2);
  44. CPPUNIT_TEST(testNonexistentHost);
  45. CPPUNIT_TEST(testOutOfMemory_init);
  46. CPPUNIT_TEST(testOutOfMemory_getaddrs1);
  47. #if !defined(__CYGWIN__) // not valid for cygwin
  48. CPPUNIT_TEST(testOutOfMemory_getaddrs2);
  49. #endif
  50. CPPUNIT_TEST(testPermuteAddrsList);
  51. CPPUNIT_TEST_SUITE_END();
  52. zhandle_t *zh;
  53. MockPthreadsNull* pthreadMock;
  54. static void watcher(zhandle_t *, int , int , const char *,void*){}
  55. FILE *logfile;
  56. public:
  57. Zookeeper_init():zh(0),pthreadMock(0){
  58. logfile = openlogfile("Zookeeper_init");
  59. }
  60. ~Zookeeper_init() {
  61. if (logfile) {
  62. fflush(logfile);
  63. fclose(logfile);
  64. logfile = 0;
  65. }
  66. }
  67. void setUp()
  68. {
  69. zoo_set_log_stream(logfile);
  70. zoo_deterministic_conn_order(0);
  71. #ifdef THREADED
  72. // disable threading
  73. pthreadMock=new MockPthreadZKNull;
  74. #endif
  75. zh=0;
  76. }
  77. void tearDown()
  78. {
  79. zookeeper_close(zh);
  80. #ifdef THREADED
  81. delete pthreadMock;
  82. #endif
  83. }
  84. void testVersion()
  85. {
  86. CPPUNIT_ASSERT_EQUAL(string(ZOO_VERSION), string(zoo_version_str()));
  87. }
  88. void testBasic()
  89. {
  90. const string EXPECTED_HOST("127.0.0.1:2121");
  91. const unsigned int EXPECTED_ADDRS_COUNT =1;
  92. const int EXPECTED_RECV_TIMEOUT=10000;
  93. clientid_t cid;
  94. memset(&cid,0xFE,sizeof(cid));
  95. zh=zookeeper_init(EXPECTED_HOST.c_str(),watcher,EXPECTED_RECV_TIMEOUT,
  96. &cid,(void*)1,0);
  97. CPPUNIT_ASSERT(zh != NULL);
  98. CPPUNIT_ASSERT(zh->fd->sock == -1);
  99. CPPUNIT_ASSERT(zh->hostname != NULL);
  100. CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count);
  101. CPPUNIT_ASSERT_EQUAL(EXPECTED_HOST,string(zh->hostname));
  102. CPPUNIT_ASSERT(zh->state == ZOO_NOTCONNECTED_STATE);
  103. CPPUNIT_ASSERT(zh->context == (void*)1);
  104. CPPUNIT_ASSERT_EQUAL(EXPECTED_RECV_TIMEOUT,zh->recv_timeout);
  105. CPPUNIT_ASSERT(zh->watcher == watcher);
  106. CPPUNIT_ASSERT(zh->addrs.next==0);
  107. CPPUNIT_ASSERT(zh->primer_buffer.buffer==zh->primer_storage_buffer);
  108. CPPUNIT_ASSERT(zh->primer_buffer.curr_offset ==0);
  109. CPPUNIT_ASSERT(zh->primer_buffer.len == sizeof(zh->primer_storage_buffer));
  110. CPPUNIT_ASSERT(zh->primer_buffer.next == 0);
  111. CPPUNIT_ASSERT(zh->last_zxid ==0);
  112. CPPUNIT_ASSERT(memcmp(&zh->client_id,&cid,sizeof(cid))==0);
  113. #ifdef THREADED
  114. // thread specific checks
  115. adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv;
  116. CPPUNIT_ASSERT(adaptor!=0);
  117. CPPUNIT_ASSERT(pthreadMock->pthread_createCounter==2);
  118. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->io));
  119. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->completion));
  120. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_process.lock));
  121. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_send.lock));
  122. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.lock));
  123. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.lock));
  124. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.cond));
  125. CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.cond));
  126. #endif
  127. }
  128. void testAddressResolution()
  129. {
  130. const char EXPECTED_IPS[][4]={{127,0,0,1}};
  131. const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS);
  132. zoo_deterministic_conn_order(1);
  133. zh=zookeeper_init("127.0.0.1:2121",0,10000,0,0,0);
  134. CPPUNIT_ASSERT(zh!=0);
  135. CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count);
  136. for(unsigned int i=0;i<zh->addrs.count;i++){
  137. sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i];
  138. CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0);
  139. CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port));
  140. }
  141. }
  142. void testMultipleAddressResolution()
  143. {
  144. const string EXPECTED_HOST("127.0.0.1:2121,127.0.0.2:3434");
  145. const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}};
  146. const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS);
  147. zoo_deterministic_conn_order(1);
  148. zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0);
  149. CPPUNIT_ASSERT(zh!=0);
  150. CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count);
  151. for(unsigned int i=0;i<zh->addrs.count;i++){
  152. sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i];
  153. CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0);
  154. if(i<1)
  155. CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port));
  156. else
  157. CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port));
  158. }
  159. }
  160. void testMultipleAddressWithSpace()
  161. {
  162. const string EXPECTED_HOST("127.0.0.1:2121, 127.0.0.2:3434");
  163. const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}};
  164. const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS);
  165. zoo_deterministic_conn_order(1);
  166. zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0);
  167. CPPUNIT_ASSERT(zh!=0);
  168. CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count);
  169. for(unsigned int i=0;i<zh->addrs.count;i++){
  170. sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i];
  171. CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0);
  172. if(i<1)
  173. CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port));
  174. else
  175. CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port));
  176. }
  177. }
  178. void testNullAddressString()
  179. {
  180. zh=zookeeper_init(NULL,0,0,0,0,0);
  181. CPPUNIT_ASSERT(zh==0);
  182. CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  183. }
  184. void testEmptyAddressString()
  185. {
  186. const string INVALID_HOST("");
  187. zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
  188. CPPUNIT_ASSERT(zh==0);
  189. CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  190. }
  191. void testOneSpaceAddressString()
  192. {
  193. const string INVALID_HOST(" ");
  194. zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
  195. CPPUNIT_ASSERT(zh==0);
  196. CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  197. }
  198. void testTwoSpacesAddressString()
  199. {
  200. const string INVALID_HOST(" ");
  201. zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
  202. CPPUNIT_ASSERT(zh==0);
  203. CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  204. }
  205. void testInvalidAddressString1()
  206. {
  207. const string INVALID_HOST("host1");
  208. zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
  209. CPPUNIT_ASSERT(zh==0);
  210. CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  211. }
  212. void testInvalidAddressString2()
  213. {
  214. const string INVALID_HOST("host1:1111+host:123");
  215. zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0);
  216. CPPUNIT_ASSERT(zh==0);
  217. CPPUNIT_ASSERT((ENOENT|EINVAL) & errno);
  218. }
  219. void testNonexistentHost()
  220. {
  221. const string EXPECTED_HOST("host1.blabadibla.bla.:1111");
  222. zh=zookeeper_init(EXPECTED_HOST.c_str(),0,0,0,0,0);
  223. CPPUNIT_ASSERT(zh==0);
  224. //With the switch to thread safe getaddrinfo, we don't get
  225. //these global variables
  226. //CPPUNIT_ASSERT_EQUAL(EINVAL,errno);
  227. //CPPUNIT_ASSERT_EQUAL(HOST_NOT_FOUND,h_errno);
  228. }
  229. void testOutOfMemory_init()
  230. {
  231. Mock_calloc mock;
  232. mock.callsBeforeFailure=0; // fail first calloc in init()
  233. zh=zookeeper_init("ahost:123",watcher,10000,0,0,0);
  234. CPPUNIT_ASSERT(zh==0);
  235. CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
  236. }
  237. void testOutOfMemory_getaddrs1()
  238. {
  239. Mock_realloc reallocMock;
  240. reallocMock.callsBeforeFailure=0; // fail on first call to realloc
  241. zh=zookeeper_init("127.0.0.1:123",0,0,0,0,0);
  242. CPPUNIT_ASSERT(zh==0);
  243. CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
  244. }
  245. void testOutOfMemory_getaddrs2()
  246. {
  247. Mock_realloc reallocMock;
  248. reallocMock.callsBeforeFailure=1; // fail on the second call to realloc
  249. zh=zookeeper_init("127.0.0.1:123,127.0.0.2:123,127.0.0.3:123,127.0.0.4:123,127.0.0.5:123,127.0.0.6:123,127.0.0.7:123,127.0.0.8:123,127.0.0.9:123,127.0.0.10:123,127.0.0.11:123,127.0.0.12:123,127.0.0.13:123,127.0.0.14:123,127.0.0.15:123,127.0.0.16:123,127.0.0.17:123",0,0,0,0,0);
  250. CPPUNIT_ASSERT(zh==0);
  251. CPPUNIT_ASSERT_EQUAL(ENOMEM,errno);
  252. }
  253. void testPermuteAddrsList()
  254. {
  255. const char EXPECTED[][5]={"\0\0\0\0","\1\1\1\1","\2\2\2\2","\3\3\3\3"};
  256. const unsigned int EXPECTED_ADDR_COUNT=COUNTOF(EXPECTED);
  257. const int RAND_SEQ[]={0,1,1,-1};
  258. const int RAND_SIZE=COUNTOF(RAND_SEQ);
  259. Mock_random randomMock;
  260. randomMock.randomReturns.assign(RAND_SEQ,RAND_SEQ+RAND_SIZE-1);
  261. zh=zookeeper_init("0.0.0.0:123,1.1.1.1:123,2.2.2.2:123,3.3.3.3:123",0,1000,0,0,0);
  262. CPPUNIT_ASSERT(zh!=0);
  263. CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDR_COUNT,zh->addrs.count);
  264. const string EXPECTED_SEQ("3210");
  265. char ACTUAL_SEQ[EXPECTED_ADDR_COUNT+1]; ACTUAL_SEQ[EXPECTED_ADDR_COUNT]=0;
  266. for(unsigned int i=0;i<zh->addrs.count;i++){
  267. sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i];
  268. // match the first byte of the EXPECTED and of the actual address
  269. ACTUAL_SEQ[i]=((char*)&addr->sin_addr)[0]+'0';
  270. }
  271. CPPUNIT_ASSERT_EQUAL(EXPECTED_SEQ,string(ACTUAL_SEQ));
  272. }
  273. };
  274. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_init);