psyc/social: get state from psycstore
[oweals/gnunet.git] / src / social / test_social.c
index d7a407c1a5219d88339628b98fd2a418ee26206a..dbcf822f85a3646c422d3986ca48fc5f5222e363 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of GNUnet
- * (C) 2013 Christian Grothoff (and other contributing authors)
+ * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
  *
  * GNUnet is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with GNUnet; see the file COPYING.  If not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 /**
  * @file social/test_social.c
@@ -36,7 +36,7 @@
 #include "gnunet_core_service.h"
 #include "gnunet_identity_service.h"
 
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
 #define DATA2ARG(data) data, sizeof (data)
 
@@ -48,7 +48,7 @@ int res;
 /**
  * Handle for task for timeout termination.
  */
-GNUNET_SCHEDULER_TaskIdentifier end_badly_task;
+struct GNUNET_SCHEDULER_Task * end_badly_task;
 
 const struct GNUNET_CONFIGURATION_Handle *cfg;
 
@@ -75,6 +75,9 @@ struct GNUNET_SOCIAL_Slicer *guest_slicer;
 struct GNUNET_SOCIAL_Host *hst;
 struct GNUNET_SOCIAL_Guest *gst;
 
+struct GNUNET_SOCIAL_Place *hst_plc;
+struct GNUNET_SOCIAL_Place *gst_plc;
+
 struct GuestEnterMessage
 {
   struct GNUNET_PSYC_Message *msg;
@@ -96,40 +99,52 @@ struct TransmitClosure
   uint8_t n;
 } tmit;
 
+struct ResultClosure {
+  uint32_t n;
+};
+
 uint8_t join_req_count;
 struct GNUNET_PSYC_Message *join_resp;
 
+uint32_t counter;
+
 enum
 {
   TEST_NONE = 0,
-  TEST_HOST_ANSWER_DOOR_REFUSE      = 1,
-  TEST_GUEST_RECV_ENTRY_DCSN_REFUSE = 2,
-  TEST_HOST_ANSWER_DOOR_ADMIT       = 3,
-  TEST_GUEST_RECV_ENTRY_DCSN_ADMIT  = 4,
-  TEST_HOST_ANNOUNCE     = 5,
-  TEST_HOST_ANNOUNCE_END = 6,
-  TEST_GUEST_TALK        = 7,
-  TEST_GUEST_LEAVE       = 8,
-  TEST_HOST_LEAVE        = 9,
+  TEST_HOST_ANSWER_DOOR_REFUSE      =  1,
+  TEST_GUEST_RECV_ENTRY_DCSN_REFUSE =  2,
+  TEST_HOST_ANSWER_DOOR_ADMIT       =  3,
+  TEST_GUEST_RECV_ENTRY_DCSN_ADMIT  =  4,
+  TEST_HOST_ANNOUNCE                       =  5,
+  TEST_HOST_ANNOUNCE_END            =  6,
+  TEST_HOST_ANNOUNCE2                      =  7,
+  TEST_HOST_ANNOUNCE2_END           =  8,
+  TEST_GUEST_TALK                   =  9,
+  TEST_GUEST_HISTORY_REPLAY         = 10,
+  TEST_GUEST_HISTORY_REPLAY_LATEST  = 11,
+  TEST_GUEST_LOOK_AT                = 12,
+  TEST_GUEST_LOOK_FOR               = 13,
+  TEST_GUEST_LEAVE                  = 14,
+  TEST_HOST_LEAVE                   = 15,
 } test;
 
 
-void
+static void
 guest_enter ();
 
 
-void
+static void
 guest_talk ();
 
 
-void
+static void
 host_announce2 ();
 
 
 /**
  * Clean up all resources used.
  */
-void
+static void
 cleanup ()
 {
   if (NULL != core)
@@ -148,11 +163,13 @@ cleanup ()
   {
     GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, NULL, NULL);
     gst = NULL;
+    gst_plc = NULL;
   }
   if (NULL != hst)
   {
     GNUNET_SOCIAL_host_leave (hst, GNUNET_NO, NULL, NULL);
     hst = NULL;
+    hst_plc = NULL;
   }
   GNUNET_SCHEDULER_shutdown ();
 }
@@ -164,7 +181,7 @@ cleanup ()
  * @param cls NULL
  * @param tc scheduler context
  */
-void
+static void
 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   res = 1;
@@ -179,7 +196,7 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls NULL
  * @param tc scheduler context
  */
-void
+static void
 end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   res = 0;
@@ -191,15 +208,15 @@ end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Finish the test case (successfully).
  */
-void
+static void
 end ()
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
 
-  if (end_badly_task != GNUNET_SCHEDULER_NO_TASK)
+  if (end_badly_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (end_badly_task);
-    end_badly_task = GNUNET_SCHEDULER_NO_TASK;
+    end_badly_task = NULL;
   }
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
                                &end_normally, NULL);
@@ -218,7 +235,7 @@ transmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-int
+static int
 notify_data (void *cls, uint16_t *data_size, void *data)
 {
   struct TransmitClosure *tmit = cls;
@@ -265,19 +282,21 @@ notify_data (void *cls, uint16_t *data_size, void *data)
 }
 
 
-void host_left ()
+static void
+host_left ()
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "The host has left the place.\n");
   GNUNET_SOCIAL_slicer_destroy (host_slicer);
   host_slicer = NULL;
   hst = NULL;
+  hst_plc = NULL;
 
   end ();
 }
 
 
-void
+static void
 schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   test = TEST_HOST_LEAVE;
@@ -285,7 +304,7 @@ schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-void
+static void
 host_farewell (void *cls,
                struct GNUNET_SOCIAL_Nym *nym,
                struct GNUNET_ENV_Environment *env,
@@ -293,19 +312,21 @@ host_farewell (void *cls,
                struct GNUNET_ENV_Modifier *variables)
 {
   // FIXME: this function is not called yet
+  struct GNUNET_CRYPTO_EcdsaPublicKey *nym_key = GNUNET_SOCIAL_nym_get_key (nym);
+  char *str;
 
-  struct GNUNET_CRYPTO_EcdsaPublicKey *
-    nym_key = GNUNET_SOCIAL_nym_get_key (nym);
+  str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Nym %s has left the place.\n",
-              GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key));
+              str);
+  GNUNET_free (str);
   GNUNET_assert (0 == memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)));
 
   GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
 }
 
 
-void
+static void
 guest_left (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -313,12 +334,13 @@ guest_left (void *cls)
   GNUNET_SOCIAL_slicer_destroy (guest_slicer);
   guest_slicer = NULL;
   gst = NULL;
+  gst_plc = NULL;
 
   GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
 }
 
 
-void
+static void
 guest_leave()
 {
   test = TEST_GUEST_LEAVE;
@@ -327,8 +349,150 @@ guest_leave()
 }
 
 
+static void
+schedule_guest_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  guest_leave ();
+}
+
+
+static void
+guest_look_for_result (void *cls, int64_t result_code,
+                      const void *data, uint16_t data_size)
+{
+  struct ResultClosure *rcls = cls;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "guest_look_for_result: %d\n", result_code);
+  GNUNET_assert (GNUNET_OK == result_code);
+  GNUNET_assert (3 == rcls->n);
+  GNUNET_free (rcls);
+  GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
+}
+
+
+static void
+guest_look_for_var (void *cls,
+                   const struct GNUNET_MessageHeader *mod,
+                   const char *name,
+                   const void *value,
+                   uint32_t value_size,
+                   uint32_t full_value_size)
+{
+  struct ResultClosure *rcls = cls;
+  rcls->n++;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "guest_look_for_var: %s\n%.*s\n",
+              name, value_size, value);
+}
+
+
+static void
+guest_look_for ()
+{
+  test = TEST_GUEST_LOOK_FOR;
+  struct ResultClosure *rcls = GNUNET_malloc (sizeof (*rcls));
+  GNUNET_SOCIAL_place_look_for (gst_plc, "_foo", guest_look_for_var, guest_look_for_result, rcls);
+}
+
+
+static void
+guest_look_at_result (void *cls, int64_t result_code,
+                      const void *data, uint16_t data_size)
+{
+  struct ResultClosure *rcls = cls;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "guest_look_at_result: %d\n", result_code);
+  GNUNET_assert (GNUNET_OK == result_code);
+  GNUNET_assert (1 == rcls->n);
+  GNUNET_free (rcls);
+  guest_look_for ();
+}
+
+
+static void
+guest_look_at_var (void *cls,
+                   const struct GNUNET_MessageHeader *mod,
+                   const char *name,
+                   const void *value,
+                   uint32_t value_size,
+                   uint32_t full_value_size)
+{
+  struct ResultClosure *rcls = cls;
+  rcls->n++;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "guest_look_at_var: %s\n%.*s\n",
+              name, value_size, value);
+}
+
+
+static void
+guest_look_at ()
+{
+  test = TEST_GUEST_LOOK_AT;
+  struct ResultClosure *rcls = GNUNET_malloc (sizeof (*rcls));
+  GNUNET_SOCIAL_place_look_at (gst_plc, "_foo_bar", guest_look_at_var, guest_look_at_result, rcls);
+}
+
+
+static void
+guest_recv_history_replay_latest_result (void *cls, int64_t result,
+                                         const void *data, uint16_t data_size)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Guest received latest history replay result: %" PRId64 "\n"
+              "%.*s\n",
+              test, result, data_size, data);
+  GNUNET_assert (2 == counter); /* message count */
+  GNUNET_assert (7 == result); /* fragment count */
+
+  guest_look_at ();
+}
+
+
+static void
+guest_history_replay_latest ()
+{
+  test = TEST_GUEST_HISTORY_REPLAY_LATEST;
+  counter = 0;
+  GNUNET_SOCIAL_place_history_replay_latest (gst_plc, 3, "",
+                                             GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
+                                             guest_slicer,
+                                             &guest_recv_history_replay_latest_result,
+                                             NULL);
+}
+
+
+static void
+guest_recv_history_replay_result (void *cls, int64_t result,
+                                  const void *data, uint16_t data_size)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Guest received history replay result: %" PRId64 "\n"
+              "%.*s\n",
+              test, result, data_size, data);
+  GNUNET_assert (2 == counter); /* message count */
+  GNUNET_assert (7 == result); /* fragment count */
+
+  guest_history_replay_latest ();
+}
+
+
+static void
+guest_history_replay ()
+{
+  test = TEST_GUEST_HISTORY_REPLAY;
+  counter = 0;
+  GNUNET_SOCIAL_place_history_replay (gst_plc, 1, 3, "",
+                                      GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
+                                      guest_slicer,
+                                      &guest_recv_history_replay_result,
+                                      NULL);
+}
+
 
-void
+static void
 guest_recv_method (void *cls,
                   const struct GNUNET_PSYC_MessageMethod *meth,
                   uint64_t message_id,
@@ -338,13 +502,13 @@ guest_recv_method (void *cls,
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
-              "%s\n",
-              test, message_id, method_name);
+              "%s (flags: %x)\n",
+              test, message_id, method_name, flags);
   /* FIXME: check message */
 }
 
 
-void
+static void
 guest_recv_modifier (void *cls,
                     const struct GNUNET_PSYC_MessageModifier *mod,
                     uint64_t message_id,
@@ -360,7 +524,7 @@ guest_recv_modifier (void *cls,
 }
 
 
-void
+static void
 guest_recv_data (void *cls,
                 const struct GNUNET_MessageHeader *msg,
                 uint64_t message_id,
@@ -375,7 +539,7 @@ guest_recv_data (void *cls,
 }
 
 
-void
+static void
 guest_recv_eom (void *cls,
                const struct GNUNET_MessageHeader *msg,
                uint64_t message_id,
@@ -393,16 +557,30 @@ guest_recv_eom (void *cls,
     break;
 
   case TEST_HOST_ANNOUNCE_END:
+    host_announce2 ();
+    break;
+
+  case TEST_HOST_ANNOUNCE2:
+    test = TEST_HOST_ANNOUNCE2_END;
+    break;
+
+  case TEST_HOST_ANNOUNCE2_END:
     guest_talk ();
     break;
 
+  case TEST_GUEST_HISTORY_REPLAY:
+  case TEST_GUEST_HISTORY_REPLAY_LATEST:
+    counter++;
+    break;
+
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
 
 
-void
+static void
 host_recv_method (void *cls,
                   const struct GNUNET_PSYC_MessageMethod *meth,
                   uint64_t message_id,
@@ -418,7 +596,7 @@ host_recv_method (void *cls,
 }
 
 
-void
+static void
 host_recv_modifier (void *cls,
                     const struct GNUNET_PSYC_MessageModifier *mod,
                     uint64_t message_id,
@@ -434,7 +612,7 @@ host_recv_modifier (void *cls,
 }
 
 
-void
+static void
 host_recv_data (void *cls,
                 const struct GNUNET_MessageHeader *msg,
                 uint64_t message_id,
@@ -449,7 +627,7 @@ host_recv_data (void *cls,
 }
 
 
-void
+static void
 host_recv_eom (void *cls,
                const struct GNUNET_MessageHeader *msg,
                uint64_t message_id,
@@ -464,24 +642,32 @@ host_recv_eom (void *cls,
   {
   case TEST_HOST_ANNOUNCE:
     test = TEST_HOST_ANNOUNCE_END;
-    //host_announce2 ();
     break;
 
   case TEST_HOST_ANNOUNCE_END:
+    host_announce2 ();
+    break;
+
+  case TEST_HOST_ANNOUNCE2:
+    test = TEST_HOST_ANNOUNCE2_END;
+    break;
+
+  case TEST_HOST_ANNOUNCE2_END:
     guest_talk ();
     break;
 
   case TEST_GUEST_TALK:
-    guest_leave ();
+      guest_history_replay ();
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
 
 
-void
+static void
 guest_talk ()
 {
   test = TEST_GUEST_TALK;
@@ -503,7 +689,8 @@ guest_talk ()
                                 GNUNET_SOCIAL_TALK_NONE);
 }
 
-void
+
+static void
 host_announce ()
 {
   test = TEST_HOST_ANNOUNCE;
@@ -525,13 +712,15 @@ host_announce ()
   tmit.host_ann
     = GNUNET_SOCIAL_host_announce (hst, "_message_host", tmit.env,
                                    &notify_data, &tmit,
-                                   GNUNET_SOCIAL_ANNOUNCE_NONE);
+                                   GNUNET_SOCIAL_ANNOUNCE_NONE
+                                   | GNUNET_PSYC_MASTER_TRANSMIT_STATE_MODIFY);
 }
 
-void
+
+static void
 host_announce2 ()
 {
-  test = TEST_HOST_ANNOUNCE;
+  test = TEST_HOST_ANNOUNCE2;
 
   tmit = (struct TransmitClosure) {};
   tmit.env = GNUNET_ENV_environment_create ();
@@ -553,7 +742,7 @@ host_announce2 ()
 }
 
 
-void
+static void
 guest_recv_entry_decision (void *cls,
                            int is_admitted,
                            const struct GNUNET_PSYC_Message *entry_resp)
@@ -589,12 +778,13 @@ guest_recv_entry_decision (void *cls,
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
 
 
-void
+static void
 host_answer_door (void *cls,
                   struct GNUNET_SOCIAL_Nym *nym,
                   const char *method_name,
@@ -628,12 +818,13 @@ host_answer_door (void *cls,
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
 
 
-void
+static void
 guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Guest entered to local place.\n");
@@ -641,7 +832,7 @@ guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
 }
 
 
-void
+static void
 guest_enter ()
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as guest.\n");
@@ -663,10 +854,12 @@ guest_enter ()
                                    &this_peer, 0, NULL, emsg->msg,
                                    guest_slicer, &guest_recv_local_enter,
                                    &guest_recv_entry_decision, NULL);
+  gst_plc = GNUNET_SOCIAL_guest_get_place (gst);
 }
 
 
-void id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
+static void
+id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
 {
   GNUNET_assert (NULL != ego);
   guest_ego = ego;
@@ -681,7 +874,8 @@ void id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
 }
 
 
-void id_guest_created (void *cls, const char *emsg)
+static void
+id_guest_created (void *cls, const char *emsg)
 {
   if (NULL != emsg)
   {
@@ -696,7 +890,8 @@ void id_guest_created (void *cls, const char *emsg)
 }
 
 
-void host_entered (void *cls, int result, uint64_t max_message_id)
+static void
+host_entered (void *cls, int result, uint64_t max_message_id)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Host entered to place.\n");
 
@@ -704,7 +899,8 @@ void host_entered (void *cls, int result, uint64_t max_message_id)
 }
 
 
-void id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
+static void
+id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
 {
   GNUNET_assert (NULL != ego);
   host_ego = ego;
@@ -719,10 +915,12 @@ void id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
                                   GNUNET_PSYC_CHANNEL_PRIVATE, host_slicer,
                                   &host_entered, &host_answer_door,
                                   &host_farewell, NULL);
+  hst_plc = GNUNET_SOCIAL_host_get_place (hst);
 }
 
 
-void id_host_created (void *cls, const char *emsg)
+static void
+id_host_created (void *cls, const char *emsg)
 {
   if (NULL != emsg)
   {
@@ -737,14 +935,15 @@ void id_host_created (void *cls, const char *emsg)
 }
 
 
-void identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
-                      void **ctx, const char *name)
+static void
+identity_ego_cb (void *cls, struct GNUNET_IDENTITY_Ego *ego,
+                 void **ctx, const char *name)
 {
 
 }
 
 
-void
+static void
 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
 {
   this_peer = *my_identity;
@@ -761,7 +960,7 @@ core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
  * @param cfg configuration we use (also to connect to Social service)
  * @param peer handle to access more of the peer (not used)
  */
-void
+static void
 #if DEBUG_TEST_SOCIAL
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)