libgnunetchat

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

test_gnunet_chat_discourse_write.c (5631B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 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_discourse_write.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_WRITE_ID        "gnunet_chat_discourse_write"
     28 #define TEST_WRITE_GROUP     "gnunet_chat_discourse_write_group"
     29 #define TEST_WRITE_DISCOURSE "gnunet_chat_discourse_write_discourse"
     30 
     31 enum GNUNET_GenericReturnValue
     32 on_gnunet_chat_discourse_write_msg(void *cls,
     33                                   struct GNUNET_CHAT_Context *context,
     34                                   struct GNUNET_CHAT_Message *message)
     35 {
     36   static unsigned int discourse_stage = 0;
     37 
     38   struct GNUNET_CHAT_Handle *handle = *(
     39       (struct GNUNET_CHAT_Handle**) cls
     40   );
     41 
     42   ck_assert_ptr_nonnull(handle);
     43   ck_assert_ptr_nonnull(message);
     44 
     45   struct GNUNET_CHAT_Account *account;
     46   account = GNUNET_CHAT_message_get_account(message);
     47 
     48   const char *name = GNUNET_CHAT_get_name(handle);
     49   struct GNUNET_CHAT_DiscourseId discourse_id;
     50 
     51   struct GNUNET_CHAT_Discourse *discourse;
     52   discourse = GNUNET_CHAT_message_get_discourse(message);
     53 
     54   switch (GNUNET_CHAT_message_get_kind(message))
     55   {
     56     case GNUNET_CHAT_KIND_WARNING:
     57       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     58       break;
     59     case GNUNET_CHAT_KIND_REFRESH:
     60       ck_assert_ptr_null(context);
     61       ck_assert_ptr_null(account);
     62 
     63       if (discourse_stage == 0)
     64       {
     65         account = GNUNET_CHAT_find_account(handle, TEST_WRITE_ID);
     66 
     67         ck_assert_ptr_nonnull(account);
     68 
     69         GNUNET_CHAT_connect(handle, account);
     70         discourse_stage = 1;
     71       }
     72 
     73       break;
     74     case GNUNET_CHAT_KIND_LOGIN:
     75       ck_assert_ptr_null(context);
     76       ck_assert_ptr_nonnull(account);
     77       ck_assert_ptr_nonnull(name);
     78       ck_assert_str_eq(name, TEST_WRITE_ID);
     79       ck_assert_uint_eq(discourse_stage, 1);
     80 
     81       GNUNET_CHAT_group_create(handle, TEST_WRITE_GROUP);
     82       discourse_stage = 2;
     83       break;
     84     case GNUNET_CHAT_KIND_LOGOUT:
     85       ck_assert_ptr_null(context);
     86       ck_assert_ptr_nonnull(account);
     87       ck_assert_uint_eq(discourse_stage, 6);
     88       
     89       GNUNET_CHAT_stop(handle);
     90       break;
     91     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
     92       break;
     93     case GNUNET_CHAT_KIND_UPDATE_CONTEXT:
     94       break;
     95     case GNUNET_CHAT_KIND_JOIN:
     96       ck_assert_ptr_nonnull(context);
     97       ck_assert_ptr_null(discourse);
     98       ck_assert_uint_eq(discourse_stage, 2);
     99 
    100       GNUNET_memcpy(
    101         &discourse_id,
    102         TEST_WRITE_DISCOURSE,
    103         sizeof(discourse_id)
    104       );
    105 
    106       discourse = GNUNET_CHAT_context_open_discourse(
    107         context,
    108         &discourse_id
    109       );
    110 
    111       ck_assert_ptr_nonnull(discourse);
    112       ck_assert_int_eq(GNUNET_CHAT_discourse_is_open(discourse), GNUNET_NO);
    113 
    114       discourse_stage = 3;
    115       break;
    116     case GNUNET_CHAT_KIND_CONTACT:
    117       break;
    118     case GNUNET_CHAT_KIND_DISCOURSE:
    119       ck_assert_ptr_nonnull(context);
    120       ck_assert_ptr_nonnull(discourse);
    121 
    122       GNUNET_memcpy(
    123         &discourse_id,
    124         TEST_WRITE_DISCOURSE,
    125         sizeof(discourse_id)
    126       );
    127       
    128       if (GNUNET_YES == GNUNET_CHAT_discourse_is_open(discourse))
    129       {
    130         ck_assert_uint_eq(discourse_stage, 3);
    131 
    132         ck_assert_int_eq(
    133           GNUNET_CHAT_discourse_write(
    134             discourse,
    135             (const char*) &discourse_id,
    136             sizeof(discourse_id)
    137           ),
    138           GNUNET_OK
    139         );
    140 
    141         discourse_stage = 4;
    142       }
    143       else
    144       {
    145         ck_assert_uint_eq(discourse_stage, 5);
    146 
    147         GNUNET_CHAT_disconnect(handle);
    148 
    149         discourse_stage = 6;
    150       }
    151 
    152       break;
    153     case GNUNET_CHAT_KIND_DATA:
    154       ck_assert_ptr_nonnull(context);
    155       ck_assert_ptr_nonnull(discourse);
    156       ck_assert_uint_eq(discourse_stage, 4);
    157 
    158       ck_assert_uint_eq(
    159         GNUNET_CHAT_message_available(message),
    160         sizeof(discourse_id)
    161       );
    162 
    163       ck_assert_int_eq(
    164         GNUNET_CHAT_message_read(
    165           message,
    166           (char*) &discourse_id,
    167           sizeof(discourse_id)
    168         ),
    169         GNUNET_OK
    170       );
    171 
    172       ck_assert_mem_eq(
    173         &discourse_id,
    174         TEST_WRITE_DISCOURSE,
    175         sizeof(discourse_id)
    176       );
    177 
    178       GNUNET_CHAT_discourse_close(discourse);
    179       discourse_stage = 5;
    180       break;
    181     default:
    182       ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message));
    183       ck_abort();
    184       break;
    185   }
    186 
    187   return GNUNET_YES;
    188 }
    189 
    190 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_discourse_write, TEST_WRITE_ID)
    191 
    192 void
    193 call_gnunet_chat_discourse_write(const struct GNUNET_CONFIGURATION_Handle *cfg)
    194 {
    195   static struct GNUNET_CHAT_Handle *handle = NULL;
    196   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_discourse_write_msg, &handle);
    197 
    198   ck_assert_ptr_nonnull(handle);
    199 }
    200 
    201 CREATE_GNUNET_TEST(test_gnunet_chat_discourse_write, gnunet_chat_discourse_write)
    202 
    203 START_SUITE(handle_suite, "Handle")
    204 ADD_TEST_TO_SUITE(test_gnunet_chat_discourse_write, "Talk")
    205 END_SUITE
    206 
    207 MAIN_SUITE(handle_suite, CK_NORMAL)