libgnunetchat

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

test_gnunet_chat_group_open.c (4315B)


      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_group_open.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_OPEN_ID     "gnunet_chat_group_open"
     28 #define TEST_OPEN_GROUP  "gnunet_chat_group_open_group"
     29 #define TEST_OPEN_SECRET "test_secret_group_open" 
     30 
     31 enum GNUNET_GenericReturnValue
     32 on_gnunet_chat_group_open_msg(void *cls,
     33                               struct GNUNET_CHAT_Context *context,
     34                               struct GNUNET_CHAT_Message *message)
     35 {
     36   static unsigned int group_stage = 0;
     37 
     38   struct GNUNET_CHAT_Handle *handle = *(
     39       (struct GNUNET_CHAT_Handle**) cls
     40   );
     41 
     42   ck_assert_ptr_nonnull(handle);
     43   ck_assert_ptr_nonnull(message);
     44 
     45   struct GNUNET_CHAT_Account *account;
     46   account = GNUNET_CHAT_message_get_account(message);
     47 
     48   const char *name = GNUNET_CHAT_get_name(handle);
     49 
     50   struct GNUNET_CHAT_Group *group;
     51   group = GNUNET_CHAT_context_get_group(context);
     52 
     53   switch (GNUNET_CHAT_message_get_kind(message))
     54   {
     55     case GNUNET_CHAT_KIND_WARNING:
     56       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     57       break;
     58     case GNUNET_CHAT_KIND_REFRESH:
     59       ck_assert_ptr_null(context);
     60       ck_assert_ptr_null(account);
     61 
     62       if (group_stage == 0)
     63       {
     64         account = GNUNET_CHAT_find_account(handle, TEST_OPEN_ID);
     65 
     66         ck_assert_ptr_nonnull(account);
     67 
     68         GNUNET_CHAT_connect(
     69           handle,
     70           account,
     71           TEST_OPEN_SECRET,
     72           strlen(TEST_OPEN_SECRET)
     73         );
     74 
     75         group_stage = 1;
     76       }
     77 
     78       break;
     79     case GNUNET_CHAT_KIND_LOGIN:
     80       ck_assert_ptr_null(context);
     81       ck_assert_ptr_nonnull(account);
     82       ck_assert_ptr_nonnull(name);
     83       ck_assert_str_eq(name, TEST_OPEN_ID);
     84       ck_assert_ptr_null(group);
     85       ck_assert_uint_eq(group_stage, 1);
     86 
     87       group = GNUNET_CHAT_group_create(handle, TEST_OPEN_GROUP);
     88 
     89       ck_assert_ptr_nonnull(group);
     90 
     91       group_stage = 2;
     92       break;
     93     case GNUNET_CHAT_KIND_LOGOUT:
     94       ck_assert_ptr_null(context);
     95       ck_assert_ptr_nonnull(account);
     96       ck_assert_ptr_null(group);
     97       ck_assert_uint_eq(group_stage, 4);
     98       
     99       GNUNET_CHAT_stop(handle);
    100       break;
    101     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
    102       ck_assert_ptr_nonnull(account);
    103       break;
    104     case GNUNET_CHAT_KIND_UPDATE_CONTEXT:
    105       ck_assert_ptr_nonnull(context);
    106       break;
    107     case GNUNET_CHAT_KIND_JOIN:
    108       ck_assert_ptr_nonnull(context);
    109       ck_assert_ptr_nonnull(group);
    110       ck_assert_uint_eq(group_stage, 2);
    111 
    112       ck_assert_int_eq(
    113         GNUNET_CHAT_group_leave(group),
    114         GNUNET_OK
    115       );
    116 
    117       group_stage = 3;
    118       break;
    119     case GNUNET_CHAT_KIND_LEAVE:
    120       ck_assert_ptr_nonnull(context);
    121       ck_assert_ptr_null(group);
    122       ck_assert_uint_eq(group_stage, 3);
    123       
    124       GNUNET_CHAT_disconnect(handle);
    125       group_stage = 4;
    126       break;
    127     case GNUNET_CHAT_KIND_CONTACT:
    128       break;
    129     default:
    130       ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message));
    131       ck_abort();
    132       break;
    133   }
    134 
    135   return GNUNET_YES;
    136 }
    137 
    138 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_group_open, TEST_OPEN_ID)
    139 
    140 void
    141 call_gnunet_chat_group_open(const struct GNUNET_CONFIGURATION_Handle *cfg)
    142 {
    143   static struct GNUNET_CHAT_Handle *handle = NULL;
    144   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_group_open_msg, &handle);
    145 
    146   ck_assert_ptr_nonnull(handle);
    147 }
    148 
    149 CREATE_GNUNET_TEST(test_gnunet_chat_group_open, gnunet_chat_group_open)
    150 
    151 START_SUITE(handle_suite, "Group")
    152 ADD_TEST_TO_SUITE(test_gnunet_chat_group_open, "Open/Close")
    153 END_SUITE
    154 
    155 MAIN_SUITE(handle_suite, CK_NORMAL)