libgnunetchat

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

test_gnunet_chat_lobby_join.c (4164B)


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