libgnunetchat

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

test_gnunet_chat_handle_connection.c (3826B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2021--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_handle_connection.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_CONNECTION_ID     "gnunet_chat_handle_connection"
     28 #define TEST_CONNECTION_SECRET "test_secret_handle_connection"
     29 
     30 enum GNUNET_GenericReturnValue
     31 on_gnunet_chat_handle_connection_msg(void *cls,
     32                                      struct GNUNET_CHAT_Context *context,
     33                                      struct GNUNET_CHAT_Message *message)
     34 {
     35   static unsigned int connection_stage = 0;
     36 
     37   struct GNUNET_CHAT_Handle *handle = *(
     38       (struct GNUNET_CHAT_Handle**) cls
     39   );
     40 
     41   ck_assert_ptr_nonnull(handle);
     42   ck_assert_ptr_null(context);
     43   ck_assert_ptr_nonnull(message);
     44 
     45   struct GNUNET_CHAT_Account *connected;
     46   struct GNUNET_CHAT_Account *account;
     47   const char *name;
     48 
     49   connected = GNUNET_CHAT_get_connected(handle);
     50   account = GNUNET_CHAT_message_get_account(message);
     51 
     52   switch (GNUNET_CHAT_message_get_kind(message))
     53   {
     54     case GNUNET_CHAT_KIND_WARNING:
     55       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     56       break;
     57     case GNUNET_CHAT_KIND_REFRESH:
     58       ck_assert_ptr_null(context);
     59       ck_assert_ptr_null(account);
     60 
     61       if (connection_stage == 0)
     62       {
     63         account = GNUNET_CHAT_find_account(handle, TEST_CONNECTION_ID);
     64 
     65         ck_assert_ptr_nonnull(account);
     66 
     67         GNUNET_CHAT_connect(
     68           handle,
     69           account,
     70           TEST_CONNECTION_SECRET,
     71           strlen(TEST_CONNECTION_SECRET)
     72         );
     73         
     74         connection_stage = 1;
     75       }
     76       
     77       break;
     78     case GNUNET_CHAT_KIND_LOGIN:
     79       ck_assert_ptr_nonnull(connected);
     80       ck_assert_ptr_nonnull(account);
     81       ck_assert_ptr_eq(connected, account);
     82       ck_assert_uint_eq(connection_stage, 1);
     83 
     84       name = GNUNET_CHAT_account_get_name(account);
     85 
     86       ck_assert_ptr_nonnull(name);
     87       ck_assert_str_eq(name, TEST_CONNECTION_ID);
     88       
     89       GNUNET_CHAT_disconnect(handle);
     90       connection_stage = 2;
     91       break;
     92     case GNUNET_CHAT_KIND_LOGOUT:
     93       ck_assert_ptr_nonnull(connected);
     94       ck_assert_ptr_nonnull(account);
     95       ck_assert_ptr_eq(connected, account);
     96       ck_assert_uint_eq(connection_stage, 2);
     97 
     98       name = GNUNET_CHAT_account_get_name(account);
     99 
    100       ck_assert_ptr_nonnull(name);
    101       ck_assert_str_eq(name, TEST_CONNECTION_ID);
    102 
    103       GNUNET_CHAT_stop(handle);
    104       connection_stage = 3;
    105       break;
    106     default:
    107       ck_abort();
    108       break;
    109   }
    110 
    111   return GNUNET_YES;
    112 }
    113 
    114 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_handle_connection, TEST_CONNECTION_ID)
    115 
    116 void
    117 call_gnunet_chat_handle_connection(const struct GNUNET_CONFIGURATION_Handle *cfg)
    118 {
    119   static struct GNUNET_CHAT_Handle *handle = NULL;
    120   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_connection_msg, &handle);
    121 
    122   ck_assert_ptr_nonnull(handle);
    123 }
    124 
    125 CREATE_GNUNET_TEST(test_gnunet_chat_handle_connection, gnunet_chat_handle_connection)
    126 
    127 START_SUITE(handle_suite, "Handle")
    128 ADD_TEST_TO_SUITE(test_gnunet_chat_handle_connection, "Connect/Disconnect")
    129 END_SUITE
    130 
    131 MAIN_SUITE(handle_suite, CK_NORMAL)