1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2023 Google LLC
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include <errno.h>
30 #include <libutil.h>
31
32 #include <atf-c.h>
33
34 ATF_TC_WITHOUT_HEAD(positivetests);
ATF_TC_BODY(positivetests,tc)35 ATF_TC_BODY(positivetests, tc)
36 {
37 int retval;
38 uint64_t num;
39
40 #define positive_tc(string, value) \
41 do { \
42 ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0); \
43 ATF_CHECK_EQ(retval, 0); \
44 ATF_CHECK_EQ(num, (value)); \
45 } while (0)
46
47 positive_tc("123456", 123456);
48 positive_tc("123456b", 123456);
49 positive_tc("1k", 1024);
50 positive_tc("1kb", 1024);
51 positive_tc("1K", 1024);
52 positive_tc("1KB", 1024);
53 positive_tc("1m", 1048576);
54 positive_tc("1M", 1048576);
55 positive_tc("1g", 1073741824);
56 positive_tc("1G", 1073741824);
57 positive_tc("1t", 1099511627776);
58 positive_tc("1T", 1099511627776);
59 positive_tc("1p", 1125899906842624);
60 positive_tc("1P", 1125899906842624);
61 positive_tc("1e", 1152921504606846976);
62 positive_tc("1E", 1152921504606846976);
63 positive_tc("15E", 17293822569102704640ULL);
64 }
65
66 ATF_TC_WITHOUT_HEAD(negativetests);
ATF_TC_BODY(negativetests,tc)67 ATF_TC_BODY(negativetests, tc)
68 {
69 uint64_t num;
70
71 ATF_CHECK_ERRNO(EINVAL, expand_number("", &num));
72 ATF_CHECK_ERRNO(EINVAL, expand_number("x", &num));
73 ATF_CHECK_ERRNO(EINVAL, expand_number("1bb", &num));
74 ATF_CHECK_ERRNO(EINVAL, expand_number("1x", &num));
75 ATF_CHECK_ERRNO(EINVAL, expand_number("1kx", &num));
76 ATF_CHECK_ERRNO(ERANGE, expand_number("16E", &num));
77 }
78
ATF_TP_ADD_TCS(tp)79 ATF_TP_ADD_TCS(tp)
80 {
81 ATF_TP_ADD_TC(tp, positivetests);
82 ATF_TP_ADD_TC(tp, negativetests);
83 return (atf_no_error());
84 }
85