libgnunetchat

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

test_gnunet_chat_handle_rename.c (4453B)


      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_rename.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_RENAME_ID_A   "gnunet_chat_handle_rename_a"
     28 #define TEST_RENAME_ID_B   "gnunet_chat_handle_rename_b"
     29 #define TEST_RENAME_SECRET "test_secret_rename"
     30 
     31 enum GNUNET_GenericReturnValue
     32 on_gnunet_chat_handle_rename_msg(void *cls,
     33                                  struct GNUNET_CHAT_Context *context,
     34                                  struct GNUNET_CHAT_Message *message)
     35 {
     36   static unsigned int rename_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_null(context);
     44   ck_assert_ptr_nonnull(message);
     45 
     46   struct GNUNET_CHAT_Account *account;
     47   account = GNUNET_CHAT_message_get_account(message);
     48 
     49   const char *name = GNUNET_CHAT_get_name(handle);
     50   char *dup = (char*) GNUNET_CHAT_get_user_pointer(handle);
     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(account);
     59       break;
     60     case GNUNET_CHAT_KIND_LOGIN:
     61       ck_assert_ptr_nonnull(account);
     62       ck_assert_ptr_nonnull(name);
     63       ck_assert_ptr_null(dup);
     64       ck_assert_str_eq(name, TEST_RENAME_ID_A);
     65       ck_assert_uint_eq(rename_stage, 1);
     66 
     67       dup = GNUNET_strdup(name);
     68 
     69       ck_assert_ptr_nonnull(dup);
     70       ck_assert_str_eq(name, dup);
     71 
     72       GNUNET_CHAT_set_user_pointer(handle, (void*) dup);
     73 
     74       ck_assert_int_eq(GNUNET_CHAT_set_name(
     75         handle,
     76         TEST_RENAME_ID_B
     77       ), GNUNET_YES);
     78 
     79       rename_stage = 2;
     80       break;
     81     case GNUNET_CHAT_KIND_LOGOUT:
     82       ck_assert_ptr_nonnull(account);
     83       ck_assert_uint_eq(rename_stage, 3);
     84 
     85       ck_assert_int_eq(GNUNET_CHAT_account_delete(
     86         handle,
     87         TEST_RENAME_ID_B
     88       ), GNUNET_OK);
     89 
     90       rename_stage = 4;
     91       break;
     92     case GNUNET_CHAT_KIND_CREATED_ACCOUNT:
     93       ck_assert_ptr_nonnull(account);
     94       ck_assert_uint_eq(rename_stage, 0);
     95 
     96       GNUNET_CHAT_connect(
     97         handle,
     98         account,
     99         TEST_RENAME_SECRET,
    100         strlen(TEST_RENAME_SECRET)
    101       );
    102 
    103       rename_stage = 1;
    104       break;
    105     case GNUNET_CHAT_KIND_DELETED_ACCOUNT:
    106       ck_assert_ptr_nonnull(account);
    107       ck_assert_uint_eq(rename_stage, 4);
    108 
    109       GNUNET_CHAT_stop(handle);
    110       break;
    111     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
    112       ck_assert_ptr_nonnull(account);
    113       ck_assert_ptr_nonnull(name);
    114       ck_assert_ptr_nonnull(dup);
    115       ck_assert_str_ne(name, dup);
    116       ck_assert_str_eq(name, TEST_RENAME_ID_B);
    117       ck_assert_str_eq(dup, TEST_RENAME_ID_A);
    118       ck_assert_uint_eq(rename_stage, 2);
    119 
    120       GNUNET_free(dup);
    121 
    122       GNUNET_CHAT_disconnect(handle);
    123       rename_stage = 3;
    124       break;
    125     default:
    126       ck_abort();
    127       break;
    128   }
    129 
    130   return GNUNET_YES;
    131 }
    132 
    133 void
    134 setup_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg)
    135 {
    136 }
    137 
    138 void
    139 call_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg)
    140 {
    141   static struct GNUNET_CHAT_Handle *handle = NULL;
    142   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_handle_rename_msg, &handle);
    143 
    144   ck_assert_ptr_nonnull(handle);
    145   ck_assert_int_eq(GNUNET_CHAT_account_create(
    146     handle, TEST_RENAME_ID_A
    147   ), GNUNET_OK);
    148 }
    149 
    150 void
    151 cleanup_gnunet_chat_handle_rename(const struct GNUNET_CONFIGURATION_Handle *cfg)
    152 {
    153 }
    154 
    155 CREATE_GNUNET_TEST(test_gnunet_chat_handle_rename, gnunet_chat_handle_rename)
    156 
    157 START_SUITE(handle_suite, "Handle")
    158 ADD_TEST_TO_SUITE(test_gnunet_chat_handle_rename, "Rename")
    159 END_SUITE
    160 
    161 MAIN_SUITE(handle_suite, CK_NORMAL)