summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/t/t-tar.c
blob: 6fa217de524affa845bad05223ed91982d69aede (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * libdpkg - Debian packaging suite library routines
 * t-tar.c - test tar implementation
 *
 * Copyright © 2017 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <errno.h>

#include <dpkg/test.h>
#include <dpkg/tarfn.h>

static void
test_tar_atol8(void)
{
	uintmax_t u;

	/* Test valid octal numbers. */
	u = tar_atoul("000000\0\0\0\0\0\0", 12, UINTMAX_MAX);
	test_pass(u == 0);
	u = tar_atoul("00000000000\0", 12, UINTMAX_MAX);
	test_pass(u == 0);
	u = tar_atoul("00000000001\0", 12, UINTMAX_MAX);
	test_pass(u == 1);
	u = tar_atoul("00000000777\0", 12, UINTMAX_MAX);
	test_pass(u == 511);
	u = tar_atoul("77777777777\0", 12, UINTMAX_MAX);
	test_pass(u == 8589934591);

	/* Test legacy formatted octal numbers. */
	u = tar_atoul("          0\0", 12, UINTMAX_MAX);
	test_pass(u == 0);
	u = tar_atoul("          1\0", 12, UINTMAX_MAX);
	test_pass(u == 1);
	u = tar_atoul("        777\0", 12, UINTMAX_MAX);
	test_pass(u == 511);

	/* Test extended octal numbers not terminated by space or NUL,
	 * (as is required by POSIX), but accepted by several implementations
	 * to get one byte larger values. */
	u = tar_atoul("000000000000", 12, UINTMAX_MAX);
	test_pass(u == 0);
	u = tar_atoul("000000000001", 12, UINTMAX_MAX);
	test_pass(u == 1);
	u = tar_atoul("000000000777", 12, UINTMAX_MAX);
	test_pass(u == 511);
	u = tar_atoul("777777777777", 12, UINTMAX_MAX);
	test_pass(u == 68719476735);

	/* Test invalid octal numbers. */
	errno = 0;
	u = tar_atoul("            ", 12, UINTMAX_MAX);
	test_pass(u == 0);
	test_pass(errno == EINVAL);

	errno = 0;
	u = tar_atoul("   11111aaa ", 12, UINTMAX_MAX);
	test_pass(u == 0);
	test_pass(errno == ERANGE);

	errno = 0;
	u = tar_atoul("          8 ", 12, UINTMAX_MAX);
	test_pass(u == 0);
	test_pass(errno == ERANGE);

	errno = 0;
	u = tar_atoul("         18 ", 12, UINTMAX_MAX);
	test_pass(u == 0);
	test_pass(errno == ERANGE);

	errno = 0;
	u = tar_atoul("    aa      ", 12, UINTMAX_MAX);
	test_pass(u == 0);
	test_pass(errno == ERANGE);
}

static void
test_tar_atol256(void)
{
	uintmax_t u;
	intmax_t i;

	/* Test positive numbers. */
	u = tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX);
	test_pass(u == 0);
	u = tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 12, UINTMAX_MAX);
	test_pass(u == 1);
	u = tar_atoul("\x80\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00", 12, UINTMAX_MAX);
	test_pass(u == 8589934592);
	u = tar_atoul("\x80\x00\x00\x00\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 12, UINTMAX_MAX);
	test_pass(u == INTMAX_MAX);

	/* Test overflow. */
	errno = 0;
	u = tar_atoul("\x80\x00\x00\x00\x80\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX);
	test_pass(u == UINTMAX_MAX);
	test_pass(errno == ERANGE);

	errno = 0;
	u = tar_atoul("\x80\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, UINTMAX_MAX);
	test_pass(u == UINTMAX_MAX);
	test_pass(errno == ERANGE);

	/* Test negative numbers. */
	i = tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == -1);
	i = tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == -2);
	i = tar_atosl("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == -8589934592);
	i = tar_atosl("\xFF\xFF\xFF\xFF\x80\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == INTMAX_MIN);

	/* Test underflow. */
	errno = 0;
	i = tar_atosl("\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == INTMAX_MIN);
	test_pass(errno == ERANGE);

	errno = 0;
	i = tar_atosl("\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12, INTMAX_MIN, INTMAX_MAX);
	test_pass(i == INTMAX_MIN);
	test_pass(errno == ERANGE);
}

TEST_ENTRY(test)
{
	test_plan(38);

	test_tar_atol8();
	test_tar_atol256();
}