2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * Aim of test_scalarproduct_api : This test creates two peers. Peer1 is the
23 * responder peer, Bob and Peer2 is the initiator peer, Alice. Both peers
24 * connect to VectorProduct Service, and use the API to issue requests to
25 * service. Test passes, when the expected scalar product is received from the
30 * @file scalarproduct/testbed_scalarproduct_api.c
31 * @brief VectorProduct API testing between 4 peers using testing API
32 * @author Gaurav Kukreja
33 * @author Christian Fuchs
40 #include "gnunet_util_lib.h"
41 #include "gnunet_testbed_service.h"
42 #include "gnunet_common.h"
43 #include "gnunet_scalarproduct_service.h"
44 #include "gnunet_protocols.h"
48 #define LOG(kind,...) GNUNET_log_from (kind, "test-scalarproduct-api",__VA_ARGS__)
51 * Structure for holding peer's sockets and IO Handles
56 * Handle to testbed peer
58 struct GNUNET_TESTBED_Peer *peer;
61 * The service connect operation to stream
63 struct GNUNET_TESTBED_Operation *op;
68 struct GNUNET_PeerIdentity our_id;
71 * Pointer to Vector Product Handle
73 struct GNUNET_SCALARPRODUCT_Handle *vh;
77 * Different states in test setup
82 * Get the identity of peer 1
87 * Get the identity of peer 2
92 * Connect to stream service of peer 1
94 PEER1_SCALARPRODUCT_CONNECT,
97 * Connect to stream service of peer 2
99 PEER2_SCALARPRODUCT_CONNECT
103 /******************************************************************************
104 *** Global Variables *****************************
105 ******************************************************************************/
108 * Maximum allowed message-ids we can check in one go (with one GNUNET_message)
110 static unsigned int max_mids;
113 * Session Key used by both the test peers
115 char input_key[103] = "helloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhelloworldhe";
118 * Input elements for peer1
120 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";
121 //char input_elements_peer1[] = "11,11,11";
124 * Input Mask for peer 1
126 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";
127 //char input_mask_peer1[] = "1,1,1";
130 * the array of converted message IDs to send to our service
132 static int32_t * elements_peer1 = NULL;
137 uint16_t element_count_peer1 = 0;
140 * Input elements for peer2
142 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";
143 //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";
152 * the array of converted message IDs to send to our service
154 static int32_t * elements_peer2 = NULL;
157 * the array of converted message IDs to send to our service
159 static unsigned char * mask_peer2 = NULL;
164 uint16_t element_count_peer2 = 0;
167 * Data context for peer 1
169 static struct PeerData peer1;
172 * Data context for peer 2
174 static struct PeerData peer2;
177 * Various states during test setup
179 static enum SetupState setup_state;
182 * Testbed operation handle
184 static struct GNUNET_TESTBED_Operation *op;
188 static int responder_ok;
190 static int requester_ok;
192 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
193 /******************************************************************************
194 *** Static Functions *****************************
195 ******************************************************************************/
198 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
202 * Close sockets and stop testing deamons nicely
205 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 if (peer1.op != NULL)
209 do_shutdown (&peer1, NULL);
211 if (peer2.op != NULL)
212 do_shutdown (&peer2, NULL);
214 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
215 GNUNET_SCHEDULER_cancel (abort_task);
217 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
223 * @param cls pointer to "struct PeerData" of the peer to be disconnected
224 * @param tc Task Context
227 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 struct PeerData* peer = (struct PeerData*) cls;
234 LOG (GNUNET_ERROR_TYPE_INFO, "Disconnecting Peer1\n\n");
235 else if (peer == &peer2)
236 LOG (GNUNET_ERROR_TYPE_INFO, "Disconnecting Peer2\n\n");
238 // peer->op contains handle to the TESTBED_connect_service operation
239 // calling operation done, leads to call to scalarproduct_da
240 if (peer->op != NULL)
242 GNUNET_TESTBED_operation_done (peer->op);
247 GNUNET_SCHEDULER_add_now (&do_close, NULL);
252 * Something went wrong and timed out. Kill everything and set error flag
255 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: ABORT due to Timeout\n");
265 * Controller event callback
268 * @param event the controller event
271 controller_event_cb (void *cls,
272 const struct GNUNET_TESTBED_EventInformation *event)
274 GNUNET_assert (event->type == GNUNET_TESTBED_ET_OPERATION_FINISHED);
278 case PEER1_SCALARPRODUCT_CONNECT:
279 case PEER2_SCALARPRODUCT_CONNECT:
280 GNUNET_assert (NULL == event->details.operation_finished.emsg);
289 responder_callback (void *cls,
290 const struct GNUNET_HashCode * key,
291 enum GNUNET_SCALARPRODUCT_ResponseStatus status)
294 if (status == GNUNET_SCALARPRODUCT_Status_Failure)
296 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received status failure\n");
299 else if (status == GNUNET_SCALARPRODUCT_Status_InvalidResponse)
301 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received status invalid response\n");
304 else if (GNUNET_SCALARPRODUCT_Status_Timeout == status)
306 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received timeout occured\n");
309 else if (GNUNET_SCALARPRODUCT_Status_ServiceDisconnected == status)
311 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client received service disconnected!!\n");
314 else if (GNUNET_SCALARPRODUCT_Status_Success == status)
316 LOG (GNUNET_ERROR_TYPE_INFO, "Responder Client expected response received!\n");
321 LOG (GNUNET_ERROR_TYPE_WARNING, "Responder Client status = %d!\n", (int) status);
324 // TODO : Responder Session Complete. Shutdown Test Cleanly!!!
325 //do_shutdown(&peer1, NULL);
326 GNUNET_SCHEDULER_add_now (&do_shutdown, &peer1);
332 requester_callback (void *cls,
333 const struct GNUNET_HashCode * key,
334 const struct GNUNET_PeerIdentity * peer,
335 enum GNUNET_SCALARPRODUCT_ResponseStatus status,
336 const struct GNUNET_SCALARPRODUCT_client_response *msg)
338 uint32_t product_len;
340 if (status == GNUNET_SCALARPRODUCT_Status_Failure)
342 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client received status failure\n");
345 else if (status == GNUNET_SCALARPRODUCT_Status_InvalidResponse)
347 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client received status invalid response\n");
350 else if (GNUNET_SCALARPRODUCT_Status_Timeout == status)
352 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client timeout occured\n");
355 else if (GNUNET_SCALARPRODUCT_Status_ServiceDisconnected == status)
357 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client service disconnected!!\n");
360 else if (GNUNET_SCALARPRODUCT_Status_Success != status)
362 LOG (GNUNET_ERROR_TYPE_WARNING, "Requester Client Status = %d\n", (int) status);
365 else if (GNUNET_SCALARPRODUCT_Status_Success == status)
367 LOG (GNUNET_ERROR_TYPE_INFO, "Requester Client expected response received!\n");
368 product_len = ntohl(msg->product_length);
373 gcry_error_t ret = 0;
375 ret = gcry_mpi_scan (&result, GCRYMPI_FMT_USG, &msg[1], product_len, &read);
379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not convert to mpi to value!\n");
386 // calculate expected product
387 gcry_mpi_t expected_result;
390 gcry_mpi_t v1_v2_prod;
392 expected_result = gcry_mpi_new (0);
394 for (i = 0; i < element_count_peer1; i++)
397 v1_v2_prod = gcry_mpi_new (0);
399 // long to gcry_mpi_t
400 value = elements_peer1[i] >= 0 ? elements_peer1[i] : -elements_peer1[i];
401 if (elements_peer1[i] < 0)
403 v1 = gcry_mpi_new (0);
404 gcry_mpi_sub_ui (v1, v1, value);
407 v1 = gcry_mpi_set_ui (NULL, value);
409 // long to gcry_mpi_t
410 value = elements_peer2[i] >= 0 ? elements_peer2[i] : -elements_peer2[i];
411 if (elements_peer2[i] < 0)
413 v2 = gcry_mpi_new (0);
414 gcry_mpi_sub_ui (v2, v2, value);
417 v2 = gcry_mpi_set_ui (NULL, value);
419 gcry_mpi_mul (v1_v2_prod, v1, v2);
420 gcry_mpi_add (expected_result, expected_result, v1_v2_prod);
422 gcry_mpi_release (v1);
423 gcry_mpi_release (v2);
424 gcry_mpi_release (v1_v2_prod);
428 // compare the result
429 if (!gcry_mpi_cmp (expected_result, result))
431 LOG (GNUNET_ERROR_TYPE_INFO, "Scalar Product matches expected Result!!\n");
436 LOG (GNUNET_ERROR_TYPE_WARNING, "Scalar Product DOES NOT match expected Result!!\n");
439 gcry_mpi_release (result);
440 gcry_mpi_release (expected_result);
444 { //currently not used, but if we get more info due to MESH we will need this
445 LOG (GNUNET_ERROR_TYPE_WARNING, "Error during computation of vector product, return code: %d\n", product_len);
450 //do_shutdown(&peer2, NULL);
451 GNUNET_SCHEDULER_add_now (&do_shutdown, &peer2);
456 * Prepare the message to be sent by peer2 to its scalarproduct service, to
457 * initiate a request to peer1.
459 static struct GNUNET_SCALARPRODUCT_QueueEntry *
464 uint16_t mask_length = 0;
465 char * begin = input_elements_peer2;
468 struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
469 struct GNUNET_HashCode key;
471 GNUNET_assert (peer2.vh != NULL);
473 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &key);
475 /* Read input_elements_peer2, and put in elements_peer2 array */
479 unsigned int mcount = element_count_peer2;
480 //ignore empty rows of ,,,,,,
481 while (*begin == ',')
483 // get the length of the current element and replace , with null
484 for (end = begin; *end && *end != ','; end++);
489 if (1 != sscanf (begin, "%" SCNd32, &element))
491 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
496 GNUNET_array_append (elements_peer2, mcount, element);
497 element_count_peer2++;
501 while (!exit_loop && element_count_peer2 < max_mids);
502 GNUNET_assert (elements_peer2 != NULL);
503 GNUNET_assert (element_count_peer2 >= 1);
505 /* Read input_mask_peer2 and read in mask_peer2 array */
506 mask_length = element_count_peer2 / 8 + (element_count_peer2 % 8 ? 1 : 0);
507 mask_peer2 = GNUNET_malloc ((element_count_peer2 / 8) + 2);
508 GNUNET_assert (NULL != mask_peer2);
509 if (NULL != input_mask_peer2)
511 begin = input_mask_peer2;
512 unsigned short mask_count = 0;
517 //ignore empty rows of ,,,,,,
518 while (* begin == ',')
520 // get the length of the current element and replace , with null
521 // gnunet_ascii-armor uses base32, thus we can use , as separator!
522 for (end = begin; *end && *end != ','; end++);
527 if (1 != sscanf (begin, "%" SCNd32, &element))
529 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
534 GNUNET_assert (mask_count <= element_count_peer2);
537 mask_peer2[mask_count / 8] = mask_peer2[mask_count / 8] | 1 << (mask_count % 8);
543 // +1 to see if we would have more data, which would indicate malformed/superficial input
544 GNUNET_assert (mask_count == element_count_peer2);
548 for (i = 0; i <= mask_length; i++)
549 mask_peer2[i] = UCHAR_MAX; // all 1's
552 qe = GNUNET_SCALARPRODUCT_request (peer2.vh,
557 elements_peer2, mask_peer2,
558 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
564 LOG(GNUNET_ERROR_TYPE_ERROR, "Could not send request to scalarproduct service! Exitting!");
574 * Function prepares the message to be sent by peer1 to its scalarproduct service
575 * to prepare response, and wait for a request session to be initiated by peer1
577 static struct GNUNET_SCALARPRODUCT_QueueEntry *
578 responder_prepare_response ()
580 GNUNET_assert (peer1.vh != NULL);
582 char * begin = input_elements_peer1;
585 struct GNUNET_SCALARPRODUCT_QueueEntry *qe;
586 struct GNUNET_HashCode key;
588 GNUNET_CRYPTO_hash (input_key, strlen (input_key), &key);
590 /* Read input_elements_peer1, and put in elements_peer1 array */
594 unsigned int mcount = element_count_peer1;
595 //ignore empty rows of ,,,,,,
596 while (*begin == ',')
598 // get the length of the current element and replace , with null
599 for (end = begin; *end && *end != ','; end++);
604 if (1 != sscanf (begin, "%" SCNd32, &element))
606 FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
611 GNUNET_array_append (elements_peer1, mcount, element);
612 element_count_peer1++;
616 while (!exit_loop && element_count_peer1 < max_mids);
617 GNUNET_assert (elements_peer1 != NULL);
618 GNUNET_assert (element_count_peer1 >= 1);
620 qe = GNUNET_SCALARPRODUCT_prepare_response (peer1.vh,
624 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
630 LOG(GNUNET_ERROR_TYPE_ERROR, "Could not send request to scalarproduct service! Exitting!");
640 * Scheduler task to initiate requester client
642 * @param cls void* to struct PeerData
643 * @param tc Task Context
646 request_task(void *cls,
647 const struct GNUNET_SCHEDULER_TaskContext
655 * Scheduler task to initiate responder client
657 * @param cls void* to struct PeerData
658 * @param tc Task Context
661 prepare_response_task(void *cls,
662 const struct GNUNET_SCHEDULER_TaskContext
665 responder_prepare_response();
671 * Adapter function called to destroy a connection to
672 * a service. This function is called when GNUNET_TESTBED_operation_done is
673 * called for peer->op, which holds the handle for GNUNET_TESTBED_service_connect
677 * @param op_result service handle returned from the connect adapter
680 scalarproduct_da (void *cls, void *op_result)
682 struct PeerData* peer = (struct PeerData*) cls;
684 GNUNET_SCALARPRODUCT_cancel (peer->vh);
690 * Adapter function called to establish a connection to
691 * a service. This function is called to by GNUNET_TESTBED_service_connect.
694 * @param cfg configuration of the peer to connect to; will be available until
695 * GNUNET_TESTBED_operation_done() is called on the operation returned
696 * from GNUNET_TESTBED_service_connect()
697 * @return service handle to return in 'op_result', NULL on error
700 scalarproduct_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
702 struct PeerData *p = cls;
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", (&peer1 == p) ? 1 : 2,
705 GNUNET_i2s (&p->our_id));
709 case PEER1_SCALARPRODUCT_CONNECT:
710 /* Connect peer 2 to scalarproduct service */
712 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "scalarproduct",
713 NULL, NULL, scalarproduct_ca,
714 scalarproduct_da, &peer2);
715 setup_state = PEER2_SCALARPRODUCT_CONNECT;
718 /* Actually connect peer 1 to scalarproduct service */
719 peer1.vh = GNUNET_SCALARPRODUCT_connect (cfg);
722 case PEER2_SCALARPRODUCT_CONNECT:
723 /* Actually connect peer 2 to scalarproduct service */
724 peer2.vh = GNUNET_SCALARPRODUCT_connect (cfg);
726 /* Schedule tasks to initiate request from peer2 and prepare_response from peer1 */
727 if(peer1.vh != NULL && peer2.vh != NULL)
729 GNUNET_SCHEDULER_add_now(&prepare_response_task, &peer1);
730 GNUNET_SCHEDULER_add_now(&request_task, &peer2);
741 * Callback to be called when the requested peer information is available
743 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
744 * @param op the operation this callback corresponds to
745 * @param pinfo the result; will be NULL if the operation has failed
746 * @param emsg error message if the operation has failed; will be NULL if the
747 * operation is successfull
750 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
751 const struct GNUNET_TESTBED_PeerInformation *pinfo,
754 GNUNET_assert (NULL == emsg);
755 GNUNET_assert (op == op_);
758 case PEER1_GET_IDENTITY:
760 memcpy (&peer1.our_id, pinfo->result.id,
761 sizeof (struct GNUNET_PeerIdentity));
762 GNUNET_TESTBED_operation_done (op);
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s_full
767 /* Request for peer id of peer 2*/
768 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
769 GNUNET_TESTBED_PIT_IDENTITY,
771 setup_state = PEER2_GET_IDENTITY;
774 case PEER2_GET_IDENTITY:
776 memcpy (&peer2.our_id, pinfo->result.id,
777 sizeof (struct GNUNET_PeerIdentity));
778 GNUNET_TESTBED_operation_done (op);
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s_full
783 /* Connect peer 1 to scalarproduct service */
784 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "scalarproduct",
785 NULL, NULL, scalarproduct_ca,
786 scalarproduct_da, &peer1);
787 setup_state = PEER1_SCALARPRODUCT_CONNECT;
797 * Signature of a main function for a testcase.
800 * @param num_peers number of peers in 'peers'
801 * @param peers handle to peers run in the testbed
804 test_master (void *cls, unsigned int num_peers,
805 struct GNUNET_TESTBED_Peer **peers)
807 GNUNET_assert (NULL != peers);
808 GNUNET_assert (NULL != peers[0]);
809 GNUNET_assert (NULL != peers[1]);
810 peer1.peer = peers[0];
811 peer2.peer = peers[1];
812 /* Get the peer identity and configuration of peer 1 */
813 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
814 GNUNET_TESTBED_PIT_IDENTITY,
816 setup_state = PEER1_GET_IDENTITY;
818 /* Abort task for stopping test on timeout */
820 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
821 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
830 main (int argc, char **argv)
836 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
837 max_mids = (GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_MessageHeader))
838 / sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
840 (void) GNUNET_TESTBED_test_run ("test_scalarproduct_api",
841 "test_scalarproduct_api_data.conf",
842 NUM_PEERS, event_mask, &controller_event_cb,
846 if (GNUNET_SYSERR == ok)
848 LOG (GNUNET_ERROR_TYPE_ERROR, "Test failing due to some error before calling API for request or prepare_response\n");
851 else if (GNUNET_SYSERR == responder_ok)
853 LOG (GNUNET_ERROR_TYPE_ERROR, "Test failing due to some error in responding_client\n");
856 else if (GNUNET_SYSERR == requester_ok)
858 LOG (GNUNET_ERROR_TYPE_ERROR, "Test failing due to some error in requesting client\n");