libgnunetchat

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

test_gnunet_chat_handle_rename.c (4316B)


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