TestZookeeperInit.cc 11 KB

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