test_status.c 26 KB

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