Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_client.c |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
| 4 | * @brief client test |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <setjmp.h> |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 21 | #include <sys/socket.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 22 | #include <sys/types.h> |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 23 | |
| 24 | #include <cmocka.h> |
| 25 | #include <libyang/libyang.h> |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 26 | #include <log.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 27 | #include <session_client.h> |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 28 | #include "tests/config.h" |
| 29 | |
| 30 | static int |
| 31 | setup_f(void **state) |
| 32 | { |
| 33 | (void)state; |
| 34 | |
| 35 | nc_verbosity(NC_VERB_VERBOSE); |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static int |
| 41 | teardown_f(void **state) |
| 42 | { |
| 43 | (void)state; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | test_nc_client_setting_schema_searchpath(void **state) |
| 50 | { |
| 51 | (void)state; |
| 52 | const char *path; |
| 53 | int ret; |
| 54 | |
| 55 | /* initiate client */ |
| 56 | nc_client_init(); |
| 57 | |
| 58 | path = nc_client_get_schema_searchpath(); |
| 59 | assert_null(path); |
| 60 | |
| 61 | ret = nc_client_set_schema_searchpath("path"); |
| 62 | assert_int_equal(ret, 0); |
| 63 | path = nc_client_get_schema_searchpath(); |
| 64 | assert_string_equal(path, "path"); |
| 65 | |
| 66 | ret = nc_client_set_schema_searchpath("path1"); |
| 67 | assert_int_equal(ret, 0); |
| 68 | path = nc_client_get_schema_searchpath(); |
| 69 | assert_string_equal(path, "path1"); |
| 70 | } |
| 71 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 72 | LY_ERR |
| 73 | test_clb(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, void *user_data, |
| 74 | LYS_INFORMAT *format, const char **model_data, void (**free_module_data)(void *model_data, void *user_data)) |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 75 | { |
| 76 | (void)mod_name; |
| 77 | (void)mod_rev; |
| 78 | (void)submod_name; |
| 79 | (void)sub_rev; |
| 80 | (void)user_data; |
| 81 | (void)format; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 82 | (void)model_data; |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 83 | (void)free_module_data; |
| 84 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 85 | return LY_SUCCESS; |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 88 | LY_ERR |
| 89 | test_clb1(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, void *user_data, |
| 90 | LYS_INFORMAT *format, const char **model_data, void (**free_module_data)(void *model_data, void *user_data)) |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 91 | { |
| 92 | (void)mod_name; |
| 93 | (void)mod_rev; |
| 94 | (void)submod_name; |
| 95 | (void)sub_rev; |
| 96 | (void)user_data; |
| 97 | (void)format; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 98 | (void)model_data; |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 99 | (void)free_module_data; |
| 100 | |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 101 | return LY_SUCCESS; |
David Sedlák | 2057be1 | 2018-09-30 21:48:55 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void |
| 105 | test_nc_client_setting_schema_callback(void **state) |
| 106 | { |
| 107 | (void)state; |
| 108 | ly_module_imp_clb ret_f; |
| 109 | char *data_ret; |
| 110 | int ret; |
| 111 | |
| 112 | ret_f = nc_client_get_schema_callback((void **)&data_ret); |
| 113 | assert_null(ret_f); |
| 114 | assert_null(data_ret); |
| 115 | |
| 116 | ret = nc_client_set_schema_callback(test_clb, "DATA"); |
| 117 | assert_int_equal(ret, 0); |
| 118 | ret_f = nc_client_get_schema_callback((void **)&data_ret); |
| 119 | assert_ptr_equal(test_clb, ret_f); |
| 120 | assert_string_equal("DATA", data_ret); |
| 121 | |
| 122 | ret = nc_client_set_schema_callback(test_clb1, "DATA1"); |
| 123 | assert_int_equal(ret, 0); |
| 124 | ret_f = nc_client_get_schema_callback((void **)&data_ret); |
| 125 | assert_ptr_equal(test_clb1, ret_f); |
| 126 | assert_string_equal("DATA1", data_ret); |
| 127 | |
| 128 | /* destroy client */ |
| 129 | nc_client_destroy(); |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | main(void) |
| 134 | { |
| 135 | const struct CMUnitTest tests[] = { |
| 136 | cmocka_unit_test_setup_teardown(test_nc_client_setting_schema_searchpath, setup_f, teardown_f), |
| 137 | cmocka_unit_test_setup_teardown(test_nc_client_setting_schema_callback, setup_f, teardown_f), |
| 138 | }; |
| 139 | |
| 140 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 141 | } |