blob: 772e4b9204256151493601ce3f790beadda364e1 [file] [log] [blame]
Andrii Anisov6e29ac42020-08-06 12:42:52 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2002, Uwe Bonnes
4 * Copyright (c) 2001-2004, Roger Dingledine.
5 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
6 * Copyright (c) 2007-2016, The Tor Project, Inc.
7 * Copyright (c) 2020, EPAM Systems Inc.
8 *
9 * Unit tests for sscanf() function
10 */
11
12#include <common.h>
13#include <command.h>
14#include <log.h>
15#include <test/lib.h>
16#include <test/test.h>
17#include <test/ut.h>
18
19#define EOF -1
20
21/**
22 * lib_sscanf() - unit test for sscanf()
23 * @uts: unit test state
24 *
25 * Test sscanf() with varied parameters in different combinations passed
26 * as arguments.
27 *
28 * Return: 0 - success
29 * 1 - failure
30 */
31static int lib_sscanf(struct unit_test_state *uts)
32{
33 char buffer[100], buffer1[100];
34 int result, ret;
35 static const char pname[] = " Hello World!\n";
36 int hour = 21, min = 59, sec = 20;
37 int number, number_so_far;
38 unsigned int u1, u2, u3;
39 char s1[20], s2[10], s3[10], ch;
40 int r, int1, int2;
41 long lng1;
42
43 /* check EOF */
44 strcpy(buffer, "");
45 ret = sscanf(buffer, "%d", &result);
46 ut_asserteq(ret, EOF);
47
48 /* check %x */
49 strcpy(buffer, "0x519");
50 ut_asserteq(sscanf(buffer, "%x", &result), 1);
51 ut_asserteq(result, 0x519);
52
53 strcpy(buffer, "0x51a");
54 ut_asserteq(sscanf(buffer, "%x", &result), 1);
55 ut_asserteq(result, 0x51a);
56
57 strcpy(buffer, "0x51g");
58 ut_asserteq(sscanf(buffer, "%x", &result), 1);
59 ut_asserteq(result, 0x51);
60
61 /* check strings */
62 ret = sprintf(buffer, " %s", pname);
63 ret = sscanf(buffer, "%*c%[^\n]", buffer1);
64 ut_asserteq(ret, 1);
65 ut_asserteq(strncmp(pname, buffer1, strlen(buffer1)), 0);
66
67 /* check digits */
68 ret = sprintf(buffer, "%d:%d:%d", hour, min, sec);
69 ret = sscanf(buffer, "%d%n", &number, &number_so_far);
70 ut_asserteq(ret, 1);
71 ut_asserteq(number, hour);
72 ut_asserteq(number_so_far, 2);
73
74 ret = sscanf(buffer + 2, "%*c%n", &number_so_far);
75 ut_asserteq(ret, 0);
76 ut_asserteq(number_so_far, 1);
77
78 /* Check %i */
79 strcpy(buffer, "123");
80 ret = sscanf(buffer, "%i", &result);
81 ut_asserteq(ret, 1);
82 ut_asserteq(result, 123);
83 ret = sscanf(buffer, "%d", &result);
84 ut_asserteq(ret, 1);
85 ut_asserteq(result, 123);
86
87 ut_asserteq(0, sscanf("hello world", "hello world"));
88 ut_asserteq(0, sscanf("hello world", "good bye"));
89 /* Excess data */
90 ut_asserteq(0, sscanf("hello 3", "%u", &u1)); /* have to match the start */
91 ut_asserteq(1, sscanf("3 hello", "%u", &u1)); /* but trailing is alright */
92
93 /* Numbers (ie. %u) */
94 ut_asserteq(0, sscanf("hello world 3", "hello worlb %u", &u1)); /* d vs b */
95 ut_asserteq(1, sscanf("12345", "%u", &u1));
96 ut_asserteq(12345u, u1);
97 ut_asserteq(1, sscanf("0", "%u", &u1));
98 ut_asserteq(0u, u1);
99 ut_asserteq(1, sscanf("0000", "%u", &u2));
100 ut_asserteq(0u, u2);
101 ut_asserteq(0, sscanf("A", "%u", &u1)); /* bogus number */
102
103 /* Numbers with size (eg. %2u) */
104 ut_asserteq(2, sscanf("123456", "%2u%u", &u1, &u2));
105 ut_asserteq(12u, u1);
106 ut_asserteq(3456u, u2);
107 ut_asserteq(1, sscanf("123456", "%8u", &u1));
108 ut_asserteq(123456u, u1);
109 ut_asserteq(1, sscanf("123457 ", "%8u", &u1));
110 ut_asserteq(123457u, u1);
111 ut_asserteq(3, sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3));
112 ut_asserteq(12u, u1);
113 ut_asserteq(3u, u2);
114 ut_asserteq(456u, u3);
115 ut_asserteq(3, sscanf("67:8:099", "%2u:%2u:%3u", &u1, &u2, &u3)); /* 0s */
116 ut_asserteq(67u, u1);
117 ut_asserteq(8u, u2);
118 ut_asserteq(99u, u3);
119 /* Arbitrary amounts of 0-padding are okay */
120 ut_asserteq(3, sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3));
121 ut_asserteq(12u, u1);
122 ut_asserteq(3u, u2);
123 ut_asserteq(99u, u3);
124
125 /* Hex (ie. %x) */
126 ut_asserteq(3, sscanf("1234 02aBcdEf ff", "%x %x %x", &u1, &u2, &u3));
127 ut_asserteq(0x1234, u1);
128 ut_asserteq(0x2ABCDEF, u2);
129 ut_asserteq(0xFF, u3);
130 /* Width works on %x */
131 ut_asserteq(3, sscanf("f00dcafe444", "%4x%4x%u", &u1, &u2, &u3));
132 ut_asserteq(0xf00d, u1);
133 ut_asserteq(0xcafe, u2);
134 ut_asserteq(444, u3);
135
136 /* Literal '%' (ie. '%%') */
137 ut_asserteq(1, sscanf("99% fresh", "%3u%% fresh", &u1));
138 ut_asserteq(99, u1);
139 ut_asserteq(0, sscanf("99 fresh", "%% %3u %s", &u1, s1));
140 ut_asserteq(1, sscanf("99 fresh", "%3u%% %s", &u1, s1));
141 ut_asserteq(2, sscanf("99 fresh", "%3u %5s %%", &u1, s1));
142 ut_asserteq(99, u1);
143 ut_asserteq_str(s1, "fresh");
144 ut_asserteq(1, sscanf("% boo", "%% %3s", s1));
145 ut_asserteq_str("boo", s1);
146
147 /* Strings (ie. %s) */
148 ut_asserteq(2, sscanf("hello", "%3s%7s", s1, s2));
149 ut_asserteq_str(s1, "hel");
150 ut_asserteq_str(s2, "lo");
151 ut_asserteq(2, sscanf("WD40", "%2s%u", s3, &u1)); /* %s%u */
152 ut_asserteq_str(s3, "WD");
153 ut_asserteq(40, u1);
154 ut_asserteq(2, sscanf("WD40", "%3s%u", s3, &u1)); /* %s%u */
155 ut_asserteq_str(s3, "WD4");
156 ut_asserteq(0, u1);
157 ut_asserteq(2, sscanf("76trombones", "%6u%9s", &u1, s1)); /* %u%s */
158 ut_asserteq(76, u1);
159 ut_asserteq_str(s1, "trombones");
160
161 ut_asserteq(3, sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
162 ut_asserteq(4, sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch));
163 ut_asserteq(' ', ch);
164
165 r = sscanf("12345 -67890 -1", "%d %ld %d", &int1, &lng1, &int2);
166 ut_asserteq(r, 3);
167 ut_asserteq(int1, 12345);
168 ut_asserteq(lng1, -67890);
169 ut_asserteq(int2, -1);
170
171 return 0;
172}
173
174LIB_TEST(lib_sscanf, 0);