test_core_quota_compliance.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file core/test_core_quota_compliance.c
  18. * @brief testcase for core_api.c focusing quota compliance on core level
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_arm_service.h"
  23. #include "gnunet_core_service.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_ats_service.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gnunet_transport_hello_service.h"
  28. #include "gnunet_statistics_service.h"
  29. #define SYMMETRIC 0
  30. #define ASYMMETRIC_SEND_LIMITED 1
  31. #define ASYMMETRIC_RECV_LIMITED 2
  32. /**
  33. * Note that this value must not significantly exceed
  34. * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
  35. * messages may be dropped even for a reliable transport.
  36. */
  37. #define TOTAL_MSGS (60000 * 10)
  38. /**
  39. * How long until we give up on transmitting the message?
  40. */
  41. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  42. /**
  43. * What delay do we request from the core service for transmission?
  44. */
  45. #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  46. 150)
  47. #define MTYPE 12345
  48. #define MESSAGESIZE (1024 - 8)
  49. #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply ( \
  50. GNUNET_TIME_UNIT_SECONDS, 30)
  51. static unsigned long long total_bytes_sent;
  52. static unsigned long long total_bytes_recv;
  53. static struct GNUNET_TIME_Absolute start_time;
  54. static struct GNUNET_SCHEDULER_Task *err_task;
  55. static struct GNUNET_SCHEDULER_Task *measure_task;
  56. struct PeerContext
  57. {
  58. struct GNUNET_CONFIGURATION_Handle *cfg;
  59. struct GNUNET_CORE_Handle *ch;
  60. struct GNUNET_MQ_Handle *mq;
  61. struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
  62. struct GNUNET_PeerIdentity id;
  63. struct GNUNET_MessageHeader *hello;
  64. struct GNUNET_STATISTICS_Handle *stats;
  65. struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
  66. struct GNUNET_ATS_ConnectivityHandle *ats;
  67. struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
  68. int connect_status;
  69. struct GNUNET_OS_Process *arm_proc;
  70. };
  71. static struct PeerContext p1;
  72. static struct PeerContext p2;
  73. static unsigned long long current_quota_p1_in;
  74. static unsigned long long current_quota_p1_out;
  75. static unsigned long long current_quota_p2_in;
  76. static unsigned long long current_quota_p2_out;
  77. static int ok;
  78. static int test;
  79. static int32_t tr_n;
  80. static int running;
  81. #if VERBOSE
  82. #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, \
  83. "Now at stage %u at %s:%u\n", ok, __FILE__, \
  84. __LINE__); } while (0)
  85. #else
  86. #define OKPP do { ok++; } while (0)
  87. #endif
  88. struct TestMessage
  89. {
  90. struct GNUNET_MessageHeader header;
  91. uint32_t num GNUNET_PACKED;
  92. uint8_t pad[MESSAGESIZE];
  93. };
  94. static void
  95. terminate_peer (struct PeerContext *p)
  96. {
  97. if (NULL != p->ch)
  98. {
  99. GNUNET_CORE_disconnect (p->ch);
  100. p->ch = NULL;
  101. }
  102. if (NULL != p->ghh)
  103. {
  104. GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
  105. p->ghh = NULL;
  106. }
  107. if (NULL != p->oh)
  108. {
  109. GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
  110. p->oh = NULL;
  111. }
  112. if (NULL != p->ats_sh)
  113. {
  114. GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
  115. p->ats_sh = NULL;
  116. }
  117. if (NULL != p->ats)
  118. {
  119. GNUNET_ATS_connectivity_done (p->ats);
  120. p->ats = NULL;
  121. }
  122. if (NULL != p->stats)
  123. {
  124. GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
  125. p->stats = NULL;
  126. }
  127. if (NULL != p->hello)
  128. {
  129. GNUNET_free (p->hello);
  130. p->hello = NULL;
  131. }
  132. }
  133. static void
  134. shutdown_task (void *cls)
  135. {
  136. if (NULL != err_task)
  137. {
  138. GNUNET_SCHEDULER_cancel (err_task);
  139. err_task = NULL;
  140. }
  141. if (NULL != measure_task)
  142. {
  143. GNUNET_SCHEDULER_cancel (measure_task);
  144. measure_task = NULL;
  145. }
  146. terminate_peer (&p1);
  147. terminate_peer (&p2);
  148. }
  149. static void
  150. terminate_task_error (void *cls)
  151. {
  152. err_task = NULL;
  153. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  154. "Testcase failed (timeout)!\n");
  155. GNUNET_SCHEDULER_shutdown ();
  156. ok = 42;
  157. }
  158. /**
  159. * Callback function to process statistic values.
  160. *
  161. * @param cls closure
  162. * @param subsystem name of subsystem that created the statistic
  163. * @param name the name of the datum
  164. * @param value the current value
  165. * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
  166. * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  167. */
  168. static int
  169. print_stat (void *cls,
  170. const char *subsystem,
  171. const char *name,
  172. uint64_t value,
  173. int is_persistent)
  174. {
  175. if (cls == &p1)
  176. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  177. "Peer1 %50s = %12llu\n",
  178. name,
  179. (unsigned long long) value);
  180. if (cls == &p2)
  181. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  182. "Peer2 %50s = %12llu\n",
  183. name,
  184. (unsigned long long) value);
  185. return GNUNET_OK;
  186. }
  187. static void
  188. measurement_stop (void *cls)
  189. {
  190. unsigned long long delta;
  191. unsigned long long throughput_out;
  192. unsigned long long throughput_in;
  193. unsigned long long max_quota_in;
  194. unsigned long long max_quota_out;
  195. unsigned long long quota_delta;
  196. enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
  197. measure_task = NULL;
  198. fprintf (stdout, "%s", "\n");
  199. running = GNUNET_NO;
  200. delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
  201. if (0 == delta)
  202. delta = 1;
  203. throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
  204. throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
  205. max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
  206. max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
  207. if (max_quota_out < max_quota_in)
  208. quota_delta = max_quota_in / 3;
  209. else
  210. quota_delta = max_quota_out / 3;
  211. if ((throughput_out > (max_quota_out + quota_delta)) ||
  212. (throughput_in > (max_quota_in + quota_delta)))
  213. ok = 1; /* fail */
  214. else
  215. ok = 0; /* pass */
  216. GNUNET_STATISTICS_get (p1.stats,
  217. "core",
  218. "# discarded CORE_SEND requests",
  219. NULL,
  220. &print_stat,
  221. &p1);
  222. GNUNET_STATISTICS_get (p1.stats,
  223. "core",
  224. "# discarded CORE_SEND request bytes",
  225. NULL,
  226. &print_stat,
  227. &p1);
  228. GNUNET_STATISTICS_get (p1.stats,
  229. "core",
  230. "# discarded lower priority CORE_SEND requests",
  231. NULL,
  232. &print_stat,
  233. NULL);
  234. GNUNET_STATISTICS_get (p1.stats,
  235. "core",
  236. "# discarded lower priority CORE_SEND request bytes",
  237. NULL,
  238. &print_stat,
  239. &p1);
  240. GNUNET_STATISTICS_get (p2.stats,
  241. "core",
  242. "# discarded CORE_SEND requests",
  243. NULL,
  244. &print_stat,
  245. &p2);
  246. GNUNET_STATISTICS_get (p2.stats,
  247. "core",
  248. "# discarded CORE_SEND request bytes",
  249. NULL,
  250. &print_stat,
  251. &p2);
  252. GNUNET_STATISTICS_get (p2.stats,
  253. "core",
  254. "# discarded lower priority CORE_SEND requests",
  255. NULL,
  256. &print_stat,
  257. &p2);
  258. GNUNET_STATISTICS_get (p2.stats,
  259. "core",
  260. "# discarded lower priority CORE_SEND request bytes",
  261. NULL,
  262. &print_stat,
  263. &p2);
  264. if (ok != 0)
  265. kind = GNUNET_ERROR_TYPE_ERROR;
  266. switch (test)
  267. {
  268. case SYMMETRIC:
  269. GNUNET_log (kind,
  270. "Core quota compliance test with symmetric quotas: %s\n",
  271. (0 == ok) ? "PASSED" : "FAILED");
  272. break;
  273. case ASYMMETRIC_SEND_LIMITED:
  274. GNUNET_log (kind,
  275. "Core quota compliance test with limited sender quota: %s\n",
  276. (0 == ok) ? "PASSED" : "FAILED");
  277. break;
  278. case ASYMMETRIC_RECV_LIMITED:
  279. GNUNET_log (kind,
  280. "Core quota compliance test with limited receiver quota: %s\n",
  281. (0 == ok) ? "PASSED" : "FAILED");
  282. break;
  283. }
  284. ;
  285. GNUNET_log (kind,
  286. "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
  287. throughput_out,
  288. total_bytes_sent,
  289. delta);
  290. GNUNET_log (kind,
  291. "Peer 1 send quota: %llu b/s\n",
  292. current_quota_p1_out);
  293. GNUNET_log (kind,
  294. "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
  295. throughput_in,
  296. total_bytes_recv,
  297. delta);
  298. GNUNET_log (kind,
  299. "Peer 2 receive quota: %llu b/s\n",
  300. current_quota_p2_in);
  301. /*
  302. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
  303. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
  304. */
  305. GNUNET_SCHEDULER_shutdown ();
  306. }
  307. static void
  308. do_transmit (void *cls)
  309. {
  310. struct TestMessage *hdr;
  311. struct GNUNET_MQ_Envelope *env;
  312. env = GNUNET_MQ_msg (hdr,
  313. MTYPE);
  314. hdr->num = htonl (tr_n);
  315. memset (&hdr->pad,
  316. tr_n,
  317. MESSAGESIZE);
  318. tr_n++;
  319. GNUNET_SCHEDULER_cancel (err_task);
  320. err_task =
  321. GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  322. &terminate_task_error,
  323. NULL);
  324. total_bytes_sent += sizeof(struct TestMessage);
  325. GNUNET_MQ_send (p1.mq,
  326. env);
  327. }
  328. static void *
  329. connect_notify (void *cls,
  330. const struct GNUNET_PeerIdentity *peer,
  331. struct GNUNET_MQ_Handle *mq)
  332. {
  333. struct PeerContext *pc = cls;
  334. if (0 == memcmp (&pc->id,
  335. peer,
  336. sizeof(struct GNUNET_PeerIdentity)))
  337. return NULL; /* loopback */
  338. GNUNET_assert (0 == pc->connect_status);
  339. pc->connect_status = 1;
  340. pc->mq = mq;
  341. if (pc == &p1)
  342. {
  343. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  344. "Encrypted connection established to peer `%s'\n",
  345. GNUNET_i2s (peer));
  346. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  347. "Asking core (1) for transmission to peer `%s'\n",
  348. GNUNET_i2s (&p2.id));
  349. GNUNET_SCHEDULER_cancel (err_task);
  350. err_task =
  351. GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  352. &terminate_task_error,
  353. NULL);
  354. start_time = GNUNET_TIME_absolute_get ();
  355. running = GNUNET_YES;
  356. measure_task =
  357. GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
  358. &measurement_stop,
  359. NULL);
  360. do_transmit (NULL);
  361. }
  362. return pc;
  363. }
  364. static void
  365. disconnect_notify (void *cls,
  366. const struct GNUNET_PeerIdentity *peer,
  367. void *internal_cls)
  368. {
  369. struct PeerContext *pc = cls;
  370. if (NULL == internal_cls)
  371. return; /* loopback */
  372. pc->connect_status = 0;
  373. pc->mq = NULL;
  374. if (NULL != measure_task)
  375. {
  376. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  377. "Measurement aborted due to disconnect!\n");
  378. GNUNET_SCHEDULER_cancel (measure_task);
  379. measure_task = NULL;
  380. }
  381. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  382. "Encrypted connection to `%s' cut\n",
  383. GNUNET_i2s (peer));
  384. }
  385. static void
  386. handle_test (void *cls,
  387. const struct TestMessage *hdr)
  388. {
  389. static int n;
  390. total_bytes_recv += sizeof(struct TestMessage);
  391. if (ntohl (hdr->num) != n)
  392. {
  393. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  394. "Expected message %u, got message %u\n",
  395. n,
  396. ntohl (hdr->num));
  397. GNUNET_SCHEDULER_cancel (err_task);
  398. err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
  399. NULL);
  400. return;
  401. }
  402. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  403. "Got message %u\n",
  404. ntohl (hdr->num));
  405. n++;
  406. if (0 == (n % 10))
  407. fprintf (stderr, "%s", ".");
  408. if (GNUNET_YES == running)
  409. do_transmit (NULL);
  410. }
  411. static void
  412. init_notify (void *cls,
  413. const struct GNUNET_PeerIdentity *my_identity)
  414. {
  415. struct PeerContext *p = cls;
  416. struct GNUNET_MQ_MessageHandler handlers[] = {
  417. GNUNET_MQ_hd_fixed_size (test,
  418. MTYPE,
  419. struct TestMessage,
  420. NULL),
  421. GNUNET_MQ_handler_end ()
  422. };
  423. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  424. "Connection to CORE service of `%s' established\n",
  425. GNUNET_i2s (my_identity));
  426. GNUNET_assert (NULL != my_identity);
  427. p->id = *my_identity;
  428. if (cls == &p1)
  429. {
  430. GNUNET_assert (ok == 2);
  431. OKPP;
  432. /* connect p2 */
  433. p2.ch = GNUNET_CORE_connect (p2.cfg,
  434. &p2,
  435. &init_notify,
  436. &connect_notify,
  437. &disconnect_notify,
  438. handlers);
  439. }
  440. else
  441. {
  442. GNUNET_assert (ok == 3);
  443. OKPP;
  444. GNUNET_assert (cls == &p2);
  445. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  446. "Asking core (1) to connect to peer `%s' and vice-versa\n",
  447. GNUNET_i2s (&p2.id));
  448. p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
  449. &p2.id,
  450. 1);
  451. p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
  452. &p1.id,
  453. 1);
  454. }
  455. }
  456. static void
  457. offer_hello_done (void *cls)
  458. {
  459. struct PeerContext *p = cls;
  460. p->oh = NULL;
  461. }
  462. static void
  463. process_hello (void *cls,
  464. const struct GNUNET_MessageHeader *message)
  465. {
  466. struct PeerContext *p = cls;
  467. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  468. "Received (my) HELLO from transport service\n");
  469. GNUNET_assert (message != NULL);
  470. p->hello = GNUNET_malloc (ntohs (message->size));
  471. GNUNET_memcpy (p->hello, message, ntohs (message->size));
  472. if ((p == &p1) &&
  473. (NULL == p2.oh))
  474. p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
  475. message,
  476. &offer_hello_done,
  477. &p2);
  478. if ((p == &p2) &&
  479. (NULL == p1.oh))
  480. p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
  481. &offer_hello_done,
  482. &p1);
  483. if ((p == &p1) &&
  484. (NULL != p2.hello) &&
  485. (NULL == p1.oh))
  486. p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
  487. p2.hello,
  488. &offer_hello_done,
  489. &p1);
  490. if ((p == &p2) &&
  491. (NULL != p1.hello) &&
  492. (NULL == p2.oh))
  493. p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
  494. p1.hello,
  495. &offer_hello_done,
  496. &p2);
  497. }
  498. static void
  499. setup_peer (struct PeerContext *p,
  500. const char *cfgname)
  501. {
  502. char *binary;
  503. binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
  504. p->cfg = GNUNET_CONFIGURATION_create ();
  505. p->arm_proc =
  506. GNUNET_OS_start_process (GNUNET_OS_INHERIT_STD_OUT_AND_ERR
  507. | GNUNET_OS_USE_PIPE_CONTROL,
  508. NULL, NULL, NULL,
  509. binary,
  510. "gnunet-service-arm",
  511. "-c",
  512. cfgname,
  513. NULL);
  514. GNUNET_assert (GNUNET_OK ==
  515. GNUNET_CONFIGURATION_load (p->cfg,
  516. cfgname));
  517. p->stats = GNUNET_STATISTICS_create ("core",
  518. p->cfg);
  519. GNUNET_assert (NULL != p->stats);
  520. p->ats = GNUNET_ATS_connectivity_init (p->cfg);
  521. GNUNET_assert (NULL != p->ats);
  522. p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
  523. GNUNET_TRANSPORT_AC_ANY,
  524. &process_hello,
  525. p);
  526. GNUNET_free (binary);
  527. }
  528. static void
  529. run (void *cls,
  530. char *const *args,
  531. const char *cfgfile,
  532. const struct GNUNET_CONFIGURATION_Handle *cfg)
  533. {
  534. struct GNUNET_MQ_MessageHandler handlers[] = {
  535. GNUNET_MQ_hd_fixed_size (test,
  536. MTYPE,
  537. struct TestMessage,
  538. NULL),
  539. GNUNET_MQ_handler_end ()
  540. };
  541. GNUNET_assert (ok == 1);
  542. OKPP;
  543. err_task =
  544. GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  545. &terminate_task_error,
  546. NULL);
  547. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  548. NULL);
  549. if (test == SYMMETRIC)
  550. {
  551. setup_peer (&p1,
  552. "test_core_quota_peer1.conf");
  553. setup_peer (&p2,
  554. "test_core_quota_peer2.conf");
  555. }
  556. else if (test == ASYMMETRIC_SEND_LIMITED)
  557. {
  558. setup_peer (&p1,
  559. "test_core_quota_asymmetric_send_limit_peer1.conf");
  560. setup_peer (&p2,
  561. "test_core_quota_asymmetric_send_limit_peer2.conf");
  562. }
  563. else if (test == ASYMMETRIC_RECV_LIMITED)
  564. {
  565. setup_peer (&p1,
  566. "test_core_quota_asymmetric_recv_limited_peer1.conf");
  567. setup_peer (&p2,
  568. "test_core_quota_asymmetric_recv_limited_peer2.conf");
  569. }
  570. GNUNET_assert (test != -1);
  571. GNUNET_assert (GNUNET_SYSERR !=
  572. GNUNET_CONFIGURATION_get_value_size (p1.cfg,
  573. "ATS",
  574. "WAN_QUOTA_IN",
  575. &current_quota_p1_in));
  576. GNUNET_assert (GNUNET_SYSERR !=
  577. GNUNET_CONFIGURATION_get_value_size (p2.cfg,
  578. "ATS",
  579. "WAN_QUOTA_IN",
  580. &current_quota_p2_in));
  581. GNUNET_assert (GNUNET_SYSERR !=
  582. GNUNET_CONFIGURATION_get_value_size (p1.cfg,
  583. "ATS",
  584. "WAN_QUOTA_OUT",
  585. &current_quota_p1_out));
  586. GNUNET_assert (GNUNET_SYSERR !=
  587. GNUNET_CONFIGURATION_get_value_size (p2.cfg,
  588. "ATS",
  589. "WAN_QUOTA_OUT",
  590. &current_quota_p2_out));
  591. p1.ch = GNUNET_CORE_connect (p1.cfg,
  592. &p1,
  593. &init_notify,
  594. &connect_notify,
  595. &disconnect_notify,
  596. handlers);
  597. }
  598. static void
  599. stop_arm (struct PeerContext *p)
  600. {
  601. if (0 != GNUNET_OS_process_kill (p->arm_proc,
  602. GNUNET_TERM_SIG))
  603. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  604. "kill");
  605. if (GNUNET_OK !=
  606. GNUNET_OS_process_wait (p->arm_proc))
  607. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  608. "waitpid");
  609. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  610. "ARM process %u stopped\n",
  611. GNUNET_OS_process_get_pid (p->arm_proc));
  612. GNUNET_OS_process_destroy (p->arm_proc);
  613. p->arm_proc = NULL;
  614. GNUNET_CONFIGURATION_destroy (p->cfg);
  615. }
  616. static int
  617. check ()
  618. {
  619. char *const argv[] = {
  620. "test-core-quota-compliance",
  621. "-c",
  622. "test_core_api_data.conf",
  623. NULL
  624. };
  625. struct GNUNET_GETOPT_CommandLineOption options[] = {
  626. GNUNET_GETOPT_OPTION_END
  627. };
  628. ok = 1;
  629. GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
  630. argv,
  631. "test-core-quota-compliance",
  632. "nohelp",
  633. options,
  634. &run,
  635. &ok);
  636. stop_arm (&p1);
  637. stop_arm (&p2);
  638. return ok;
  639. }
  640. static void
  641. cleanup_directory (int test)
  642. {
  643. switch (test)
  644. {
  645. case SYMMETRIC:
  646. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
  647. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
  648. break;
  649. case ASYMMETRIC_SEND_LIMITED:
  650. GNUNET_DISK_directory_remove
  651. ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
  652. GNUNET_DISK_directory_remove
  653. ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
  654. break;
  655. case ASYMMETRIC_RECV_LIMITED:
  656. GNUNET_DISK_directory_remove
  657. ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
  658. GNUNET_DISK_directory_remove
  659. ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
  660. break;
  661. }
  662. }
  663. int
  664. main (int argc,
  665. char *argv[])
  666. {
  667. int ret;
  668. test = -1;
  669. if (NULL != strstr (argv[0],
  670. "_symmetric"))
  671. {
  672. test = SYMMETRIC;
  673. }
  674. else if (NULL != strstr (argv[0],
  675. "_asymmetric_send"))
  676. {
  677. test = ASYMMETRIC_SEND_LIMITED;
  678. }
  679. else if (NULL != strstr (argv[0],
  680. "_asymmetric_recv"))
  681. {
  682. test = ASYMMETRIC_RECV_LIMITED;
  683. }
  684. GNUNET_assert (test != -1);
  685. cleanup_directory (test);
  686. GNUNET_log_setup ("test-core-quota-compliance",
  687. "WARNING",
  688. NULL);
  689. ret = check ();
  690. cleanup_directory (test);
  691. return ret;
  692. }
  693. /* end of test_core_quota_compliance.c */