108300d84SXin LI /*-
208300d84SXin LI * SPDX-License-Identifier: BSD-2-Clause
308300d84SXin LI *
408300d84SXin LI * Copyright (c) 2023 Google LLC
508300d84SXin LI *
608300d84SXin LI * Redistribution and use in source and binary forms, with or without
708300d84SXin LI * modification, are permitted provided that the following conditions
808300d84SXin LI * are met:
908300d84SXin LI * 1. Redistributions of source code must retain the above copyright
1008300d84SXin LI * notice, this list of conditions and the following disclaimer.
1108300d84SXin LI * 2. Redistributions in binary form must reproduce the above copyright
1208300d84SXin LI * notice, this list of conditions and the following disclaimer in the
1308300d84SXin LI * documentation and/or other materials provided with the distribution.
1408300d84SXin LI *
1508300d84SXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1608300d84SXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1708300d84SXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1808300d84SXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1908300d84SXin LI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2008300d84SXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2108300d84SXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2208300d84SXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2308300d84SXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2408300d84SXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2508300d84SXin LI * SUCH DAMAGE.
2608300d84SXin LI */
2708300d84SXin LI
2808300d84SXin LI #include <sys/cdefs.h>
2908300d84SXin LI #include <errno.h>
3008300d84SXin LI #include <libutil.h>
3108300d84SXin LI
3208300d84SXin LI #include <atf-c.h>
3308300d84SXin LI
3408300d84SXin LI ATF_TC_WITHOUT_HEAD(positivetests);
ATF_TC_BODY(positivetests,tc)3508300d84SXin LI ATF_TC_BODY(positivetests, tc)
3608300d84SXin LI {
3708300d84SXin LI int retval;
3808300d84SXin LI uint64_t num;
3908300d84SXin LI
4008300d84SXin LI #define positive_tc(string, value) \
4108300d84SXin LI do { \
4208300d84SXin LI ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0); \
4308300d84SXin LI ATF_CHECK_EQ(retval, 0); \
4408300d84SXin LI ATF_CHECK_EQ(num, (value)); \
4508300d84SXin LI } while (0)
4608300d84SXin LI
4708300d84SXin LI positive_tc("123456", 123456);
4808300d84SXin LI positive_tc("123456b", 123456);
4908300d84SXin LI positive_tc("1k", 1024);
5008300d84SXin LI positive_tc("1kb", 1024);
5108300d84SXin LI positive_tc("1K", 1024);
5208300d84SXin LI positive_tc("1KB", 1024);
5308300d84SXin LI positive_tc("1m", 1048576);
5408300d84SXin LI positive_tc("1M", 1048576);
5508300d84SXin LI positive_tc("1g", 1073741824);
5608300d84SXin LI positive_tc("1G", 1073741824);
5708300d84SXin LI positive_tc("1t", 1099511627776);
5808300d84SXin LI positive_tc("1T", 1099511627776);
5908300d84SXin LI positive_tc("1p", 1125899906842624);
6008300d84SXin LI positive_tc("1P", 1125899906842624);
6108300d84SXin LI positive_tc("1e", 1152921504606846976);
6208300d84SXin LI positive_tc("1E", 1152921504606846976);
6308300d84SXin LI positive_tc("15E", 17293822569102704640ULL);
6408300d84SXin LI }
6508300d84SXin LI
6608300d84SXin LI ATF_TC_WITHOUT_HEAD(negativetests);
ATF_TC_BODY(negativetests,tc)6708300d84SXin LI ATF_TC_BODY(negativetests, tc)
6808300d84SXin LI {
6908300d84SXin LI uint64_t num;
7008300d84SXin LI
71*6cc4a1c3SXin LI ATF_CHECK_ERRNO(EINVAL, expand_number("", &num));
72*6cc4a1c3SXin LI ATF_CHECK_ERRNO(EINVAL, expand_number("x", &num));
73*6cc4a1c3SXin LI ATF_CHECK_ERRNO(EINVAL, expand_number("1bb", &num));
74*6cc4a1c3SXin LI ATF_CHECK_ERRNO(EINVAL, expand_number("1x", &num));
75*6cc4a1c3SXin LI ATF_CHECK_ERRNO(EINVAL, expand_number("1kx", &num));
76*6cc4a1c3SXin LI ATF_CHECK_ERRNO(ERANGE, expand_number("16E", &num));
7708300d84SXin LI }
7808300d84SXin LI
ATF_TP_ADD_TCS(tp)7908300d84SXin LI ATF_TP_ADD_TCS(tp)
8008300d84SXin LI {
8108300d84SXin LI ATF_TP_ADD_TC(tp, positivetests);
8208300d84SXin LI ATF_TP_ADD_TC(tp, negativetests);
8308300d84SXin LI return (atf_no_error());
8408300d84SXin LI }
85