3 This file is part of GNUnet.
4 (C) 2013 Christian Grothoff (and other contributing authors)
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
23 * @file vectorproduct/test_vectorproduct_api_regression.c
24 * @brief VectorProduct API regression test
25 * @author Gaurav Kukreja
26 * @author Christian Fuchs
30 * AIM of the regression test
32 * This test tries to check whether the service can handle abrupt client disconnect.
34 * 1. We create a responder peer, and ask the service to prepare_response. After this,
35 * we disconnect responder peer from service.
37 * 2. Then we create a requester peer, and ask service to request another peer. We
38 * should check that the service on responder peer is still active and receives
39 * request from the requester. We then disconnect requester peer from service. Both
40 * the requester and responder service should handle this cleanly.
47 #include "gnunet_util_lib.h"
48 #include "gnunet_testbed_service.h"
49 #include "gnunet_common.h"
50 #include "gnunet_vectorproduct_service.h"
51 #include "gnunet_protocols.h"
53 #define LOG(kind,...) GNUNET_log_from (kind, "test-vectorproduct-api-regression",__VA_ARGS__)
57 * Structure for holding peer's sockets and IO Handles
62 * Handle to testbed peer
64 struct GNUNET_TESTBED_Peer *peer;
67 * The service connect operation to stream
69 struct GNUNET_TESTBED_Operation *op;
74 struct GNUNET_PeerIdentity our_id;
77 * Pointer to Vector Product Handle
79 struct GNUNET_VECTORPRODUCT_Handle *vh;
83 * Different states in test setup
88 * Get the identity of peer 1
93 * Get the identity of peer 2
98 * Connect to stream service of peer 1
100 PEER1_VECTORPRODUCT_CONNECT,
103 * Connect to stream service of peer 2
105 PEER2_VECTORPRODUCT_CONNECT
109 /******************************************************************************
110 *** Global Variables *****************************
111 ******************************************************************************/
114 * Maximum allowed message-ids we can check in one go (with one GNUNET_message)
116 static unsigned int max_mids;
119 * Session Key used by both the test peers
121 char input_key[103] = "helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhe";
124 * Input elements for peer1
126 //char input_elements_peer1[] = "11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11";
127 char input_elements_peer1[] = "11,11,11";
130 * Input Mask for peer 1
132 //char input_mask_peer1[] = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1";
133 char input_mask_peer1[] = "1,1,1";
136 * the array of converted message IDs to send to our service
138 static int32_t * elements_peer1 = NULL;
141 * Input elements for peer2
143 //char input_elements_peer2[] = "11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11";
144 char input_elements_peer2[] = "11,11,11";
146 * Input Mask for peer 2
148 //char input_mask_peer2[] = "1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1";
149 char input_mask_peer2[] = "1,1,1";
151 * the array of converted message IDs to send to our service
153 static int32_t * elements_peer2 = NULL;
156 * the array of converted message IDs to send to our service
158 static unsigned char * mask_peer2 = NULL;
161 * Data context for peer 1
163 static struct PeerData peer1;
166 * Data context for peer 2
168 static struct PeerData peer2;
171 * Various states during test setup
173 static enum SetupState setup_state;
176 * Testbed operation handle
178 static struct GNUNET_TESTBED_Operation *op;
181 * Return value of the test.
186 * Abort Task for timeout
188 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
189 /******************************************************************************
190 *** Static Functions *****************************
191 ******************************************************************************/
194 * Helper function to shutdown a test peer
196 * @param cls void* to struct PeerData of the peer to be disconnected
197 * @param tc Task Context
200 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
204 * Helper function to connect a test peer
206 * @param cls void* to struct PeerData of the peer to be connected
207 * @param tc Task Context
210 connect_peer (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext * tc);
215 * Close sockets and stop testing deamons nicely
218 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 if (peer1.op != NULL)
222 do_shutdown (&peer1, NULL);
224 if (peer2.op != NULL)
225 do_shutdown (&peer2, NULL);
227 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
228 GNUNET_SCHEDULER_cancel (abort_task);
230 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
235 * Helper function to shutdown a test peer
237 * @param cls void* to struct PeerData of the peer to be disconnected
238 * @param tc Task Context
241 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct PeerData* peer = (struct PeerData*) cls;
248 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting Peer1\n\n");
249 else if (peer == &peer2)
250 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting Peer2\n\n");
252 // peer->op contains handle to the TESTBED_connect_service operation
253 // calling operation done, leads to call to vectorproduct_da
254 if (peer->op != NULL)
256 GNUNET_TESTBED_operation_done (peer->op);
261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10), &do_close, NULL);
266 * Something went wrong and timed out. Kill everything and set error flag
269 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: ABORT due to Timeout\n");
279 * Controller event callback
282 * @param event the controller event
285 controller_event_cb (void *cls,
286 const struct GNUNET_TESTBED_EventInformation *event)
290 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
293 case PEER1_VECTORPRODUCT_CONNECT:
294 case PEER2_VECTORPRODUCT_CONNECT:
295 GNUNET_assert (NULL == event->details.operation_finished.emsg);
308 * Callback function called for the responder peer i.e. peer1
311 * @param key Session key
312 * @param status Status of the message
315 responder_callback (void *cls,
316 const struct GNUNET_HashCode * key,
317 enum GNUNET_VECTORPRODUCT_ResponseStatus status)
319 if (status == GNUNET_VECTORPRODUCT_Status_Failure)
321 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received status failure\n");
324 else if (status == GNUNET_VECTORPRODUCT_Status_InvalidResponse)
326 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received status invalid response\n");
329 else if (GNUNET_VECTORPRODUCT_Status_Timeout == status)
331 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received timeout occured\n");
334 else if (GNUNET_VECTORPRODUCT_Status_ServiceDisconnected == status)
336 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received service disconnected!!\n");
339 else if (GNUNET_VECTORPRODUCT_Status_Success == status)
341 LOG (GNUNET_ERROR_TYPE_DEBUG, "Responder Client expected response received!\n");
346 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client status = %d!\n", (int) status);
350 // Not shutting down this time, only for this regression test. We have shutdown explicitly earlier.
351 // Shutting down again is causing problems.
353 // if(peer1.vh != NULL)
355 // GNUNET_SCHEDULER_add_now(&do_shutdown, &peer1);
362 * Callback function called for the requester peer i.e. peer2
365 * @param key Session key
366 * @param status Status of the message
369 requester_callback (void *cls,
370 const struct GNUNET_HashCode * key,
371 const struct GNUNET_PeerIdentity * peer,
372 enum GNUNET_VECTORPRODUCT_ResponseStatus status,
373 const struct GNUNET_VECTORPRODUCT_client_response *msg)
375 uint32_t product_len;
377 if (status == GNUNET_VECTORPRODUCT_Status_Failure)
379 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client received status failure\n");
382 else if (status == GNUNET_VECTORPRODUCT_Status_InvalidResponse)
384 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client received status invalid response\n");
387 else if (GNUNET_VECTORPRODUCT_Status_Timeout == status)
389 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client timeout occured\n");
392 else if (GNUNET_VECTORPRODUCT_Status_ServiceDisconnected == status)
394 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client service disconnected!!\n");
397 else if (GNUNET_VECTORPRODUCT_Status_Success != status)
399 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requester Client Status = %d\n", (int) status);
402 else if (GNUNET_VECTORPRODUCT_Status_Success == status)
404 product_len = ntohl (msg->product_length);
409 gcry_error_t ret = 0;
412 ret = gcry_mpi_scan (&result, GCRYMPI_FMT_USG, (void *) &msg[1], product_len, &read);
416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not convert to mpi to value!\n");
420 gcry_mpi_dump (result);
421 gcry_mpi_release (result);
426 { //currently not used, but if we get more info due to MESH we will need this
427 LOG (GNUNET_ERROR_TYPE_WARNING, "Error during computation of vector product, return code: %d\n", product_len);
432 // Not shutting down this time, only for this regression test. We have shutdown explicitly earlier.
433 // Shutting down again is causing problems.
435 // if(peer2.vh != NULL)
437 // GNUNET_SCHEDULER_add_now(&do_shutdown, &peer2);
444 requester_request (void *cls,
445 const struct GNUNET_SCHEDULER_TaskContext * tc)
447 GNUNET_assert (peer2.vh != NULL);
450 uint16_t element_count = 0;
451 uint16_t mask_length = 0;
452 char * begin = input_elements_peer2;
455 struct GNUNET_VECTORPRODUCT_QueueEntry *qe;
456 struct GNUNET_HashCode key;
459 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &key);
461 /* Read input_elements_peer2, and put in elements_peer2 array */
465 unsigned int mcount = element_count;
466 //ignore empty rows of ,,,,,,
467 while (*begin == ',')
469 // get the length of the current element and replace , with null
470 for (end = begin; *end && *end != ','; end++);
475 if (1 != sscanf (begin, "%" SCNd32, &element))
477 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
482 GNUNET_array_append (elements_peer2, mcount, element);
487 while (!exit_loop && element_count < max_mids);
488 GNUNET_assert (elements_peer2 != NULL);
489 GNUNET_assert (element_count >= 1);
491 /* Read input_mask_peer2 and read in mask_peer2 array */
492 mask_length = element_count / 8 + (element_count % 8 ? 1 : 0);
493 mask_peer2 = GNUNET_malloc ((element_count / 8) + 2);
494 GNUNET_assert (NULL != mask_peer2);
495 if (NULL != input_mask_peer2)
497 begin = input_mask_peer2;
498 unsigned short mask_count = 0;
503 //ignore empty rows of ,,,,,,
504 while (* begin == ',')
506 // get the length of the current element and replace , with null
507 // gnunet_ascii-armor uses base32, thus we can use , as separator!
508 for (end = begin; *end && *end != ','; end++);
513 if (1 != sscanf (begin, "%" SCNd32, &element))
515 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
520 GNUNET_assert (mask_count <= element_count);
523 mask_peer2[mask_count / 8] = mask_peer2[mask_count / 8] | 1 << (mask_count % 8);
529 // +1 to see if we would have more data, which would indicate malformed/superficial input
530 GNUNET_assert (mask_count == element_count);
534 for (i = 0; i <= mask_length; i++)
535 mask_peer2[i] = UCHAR_MAX; // all 1's
538 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Responder peer key %s\n", &peer1.our_id);
540 qe = GNUNET_VECTORPRODUCT_request (peer2.vh,
545 elements_peer2, mask_peer2,
546 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
552 FPRINTF (stderr, "%s", _ ("Could not send request to vectorproduct service! Exitting!"));
558 * For regression, we shutdown the initiator peer, peer2, one second after
559 * issuing a request. Hopefully, peer1 notices that the tunnel has been
560 * been destroyed, and will shutdown cleanly.
562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, &peer2);
569 * Function prepares the message to be sent by peer1 to its vectorproduct service
570 * to prepare response, and wait for a request session to be initiated by peer1
573 responder_prepare_response (void *cls,
574 const struct GNUNET_SCHEDULER_TaskContext * tc)
576 GNUNET_assert (peer1.vh != NULL);
578 uint16_t element_count = 0;
579 char * begin = input_elements_peer1;
583 struct GNUNET_VECTORPRODUCT_QueueEntry *qe;
585 struct GNUNET_HashCode key;
586 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &key);
589 /* Read input_elements_peer1, and put in elements_peer1 array */
592 unsigned int mcount = element_count;
593 //ignore empty rows of ,,,,,,
594 while (*begin == ',')
596 // get the length of the current element and replace , with null
597 for (end = begin; *end && *end != ','; end++);
602 if (1 != sscanf (begin, "%" SCNd32, &element))
604 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
609 GNUNET_array_append (elements_peer1, mcount, element);
614 while (!exit_loop && element_count < max_mids);
616 GNUNET_assert (elements_peer1 != NULL);
617 GNUNET_assert (element_count >= 1);
619 qe = GNUNET_VECTORPRODUCT_prepare_response (peer1.vh,
623 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
629 FPRINTF (stderr, "%s", _ ("Could not send request to vectorproduct service! Exitting!"));
634 // connect the second peer
635 setup_state = PEER2_VECTORPRODUCT_CONNECT;
636 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), &connect_peer, &peer2);
638 // while the service is waiting for a matching request, disconnect the test client
639 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &do_shutdown, &peer1);
646 * Adapter function called to destroy a connection to
647 * a service. This function is called when GNUNET_TESTBED_operation_done is
648 * called for peer->op, which holds the handle for GNUNET_TESTBED_service_connect
652 * @param op_result service handle returned from the connect adapter
655 vectorproduct_da (void *cls, void *op_result)
657 struct PeerData* peer = (struct PeerData*) cls;
659 GNUNET_VECTORPRODUCT_disconnect (peer->vh);
666 * Adapter function called to establish a connection to
667 * a service. This function is called to by GNUNET_TESTBED_service_connect.
670 * @param cfg configuration of the peer to connect to; will be available until
671 * GNUNET_TESTBED_operation_done() is called on the operation returned
672 * from GNUNET_TESTBED_service_connect()
673 * @return service handle to return in 'op_result', NULL on error
676 vectorproduct_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
678 struct PeerData *p = cls;
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", (&peer1 == p) ? 1 : 2,
681 GNUNET_i2s (&p->our_id));
685 case PEER1_VECTORPRODUCT_CONNECT:
686 peer1.vh = GNUNET_VECTORPRODUCT_connect (cfg);
688 if (peer1.vh != NULL)
690 /* prepare_response from peer1 */
691 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, &responder_prepare_response, NULL);
701 case PEER2_VECTORPRODUCT_CONNECT:
702 /* Actually connect peer 2 to vectorproduct service */
703 peer2.vh = GNUNET_VECTORPRODUCT_connect (cfg);
705 if (peer2.vh != NULL)
707 /* initiate request from peer2 */
708 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, &requester_request, NULL);
724 * Helper function to connect a test peer
726 * @param cls void* to struct PeerData of the peer to be connected
727 * @param tc Task Context
730 connect_peer (void *cls,
731 const struct GNUNET_SCHEDULER_TaskContext * tc)
733 struct PeerData *peer = cls;
735 peer->op = GNUNET_TESTBED_service_connect (peer, peer->peer, "vectorproduct",
736 NULL, NULL, vectorproduct_ca,
737 vectorproduct_da, peer);
743 * Callback to be called when the requested peer information is available
745 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
746 * @param op the operation this callback corresponds to
747 * @param pinfo the result; will be NULL if the operation has failed
748 * @param emsg error message if the operation has failed; will be NULL if the
749 * operation is successfull
752 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
753 const struct GNUNET_TESTBED_PeerInformation *pinfo,
756 GNUNET_assert (NULL == emsg);
757 GNUNET_assert (op == op_);
761 case PEER1_GET_IDENTITY:
763 memcpy (&peer1.our_id, pinfo->result.id,
764 sizeof (struct GNUNET_PeerIdentity));
765 GNUNET_TESTBED_operation_done (op);
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s_full
770 /* Request for peer id of peer 2*/
771 setup_state = PEER2_GET_IDENTITY;
772 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
773 GNUNET_TESTBED_PIT_IDENTITY,
777 case PEER2_GET_IDENTITY:
779 memcpy (&peer2.our_id, pinfo->result.id,
780 sizeof (struct GNUNET_PeerIdentity));
781 GNUNET_TESTBED_operation_done (op);
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s_full
786 /* Connect peer 1 to vectorproduct service */
787 setup_state = PEER1_VECTORPRODUCT_CONNECT;
788 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, &connect_peer, &peer1);
798 * Signature of a main function for a testcase.
801 * @param num_peers number of peers in 'peers'
802 * @param peers handle to peers run in the testbed
805 test_master (void *cls, unsigned int num_peers,
806 struct GNUNET_TESTBED_Peer **peers)
808 GNUNET_assert (NULL != peers);
809 GNUNET_assert (NULL != peers[0]);
810 GNUNET_assert (NULL != peers[1]);
811 peer1.peer = peers[0];
812 peer2.peer = peers[1];
814 /* Get the peer identity and configuration of peer 1 */
815 setup_state = PEER1_GET_IDENTITY;
816 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
817 GNUNET_TESTBED_PIT_IDENTITY,
821 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
822 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
831 main (int argc, char **argv)
837 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
838 max_mids = (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_MessageHeader))
839 / sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
841 (void) GNUNET_TESTBED_test_run ("test_vectorproduct_api_regression",
842 "test_vectorproduct_api_data.conf",
843 NUM_PEERS, event_mask, &controller_event_cb,
847 if (GNUNET_SYSERR == ok)