Commit | Line | Data |
---|---|---|
1479465f GJ |
1 | /* |
2 | * libdpkg - Debian packaging suite library routines | |
3 | * t-tar.c - test tar implementation | |
4 | * | |
5 | * Copyright © 2017 Guillem Jover <guillem@debian.org> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <compat.h> | |
23 | ||
24 | #include <errno.h> | |
25 | ||
26 | #include <dpkg/test.h> | |
27 | #include <dpkg/tarfn.h> | |
28 | ||
29 | static void | |
30 | test_tar_atol8(void) | |
31 | { | |
32 | uintmax_t u; | |
33 | ||
34 | /* Test valid octal numbers. */ | |
35 | u = tar_atoul("000000\0\0\0\0\0\0", 12, UINTMAX_MAX); | |
36 | test_pass(u == 0); | |
37 | u = tar_atoul("00000000000\0", 12, UINTMAX_MAX); | |
38 | test_pass(u == 0); | |
39 | u = tar_atoul("00000000001\0", 12, UINTMAX_MAX); | |
40 | test_pass(u == 1); | |
41 | u = tar_atoul("00000000777\0", 12, UINTMAX_MAX); | |
42 | test_pass(u == 511); | |
43 | u = tar_atoul("77777777777\0", 12, UINTMAX_MAX); | |
44 | test_pass(u == 8589934591); | |
45 | ||
46 | /* Test legacy formatted octal numbers. */ | |
47 | u = tar_atoul(" 0\0", 12, UINTMAX_MAX); | |
48 | test_pass(u == 0); | |
49 | u = tar_atoul(" 1\0", 12, UINTMAX_MAX); | |
50 | test_pass(u == 1); | |
51 | u = tar_atoul(" 777\0", 12, UINTMAX_MAX); | |
52 | test_pass(u == 511); | |
53 | ||
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); | |
58 | test_pass(u == 0); | |
59 | u = tar_atoul("000000000001", 12, UINTMAX_MAX); | |
60 | test_pass(u == 1); | |
61 | u = tar_atoul("000000000777", 12, UINTMAX_MAX); | |
62 | test_pass(u == 511); | |
63 | u = tar_atoul("777777777777", 12, UINTMAX_MAX); | |
64 | test_pass(u == 68719476735); | |
65 | ||
66 | /* Test invalid octal numbers. */ | |
67 | errno = 0; | |
68 | u = tar_atoul(" ", 12, UINTMAX_MAX); | |
69 | test_pass(u == 0); | |
70 | test_pass(errno == EINVAL); | |
71 | ||
72 | errno = 0; | |
73 | u = tar_atoul(" 11111aaa ", 12, UINTMAX_MAX); | |
74 | test_pass(u == 0); | |
75 | test_pass(errno == EINVAL); | |
76 | ||
77 | errno = 0; | |
78 | u = tar_atoul(" 8 ", 12, UINTMAX_MAX); | |
79 | test_pass(u == 0); | |
80 | test_pass(errno == EINVAL); | |
81 | ||
82 | errno = 0; | |
83 | u = tar_atoul(" 18 ", 12, UINTMAX_MAX); | |
84 | test_pass(u == 0); | |
85 | test_pass(errno == EINVAL); | |
86 | ||
87 | errno = 0; | |
88 | u = tar_atoul(" aa ", 12, UINTMAX_MAX); | |
89 | test_pass(u == 0); | |
90 | test_pass(errno == EINVAL); | |
91 | } | |
92 | ||
93 | static void | |
94 | test_tar_atol256(void) | |
95 | { | |
96 | uintmax_t u; | |
97 | intmax_t i; | |
98 | ||
99 | /* Test positive numbers. */ | |
100 | u = tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX); | |
101 | test_pass(u == 0); | |
102 | u = tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 12, UINTMAX_MAX); | |
103 | test_pass(u == 1); | |
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); | |
108 | ||
109 | /* Test overflow. */ | |
110 | errno = 0; | |
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); | |
114 | ||
115 | errno = 0; | |
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); | |
119 | ||
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); | |
122 | test_pass(i == -1); | |
123 | i = tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE", 12, INTMAX_MIN, INTMAX_MAX); | |
124 | test_pass(i == -2); | |
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); | |
129 | ||
130 | /* Test underflow. */ | |
131 | errno = 0; | |
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); | |
135 | ||
136 | errno = 0; | |
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); | |
140 | } | |
141 | ||
142 | TEST_ENTRY(test) | |
143 | { | |
144 | test_plan(38); | |
145 | ||
146 | test_tar_atol8(); | |
147 | test_tar_atol256(); | |
148 | } |