libgnunetchat

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

test_gnunet_chat_message_text.c (4497B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2023--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_message_text.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_TEXT_ID     "gnunet_chat_message_text"
     28 #define TEST_TEXT_GROUP  "gnunet_chat_message_text_group"
     29 #define TEST_TEXT_MSG    "test_text_message"
     30 #define TEST_TEXT_SECRET "test_secret_text"
     31 
     32 enum GNUNET_GenericReturnValue
     33 on_gnunet_chat_message_text_msg(void *cls,
     34                                 struct GNUNET_CHAT_Context *context,
     35                                 struct GNUNET_CHAT_Message *message)
     36 {
     37   static unsigned int text_stage = 0;
     38 
     39   struct GNUNET_CHAT_Handle *handle = *(
     40     (struct GNUNET_CHAT_Handle**) cls
     41   );
     42 
     43   struct GNUNET_CHAT_Account *account;
     44   struct GNUNET_CHAT_Group *group;
     45   const char *text;
     46 
     47   ck_assert_ptr_nonnull(handle);
     48   ck_assert_ptr_nonnull(message);
     49 
     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 (text_stage == 0)
     62       {
     63         account = GNUNET_CHAT_find_account(handle, TEST_TEXT_ID);
     64 
     65         ck_assert_ptr_nonnull(account);
     66 
     67         GNUNET_CHAT_connect(
     68           handle,
     69           account,
     70           TEST_TEXT_SECRET,
     71           strlen(TEST_TEXT_SECRET)
     72         );
     73 
     74         text_stage = 1;
     75       }
     76 
     77       break;
     78     case GNUNET_CHAT_KIND_LOGIN:
     79       ck_assert_ptr_null(context);
     80       ck_assert_ptr_nonnull(account);
     81       ck_assert_uint_eq(text_stage, 1);
     82 
     83       group = GNUNET_CHAT_group_create(handle, TEST_TEXT_GROUP);
     84 
     85       ck_assert_ptr_nonnull(group);
     86 
     87       text_stage = 2;
     88       break;
     89     case GNUNET_CHAT_KIND_LOGOUT:
     90       ck_assert_ptr_null(context);
     91       ck_assert_ptr_nonnull(account);
     92       ck_assert_uint_eq(text_stage, 5);
     93 
     94       GNUNET_CHAT_stop(handle);
     95       break;
     96     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
     97       ck_assert_ptr_nonnull(account);
     98       break;
     99     case GNUNET_CHAT_KIND_UPDATE_CONTEXT:
    100       ck_assert_ptr_nonnull(context);
    101       break;
    102     case GNUNET_CHAT_KIND_JOIN:
    103       ck_assert_ptr_nonnull(context);
    104       ck_assert_uint_eq(text_stage, 2);
    105 
    106       ck_assert_int_eq(GNUNET_CHAT_context_send_text(
    107 	      context, TEST_TEXT_MSG
    108       ), GNUNET_OK);
    109 
    110       text_stage = 3;
    111       break;
    112     case GNUNET_CHAT_KIND_LEAVE:
    113       ck_assert_ptr_nonnull(context);
    114       ck_assert_uint_eq(text_stage, 4);
    115       
    116       GNUNET_CHAT_disconnect(handle);
    117       text_stage = 5;
    118       break;
    119     case GNUNET_CHAT_KIND_CONTACT:
    120       ck_assert_ptr_nonnull(context);
    121       break;
    122     case GNUNET_CHAT_KIND_TEXT:
    123       ck_assert_ptr_nonnull(context);
    124       ck_assert_uint_eq(text_stage, 3);
    125 
    126       group = GNUNET_CHAT_context_get_group(context);
    127 
    128       ck_assert_ptr_nonnull(group);
    129 
    130       text = GNUNET_CHAT_message_get_text(message);
    131 
    132       ck_assert_str_eq(text, TEST_TEXT_MSG);
    133       ck_assert_int_eq(GNUNET_CHAT_group_leave(group), GNUNET_OK);
    134 
    135       text_stage = 4;
    136       break;
    137     default:
    138       ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message));
    139       break;
    140   }
    141 
    142   return GNUNET_YES;
    143 }
    144 
    145 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_message_text, TEST_TEXT_ID)
    146 
    147 void
    148 call_gnunet_chat_message_text(const struct GNUNET_CONFIGURATION_Handle *cfg)
    149 {
    150   static struct GNUNET_CHAT_Handle *handle = NULL;
    151   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_message_text_msg, &handle);
    152 
    153   ck_assert_ptr_nonnull(handle);
    154 }
    155 
    156 CREATE_GNUNET_TEST(test_gnunet_chat_message_text, gnunet_chat_message_text)
    157 
    158 START_SUITE(handle_suite, "Message")
    159 ADD_TEST_TO_SUITE(test_gnunet_chat_message_text, "Text")
    160 END_SUITE
    161 
    162 MAIN_SUITE(handle_suite, CK_NORMAL)