2 * libdpkg - Debian packaging suite library routines
3 * t-tar.c - test tar implementation
5 * Copyright © 2017 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 #include <dpkg/test.h>
27 #include <dpkg/tarfn.h>
34 /* Test valid octal numbers. */
35 u
= tar_atoul("000000\0\0\0\0\0\0", 12, UINTMAX_MAX
);
37 u
= tar_atoul("00000000000\0", 12, UINTMAX_MAX
);
39 u
= tar_atoul("00000000001\0", 12, UINTMAX_MAX
);
41 u
= tar_atoul("00000000777\0", 12, UINTMAX_MAX
);
43 u
= tar_atoul("77777777777\0", 12, UINTMAX_MAX
);
44 test_pass(u
== 8589934591);
46 /* Test legacy formatted octal numbers. */
47 u
= tar_atoul(" 0\0", 12, UINTMAX_MAX
);
49 u
= tar_atoul(" 1\0", 12, UINTMAX_MAX
);
51 u
= tar_atoul(" 777\0", 12, UINTMAX_MAX
);
54 /* Test extended octal numbers not terminated by space or NUL,
55 * (as is required by POSIX), but accepted by several implementations
56 * to get one byte larger values. */
57 u
= tar_atoul("000000000000", 12, UINTMAX_MAX
);
59 u
= tar_atoul("000000000001", 12, UINTMAX_MAX
);
61 u
= tar_atoul("000000000777", 12, UINTMAX_MAX
);
63 u
= tar_atoul("777777777777", 12, UINTMAX_MAX
);
64 test_pass(u
== 68719476735);
66 /* Test invalid octal numbers. */
68 u
= tar_atoul(" ", 12, UINTMAX_MAX
);
70 test_pass(errno
== EINVAL
);
73 u
= tar_atoul(" 11111aaa ", 12, UINTMAX_MAX
);
75 test_pass(errno
== EINVAL
);
78 u
= tar_atoul(" 8 ", 12, UINTMAX_MAX
);
80 test_pass(errno
== EINVAL
);
83 u
= tar_atoul(" 18 ", 12, UINTMAX_MAX
);
85 test_pass(errno
== EINVAL
);
88 u
= tar_atoul(" aa ", 12, UINTMAX_MAX
);
90 test_pass(errno
== EINVAL
);
94 test_tar_atol256(void)
99 /* Test positive numbers. */
100 u
= tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX
);
102 u
= tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 12, UINTMAX_MAX
);
104 u
= tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00", 12, UINTMAX_MAX
);
105 test_pass(u
== 8589934592);
106 u
= tar_atoul("\x80\x00\x00\x00\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 12, UINTMAX_MAX
);
107 test_pass(u
== INTMAX_MAX
);
111 u
= tar_atoul("\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX
);
112 test_pass(u
== UINTMAX_MAX
);
113 test_pass(errno
== ERANGE
);
116 u
= tar_atoul("\x80\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX
);
117 test_pass(u
== UINTMAX_MAX
);
118 test_pass(errno
== ERANGE
);
120 /* Test negative numbers. */
121 i
= tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 12, INTMAX_MIN
, INTMAX_MAX
);
123 i
= tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE", 12, INTMAX_MIN
, INTMAX_MAX
);
125 i
= tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 12, INTMAX_MIN
, INTMAX_MAX
);
126 test_pass(i
== -8589934592);
127 i
= tar_atosl("\xFF\xFF\xFF\xFF\x80\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN
, INTMAX_MAX
);
128 test_pass(i
== INTMAX_MIN
);
130 /* Test underflow. */
132 i
= tar_atosl("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN
, INTMAX_MAX
);
133 test_pass(i
== INTMAX_MIN
);
134 test_pass(errno
== ERANGE
);
137 i
= tar_atosl("\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN
, INTMAX_MAX
);
138 test_pass(i
== INTMAX_MIN
);
139 test_pass(errno
== ERANGE
);