xref: /aosp_15_r20/external/libusb/tests/init_context.c (revision 86b64dcb59b3a0b37502ecd56e119234366a6f7e)
1 /* -*- Mode: C; indent-tabs-mode:nil -*- */
2 /*
3  * Unit tests for libusb_set_option
4  * Copyright © 2023 Nathan Hjelm <[email protected]>
5  * Copyright © 2023 Google, LLC. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <inttypes.h>
27 #include "libusbi.h"
28 #include "libusb_testlib.h"
29 
30 #if defined(_WIN32) && !defined(__CYGWIN__)
31 #include <winbase.h>
32 
unsetenv(const char * env)33 static int unsetenv(const char *env) {
34   return _putenv_s(env, "");
35 }
36 #endif
37 
38 #define LIBUSB_TEST_CLEAN_EXIT(code) \
39   do {                               \
40     if (test_ctx != NULL) {          \
41       libusb_exit(test_ctx);         \
42     }                                \
43     unsetenv("LIBUSB_DEBUG");        \
44     return (code);                   \
45   } while (0)
46 
47 /**
48  * Fail the test if the expression does not evaluate to LIBUSB_SUCCESS.
49  */
50 #define LIBUSB_TEST_RETURN_ON_ERROR(expr)                       \
51   do {                                                          \
52     int _result = (expr);                                       \
53     if (LIBUSB_SUCCESS != _result) {                            \
54       libusb_testlib_logf("Not success (%s) at %s:%d", #expr,   \
55                           __FILE__, __LINE__);                  \
56       LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE);              \
57     }                                                           \
58   } while (0)
59 
60 /**
61  * Use relational operator to compare two values and fail the test if the
62  * comparison is false. Intended to compare integer or pointer types.
63  *
64  * Example: LIBUSB_EXPECT(==, 0, 1) -> fail, LIBUSB_EXPECT(==, 0, 0) -> ok.
65  */
66 #define LIBUSB_EXPECT(operator, lhs, rhs)                               \
67   do {                                                                  \
68     int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
69     if (!(_lhs operator _rhs)) {                                        \
70       libusb_testlib_logf("Expected %s (%" PRId64 ") " #operator        \
71                           " %s (%" PRId64 ") at %s:%d", #lhs,           \
72                           (int64_t)(intptr_t)_lhs, #rhs,                \
73                           (int64_t)(intptr_t)_rhs, __FILE__,            \
74                           __LINE__);                                    \
75       LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE);                      \
76     }                                                                   \
77   } while (0)
78 
79 
test_init_context_basic(void)80 static libusb_testlib_result test_init_context_basic(void) {
81   libusb_context *test_ctx = NULL;
82 
83   /* test basic functionality */
84   LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
85                                                   /*num_options=*/0));
86 
87   LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
88 }
89 
test_init_context_log_level(void)90 static libusb_testlib_result test_init_context_log_level(void) {
91   libusb_context *test_ctx = NULL;
92 
93   struct libusb_init_option options[] = {
94     {
95       .option = LIBUSB_OPTION_LOG_LEVEL,
96       .value = {
97         .ival = LIBUSB_LOG_LEVEL_ERROR,
98       },
99     }
100   };
101 
102   /* test basic functionality */
103   LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, options,
104                                                   /*num_options=*/1));
105 
106 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
107   LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR);
108 #endif
109 
110   LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
111 }
112 
test_log_cb(libusb_context * ctx,enum libusb_log_level level,const char * str)113 static void LIBUSB_CALL test_log_cb(libusb_context *ctx, enum libusb_log_level level,
114                                     const char *str) {
115   UNUSED(ctx);
116   UNUSED(level);
117   UNUSED(str);
118 }
119 
test_init_context_log_cb(void)120 static libusb_testlib_result test_init_context_log_cb(void) {
121   libusb_context *test_ctx = NULL;
122 
123   struct libusb_init_option options[] = {
124     {
125       .option = LIBUSB_OPTION_LOG_CB,
126       .value = {
127         .log_cbval = (libusb_log_cb) &test_log_cb,
128       },
129     }
130   };
131 
132   /* test basic functionality */
133   LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, options,
134                                                   /*num_options=*/1));
135 
136 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
137   LIBUSB_EXPECT(==, test_ctx->log_handler, test_log_cb);
138 #endif
139 
140   LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
141 }
142 
143 static const libusb_testlib_test tests[] = {
144   { "test_init_context_basic", &test_init_context_basic },
145   { "test_init_context_log_level", &test_init_context_log_level },
146   { "test_init_context_log_cb", &test_init_context_log_cb },
147   LIBUSB_NULL_TEST
148 };
149 
main(int argc,char * argv[])150 int main(int argc, char *argv[])
151 {
152   return libusb_testlib_run_tests(argc, argv, tests);
153 }
154