libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

test_gnunet_chat_attribute_share.c (7749B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2024--2026 GNUnet e.V.
      4 
      5    GNUnet is free software: you can redistribute it and/or modify it
      6    under the terms of the GNU Affero General Public License as published
      7    by the Free Software Foundation, either version 3 of the License,
      8    or (at your option) any later version.
      9 
     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    Affero General Public License for more details.
     14 
     15    You should have received a copy of the GNU Affero General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18    SPDX-License-Identifier: AGPL3.0-or-later
     19  */
     20 /*
     21  * @author Tobias Frisch
     22  * @file test_gnunet_chat_attribute_share.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_SHARE_ID_A     "gnunet_chat_attribute_share_a"
     28 #define TEST_SHARE_ID_B     "gnunet_chat_attribute_share_b"
     29 #define TEST_SHARE_GROUP    "test_attribute_share_group"
     30 #define TEST_SHARE_NAME     "test_attribute_share_name"
     31 #define TEST_SHARE_VALUE    "test_attribute_share_value"
     32 #define TEST_SHARE_SECRET_A "test_secret_attribute_share_a"
     33 #define TEST_SHARE_SECRET_B "test_secret_attribute_share_b"
     34 
     35 enum GNUNET_GenericReturnValue
     36 on_gnunet_chat_attribute_share_attr(void *cls,
     37                                     struct GNUNET_CHAT_Contact *contact,
     38                                     const char *name,
     39                                     const char *value)
     40 {
     41   ck_assert_ptr_nonnull(cls);
     42   ck_assert_ptr_nonnull(contact);
     43   ck_assert_ptr_nonnull(name);
     44   ck_assert_ptr_nonnull(value);
     45 
     46   struct GNUNET_CHAT_Handle *handle = (
     47     (struct GNUNET_CHAT_Handle*) cls
     48   );
     49 
     50   const struct GNUNET_CHAT_Account *account;
     51   account = GNUNET_CHAT_get_connected(handle);
     52 
     53   const char *account_name = GNUNET_CHAT_account_get_name(account);
     54   const char *contact_name = GNUNET_CHAT_contact_get_name(contact);
     55 
     56   ck_assert_ptr_nonnull(account);
     57   ck_assert_str_eq(account_name, TEST_SHARE_ID_B);
     58 
     59   if (contact_name)
     60     ck_assert_str_eq(contact_name, TEST_SHARE_ID_A);
     61 
     62   ck_assert_str_eq(name, TEST_SHARE_NAME);
     63   ck_assert_str_eq(value, TEST_SHARE_VALUE);
     64 
     65   GNUNET_CHAT_unshare_attribute_from(handle, contact, name);
     66   return GNUNET_NO;
     67 }
     68 
     69 enum GNUNET_GenericReturnValue
     70 on_gnunet_chat_attribute_share_msg(void *cls,
     71                                    struct GNUNET_CHAT_Context *context,
     72                                    struct GNUNET_CHAT_Message *message)
     73 {
     74   static unsigned int share_stage = 0;
     75 
     76   struct GNUNET_CHAT_Handle *handle = *(
     77     (struct GNUNET_CHAT_Handle**) cls
     78   );
     79 
     80   struct GNUNET_CHAT_Account *account;
     81   struct GNUNET_CHAT_Contact *recipient;
     82   struct GNUNET_CHAT_Contact *sender;
     83   const char *name;
     84 
     85   ck_assert_ptr_nonnull(handle);
     86   ck_assert_ptr_nonnull(message);
     87 
     88   account = GNUNET_CHAT_message_get_account(message);
     89   recipient = GNUNET_CHAT_message_get_recipient(message);
     90   sender = GNUNET_CHAT_message_get_sender(message);
     91 
     92   switch (GNUNET_CHAT_message_get_kind(message))
     93   {
     94     case GNUNET_CHAT_KIND_WARNING:
     95       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     96       break;
     97     case GNUNET_CHAT_KIND_REFRESH:
     98       ck_assert_ptr_null(context);
     99       ck_assert_ptr_null(account);
    100 
    101       if (share_stage == 0)
    102       {
    103         account = GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_A);
    104 
    105         ck_assert_ptr_nonnull(account);
    106 
    107         GNUNET_CHAT_connect(
    108           handle,
    109           account,
    110           TEST_SHARE_SECRET_A,
    111           strlen(TEST_SHARE_SECRET_A)
    112         );
    113 
    114         share_stage = 1;
    115       }
    116 
    117       break;
    118     case GNUNET_CHAT_KIND_LOGIN:
    119       ck_assert_ptr_null(context);
    120       ck_assert_ptr_nonnull(account);
    121 
    122       name = GNUNET_CHAT_account_get_name(account);
    123 
    124       ck_assert_ptr_nonnull(name);
    125       ck_assert_uint_ge(share_stage, 1);
    126 
    127       if (0 == strcmp(name, TEST_SHARE_ID_B))
    128       {
    129         ck_assert_uint_eq(share_stage, 4);
    130 
    131         GNUNET_CHAT_set_attribute(
    132           handle,
    133           TEST_SHARE_NAME,
    134           TEST_SHARE_VALUE
    135         );
    136 
    137         share_stage = 5;
    138       }
    139       else
    140       {
    141         ck_assert_uint_eq(share_stage, 1);
    142 
    143         share_stage = 2;
    144       }
    145 
    146       ck_assert_ptr_nonnull(GNUNET_CHAT_group_create(
    147         handle, TEST_SHARE_GROUP
    148       ));
    149       break;
    150     case GNUNET_CHAT_KIND_LOGOUT:
    151       ck_assert_ptr_null(context);
    152       ck_assert_ptr_nonnull(account);
    153       ck_assert_uint_ge(share_stage, 3);
    154 
    155       name = GNUNET_CHAT_account_get_name(account);
    156 
    157       ck_assert_ptr_nonnull(name);
    158 
    159       if (0 == strcmp(name, TEST_SHARE_ID_A))
    160       {
    161         ck_assert_uint_eq(share_stage, 3);
    162 
    163         share_stage = 4;
    164       }
    165       else if (0 == strcmp(name, TEST_SHARE_ID_B))
    166       {
    167         ck_assert_uint_eq(share_stage, 7);
    168 
    169         GNUNET_CHAT_stop(handle);
    170       }
    171 
    172       break;
    173     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
    174       ck_assert_ptr_nonnull(account);
    175       break;
    176     case GNUNET_CHAT_KIND_UPDATE_CONTEXT:
    177       ck_assert_ptr_nonnull(context);
    178       break;
    179     case GNUNET_CHAT_KIND_JOIN:
    180       ck_assert_ptr_nonnull(context);
    181       ck_assert_ptr_nonnull(account);
    182       ck_assert_ptr_nonnull(sender);
    183 
    184       name = GNUNET_CHAT_account_get_name(account);
    185 
    186       ck_assert_ptr_nonnull(name);
    187 
    188       if ((0 == strcmp(name, TEST_SHARE_ID_A)) &&
    189           (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)))
    190       {
    191         ck_assert_uint_eq(share_stage, 2);
    192 
    193         GNUNET_CHAT_connect(
    194           handle, 
    195           GNUNET_CHAT_find_account(handle, TEST_SHARE_ID_B),
    196           TEST_SHARE_SECRET_B,
    197           strlen(TEST_SHARE_SECRET_B)
    198         );
    199 
    200         share_stage = 3;
    201       }
    202       else if ((GNUNET_YES != GNUNET_CHAT_message_is_sent(message)) &&
    203                (share_stage == 5))
    204       {
    205         GNUNET_CHAT_share_attribute_with(handle, sender, TEST_SHARE_NAME);
    206         share_stage = 6;
    207       }
    208       
    209       break;
    210     case GNUNET_CHAT_KIND_CONTACT:
    211       ck_assert_ptr_nonnull(context);
    212       ck_assert_ptr_nonnull(sender);
    213       break;
    214     case GNUNET_CHAT_KIND_ATTRIBUTES:
    215       ck_assert_ptr_null(context);
    216       ck_assert_uint_ge(share_stage, 5);
    217       break;
    218     case GNUNET_CHAT_KIND_SHARED_ATTRIBUTES:
    219       ck_assert_uint_ge(share_stage, 6);
    220 
    221       if (!context)
    222       {
    223         ck_assert_ptr_null(account);
    224         ck_assert_ptr_null(sender);
    225         ck_assert_ptr_null(recipient);
    226 
    227         GNUNET_CHAT_disconnect(handle);
    228         share_stage = 7;
    229       }
    230       else if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message))
    231       {
    232         ck_assert_ptr_nonnull(account);
    233         ck_assert_ptr_nonnull(sender);
    234         ck_assert_ptr_nonnull(recipient);
    235         ck_assert_ptr_ne(sender, recipient);
    236 
    237         GNUNET_CHAT_get_shared_attributes(
    238           handle,
    239           recipient,
    240           on_gnunet_chat_attribute_share_attr,
    241           handle
    242         );
    243       }
    244       break;
    245     default:
    246       ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message));
    247       break;
    248   }
    249 
    250   return GNUNET_YES;
    251 }
    252 
    253 const char *TEST_SHARE_IDS [] = {
    254   TEST_SHARE_ID_A,
    255   TEST_SHARE_ID_B,
    256   NULL
    257 };
    258 
    259 SETUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS)
    260 CLEANUP_GNUNET_CHAT_ACCOUNTS(gnunet_chat_attribute_share, TEST_SHARE_IDS)
    261 
    262 void
    263 call_gnunet_chat_attribute_share(const struct GNUNET_CONFIGURATION_Handle *cfg)
    264 {
    265   static struct GNUNET_CHAT_Handle *handle = NULL;
    266   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_share_msg, &handle);
    267 
    268   ck_assert_ptr_nonnull(handle);
    269 }
    270 
    271 CREATE_GNUNET_TEST(test_gnunet_chat_attribute_share, gnunet_chat_attribute_share)
    272 
    273 START_SUITE(handle_suite, "Attribute")
    274 ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_share, "Share")
    275 END_SUITE
    276 
    277 MAIN_SUITE(handle_suite, CK_NORMAL)