libgnunetchat

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

test_gnunet_chat_attribute_check.c (4693B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2024--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_attribute_check.c
     23  */
     24 
     25 #include "test_gnunet_chat.h"
     26 
     27 #define TEST_CHECK_ID     "gnunet_chat_attribute_check"
     28 #define TEST_CHECK_NAME   "test_attribute_check_name"
     29 #define TEST_CHECK_VALUE  "test_attribute_check_value"
     30 #define TEST_CHECK_SECRET "test_secret_attribute_check"
     31 
     32 enum GNUNET_GenericReturnValue
     33 on_gnunet_chat_attribute_check_attr(void *cls,
     34                                     struct GNUNET_CHAT_Handle *handle,
     35                                     const char *name,
     36                                     const char *value)
     37 {
     38   ck_assert_ptr_null(cls);
     39   ck_assert_ptr_nonnull(handle);
     40   ck_assert_ptr_nonnull(name);
     41 
     42   if (0 == strcmp(name, TEST_CHECK_NAME))
     43   {
     44     ck_assert_ptr_nonnull(value);
     45     ck_assert_str_eq(value, TEST_CHECK_VALUE);
     46 
     47     GNUNET_CHAT_delete_attribute(handle, TEST_CHECK_NAME);
     48     return GNUNET_NO;
     49   }
     50 
     51   return GNUNET_YES;
     52 }
     53 
     54 enum GNUNET_GenericReturnValue
     55 on_gnunet_chat_attribute_check_msg(void *cls,
     56                                    struct GNUNET_CHAT_Context *context,
     57                                    struct GNUNET_CHAT_Message *message)
     58 {
     59   static unsigned int attribute_stage = 0;
     60 
     61   struct GNUNET_CHAT_Handle *handle = *(
     62     (struct GNUNET_CHAT_Handle**) cls
     63   );
     64 
     65   struct GNUNET_CHAT_Account *account;
     66   const char *text;
     67 
     68   ck_assert_ptr_nonnull(handle);
     69   ck_assert_ptr_nonnull(message);
     70 
     71   account = GNUNET_CHAT_message_get_account(message);
     72 
     73   switch (GNUNET_CHAT_message_get_kind(message))
     74   {
     75     case GNUNET_CHAT_KIND_WARNING:
     76       ck_abort_msg("%s\n", GNUNET_CHAT_message_get_text(message));
     77       break;
     78     case GNUNET_CHAT_KIND_REFRESH:
     79       ck_assert_ptr_null(context);
     80       ck_assert_ptr_null(account);
     81 
     82       if (attribute_stage == 0)
     83       {
     84         account = GNUNET_CHAT_find_account(handle, TEST_CHECK_ID);
     85 
     86         ck_assert_ptr_nonnull(account);
     87 
     88         GNUNET_CHAT_connect(
     89           handle,
     90           account,
     91           TEST_CHECK_SECRET,
     92           strlen(TEST_CHECK_SECRET)
     93         );
     94 
     95         attribute_stage = 1;
     96       }
     97 
     98       break;
     99     case GNUNET_CHAT_KIND_LOGIN:
    100       ck_assert_ptr_null(context);
    101       ck_assert_ptr_nonnull(account);
    102       ck_assert_uint_eq(attribute_stage, 1);
    103 
    104       GNUNET_CHAT_set_attribute(
    105         handle,
    106         TEST_CHECK_NAME,
    107         TEST_CHECK_VALUE
    108       );
    109 
    110       attribute_stage = 2;
    111       break;
    112     case GNUNET_CHAT_KIND_LOGOUT:
    113       ck_assert_ptr_null(context);
    114       ck_assert_ptr_nonnull(account);
    115       ck_assert_uint_eq(attribute_stage, 4);
    116 
    117       GNUNET_CHAT_stop(handle);
    118       break;
    119     case GNUNET_CHAT_KIND_UPDATE_ACCOUNT:
    120       ck_assert_ptr_nonnull(account);
    121       break;
    122     case GNUNET_CHAT_KIND_ATTRIBUTES:
    123       ck_assert_ptr_null(context);
    124 
    125       text = GNUNET_CHAT_message_get_text(message);
    126 
    127       if (text)
    128       {
    129         ck_assert_str_eq(text, TEST_CHECK_NAME);
    130         ck_assert_uint_eq(attribute_stage, 2);
    131 
    132         GNUNET_CHAT_get_attributes(
    133           handle,
    134           on_gnunet_chat_attribute_check_attr,
    135           NULL
    136         );
    137 
    138         attribute_stage = 3;
    139       }
    140       else
    141       {
    142         ck_assert_uint_eq(attribute_stage, 3);
    143 
    144         GNUNET_CHAT_disconnect(handle);
    145 
    146         attribute_stage = 4;
    147       }
    148 
    149       break;
    150     default:
    151       ck_abort_msg("%d\n", GNUNET_CHAT_message_get_kind(message));
    152       break;
    153   }
    154 
    155   return GNUNET_YES;
    156 }
    157 
    158 REQUIRE_GNUNET_CHAT_ACCOUNT(gnunet_chat_attribute_check, TEST_CHECK_ID)
    159 
    160 void
    161 call_gnunet_chat_attribute_check(const struct GNUNET_CONFIGURATION_Handle *cfg)
    162 {
    163   static struct GNUNET_CHAT_Handle *handle = NULL;
    164   handle = GNUNET_CHAT_start(cfg, on_gnunet_chat_attribute_check_msg, &handle);
    165 
    166   ck_assert_ptr_nonnull(handle);
    167 }
    168 
    169 CREATE_GNUNET_TEST(test_gnunet_chat_attribute_check, gnunet_chat_attribute_check)
    170 
    171 START_SUITE(handle_suite, "Attribute")
    172 ADD_TEST_TO_SUITE(test_gnunet_chat_attribute_check, "Check")
    173 END_SUITE
    174 
    175 MAIN_SUITE(handle_suite, CK_NORMAL)