2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2009-2011 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 __FBSDID("$FreeBSD$");
29 static const char archive[] = {
30 "begin 644 test_read_uu.Z\n"
31 "M'YV0+@`('$BPH,&#\"!,J7,BP(4(8$&_4J`$\"`,08$F%4O)AQ(\\2/(#7&@#%C\n"
32 "M!@T8-##.L`$\"QL@:-F(``%'#H<V;.'/J!%!G#ITP<BS\"H).FS<Z$1(T>/1A2\n"
33 "IHU\"0%9=*G4JUJM6K6+-JW<JUJ]>O8,.*'4NVK-FS:-.J7<NVK=NW9P$`\n"
38 static const char archive64[] = {
39 "begin-base64 644 test_read_uu.Z\n"
40 "H52QLgAIHEiwoMGDCBMqXMiwIUIYEG/UqAECAMQYEmFUvJhxI8SPIDXGgDFjBg0YNDDOsAECxsga\n"
41 "NmIAAFHDoc2bOHPqBFBnDp0wcizCoJOmzc6ERI0ePRhSo1CQFZdKnUq1qtWrWLNq3cq1q9evYMOK\n"
42 "HUu2rNmzaNOqXcu2rdu3ZwE=\n"
46 static const char extradata[] = {
47 "From uudecode@libarchive Mon Jun 2 03:03:31 2008\n"
48 "Return-Path: <uudecode@libarchive>\n"
49 "Received: from libarchive (localhost [127.0.0.1])\n"
50 " by libarchive (8.14.2/8.14.2) with ESMTP id m5233UT1006448\n"
51 " for <uudecode@libarchive>; Mon, 2 Jun 2008 03:03:31 GMT\n"
52 " (envelope-from uudecode@libarchive)\n"
53 "Received: (from uudecode@localhost)\n"
54 " by libarchive (8.14.2/8.14.2/Submit) id m5233U3e006406\n"
55 " for uudecode; Mon, 2 Jun 2008 03:03:30 GMT\n"
56 " (envelope-from root)\n"
57 "Date: Mon, 2 Jun 2008 03:03:30 GMT\n"
58 "From: Libarchive Test <uudecode@libarchive>\n"
59 "Message-Id: <200806020303.m5233U3e006406@libarchive>\n"
60 "To: uudecode@libarchive\n"
61 "Subject: Libarchive uudecode test\n"
63 "* Redistribution and use in source and binary forms, with or without\n"
64 "* modification, are permitted provided that the following conditions\n"
67 "01234567890abcdeghijklmnopqrstuvwxyz\n"
68 "01234567890ABCEFGHIJKLMNOPQRSTUVWXYZ\n"
73 test_read_uu_sub(const char *uudata, size_t uusize, int no_nl)
75 struct archive_entry *ae;
78 char extradata_no_nl[sizeof(extradata)];
79 const char *extradata_ptr;
84 /* Remove '\n' from extra data to make a very long line. */
86 memcpy(extradata_no_nl, extradata, sizeof(extradata));
87 extradata_ptr = extradata_no_nl;
88 for (p = extradata_no_nl;
89 *p && (p = strchr(p, '\n')) != NULL; p++)
90 *p = ' ';/* Replace '\n' with ' ' a space character. */
92 extradata_ptr = extradata;
94 assert(NULL != (buff = malloc(uusize + 1024 * 1024)));
97 for (extra = 0; extra <= 64; extra = extra==0?1:extra*2) {
101 /* Add extra text size of which is from 1K bytes to
102 * 64Kbytes before uuencoded data. */
104 if (size > sizeof(extradata)-1) {
105 memcpy(p, extradata_ptr, sizeof(extradata)-1);
106 p += sizeof(extradata)-1;
107 size -= sizeof(extradata)-1;
109 memcpy(p, extradata_ptr, size-1);
111 *p++ = '\n';/* the last of extra text must have
116 memcpy(p, uudata, uusize);
117 size = extra * 1024 + uusize;
119 assert((a = archive_read_new()) != NULL);
120 assertEqualIntA(a, ARCHIVE_OK,
121 archive_read_support_filter_all(a));
122 assertEqualIntA(a, ARCHIVE_OK,
123 archive_read_support_format_all(a));
124 assertEqualIntA(a, ARCHIVE_OK,
125 read_open_memory(a, buff, size, 2));
126 assertEqualIntA(a, ARCHIVE_OK,
127 archive_read_next_header(a, &ae));
128 failure("archive_compression_name(a)=\"%s\""
130 archive_compression_name(a), extra, !no_nl);
131 assertEqualInt(archive_compression(a),
132 ARCHIVE_COMPRESSION_COMPRESS);
133 failure("archive_format_name(a)=\"%s\""
135 archive_format_name(a), extra, !no_nl);
136 assertEqualInt(archive_format(a),
137 ARCHIVE_FORMAT_TAR_USTAR);
138 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
139 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
142 /* UUdecode bidder shouldn't scan too much data; make sure it
143 * fails if we put 512k of data before the start. */
145 for (extra = 0; (size_t)extra < size; ++extra)
146 buff[extra + 1024] = buff[extra];
147 buff[size - 1] = '\n';
148 memcpy(buff + size, uudata, uusize);
150 assert((a = archive_read_new()) != NULL);
151 assertEqualIntA(a, ARCHIVE_OK,
152 archive_read_support_filter_all(a));
153 assertEqualIntA(a, ARCHIVE_OK,
154 archive_read_support_format_all(a));
155 assertEqualIntA(a, ARCHIVE_FATAL,
156 read_open_memory(a, buff, size, 2));
157 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
162 DEFINE_TEST(test_read_uu)
164 /* Read the traditional uuencoded data. */
165 test_read_uu_sub(archive, sizeof(archive)-1, 0);
166 /* Read the Base64 uuencoded data. */
167 test_read_uu_sub(archive64, sizeof(archive64)-1, 0);
168 /* Read the traditional uuencoded data with very long line extra
169 * data in front of it. */
170 test_read_uu_sub(archive, sizeof(archive)-1, 1);
171 /* Read the Base64 uuencoded data with very long line extra data
173 test_read_uu_sub(archive64, sizeof(archive64)-1, 1);