libgnunetchat

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

test_gnunet_chat_lobby_join.c (4015B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2022--2024 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_lobby_join.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_JOIN_ID "gnunet_chat_lobby_join"
     28 
     29 void
     30 on_gnunet_chat_lobby_join_open(void *cls,
     31                                const struct GNUNET_CHAT_Uri *uri)
     32 {
     33   struct GNUNET_CHAT_Handle *chat = (struct GNUNET_CHAT_Handle*) cls;
     34 
     35   ck_assert_ptr_nonnull(chat);
     36   ck_assert_ptr_nonnull(uri);
     37 
     38   GNUNET_CHAT_lobby_join(chat, uri);
     39 }
     40 
     41 enum GNUNET_GenericReturnValue
     42 on_gnunet_chat_lobby_join_msg(void *cls,
     43                               struct GNUNET_CHAT_Context *context,
     44                               struct GNUNET_CHAT_Message *message)
     45 {
     46   static unsigned int lobby_stage = 0;
     47   static struct GNUNET_CHAT_Lobby *lobby = NULL;
     48 
     49   struct GNUNET_CHAT_Handle *handle = *(
     50       (struct GNUNET_CHAT_Handle**) cls
     51   );
     52 
     53   ck_assert_ptr_nonnull(handle);
     54   ck_assert_ptr_nonnull(message);
     55 
     56   struct GNUNET_CHAT_Account *account;
     57   account = GNUNET_CHAT_message_get_account(message);
     58 
     59   switch (GNUNET_CHAT_message_get_kind(message))
     60   {
     61     case GNUNET_CHAT_KIND_WARNING:
     62       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     63       break;
     64     case GNUNET_CHAT_KIND_REFRESH:
     65       ck_assert_ptr_null(context);
     66       ck_assert_ptr_null(account);
     67 
     68       if (lobby_stage == 0)
     69       {
     70         account = GNUNET_CHAT_find_account(handle, TEST_JOIN_ID);
     71 
     72         ck_assert_ptr_nonnull(account);
     73 
     74         GNUNET_CHAT_connect(handle, account);
     75         lobby_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_null(lobby);
     83       ck_assert_uint_eq(lobby_stage, 1);
     84 
     85       lobby = GNUNET_CHAT_lobby_open(
     86         handle,
     87         1,
     88         on_gnunet_chat_lobby_join_open,
     89         handle
     90       );
     91 
     92       ck_assert_ptr_nonnull(lobby);
     93       lobby_stage = 2;
     94       break;
     95     case GNUNET_CHAT_KIND_LOGOUT:
     96       ck_assert_ptr_null(context);
     97       ck_assert_ptr_nonnull(account);
     98       ck_assert_ptr_null(lobby);
     99       ck_assert_uint_eq(lobby_stage, 3);
    100 
    101       GNUNET_CHAT_stop(handle);
    102       break;
    103     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
    104       ck_assert_ptr_null(context);
    105       ck_assert_ptr_nonnull(account);
    106       break;
    107     case GNUNET_CHAT_KIND_UPDATE_CONTEXT:
    108       ck_assert_ptr_nonnull(context);
    109       break;
    110     case GNUNET_CHAT_KIND_JOIN:
    111       ck_assert_ptr_nonnull(context);
    112       ck_assert_ptr_nonnull(account);
    113       ck_assert_ptr_nonnull(lobby);
    114       ck_assert_uint_eq(lobby_stage, 2);
    115 
    116       GNUNET_CHAT_lobby_close(lobby);
    117       lobby = NULL;
    118 
    119       GNUNET_CHAT_disconnect(handle);
    120       lobby_stage = 3;
    121       break;
    122     default:
    123       ck_abort();
    124       break;
    125   }
    126 
    127   return GNUNET_YES;
    128 }
    129 
    130 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_lobby_join, TEST_JOIN_ID)
    131 
    132 void
    133 call_gnunet_chat_lobby_join(const struct GNUNET_CONFIGURATION_Handle *cfg)
    134 {
    135   static struct GNUNET_CHAT_Handle *handle = NULL;
    136   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_lobby_join_msg, &handle);
    137 
    138   ck_assert_ptr_nonnull(handle);
    139 }
    140 
    141 CREATE_GNUNET_TEST(test_gnunet_chat_lobby_join, gnunet_chat_lobby_join)
    142 
    143 START_SUITE(lobby_suite, "Lobby")
    144 ADD_TEST_TO_SUITE(test_gnunet_chat_lobby_join, "Join")
    145 END_SUITE
    146 
    147 MAIN_SUITE(lobby_suite, CK_NORMAL)