libgnunetchat

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

test_gnunet_chat_message_text.c (4355B)


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