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 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_uu.c 201248 2009-12-30 06:12:03Z kientzle $");
28 
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"
34 "`\n"
35 "end\n"
36 };
37 
38 static const char archive64[] = {
39 "begin-base64 644 test_read_uu.Z\n"
40 "H52QLgAIHEiwoMGDCBMqXMiwIUIYEG/UqAECAMQYEmFUvJhxI8SPIDXGgDFjBg0YNDDOsAECxsga\n"
41 "NmIAAFHDoc2bOHPqBFBnDp0wcizCoJOmzc6ERI0ePRhSo1CQFZdKnUq1qtWrWLNq3cq1q9evYMOK\n"
42 "HUu2rNmzaNOqXcu2rdu3ZwE=\n"
43 "====\n"
44 };
45 
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"
62 "\n"
63 "* Redistribution and use in source and binary forms, with or without\n"
64 "* modification, are permitted provided that the following conditions\n"
65 "* are met:\n"
66 "\n"
67 "01234567890abcdeghijklmnopqrstuvwxyz\n"
68 "01234567890ABCEFGHIJKLMNOPQRSTUVWXYZ\n"
69 "\n"
70 };
71 
72 static void
73 test_read_uu_sub(const char *uudata, size_t uusize, int no_nl)
74 {
75 	struct archive_entry *ae;
76 	struct archive *a;
77 	char *buff;
78 	char extradata_no_nl[sizeof(extradata)];
79 	const char *extradata_ptr;
80 	int extra;
81 	size_t size;
82 
83 	if (no_nl) {
84 		/* Remove '\n' from extra data to make a very long line. */
85 		char *p;
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. */
91 	} else
92 		extradata_ptr = extradata;
93 
94 	assert(NULL != (buff = malloc(uusize + 1024 * 1024)));
95 	if (buff == NULL)
96 		return;
97 	for (extra = 0; extra <= 64; extra = extra==0?1:extra*2) {
98 		char *p = buff;
99 
100 		size = extra * 1024;
101 		/* Add extra text size of which is from 1K bytes to
102 		 * 64Kbytes before uuencoded data. */
103 		while (size) {
104 			if (size > sizeof(extradata)-1) {
105 				memcpy(p, extradata_ptr, sizeof(extradata)-1);
106 				p += sizeof(extradata)-1;
107 				size -= sizeof(extradata)-1;
108 			} else {
109 				memcpy(p, extradata_ptr, size-1);
110 				p += size-1;
111 				*p++ = '\n';/* the last of extra text must have
112 					     * '\n' character. */
113 				break;
114 			}
115 		}
116 		memcpy(p, uudata, uusize);
117 		size = extra * 1024 + uusize;
118 
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_filter_name(a, 0)=\"%s\""
129 		    "extra %d, NL %d",
130 		    archive_filter_name(a, 0), extra, !no_nl);
131 		assertEqualInt(archive_filter_code(a, 0),
132 		    ARCHIVE_FILTER_COMPRESS);
133 		failure("archive_format_name(a)=\"%s\""
134 		    "extra %d, NL %d",
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));
140 	}
141 
142 	/* UUdecode bidder shouldn't scan too much data; make sure it
143 	 * fails if we put 512k of data before the start. */
144 	size = 512 * 1024;
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);
149 	size += 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));
158 
159 	free(buff);
160 }
161 
162 DEFINE_TEST(test_read_filter_uudecode)
163 {
164 	/* Read the traditional uuencoded data. */
165 	test_read_uu_sub(archive, sizeof(archive)-1, 0);
166 	/* Read the traditional uuencoded data with very long line extra
167 	 * data in front of it. */
168 	test_read_uu_sub(archive, sizeof(archive)-1, 1);
169 }
170 
171 DEFINE_TEST(test_read_filter_uudecode_base64)
172 {
173 	/* Read the Base64 uuencoded data. */
174 	test_read_uu_sub(archive64, sizeof(archive64)-1, 0);
175 	/* Read the Base64 uuencoded data with very long line extra data
176 	 * in front of it. */
177 	test_read_uu_sub(archive64, sizeof(archive64)-1, 1);
178 }
179