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 <stdbool.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <inttypes.h>
28 #include "libusbi.h"
29 #include "libusb_testlib.h"
30
31 #define LIBUSB_TEST_CLEAN_EXIT(code) \
32 do { \
33 if (test_ctx != NULL) { \
34 libusb_exit(test_ctx); \
35 } \
36 unsetenv("LIBUSB_DEBUG"); \
37 return (code); \
38 } while (0)
39
40 /**
41 * Fail the test if the expression does not evaluate to LIBUSB_SUCCESS.
42 */
43 #define LIBUSB_TEST_RETURN_ON_ERROR(expr) \
44 do { \
45 int _result = (expr); \
46 if (LIBUSB_SUCCESS != _result) { \
47 libusb_testlib_logf("Not success (%s) at %s:%d", #expr, \
48 __FILE__, __LINE__); \
49 LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE); \
50 } \
51 } while (0)
52
53 /**
54 * Use relational operator to compare two values and fail the test if the
55 * comparison is false. Intended to compare integer or pointer types.
56 *
57 * Example: LIBUSB_EXPECT(==, 0, 1) -> fail, LIBUSB_EXPECT(==, 0, 0) -> ok.
58 */
59 #define LIBUSB_EXPECT(operator, lhs, rhs) \
60 do { \
61 int64_t _lhs = (lhs), _rhs = (rhs); \
62 if (!(_lhs operator _rhs)) { \
63 libusb_testlib_logf("Expected %s (%" PRId64 ") " #operator \
64 " %s (%" PRId64 ") at %s:%d", #lhs, \
65 (int64_t)(intptr_t)_lhs, #rhs, \
66 (int64_t)(intptr_t)_rhs, __FILE__, \
67 __LINE__); \
68 LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_FAILURE); \
69 } \
70 } while (0)
71
72
73 extern uint32_t libusb_testonly_fake_running_version;
74 extern uint32_t libusb_testonly_using_running_interface_version;
75 extern uint32_t libusb_testonly_using_running_device_version;
76 extern bool libusb_testonly_clear_running_version_cache;
77
test_macos_version_fallback(void)78 static libusb_testlib_result test_macos_version_fallback(void) {
79 libusb_context *test_ctx = NULL;
80 libusb_testonly_fake_running_version = 100001;
81 libusb_testonly_clear_running_version_cache = true;
82
83 LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
84 /*num_options=*/0));
85 LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 220);
86 LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 197);
87
88 libusb_exit(test_ctx);
89 test_ctx = NULL;
90
91 libusb_testonly_fake_running_version = 100900;
92
93 LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
94 /*num_options=*/0));
95 LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 650);
96 LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 650);
97
98 libusb_exit(test_ctx);
99 test_ctx = NULL;
100
101 libusb_testonly_fake_running_version = 101200;
102
103 LIBUSB_TEST_RETURN_ON_ERROR(libusb_init_context(&test_ctx, /*options=*/NULL,
104 /*num_options=*/0));
105 LIBUSB_EXPECT(==, libusb_testonly_using_running_interface_version, 800);
106 LIBUSB_EXPECT(==, libusb_testonly_using_running_device_version, 650);
107
108 libusb_exit(test_ctx);
109 test_ctx = NULL;
110
111 // Test a version smaller than 10.0. Initialization should fail.
112 libusb_testonly_fake_running_version = 99999;
113
114 int error = libusb_init_context(&test_ctx, /*options=*/NULL,
115 /*num_options=*/0);
116 LIBUSB_EXPECT(!=, error, LIBUSB_SUCCESS);
117
118
119 LIBUSB_TEST_CLEAN_EXIT(TEST_STATUS_SUCCESS);
120 }
121
122 static const libusb_testlib_test tests[] = {
123 { "test_macos_version_fallback", &test_macos_version_fallback },
124 LIBUSB_NULL_TEST
125 };
126
main(int argc,char * argv[])127 int main(int argc, char *argv[])
128 {
129 return libusb_testlib_run_tests(argc, argv, tests);
130 }
131