test_status.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. #define STATUS_PRIVATE
  2. #define HIBERNATE_PRIVATE
  3. #define LOG_PRIVATE
  4. #define REPHIST_PRIVATE
  5. #include <float.h>
  6. #include <math.h>
  7. #include "or.h"
  8. #include "torlog.h"
  9. #include "tor_queue.h"
  10. #include "status.h"
  11. #include "circuitlist.h"
  12. #include "config.h"
  13. #include "hibernate.h"
  14. #include "rephist.h"
  15. #include "relay.h"
  16. #include "router.h"
  17. #include "main.h"
  18. #include "nodelist.h"
  19. #include "statefile.h"
  20. #include "test.h"
  21. #define NS_MODULE status
  22. #define NS_SUBMODULE count_circuits
  23. /*
  24. * Test that count_circuits() is correctly counting the number of
  25. * global circuits.
  26. */
  27. static smartlist_t * mock_global_circuitlist = NULL;
  28. NS_DECL(smartlist_t *, circuit_get_global_list, (void));
  29. static void
  30. NS(test_main)(void *arg)
  31. {
  32. /* Choose origin_circuit_t wlog. */
  33. origin_circuit_t *mock_circuit1, *mock_circuit2;
  34. int expected_circuits = 2, actual_circuits;
  35. (void)arg;
  36. mock_circuit1 = tor_malloc_zero(sizeof(origin_circuit_t));
  37. mock_circuit2 = tor_malloc_zero(sizeof(origin_circuit_t));
  38. mock_global_circuitlist = smartlist_new();
  39. smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit1));
  40. smartlist_add(mock_global_circuitlist, TO_CIRCUIT(mock_circuit2));
  41. NS_MOCK(circuit_get_global_list);
  42. actual_circuits = count_circuits();
  43. tt_assert(expected_circuits == actual_circuits);
  44. done:
  45. tor_free(mock_circuit1);
  46. tor_free(mock_circuit2);
  47. smartlist_free(mock_global_circuitlist);
  48. mock_global_circuitlist = NULL;
  49. NS_UNMOCK(circuit_get_global_list);
  50. }
  51. static smartlist_t *
  52. NS(circuit_get_global_list)(void)
  53. {
  54. return mock_global_circuitlist;
  55. }
  56. #undef NS_SUBMODULE
  57. #define NS_SUBMODULE secs_to_uptime
  58. /*
  59. * Test that secs_to_uptime() is converting the number of seconds that
  60. * Tor is up for into the appropriate string form containing hours and minutes.
  61. */
  62. static void
  63. NS(test_main)(void *arg)
  64. {
  65. const char *expected;
  66. char *actual;
  67. (void)arg;
  68. expected = "0:00 hours";
  69. actual = secs_to_uptime(0);
  70. tt_str_op(actual, OP_EQ, expected);
  71. tor_free(actual);
  72. expected = "0:00 hours";
  73. actual = secs_to_uptime(1);
  74. tt_str_op(actual, OP_EQ, expected);
  75. tor_free(actual);
  76. expected = "0:01 hours";
  77. actual = secs_to_uptime(60);
  78. tt_str_op(actual, OP_EQ, expected);
  79. tor_free(actual);
  80. expected = "0:59 hours";
  81. actual = secs_to_uptime(60 * 59);
  82. tt_str_op(actual, OP_EQ, expected);
  83. tor_free(actual);
  84. expected = "1:00 hours";
  85. actual = secs_to_uptime(60 * 60);
  86. tt_str_op(actual, OP_EQ, expected);
  87. tor_free(actual);
  88. expected = "23:59 hours";
  89. actual = secs_to_uptime(60 * 60 * 23 + 60 * 59);
  90. tt_str_op(actual, OP_EQ, expected);
  91. tor_free(actual);
  92. expected = "1 day 0:00 hours";
  93. actual = secs_to_uptime(60 * 60 * 23 + 60 * 60);
  94. tt_str_op(actual, OP_EQ, expected);
  95. tor_free(actual);
  96. expected = "1 day 0:00 hours";
  97. actual = secs_to_uptime(86400 + 1);
  98. tt_str_op(actual, OP_EQ, expected);
  99. tor_free(actual);
  100. expected = "1 day 0:01 hours";
  101. actual = secs_to_uptime(86400 + 60);
  102. tt_str_op(actual, OP_EQ, expected);
  103. tor_free(actual);
  104. expected = "10 days 0:00 hours";
  105. actual = secs_to_uptime(86400 * 10);
  106. tt_str_op(actual, OP_EQ, expected);
  107. tor_free(actual);
  108. expected = "10 days 0:00 hours";
  109. actual = secs_to_uptime(864000 + 1);
  110. tt_str_op(actual, OP_EQ, expected);
  111. tor_free(actual);
  112. expected = "10 days 0:01 hours";
  113. actual = secs_to_uptime(864000 + 60);
  114. tt_str_op(actual, OP_EQ, expected);
  115. tor_free(actual);
  116. done:
  117. if (actual != NULL)
  118. tor_free(actual);
  119. }
  120. #undef NS_SUBMODULE
  121. #define NS_SUBMODULE bytes_to_usage
  122. /*
  123. * Test that bytes_to_usage() is correctly converting the number of bytes that
  124. * Tor has read/written into the appropriate string form containing kilobytes,
  125. * megabytes, or gigabytes.
  126. */
  127. static void
  128. NS(test_main)(void *arg)
  129. {
  130. const char *expected;
  131. char *actual;
  132. (void)arg;
  133. expected = "0 kB";
  134. actual = bytes_to_usage(0);
  135. tt_str_op(actual, OP_EQ, expected);
  136. tor_free(actual);
  137. expected = "0 kB";
  138. actual = bytes_to_usage(1);
  139. tt_str_op(actual, OP_EQ, expected);
  140. tor_free(actual);
  141. expected = "1 kB";
  142. actual = bytes_to_usage(1024);
  143. tt_str_op(actual, OP_EQ, expected);
  144. tor_free(actual);
  145. expected = "1023 kB";
  146. actual = bytes_to_usage((1 << 20) - 1);
  147. tt_str_op(actual, OP_EQ, expected);
  148. tor_free(actual);
  149. expected = "1.00 MB";
  150. actual = bytes_to_usage((1 << 20));
  151. tt_str_op(actual, OP_EQ, expected);
  152. tor_free(actual);
  153. expected = "1.00 MB";
  154. actual = bytes_to_usage((1 << 20) + 5242);
  155. tt_str_op(actual, OP_EQ, expected);
  156. tor_free(actual);
  157. expected = "1.01 MB";
  158. actual = bytes_to_usage((1 << 20) + 5243);
  159. tt_str_op(actual, OP_EQ, expected);
  160. tor_free(actual);
  161. expected = "1024.00 MB";
  162. actual = bytes_to_usage((1 << 30) - 1);
  163. tt_str_op(actual, OP_EQ, expected);
  164. tor_free(actual);
  165. expected = "1.00 GB";
  166. actual = bytes_to_usage((1 << 30));
  167. tt_str_op(actual, OP_EQ, expected);
  168. tor_free(actual);
  169. expected = "1.00 GB";
  170. actual = bytes_to_usage((1 << 30) + 5368709);
  171. tt_str_op(actual, OP_EQ, expected);
  172. tor_free(actual);
  173. expected = "1.01 GB";
  174. actual = bytes_to_usage((1 << 30) + 5368710);
  175. tt_str_op(actual, OP_EQ, expected);
  176. tor_free(actual);
  177. expected = "10.00 GB";
  178. actual = bytes_to_usage((U64_LITERAL(1) << 30) * 10L);
  179. tt_str_op(actual, OP_EQ, expected);
  180. tor_free(actual);
  181. done:
  182. if (actual != NULL)
  183. tor_free(actual);
  184. }
  185. #undef NS_SUBMODULE
  186. #define NS_SUBMODULE ASPECT(log_heartbeat, fails)
  187. /*
  188. * Tests that log_heartbeat() fails when in the public server mode,
  189. * not hibernating, and we couldn't get the current routerinfo.
  190. */
  191. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  192. NS_DECL(int, we_are_hibernating, (void));
  193. NS_DECL(const or_options_t *, get_options, (void));
  194. NS_DECL(int, public_server_mode, (const or_options_t *options));
  195. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  196. static void
  197. NS(test_main)(void *arg)
  198. {
  199. int expected, actual;
  200. (void)arg;
  201. NS_MOCK(tls_get_write_overhead_ratio);
  202. NS_MOCK(we_are_hibernating);
  203. NS_MOCK(get_options);
  204. NS_MOCK(public_server_mode);
  205. NS_MOCK(router_get_my_routerinfo);
  206. expected = -1;
  207. actual = log_heartbeat(0);
  208. tt_int_op(actual, OP_EQ, expected);
  209. done:
  210. NS_UNMOCK(tls_get_write_overhead_ratio);
  211. NS_UNMOCK(we_are_hibernating);
  212. NS_UNMOCK(get_options);
  213. NS_UNMOCK(public_server_mode);
  214. NS_UNMOCK(router_get_my_routerinfo);
  215. }
  216. static double
  217. NS(tls_get_write_overhead_ratio)(void)
  218. {
  219. return 2.0;
  220. }
  221. static int
  222. NS(we_are_hibernating)(void)
  223. {
  224. return 0;
  225. }
  226. static const or_options_t *
  227. NS(get_options)(void)
  228. {
  229. return NULL;
  230. }
  231. static int
  232. NS(public_server_mode)(const or_options_t *options)
  233. {
  234. (void)options;
  235. return 1;
  236. }
  237. static const routerinfo_t *
  238. NS(router_get_my_routerinfo)(void)
  239. {
  240. return NULL;
  241. }
  242. #undef NS_SUBMODULE
  243. #define NS_SUBMODULE ASPECT(log_heartbeat, not_in_consensus)
  244. /*
  245. * Tests that log_heartbeat() logs appropriately if we are not in the cached
  246. * consensus.
  247. */
  248. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  249. NS_DECL(int, we_are_hibernating, (void));
  250. NS_DECL(const or_options_t *, get_options, (void));
  251. NS_DECL(int, public_server_mode, (const or_options_t *options));
  252. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  253. NS_DECL(const node_t *, node_get_by_id, (const char *identity_digest));
  254. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  255. const char *funcname, const char *suffix, const char *format, va_list ap));
  256. NS_DECL(int, server_mode, (const or_options_t *options));
  257. static routerinfo_t *mock_routerinfo;
  258. extern int onion_handshakes_requested[MAX_ONION_HANDSHAKE_TYPE+1];
  259. extern int onion_handshakes_assigned[MAX_ONION_HANDSHAKE_TYPE+1];
  260. static void
  261. NS(test_main)(void *arg)
  262. {
  263. int expected, actual;
  264. (void)arg;
  265. NS_MOCK(tls_get_write_overhead_ratio);
  266. NS_MOCK(we_are_hibernating);
  267. NS_MOCK(get_options);
  268. NS_MOCK(public_server_mode);
  269. NS_MOCK(router_get_my_routerinfo);
  270. NS_MOCK(node_get_by_id);
  271. NS_MOCK(logv);
  272. NS_MOCK(server_mode);
  273. log_global_min_severity_ = LOG_DEBUG;
  274. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_TAP] = 1;
  275. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_TAP] = 1;
  276. onion_handshakes_requested[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  277. onion_handshakes_assigned[ONION_HANDSHAKE_TYPE_NTOR] = 1;
  278. expected = 0;
  279. actual = log_heartbeat(0);
  280. tt_int_op(actual, OP_EQ, expected);
  281. tt_int_op(CALLED(logv), OP_EQ, 3);
  282. done:
  283. NS_UNMOCK(tls_get_write_overhead_ratio);
  284. NS_UNMOCK(we_are_hibernating);
  285. NS_UNMOCK(get_options);
  286. NS_UNMOCK(public_server_mode);
  287. NS_UNMOCK(router_get_my_routerinfo);
  288. NS_UNMOCK(node_get_by_id);
  289. NS_UNMOCK(logv);
  290. NS_UNMOCK(server_mode);
  291. tor_free(mock_routerinfo);
  292. }
  293. static double
  294. NS(tls_get_write_overhead_ratio)(void)
  295. {
  296. return 1.0;
  297. }
  298. static int
  299. NS(we_are_hibernating)(void)
  300. {
  301. return 0;
  302. }
  303. static const or_options_t *
  304. NS(get_options)(void)
  305. {
  306. return NULL;
  307. }
  308. static int
  309. NS(public_server_mode)(const or_options_t *options)
  310. {
  311. (void)options;
  312. return 1;
  313. }
  314. static const routerinfo_t *
  315. NS(router_get_my_routerinfo)(void)
  316. {
  317. mock_routerinfo = tor_malloc(sizeof(routerinfo_t));
  318. return mock_routerinfo;
  319. }
  320. static const node_t *
  321. NS(node_get_by_id)(const char *identity_digest)
  322. {
  323. (void)identity_digest;
  324. return NULL;
  325. }
  326. static void
  327. NS(logv)(int severity, log_domain_mask_t domain,
  328. const char *funcname, const char *suffix, const char *format, va_list ap)
  329. {
  330. switch (CALLED(logv))
  331. {
  332. case 0:
  333. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  334. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  335. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  336. tt_ptr_op(suffix, OP_EQ, NULL);
  337. tt_str_op(format, OP_EQ,
  338. "Heartbeat: It seems like we are not in the cached consensus.");
  339. break;
  340. case 1:
  341. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  342. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  343. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  344. tt_ptr_op(suffix, OP_EQ, NULL);
  345. tt_str_op(format, OP_EQ,
  346. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  347. "I've sent %s and received %s.%s");
  348. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  349. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  350. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  351. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  352. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  353. break;
  354. case 2:
  355. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  356. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  357. tt_ptr_op(
  358. strstr(funcname, "rep_hist_log_circuit_handshake_stats"), OP_NE, NULL);
  359. tt_ptr_op(suffix, OP_EQ, NULL);
  360. tt_str_op(format, OP_EQ,
  361. "Circuit handshake stats since last time: %d/%d TAP, %d/%d NTor.");
  362. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (TAP) */
  363. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (TAP) */
  364. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes assigned (NTOR) */
  365. tt_int_op(va_arg(ap, int), OP_EQ, 1); /* handshakes requested (NTOR) */
  366. break;
  367. default:
  368. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  369. break;
  370. }
  371. done:
  372. CALLED(logv)++;
  373. }
  374. static int
  375. NS(server_mode)(const or_options_t *options)
  376. {
  377. (void)options;
  378. return 0;
  379. }
  380. #undef NS_SUBMODULE
  381. #define NS_SUBMODULE ASPECT(log_heartbeat, simple)
  382. /*
  383. * Tests that log_heartbeat() correctly logs heartbeat information
  384. * normally.
  385. */
  386. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  387. NS_DECL(int, we_are_hibernating, (void));
  388. NS_DECL(const or_options_t *, get_options, (void));
  389. NS_DECL(int, public_server_mode, (const or_options_t *options));
  390. NS_DECL(long, get_uptime, (void));
  391. NS_DECL(uint64_t, get_bytes_read, (void));
  392. NS_DECL(uint64_t, get_bytes_written, (void));
  393. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  394. const char *funcname, const char *suffix, const char *format, va_list ap));
  395. NS_DECL(int, server_mode, (const or_options_t *options));
  396. static void
  397. NS(test_main)(void *arg)
  398. {
  399. int expected, actual;
  400. (void)arg;
  401. NS_MOCK(tls_get_write_overhead_ratio);
  402. NS_MOCK(we_are_hibernating);
  403. NS_MOCK(get_options);
  404. NS_MOCK(public_server_mode);
  405. NS_MOCK(get_uptime);
  406. NS_MOCK(get_bytes_read);
  407. NS_MOCK(get_bytes_written);
  408. NS_MOCK(logv);
  409. NS_MOCK(server_mode);
  410. log_global_min_severity_ = LOG_DEBUG;
  411. expected = 0;
  412. actual = log_heartbeat(0);
  413. tt_int_op(actual, OP_EQ, expected);
  414. done:
  415. NS_UNMOCK(tls_get_write_overhead_ratio);
  416. NS_UNMOCK(we_are_hibernating);
  417. NS_UNMOCK(get_options);
  418. NS_UNMOCK(public_server_mode);
  419. NS_UNMOCK(get_uptime);
  420. NS_UNMOCK(get_bytes_read);
  421. NS_UNMOCK(get_bytes_written);
  422. NS_UNMOCK(logv);
  423. NS_UNMOCK(server_mode);
  424. }
  425. static double
  426. NS(tls_get_write_overhead_ratio)(void)
  427. {
  428. return 1.0;
  429. }
  430. static int
  431. NS(we_are_hibernating)(void)
  432. {
  433. return 1;
  434. }
  435. static const or_options_t *
  436. NS(get_options)(void)
  437. {
  438. return NULL;
  439. }
  440. static int
  441. NS(public_server_mode)(const or_options_t *options)
  442. {
  443. (void)options;
  444. return 0;
  445. }
  446. static long
  447. NS(get_uptime)(void)
  448. {
  449. return 0;
  450. }
  451. static uint64_t
  452. NS(get_bytes_read)(void)
  453. {
  454. return 0;
  455. }
  456. static uint64_t
  457. NS(get_bytes_written)(void)
  458. {
  459. return 0;
  460. }
  461. static void
  462. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  463. const char *suffix, const char *format, va_list ap)
  464. {
  465. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  466. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  467. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  468. tt_ptr_op(suffix, OP_EQ, NULL);
  469. tt_str_op(format, OP_EQ,
  470. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  471. "I've sent %s and received %s.%s");
  472. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  473. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  474. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  475. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  476. tt_str_op(va_arg(ap, char *), OP_EQ, " We are currently hibernating.");
  477. done:
  478. ;
  479. }
  480. static int
  481. NS(server_mode)(const or_options_t *options)
  482. {
  483. (void)options;
  484. return 0;
  485. }
  486. #undef NS_SUBMODULE
  487. #define NS_SUBMODULE ASPECT(log_heartbeat, calls_log_accounting)
  488. /*
  489. * Tests that log_heartbeat() correctly logs heartbeat information
  490. * and accounting information when configured.
  491. */
  492. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  493. NS_DECL(int, we_are_hibernating, (void));
  494. NS_DECL(const or_options_t *, get_options, (void));
  495. NS_DECL(int, public_server_mode, (const or_options_t *options));
  496. NS_DECL(long, get_uptime, (void));
  497. NS_DECL(uint64_t, get_bytes_read, (void));
  498. NS_DECL(uint64_t, get_bytes_written, (void));
  499. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  500. const char *funcname, const char *suffix, const char *format, va_list ap));
  501. NS_DECL(int, server_mode, (const or_options_t *options));
  502. NS_DECL(or_state_t *, get_or_state, (void));
  503. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  504. NS_DECL(time_t, accounting_get_end_time, (void));
  505. static or_state_t * NS(mock_state) = NULL;
  506. static or_options_t * NS(mock_options) = NULL;
  507. static void
  508. NS(test_main)(void *arg)
  509. {
  510. int expected, actual;
  511. (void)arg;
  512. NS_MOCK(tls_get_write_overhead_ratio);
  513. NS_MOCK(we_are_hibernating);
  514. NS_MOCK(get_options);
  515. NS_MOCK(public_server_mode);
  516. NS_MOCK(get_uptime);
  517. NS_MOCK(get_bytes_read);
  518. NS_MOCK(get_bytes_written);
  519. NS_MOCK(logv);
  520. NS_MOCK(server_mode);
  521. NS_MOCK(get_or_state);
  522. NS_MOCK(accounting_is_enabled);
  523. NS_MOCK(accounting_get_end_time);
  524. log_global_min_severity_ = LOG_DEBUG;
  525. expected = 0;
  526. actual = log_heartbeat(0);
  527. tt_int_op(actual, OP_EQ, expected);
  528. tt_int_op(CALLED(logv), OP_EQ, 2);
  529. done:
  530. NS_UNMOCK(tls_get_write_overhead_ratio);
  531. NS_UNMOCK(we_are_hibernating);
  532. NS_UNMOCK(get_options);
  533. NS_UNMOCK(public_server_mode);
  534. NS_UNMOCK(get_uptime);
  535. NS_UNMOCK(get_bytes_read);
  536. NS_UNMOCK(get_bytes_written);
  537. NS_UNMOCK(logv);
  538. NS_UNMOCK(server_mode);
  539. NS_UNMOCK(accounting_is_enabled);
  540. NS_UNMOCK(accounting_get_end_time);
  541. tor_free_(NS(mock_state));
  542. tor_free_(NS(mock_options));
  543. }
  544. static double
  545. NS(tls_get_write_overhead_ratio)(void)
  546. {
  547. return 1.0;
  548. }
  549. static int
  550. NS(we_are_hibernating)(void)
  551. {
  552. return 0;
  553. }
  554. static const or_options_t *
  555. NS(get_options)(void)
  556. {
  557. NS(mock_options) = tor_malloc_zero(sizeof(or_options_t));
  558. NS(mock_options)->AccountingMax = 0;
  559. return NS(mock_options);
  560. }
  561. static int
  562. NS(public_server_mode)(const or_options_t *options)
  563. {
  564. (void)options;
  565. return 0;
  566. }
  567. static long
  568. NS(get_uptime)(void)
  569. {
  570. return 0;
  571. }
  572. static uint64_t
  573. NS(get_bytes_read)(void)
  574. {
  575. return 0;
  576. }
  577. static uint64_t
  578. NS(get_bytes_written)(void)
  579. {
  580. return 0;
  581. }
  582. static void
  583. NS(logv)(int severity, log_domain_mask_t domain,
  584. const char *funcname, const char *suffix, const char *format, va_list ap)
  585. {
  586. switch (CALLED(logv))
  587. {
  588. case 0:
  589. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  590. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  591. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  592. tt_ptr_op(suffix, OP_EQ, NULL);
  593. tt_str_op(format, OP_EQ,
  594. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  595. "I've sent %s and received %s.%s");
  596. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  597. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  598. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  599. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  600. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  601. break;
  602. case 1:
  603. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  604. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  605. tt_ptr_op(strstr(funcname, "log_accounting"), OP_NE, NULL);
  606. tt_ptr_op(suffix, OP_EQ, NULL);
  607. tt_str_op(format, OP_EQ,
  608. "Heartbeat: Accounting enabled. Sent: %s / %s, Received: %s / %s. "
  609. "The current accounting interval ends on %s, in %s.");
  610. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_sent */
  611. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
  612. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_rcvd */
  613. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* acc_max */
  614. /* format_local_iso_time uses local tz, just check mins and secs. */
  615. tt_ptr_op(strstr(va_arg(ap, char *), ":01:00"), OP_NE, NULL); /* end_buf */
  616. tt_str_op(va_arg(ap, char *), OP_EQ, "0:01 hours"); /* remaining */
  617. break;
  618. default:
  619. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  620. break;
  621. }
  622. done:
  623. CALLED(logv)++;
  624. }
  625. static int
  626. NS(server_mode)(const or_options_t *options)
  627. {
  628. (void)options;
  629. return 1;
  630. }
  631. static int
  632. NS(accounting_is_enabled)(const or_options_t *options)
  633. {
  634. (void)options;
  635. return 1;
  636. }
  637. static time_t
  638. NS(accounting_get_end_time)(void)
  639. {
  640. return 60;
  641. }
  642. static or_state_t *
  643. NS(get_or_state)(void)
  644. {
  645. NS(mock_state) = tor_malloc_zero(sizeof(or_state_t));
  646. NS(mock_state)->AccountingBytesReadInInterval = 0;
  647. NS(mock_state)->AccountingBytesWrittenInInterval = 0;
  648. return NS(mock_state);
  649. }
  650. #undef NS_SUBMODULE
  651. #define NS_SUBMODULE ASPECT(log_heartbeat, packaged_cell_fullness)
  652. /*
  653. * Tests that log_heartbeat() correctly logs packaged cell
  654. * fullness information.
  655. */
  656. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  657. NS_DECL(int, we_are_hibernating, (void));
  658. NS_DECL(const or_options_t *, get_options, (void));
  659. NS_DECL(int, public_server_mode, (const or_options_t *options));
  660. NS_DECL(long, get_uptime, (void));
  661. NS_DECL(uint64_t, get_bytes_read, (void));
  662. NS_DECL(uint64_t, get_bytes_written, (void));
  663. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  664. const char *funcname, const char *suffix, const char *format, va_list ap));
  665. NS_DECL(int, server_mode, (const or_options_t *options));
  666. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  667. static void
  668. NS(test_main)(void *arg)
  669. {
  670. int expected, actual;
  671. (void)arg;
  672. NS_MOCK(tls_get_write_overhead_ratio);
  673. NS_MOCK(we_are_hibernating);
  674. NS_MOCK(get_options);
  675. NS_MOCK(public_server_mode);
  676. NS_MOCK(get_uptime);
  677. NS_MOCK(get_bytes_read);
  678. NS_MOCK(get_bytes_written);
  679. NS_MOCK(logv);
  680. NS_MOCK(server_mode);
  681. NS_MOCK(accounting_is_enabled);
  682. log_global_min_severity_ = LOG_DEBUG;
  683. stats_n_data_bytes_packaged = RELAY_PAYLOAD_SIZE;
  684. stats_n_data_cells_packaged = 1;
  685. expected = 0;
  686. actual = log_heartbeat(0);
  687. tt_int_op(actual, OP_EQ, expected);
  688. tt_int_op(CALLED(logv), OP_EQ, 2);
  689. done:
  690. stats_n_data_bytes_packaged = 0;
  691. stats_n_data_cells_packaged = 0;
  692. NS_UNMOCK(tls_get_write_overhead_ratio);
  693. NS_UNMOCK(we_are_hibernating);
  694. NS_UNMOCK(get_options);
  695. NS_UNMOCK(public_server_mode);
  696. NS_UNMOCK(get_uptime);
  697. NS_UNMOCK(get_bytes_read);
  698. NS_UNMOCK(get_bytes_written);
  699. NS_UNMOCK(logv);
  700. NS_UNMOCK(server_mode);
  701. NS_UNMOCK(accounting_is_enabled);
  702. }
  703. static double
  704. NS(tls_get_write_overhead_ratio)(void)
  705. {
  706. return 1.0;
  707. }
  708. static int
  709. NS(we_are_hibernating)(void)
  710. {
  711. return 0;
  712. }
  713. static const or_options_t *
  714. NS(get_options)(void)
  715. {
  716. return NULL;
  717. }
  718. static int
  719. NS(public_server_mode)(const or_options_t *options)
  720. {
  721. (void)options;
  722. return 0;
  723. }
  724. static long
  725. NS(get_uptime)(void)
  726. {
  727. return 0;
  728. }
  729. static uint64_t
  730. NS(get_bytes_read)(void)
  731. {
  732. return 0;
  733. }
  734. static uint64_t
  735. NS(get_bytes_written)(void)
  736. {
  737. return 0;
  738. }
  739. static void
  740. NS(logv)(int severity, log_domain_mask_t domain, const char *funcname,
  741. const char *suffix, const char *format, va_list ap)
  742. {
  743. switch (CALLED(logv))
  744. {
  745. case 0:
  746. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  747. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  748. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  749. tt_ptr_op(suffix, OP_EQ, NULL);
  750. tt_str_op(format, OP_EQ,
  751. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  752. "I've sent %s and received %s.%s");
  753. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  754. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  755. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  756. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  757. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  758. break;
  759. case 1:
  760. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  761. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  762. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  763. tt_ptr_op(suffix, OP_EQ, NULL);
  764. tt_str_op(format, OP_EQ,
  765. "Average packaged cell fullness: %2.3f%%");
  766. tt_int_op(fabs(va_arg(ap, double) - 100.0) <= DBL_EPSILON, OP_EQ, 1);
  767. break;
  768. default:
  769. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  770. break;
  771. }
  772. done:
  773. CALLED(logv)++;
  774. }
  775. static int
  776. NS(server_mode)(const or_options_t *options)
  777. {
  778. (void)options;
  779. return 0;
  780. }
  781. static int
  782. NS(accounting_is_enabled)(const or_options_t *options)
  783. {
  784. (void)options;
  785. return 0;
  786. }
  787. #undef NS_SUBMODULE
  788. #define NS_SUBMODULE ASPECT(log_heartbeat, tls_write_overhead)
  789. /*
  790. * Tests that log_heartbeat() correctly logs the TLS write overhead information
  791. * when the TLS write overhead ratio exceeds 1.
  792. */
  793. NS_DECL(double, tls_get_write_overhead_ratio, (void));
  794. NS_DECL(int, we_are_hibernating, (void));
  795. NS_DECL(const or_options_t *, get_options, (void));
  796. NS_DECL(int, public_server_mode, (const or_options_t *options));
  797. NS_DECL(long, get_uptime, (void));
  798. NS_DECL(uint64_t, get_bytes_read, (void));
  799. NS_DECL(uint64_t, get_bytes_written, (void));
  800. NS_DECL(void, logv, (int severity, log_domain_mask_t domain,
  801. const char *funcname, const char *suffix, const char *format, va_list ap));
  802. NS_DECL(int, server_mode, (const or_options_t *options));
  803. NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
  804. static void
  805. NS(test_main)(void *arg)
  806. {
  807. int expected, actual;
  808. (void)arg;
  809. NS_MOCK(tls_get_write_overhead_ratio);
  810. NS_MOCK(we_are_hibernating);
  811. NS_MOCK(get_options);
  812. NS_MOCK(public_server_mode);
  813. NS_MOCK(get_uptime);
  814. NS_MOCK(get_bytes_read);
  815. NS_MOCK(get_bytes_written);
  816. NS_MOCK(logv);
  817. NS_MOCK(server_mode);
  818. NS_MOCK(accounting_is_enabled);
  819. stats_n_data_cells_packaged = 0;
  820. log_global_min_severity_ = LOG_DEBUG;
  821. expected = 0;
  822. actual = log_heartbeat(0);
  823. tt_int_op(actual, OP_EQ, expected);
  824. tt_int_op(CALLED(logv), OP_EQ, 2);
  825. done:
  826. NS_UNMOCK(tls_get_write_overhead_ratio);
  827. NS_UNMOCK(we_are_hibernating);
  828. NS_UNMOCK(get_options);
  829. NS_UNMOCK(public_server_mode);
  830. NS_UNMOCK(get_uptime);
  831. NS_UNMOCK(get_bytes_read);
  832. NS_UNMOCK(get_bytes_written);
  833. NS_UNMOCK(logv);
  834. NS_UNMOCK(server_mode);
  835. NS_UNMOCK(accounting_is_enabled);
  836. }
  837. static double
  838. NS(tls_get_write_overhead_ratio)(void)
  839. {
  840. return 2.0;
  841. }
  842. static int
  843. NS(we_are_hibernating)(void)
  844. {
  845. return 0;
  846. }
  847. static const or_options_t *
  848. NS(get_options)(void)
  849. {
  850. return NULL;
  851. }
  852. static int
  853. NS(public_server_mode)(const or_options_t *options)
  854. {
  855. (void)options;
  856. return 0;
  857. }
  858. static long
  859. NS(get_uptime)(void)
  860. {
  861. return 0;
  862. }
  863. static uint64_t
  864. NS(get_bytes_read)(void)
  865. {
  866. return 0;
  867. }
  868. static uint64_t
  869. NS(get_bytes_written)(void)
  870. {
  871. return 0;
  872. }
  873. static void
  874. NS(logv)(int severity, log_domain_mask_t domain,
  875. const char *funcname, const char *suffix, const char *format, va_list ap)
  876. {
  877. switch (CALLED(logv))
  878. {
  879. case 0:
  880. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  881. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  882. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  883. tt_ptr_op(suffix, OP_EQ, NULL);
  884. tt_str_op(format, OP_EQ,
  885. "Heartbeat: Tor's uptime is %s, with %d circuits open. "
  886. "I've sent %s and received %s.%s");
  887. tt_str_op(va_arg(ap, char *), OP_EQ, "0:00 hours"); /* uptime */
  888. tt_int_op(va_arg(ap, int), OP_EQ, 0); /* count_circuits() */
  889. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_sent */
  890. tt_str_op(va_arg(ap, char *), OP_EQ, "0 kB"); /* bw_rcvd */
  891. tt_str_op(va_arg(ap, char *), OP_EQ, ""); /* hibernating */
  892. break;
  893. case 1:
  894. tt_int_op(severity, OP_EQ, LOG_NOTICE);
  895. tt_int_op(domain, OP_EQ, LD_HEARTBEAT);
  896. tt_ptr_op(strstr(funcname, "log_heartbeat"), OP_NE, NULL);
  897. tt_ptr_op(suffix, OP_EQ, NULL);
  898. tt_str_op(format, OP_EQ, "TLS write overhead: %.f%%");
  899. tt_int_op(fabs(va_arg(ap, double) - 100.0) <= DBL_EPSILON, OP_EQ, 1);
  900. break;
  901. default:
  902. tt_abort_msg("unexpected call to logv()"); // TODO: prettyprint args
  903. break;
  904. }
  905. done:
  906. CALLED(logv)++;
  907. }
  908. static int
  909. NS(server_mode)(const or_options_t *options)
  910. {
  911. (void)options;
  912. return 0;
  913. }
  914. static int
  915. NS(accounting_is_enabled)(const or_options_t *options)
  916. {
  917. (void)options;
  918. return 0;
  919. }
  920. #undef NS_SUBMODULE
  921. struct testcase_t status_tests[] = {
  922. TEST_CASE(count_circuits),
  923. TEST_CASE(secs_to_uptime),
  924. TEST_CASE(bytes_to_usage),
  925. TEST_CASE_ASPECT(log_heartbeat, fails),
  926. TEST_CASE_ASPECT(log_heartbeat, simple),
  927. TEST_CASE_ASPECT(log_heartbeat, not_in_consensus),
  928. TEST_CASE_ASPECT(log_heartbeat, calls_log_accounting),
  929. TEST_CASE_ASPECT(log_heartbeat, packaged_cell_fullness),
  930. TEST_CASE_ASPECT(log_heartbeat, tls_write_overhead),
  931. END_OF_TESTCASES
  932. };