]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/test/unit/lib/util/string.c/string_ut.c
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk_cunit.h"
38 #include "util/string.c"
41 test_parse_ip_addr(void)
49 snprintf(ip
, 255, "%s", "192.168.0.1");
50 rc
= spdk_parse_ip_addr(ip
, &host
, &port
);
51 CU_ASSERT_EQUAL(rc
, 0);
52 SPDK_CU_ASSERT_FATAL(host
!= NULL
);
53 CU_ASSERT(strcmp(host
, "192.168.0.1") == 0);
54 CU_ASSERT_EQUAL(strlen(host
), 11);
55 CU_ASSERT_EQUAL(port
, NULL
);
58 snprintf(ip
, 255, "%s", "123.456.789.0:5520");
59 rc
= spdk_parse_ip_addr(ip
, &host
, &port
);
60 CU_ASSERT_EQUAL(rc
, 0);
61 SPDK_CU_ASSERT_FATAL(host
!= NULL
);
62 CU_ASSERT(strcmp(host
, "123.456.789.0") == 0);
63 CU_ASSERT_EQUAL(strlen(host
), 13);
64 SPDK_CU_ASSERT_FATAL(port
!= NULL
);
65 CU_ASSERT(strcmp(port
, "5520") == 0);
66 CU_ASSERT_EQUAL(strlen(port
), 4);
69 snprintf(ip
, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]");
70 rc
= spdk_parse_ip_addr(ip
, &host
, &port
);
71 CU_ASSERT_EQUAL(rc
, 0);
72 SPDK_CU_ASSERT_FATAL(host
!= NULL
);
73 CU_ASSERT(strcmp(host
, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
74 CU_ASSERT_EQUAL(strlen(host
), 36);
75 CU_ASSERT_EQUAL(port
, NULL
);
78 snprintf(ip
, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443");
79 rc
= spdk_parse_ip_addr(ip
, &host
, &port
);
80 CU_ASSERT_EQUAL(rc
, 0);
81 SPDK_CU_ASSERT_FATAL(host
!= NULL
);
82 CU_ASSERT(strcmp(host
, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
83 CU_ASSERT_EQUAL(strlen(host
), 36);
84 SPDK_CU_ASSERT_FATAL(port
!= NULL
);
85 CU_ASSERT(strcmp(port
, "443") == 0);
86 CU_ASSERT_EQUAL(strlen(port
), 3);
88 /* IPv6 dangling colon */
89 snprintf(ip
, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:");
90 rc
= spdk_parse_ip_addr(ip
, &host
, &port
);
91 CU_ASSERT_EQUAL(rc
, 0);
92 SPDK_CU_ASSERT_FATAL(host
!= NULL
);
93 CU_ASSERT(strcmp(host
, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
94 CU_ASSERT_EQUAL(strlen(host
), 36);
95 CU_ASSERT_EQUAL(port
, NULL
);
104 snprintf(s
, sizeof(s
), "%s", "hello world\n");
105 CU_ASSERT(spdk_str_chomp(s
) == 1);
106 CU_ASSERT(strcmp(s
, "hello world") == 0);
108 /* One \r\n newline */
109 snprintf(s
, sizeof(s
), "%s", "hello world\r\n");
110 CU_ASSERT(spdk_str_chomp(s
) == 2);
111 CU_ASSERT(strcmp(s
, "hello world") == 0);
114 snprintf(s
, sizeof(s
), "%s", "hello world");
115 CU_ASSERT(spdk_str_chomp(s
) == 0);
116 CU_ASSERT(strcmp(s
, "hello world") == 0);
119 snprintf(s
, sizeof(s
), "%s", "hello world\n\n");
120 CU_ASSERT(spdk_str_chomp(s
) == 2);
121 CU_ASSERT(strcmp(s
, "hello world") == 0);
124 snprintf(s
, sizeof(s
), "%s", "");
125 CU_ASSERT(spdk_str_chomp(s
) == 0);
126 CU_ASSERT(strcmp(s
, "") == 0);
128 /* One-character string with only \n */
129 snprintf(s
, sizeof(s
), "%s", "\n");
130 CU_ASSERT(spdk_str_chomp(s
) == 1);
131 CU_ASSERT(strcmp(s
, "") == 0);
133 /* One-character string without a newline */
134 snprintf(s
, sizeof(s
), "%s", "a");
135 CU_ASSERT(spdk_str_chomp(s
) == 0);
136 CU_ASSERT(strcmp(s
, "a") == 0);
140 test_parse_capacity(void)
145 bool has_prefix
= true;
147 rc
= spdk_parse_capacity("472", &cap
, &has_prefix
);
149 CU_ASSERT(cap
== 472);
150 CU_ASSERT(has_prefix
== false);
152 snprintf(str
, sizeof(str
), "%"PRIu64
, UINT64_MAX
);
153 rc
= spdk_parse_capacity(str
, &cap
, &has_prefix
);
155 CU_ASSERT(cap
== UINT64_MAX
);
156 CU_ASSERT(has_prefix
== false);
158 rc
= spdk_parse_capacity("12k", &cap
, &has_prefix
);
160 CU_ASSERT(cap
== 12 * 1024);
161 CU_ASSERT(has_prefix
== true);
163 rc
= spdk_parse_capacity("12K", &cap
, &has_prefix
);
165 CU_ASSERT(cap
== 12 * 1024);
166 CU_ASSERT(has_prefix
== true);
168 rc
= spdk_parse_capacity("12KB", &cap
, &has_prefix
);
170 CU_ASSERT(cap
== 12 * 1024);
171 CU_ASSERT(has_prefix
== true);
173 rc
= spdk_parse_capacity("100M", &cap
, &has_prefix
);
175 CU_ASSERT(cap
== 100 * 1024 * 1024);
176 CU_ASSERT(has_prefix
== true);
178 rc
= spdk_parse_capacity("128M", &cap
, &has_prefix
);
180 CU_ASSERT(cap
== 128 * 1024 * 1024);
181 CU_ASSERT(has_prefix
== true);
183 rc
= spdk_parse_capacity("4G", &cap
, &has_prefix
);
185 CU_ASSERT(cap
== 4ULL * 1024 * 1024 * 1024);
186 CU_ASSERT(has_prefix
== true);
188 rc
= spdk_parse_capacity("100M 512k", &cap
, &has_prefix
);
190 CU_ASSERT(cap
== 100ULL * 1024 * 1024);
192 rc
= spdk_parse_capacity("12k8K", &cap
, &has_prefix
);
194 CU_ASSERT(cap
== 12 * 1024);
195 CU_ASSERT(has_prefix
== true);
198 rc
= spdk_parse_capacity("G", &cap
, &has_prefix
);
201 rc
= spdk_parse_capacity("darsto", &cap
, &has_prefix
);
206 main(int argc
, char **argv
)
208 CU_pSuite suite
= NULL
;
209 unsigned int num_failures
;
211 if (CU_initialize_registry() != CUE_SUCCESS
) {
212 return CU_get_error();
215 suite
= CU_add_suite("string", NULL
, NULL
);
217 CU_cleanup_registry();
218 return CU_get_error();
222 CU_add_test(suite
, "test_parse_ip_addr", test_parse_ip_addr
) == NULL
||
223 CU_add_test(suite
, "test_str_chomp", test_str_chomp
) == NULL
||
224 CU_add_test(suite
, "test_parse_capacity", test_parse_capacity
) == NULL
) {
225 CU_cleanup_registry();
226 return CU_get_error();
229 CU_basic_set_mode(CU_BRM_VERBOSE
);
231 CU_basic_run_tests();
233 num_failures
= CU_get_number_of_failures();
234 CU_cleanup_registry();