social: farewell
[oweals/gnunet.git] / src / social / test_social.c
index b18c9596c10625bee4985b2bada6a79e8c184172..ad4101bd6bb36c407c3fc30b82fc4a7dd7a5f2b3 100644 (file)
@@ -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
@@ -78,6 +78,8 @@ struct GNUNET_SOCIAL_Guest *gst;
 struct GNUNET_SOCIAL_Place *hst_plc;
 struct GNUNET_SOCIAL_Place *gst_plc;
 
+struct GNUNET_SOCIAL_Nym *nym_eject;
+
 struct GuestEnterMessage
 {
   struct GNUNET_PSYC_Message *msg;
@@ -99,6 +101,10 @@ struct TransmitClosure
   uint8_t n;
 } tmit;
 
+struct ResultClosure {
+  uint32_t n;
+} mod_foo_bar_rcls;
+
 uint8_t join_req_count;
 struct GNUNET_PSYC_Message *join_resp;
 
@@ -107,19 +113,21 @@ 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_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_LEAVE                 = 12,
-  TEST_HOST_LEAVE                  = 13,
+  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;
 
 
@@ -155,7 +163,7 @@ cleanup ()
 
   if (NULL != gst)
   {
-    GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, NULL, NULL);
+    GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, NULL, NULL, NULL);
     gst = NULL;
     gst_plc = NULL;
   }
@@ -300,21 +308,26 @@ schedule_host_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
 static void
 host_farewell (void *cls,
-               struct GNUNET_SOCIAL_Nym *nym,
-               struct GNUNET_ENV_Environment *env,
-               size_t variable_count,
-               struct GNUNET_ENV_Modifier *variables)
+               const struct GNUNET_SOCIAL_Nym *nym,
+               struct GNUNET_ENV_Environment *env)
 {
-  // FIXME: this function is not called yet
-  struct GNUNET_CRYPTO_EcdsaPublicKey *nym_key = GNUNET_SOCIAL_nym_get_key (nym);
+  const struct GNUNET_CRYPTO_EcdsaPublicKey *nym_key = GNUNET_SOCIAL_nym_get_key (nym);
   char *str;
 
   str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "Nym %s has left the place.\n",
+              "Farewell: nym %s has left the place.\n",
               str);
   GNUNET_free (str);
-  GNUNET_assert (0 == memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)));
+  GNUNET_assert (1 == GNUNET_ENV_environment_get_count (env));
+  if (0 != memcmp (&guest_pub_key, nym_key, sizeof (*nym_key)))
+  {
+    str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&guest_pub_key);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Farewell: nym does not match guest: %s\n", str);
+    GNUNET_free (str);
+    GNUNET_assert (0);
+  }
 
   GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
 }
@@ -329,8 +342,6 @@ guest_left (void *cls)
   guest_slicer = NULL;
   gst = NULL;
   gst_plc = NULL;
-
-  GNUNET_SCHEDULER_add_now (&schedule_host_leave, NULL);
 }
 
 
@@ -338,8 +349,14 @@ static void
 guest_leave()
 {
   test = TEST_GUEST_LEAVE;
-  /* FIXME test keep_active */
-  GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, &guest_left, NULL);
+
+  struct GNUNET_ENV_Environment *env = GNUNET_ENV_environment_create ();
+  GNUNET_ENV_environment_add (env, GNUNET_ENV_OP_SET,
+                              "_message", DATA2ARG ("Leaving."));
+  GNUNET_SOCIAL_guest_leave (gst, GNUNET_NO, env, &guest_left, NULL);
+  GNUNET_ENV_environment_destroy (env);
+
+  /* @todo test keep_active */
 }
 
 
@@ -350,6 +367,86 @@ schedule_guest_leave (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+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)
@@ -361,7 +458,7 @@ guest_recv_history_replay_latest_result (void *cls, int64_t result,
   GNUNET_assert (2 == counter); /* message count */
   GNUNET_assert (7 == result); /* fragment count */
 
-  GNUNET_SCHEDULER_add_now (&schedule_guest_leave, NULL);
+  guest_look_at ();
 }
 
 
@@ -418,23 +515,44 @@ guest_recv_method (void *cls,
               "Test #%u: Guest received method for message ID %" PRIu64 ":\n"
               "%s (flags: %x)\n",
               test, message_id, method_name, flags);
-  /* FIXME: check message */
+  /** @todo FIXME: check message */
 }
 
 
 static void
 guest_recv_modifier (void *cls,
-                    const struct GNUNET_PSYC_MessageModifier *mod,
-                    uint64_t message_id,
-                    enum GNUNET_ENV_Operator oper,
-                    const char *name,
-                    const void *value,
-                    uint16_t value_size)
+                     const struct GNUNET_MessageHeader *msg,
+                     uint64_t message_id,
+                     enum GNUNET_ENV_Operator oper,
+                     const char *name,
+                     const void *value,
+                     uint16_t value_size,
+                     uint16_t full_value_size)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Test #%u: Guest received modifier for message ID %" PRIu64 ":\n"
-              "%c%s: %.*s\n",
-              test, message_id, oper, name, value_size, value);
+              "%c%s: %.*s (size: %u)\n",
+              test, message_id, oper, name, value_size, value, value_size);
+  /** @todo FIXME: check modifier */
+}
+
+static void
+guest_recv_mod_foo_bar (void *cls,
+                        const struct GNUNET_MessageHeader *msg,
+                        uint64_t message_id,
+                        enum GNUNET_ENV_Operator oper,
+                        const char *name,
+                        const void *value,
+                        uint16_t value_size,
+                        uint16_t full_value_size)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Guest received modifier matching _foo_bar for message ID %" PRIu64 ":\n"
+              "%c%s: %.*s (size: %u)\n",
+              test, message_id, oper, name, value_size, value, value_size);
+  struct ResultClosure *rc = cls;
+  rc->n++;
+  /** @todo FIXME: check modifier */
 }
 
 
@@ -450,6 +568,7 @@ guest_recv_data (void *cls,
               "Test #%u: Guest received data for message ID %" PRIu64 ":\n"
               "%.*s\n",
               test, message_id, data_size, data);
+  /** @todo FIXME: check data */
 }
 
 
@@ -488,6 +607,7 @@ guest_recv_eom (void *cls,
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
@@ -505,18 +625,19 @@ host_recv_method (void *cls,
               "Test #%u: Host received method for message ID %" PRIu64 ":\n"
               "%s\n",
               test, message_id, method_name);
-  /* FIXME: check message */
+  /** @todo FIXME: check message */
 }
 
 
 static void
 host_recv_modifier (void *cls,
-                    const struct GNUNET_PSYC_MessageModifier *mod,
+                    const struct GNUNET_MessageHeader *msg,
                     uint64_t message_id,
                     enum GNUNET_ENV_Operator oper,
                     const char *name,
                     const void *value,
-                    uint16_t value_size)
+                    uint16_t value_size,
+                    uint16_t full_value_size)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
               "Test #%u: Host received modifier for message ID %" PRIu64 ":\n"
@@ -573,7 +694,11 @@ host_recv_eom (void *cls,
     guest_history_replay ();
     break;
 
+  case TEST_GUEST_LEAVE:
+    break;
+
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
@@ -607,6 +732,9 @@ host_announce ()
 {
   test = TEST_HOST_ANNOUNCE;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Host announcement.\n", test);
+
   tmit = (struct TransmitClosure) {};
   tmit.env = GNUNET_ENV_environment_create ();
   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
@@ -631,8 +759,15 @@ host_announce ()
 static void
 host_announce2 ()
 {
+  GNUNET_assert (2 == mod_foo_bar_rcls.n);
+  GNUNET_SOCIAL_slicer_modifier_remove (guest_slicer, "_foo_bar",
+                                        guest_recv_mod_foo_bar);
+
   test = TEST_HOST_ANNOUNCE2;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Host announcement 2.\n", test);
+
   tmit = (struct TransmitClosure) {};
   tmit.env = GNUNET_ENV_environment_create ();
   GNUNET_ENV_environment_add (tmit.env, GNUNET_ENV_OP_ASSIGN,
@@ -659,8 +794,8 @@ guest_recv_entry_decision (void *cls,
                            const struct GNUNET_PSYC_Message *entry_resp)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "Guest received entry decision (try %u): %d.\n",
-              join_req_count, is_admitted);
+              "Test #%u: Guest received entry decision (try %u): %d.\n",
+              test, join_req_count, is_admitted);
 
   if (NULL != entry_resp)
   {
@@ -673,7 +808,7 @@ guest_recv_entry_decision (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s\n%.*s\n",
                 method_name, data_size, data);
-    /* FIXME: check response message */
+    /** @todo FIXME: check response message */
   }
 
   switch (test)
@@ -689,6 +824,7 @@ guest_recv_entry_decision (void *cls,
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
@@ -705,8 +841,8 @@ host_answer_door (void *cls,
   join_req_count++;
 
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "Host received entry request from guest (try %u).\n",
-                join_req_count);
+              "Test #%u: Host received entry request from guest (try %u).\n",
+              test, join_req_count);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "%s\n%.*s\n",
               method_name, data_size, data);
@@ -728,6 +864,7 @@ host_answer_door (void *cls,
     break;
 
   default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "invalid test: %d\n", test);
     GNUNET_assert (0);
   }
 }
@@ -736,7 +873,8 @@ host_answer_door (void *cls,
 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");
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Guest entered to local place.\n", test);
 
 }
 
@@ -744,7 +882,8 @@ guest_recv_local_enter (void *cls, int result, uint64_t max_message_id)
 static void
 guest_enter ()
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as guest.\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: Entering to place as guest.\n", test);
 
   struct GuestEnterMessage *emsg = &guest_enter_msg;
 
@@ -772,13 +911,16 @@ id_guest_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
 {
   GNUNET_assert (NULL != ego);
   guest_ego = ego;
+  GNUNET_IDENTITY_ego_get_public_key (ego, &guest_pub_key);
 
   guest_slicer = GNUNET_SOCIAL_slicer_create ();
-  GNUNET_SOCIAL_slicer_add (guest_slicer, "",
-                            &guest_recv_method, &guest_recv_modifier,
-                            &guest_recv_data, &guest_recv_eom, NULL);
+  GNUNET_SOCIAL_slicer_method_add (guest_slicer, "",
+                                   guest_recv_method, guest_recv_modifier,
+                                   guest_recv_data, guest_recv_eom, NULL);
+  GNUNET_SOCIAL_slicer_modifier_add (guest_slicer, "_foo_bar",
+                                     guest_recv_mod_foo_bar, &mod_foo_bar_rcls);
   test = TEST_HOST_ANSWER_DOOR_ADMIT;
-  //host_announce ();
+
   guest_enter ();
 }
 
@@ -815,15 +957,15 @@ id_host_ego_cb (void *cls, const struct GNUNET_IDENTITY_Ego *ego)
   host_ego = ego;
 
   host_slicer = GNUNET_SOCIAL_slicer_create ();
-  GNUNET_SOCIAL_slicer_add (host_slicer, "",
-                            &host_recv_method, &host_recv_modifier,
-                            &host_recv_data, &host_recv_eom, NULL);
+  GNUNET_SOCIAL_slicer_method_add (host_slicer, "",
+                                   &host_recv_method, &host_recv_modifier,
+                                   &host_recv_data, &host_recv_eom, NULL);
 
   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Entering to place as host.\n");
   hst = GNUNET_SOCIAL_host_enter (cfg, host_ego, place_key,
                                   GNUNET_PSYC_CHANNEL_PRIVATE, host_slicer,
-                                  &host_entered, &host_answer_door,
-                                  &host_farewell, NULL);
+                                  host_entered, host_answer_door,
+                                  host_farewell, NULL);
   hst_plc = GNUNET_SOCIAL_host_get_place (hst);
 }
 
@@ -883,10 +1025,7 @@ run (void *cls,
   end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
 
   place_key = GNUNET_CRYPTO_eddsa_key_create ();
-  guest_key = GNUNET_CRYPTO_ecdsa_key_create ();
-
   GNUNET_CRYPTO_eddsa_key_get_public (place_key, &place_pub_key);
-  GNUNET_CRYPTO_ecdsa_key_get_public (guest_key, &guest_pub_key);
 
   core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
                               NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);