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 __FBSDID("$FreeBSD$"); 30 31 #include <errno.h> 32 #include <libutil.h> 33 34 #include <atf-c.h> 35 36 ATF_TC_WITHOUT_HEAD(positivetests); 37 ATF_TC_BODY(positivetests, tc) 38 { 39 int retval; 40 uint64_t num; 41 42 #define positive_tc(string, value) \ 43 do { \ 44 ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0); \ 45 ATF_CHECK_EQ(retval, 0); \ 46 ATF_CHECK_EQ(num, (value)); \ 47 } while (0) 48 49 positive_tc("123456", 123456); 50 positive_tc("123456b", 123456); 51 positive_tc("1k", 1024); 52 positive_tc("1kb", 1024); 53 positive_tc("1K", 1024); 54 positive_tc("1KB", 1024); 55 positive_tc("1m", 1048576); 56 positive_tc("1M", 1048576); 57 positive_tc("1g", 1073741824); 58 positive_tc("1G", 1073741824); 59 positive_tc("1t", 1099511627776); 60 positive_tc("1T", 1099511627776); 61 positive_tc("1p", 1125899906842624); 62 positive_tc("1P", 1125899906842624); 63 positive_tc("1e", 1152921504606846976); 64 positive_tc("1E", 1152921504606846976); 65 positive_tc("15E", 17293822569102704640ULL); 66 } 67 68 ATF_TC_WITHOUT_HEAD(negativetests); 69 ATF_TC_BODY(negativetests, tc) 70 { 71 uint64_t num; 72 73 ATF_CHECK_ERRNO(EINVAL, expand_number("", &num)); 74 ATF_CHECK_ERRNO(EINVAL, expand_number("x", &num)); 75 ATF_CHECK_ERRNO(EINVAL, expand_number("1bb", &num)); 76 ATF_CHECK_ERRNO(EINVAL, expand_number("1x", &num)); 77 ATF_CHECK_ERRNO(EINVAL, expand_number("1kx", &num)); 78 ATF_CHECK_ERRNO(ERANGE, expand_number("16E", &num)); 79 } 80 81 ATF_TP_ADD_TCS(tp) 82 { 83 ATF_TP_ADD_TC(tp, positivetests); 84 ATF_TP_ADD_TC(tp, negativetests); 85 return (atf_no_error()); 86 } 87