summaryrefslogtreecommitdiff
path: root/tests/set_option.c
blob: df2ae1d1201cce7f185bd444c9d344b44cefccc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* -*- Mode: C; indent-tabs-mode:nil -*- */
/*
 * Unit tests for libusb_set_option
 * Copyright © 2023 Nathan Hjelm <hjelmn@cs.unm.edu>
 * Copyright © 2023 Google, LLC. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include "libusbi.h"
#include "libusb_testlib.h"

#if defined(_WIN32) && !defined(__CYGWIN__)
#include <winbase.h>

static int unsetenv(const char *env) {
  return _putenv_s(env, "");
}

static int setenv(const char *env, const char *value, int overwrite) {
  if (getenv(env) && !overwrite)
    return 0;
  return _putenv_s(env, value);
}
#endif

#define LIBUSB_TEST_CLEAN_EXIT(code) \
  do {                               \
    if (test_ctx != NULL) {          \
      libusb_exit(test_ctx);         \
    }                                \
    unsetenv("LIBUSB_DEBUG");        \
    return (code);                   \
  } while (0)

/**
 * Fail the test if the expression does not evaluate to LIBUSB_SUCCESS.
 */
#define LIBUSB_TEST_RETURN_ON_ERROR(expr)                       \
  do {                                                          \
    int _result = (expr);                                       \
    if (LIBUSB_SUCCESS != _result) {                            \
      libusb_testlib_logf("Not success (%s) at %s:%d", #expr,   \
                          __FILE__, __LINE__);                  \
      LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE);              \
    }                                                           \
  } while (0)

/**
 * Use relational operatator to compare two values and fail the test if the
 * comparison is false. Intended to compare integer or pointer types.
 *
 * Example: LIBUSB_EXPECT(==, 0, 1) -> fail, LIBUSB_EXPECT(==, 0, 0) -> ok.
 */
#define LIBUSB_EXPECT(operator, lhs, rhs)                               \
  do {                                                                  \
    int64_t _lhs = (int64_t)(intptr_t)(lhs), _rhs = (int64_t)(intptr_t)(rhs); \
    if (!(_lhs operator _rhs)) {                                        \
      libusb_testlib_logf("Expected %s (%" PRId64 ") " #operator        \
                          " %s (%" PRId64 ") at %s:%d", #lhs,           \
                          (int64_t)(intptr_t)_lhs, #rhs,                \
                          (int64_t)(intptr_t)_rhs, __FILE__,            \
                          __LINE__);                                    \
      LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE);                      \
    }                                                                   \
  } while (0)


static libusb_testlib_result test_set_log_level_basic(void) {
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
  libusb_context *test_ctx = NULL;

  /* unset LIBUSB_DEBUG if it is set */
  unsetenv("LIBUSB_DEBUG");

  /* test basic functionality */
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx,
                                                LIBUSB_OPTION_LOG_LEVEL,
                                                LIBUSB_LOG_LEVEL_ERROR));
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR);
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx,
                                                LIBUSB_OPTION_LOG_LEVEL,
                                                LIBUSB_LOG_LEVEL_NONE));
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_NONE);

  LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
#else
  return TEST_STATUS_SKIP;
#endif
}

static libusb_testlib_result test_set_log_level_default(void) {
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
  libusb_context *test_ctx = NULL;

  /* set the default debug level */
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL,
                                                LIBUSB_LOG_LEVEL_ERROR));

  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  /* check that debug level came from the default */
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_ERROR);

  /* try to override the old log level. since this was set from the default it
   * should be possible to change it */
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx,
                                                LIBUSB_OPTION_LOG_LEVEL,
                                                LIBUSB_LOG_LEVEL_NONE));
  LIBUSB_EXPECT(==, test_ctx->debug, LIBUSB_LOG_LEVEL_NONE);

  LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
#else
  return TEST_STATUS_SKIP;
#endif
}

static libusb_testlib_result test_set_log_level_env(void) {
#if defined(ENABLE_LOGGING)
  libusb_context *test_ctx = NULL;

  /* check that libusb_set_option does not change the log level when it was set
   * from the environment. */
  setenv("LIBUSB_DEBUG", "4", /*overwrite=*/0);
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
#ifndef ENABLE_DEBUG_LOGGING
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
#endif

  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx,
                                                LIBUSB_OPTION_LOG_LEVEL,
                                                LIBUSB_LOG_LEVEL_ERROR));
  /* environment variable should always override LIBUSB_OPTION_LOG_LEVEL if set */
#ifndef ENABLE_DEBUG_LOGGING
  LIBUSB_EXPECT(==, test_ctx->debug, 4);
#endif

  LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
#else
  return TEST_STATUS_SKIP;
#endif
}


static libusb_testlib_result test_no_discovery(void)
{
#if defined(__linux__)
  libusb_context *test_ctx;
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  libusb_device **device_list = NULL;
  ssize_t num_devices = libusb_get_device_list(test_ctx, &device_list);
  libusb_free_device_list(device_list, /*unref_devices=*/1);
  libusb_exit(test_ctx);

  LIBUSB_EXPECT(>, num_devices, 0);

  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY));
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  device_list = NULL;
  num_devices = libusb_get_device_list(test_ctx, &device_list);
  libusb_free_device_list(device_list, /*unref_devices=*/1);

  LIBUSB_EXPECT(==, num_devices, 0);
  LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
#else
  return TEST_STATUS_SKIP;
#endif
}

static void test_log_cb(libusb_context *ctx, enum libusb_log_level level,
                        const char *str) {
  UNUSED(ctx);
  UNUSED(level);
  UNUSED(str);
}


static libusb_testlib_result test_set_log_cb(void)
{
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
  libusb_context *test_ctx = NULL;

  /* set the log callback on the context */
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(test_ctx, LIBUSB_OPTION_LOG_CB,
                                                test_log_cb));

  /* check that debug level came from the default */
  LIBUSB_EXPECT(==, test_ctx->log_handler, test_log_cb);

  libusb_exit(test_ctx);
  test_ctx = NULL;

  /* set the log callback for all future contexts */
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_set_option(/*ctx=*/NULL, LIBUSB_OPTION_LOG_CB,
                                                test_log_cb));
  LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
                                                  /*num_options=*/0));
  LIBUSB_EXPECT(==, test_ctx->log_handler, test_log_cb);


  LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
#else
  return TEST_STATUS_SKIP;
#endif
}

static const libusb_testlib_test tests[] = {
  { "test_set_log_level_basic", &test_set_log_level_basic },
  { "test_set_log_level_env", &test_set_log_level_env },
  { "test_no_discovery", &test_no_discovery },
  /* since default options can't be unset, run this one last */
  { "test_set_log_level_default", &test_set_log_level_default },
  { "test_set_log_cb", &test_set_log_cb },
  LIBUSB_NULL_TEST
};

int main(int argc, char *argv[])
{
  return libusb_testlib_run_tests(argc, argv, tests);
}