1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2009-2011 Michihiro NAKAJIMA
4  * All rights reserved.
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(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.
25  */
26 #include "test.h"
27 
28 static const char archive[] = {
29 "begin 644 test_read_uu.Z\n"
30 "M'YV0+@`('$BPH,&#\"!,J7,BP(4(8$&_4J`$\"`,08$F%4O)AQ(\\2/(#7&@#%C\n"
31 "M!@T8-##.L`$\"QL@:-F(``%'#H<V;.'/J!%!G#ITP<BS\"H).FS<Z$1(T>/1A2\n"
32 "IHU\"0%9=*G4JUJM6K6+-JW<JUJ]>O8,.*'4NVK-FS:-.J7<NVK=NW9P$`\n"
33 "`\n"
34 "end\n"
35 };
36 
37 static const char archive64[] = {
38 "begin-base64 644 test_read_uu.Z\n"
39 "H52QLgAIHEiwoMGDCBMqXMiwIUIYEG/UqAECAMQYEmFUvJhxI8SPIDXGgDFjBg0YNDDOsAECxsga\n"
40 "NmIAAFHDoc2bOHPqBFBnDp0wcizCoJOmzc6ERI0ePRhSo1CQFZdKnUq1qtWrWLNq3cq1q9evYMOK\n"
41 "HUu2rNmzaNOqXcu2rdu3ZwE=\n"
42 "====\n"
43 };
44 
45 static const char extradata[] = {
46 "From uudecode@libarchive Mon Jun  2 03:03:31 2008\n"
47 "Return-Path: <uudecode@libarchive>\n"
48 "Received: from libarchive (localhost [127.0.0.1])\n"
49 "        by libarchive (8.14.2/8.14.2) with ESMTP id m5233UT1006448\n"
50 "        for <uudecode@libarchive>; Mon, 2 Jun 2008 03:03:31 GMT\n"
51 "        (envelope-from uudecode@libarchive)\n"
52 "Received: (from uudecode@localhost)\n"
53 "        by libarchive (8.14.2/8.14.2/Submit) id m5233U3e006406\n"
54 "        for uudecode; Mon, 2 Jun 2008 03:03:30 GMT\n"
55 "        (envelope-from root)\n"
56 "Date: Mon, 2 Jun 2008 03:03:30 GMT\n"
57 "From: Libarchive Test <uudecode@libarchive>\n"
58 "Message-Id: <200806020303.m5233U3e006406@libarchive>\n"
59 "To: uudecode@libarchive\n"
60 "Subject: Libarchive uudecode test\n"
61 "\n"
62 "* Redistribution and use in source and binary forms, with or without\n"
63 "* modification, are permitted provided that the following conditions\n"
64 "* are met:\n"
65 "\n"
66 "01234567890abcdeghijklmnopqrstuvwxyz\n"
67 "01234567890ABCEFGHIJKLMNOPQRSTUVWXYZ\n"
68 "\n"
69 };
70 
71 static void
72 test_read_uu_sub(const char *uudata, size_t uusize, int no_nl)
73 {
74 	struct archive_entry *ae;
75 	struct archive *a;
76 	char *buff;
77 	char extradata_no_nl[sizeof(extradata)];
78 	const char *extradata_ptr;
79 	int extra;
80 	size_t size;
81 
82 	if (no_nl) {
83 		/* Remove '\n' from extra data to make a very long line. */
84 		char *p;
85 		memcpy(extradata_no_nl, extradata, sizeof(extradata));
86 		extradata_ptr = extradata_no_nl;
87 		for (p = extradata_no_nl;
88 		    *p && (p = strchr(p, '\n')) != NULL; p++)
89 			*p = ' ';/* Replace '\n' with ' ' a space character. */
90 	} else
91 		extradata_ptr = extradata;
92 
93 	assert(NULL != (buff = malloc(uusize + 1024 * 1024)));
94 	if (buff == NULL)
95 		return;
96 	for (extra = 0; extra <= 64; extra = extra==0?1:extra*2) {
97 		char *p = buff;
98 
99 		size = extra * 1024;
100 		/* Add extra text size of which is from 1K bytes to
101 		 * 64Kbytes before uuencoded data. */
102 		while (size) {
103 			if (size > sizeof(extradata)-1) {
104 				memcpy(p, extradata_ptr, sizeof(extradata)-1);
105 				p += sizeof(extradata)-1;
106 				size -= sizeof(extradata)-1;
107 			} else {
108 				memcpy(p, extradata_ptr, size-1);
109 				p += size-1;
110 				*p++ = '\n';/* the last of extra text must have
111 					     * '\n' character. */
112 				break;
113 			}
114 		}
115 		memcpy(p, uudata, uusize);
116 		size = extra * 1024 + uusize;
117 
118 		assert((a = archive_read_new()) != NULL);
119 		assertEqualIntA(a, ARCHIVE_OK,
120 		    archive_read_support_filter_all(a));
121 		assertEqualIntA(a, ARCHIVE_OK,
122 		    archive_read_support_format_all(a));
123 		assertEqualIntA(a, ARCHIVE_OK,
124 		    read_open_memory(a, buff, size, 2));
125 		assertEqualIntA(a, ARCHIVE_OK,
126 		    archive_read_next_header(a, &ae));
127 		failure("archive_filter_name(a, 0)=\"%s\""
128 		    "extra %d, NL %d",
129 		    archive_filter_name(a, 0), extra, !no_nl);
130 		assertEqualInt(archive_filter_code(a, 0),
131 		    ARCHIVE_FILTER_COMPRESS);
132 		failure("archive_format_name(a)=\"%s\""
133 		    "extra %d, NL %d",
134 		    archive_format_name(a), extra, !no_nl);
135 		assertEqualInt(archive_format(a),
136 		    ARCHIVE_FORMAT_TAR_USTAR);
137 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
138 		assertEqualInt(ARCHIVE_OK, archive_read_free(a));
139 	}
140 
141 	/* UUdecode bidder shouldn't scan too much data; make sure it
142 	 * fails if we put 512k of data before the start. */
143 	size = 512 * 1024;
144 	for (extra = 0; (size_t)extra < size; ++extra)
145 		buff[extra + 1024] = buff[extra];
146 	buff[size - 1] = '\n';
147 	memcpy(buff + size, uudata, uusize);
148 	size += uusize;
149 	assert((a = archive_read_new()) != NULL);
150 	assertEqualIntA(a, ARCHIVE_OK,
151 	    archive_read_support_filter_all(a));
152 	assertEqualIntA(a, ARCHIVE_OK,
153 	    archive_read_support_format_all(a));
154 	assertEqualIntA(a, ARCHIVE_FATAL,
155 	    read_open_memory(a, buff, size, 2));
156 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
157 
158 	free(buff);
159 }
160 
161 DEFINE_TEST(test_read_filter_uudecode)
162 {
163 	/* Read the traditional uuencoded data. */
164 	test_read_uu_sub(archive, sizeof(archive)-1, 0);
165 	/* Read the traditional uuencoded data with very long line extra
166 	 * data in front of it. */
167 	test_read_uu_sub(archive, sizeof(archive)-1, 1);
168 }
169 
170 DEFINE_TEST(test_read_filter_uudecode_base64)
171 {
172 	/* Read the Base64 uuencoded data. */
173 	test_read_uu_sub(archive64, sizeof(archive64)-1, 0);
174 	/* Read the Base64 uuencoded data with very long line extra data
175 	 * in front of it. */
176 	test_read_uu_sub(archive64, sizeof(archive64)-1, 1);
177 }
178