test_read_filter_uudecode.c revision 248616
154272Ssos/*-
255522Ssos * Copyright (c) 2003-2007 Tim Kientzle
354272Ssos * Copyright (c) 2009-2011 Michihiro NAKAJIMA
454272Ssos * All rights reserved.
554272Ssos *
654272Ssos * Redistribution and use in source and binary forms, with or without
754272Ssos * modification, are permitted provided that the following conditions
854272Ssos * are met:
954272Ssos * 1. Redistributions of source code must retain the above copyright
1054272Ssos *    notice, this list of conditions and the following disclaimer.
1154272Ssos * 2. Redistributions in binary form must reproduce the above copyright
1254272Ssos *    notice, this list of conditions and the following disclaimer in the
1354272Ssos *    documentation and/or other materials provided with the distribution.
1454272Ssos *
1554272Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1654272Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1754272Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1854272Ssos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
1954272Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2054272Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2154272Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2254272Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2354272Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2454272Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2554272Ssos */
2654272Ssos#include "test.h"
2754272Ssos__FBSDID("$FreeBSD: head/lib/libarchive/test/test_read_uu.c 201248 2009-12-30 06:12:03Z kientzle $");
2854272Ssos
2954272Ssosstatic const char archive[] = {
3054272Ssos"begin 644 test_read_uu.Z\n"
3155522Ssos"M'YV0+@`('$BPH,&#\"!,J7,BP(4(8$&_4J`$\"`,08$F%4O)AQ(\\2/(#7&@#%C\n"
3255522Ssos"M!@T8-##.L`$\"QL@:-F(``%'#H<V;.'/J!%!G#ITP<BS\"H).FS<Z$1(T>/1A2\n"
3355522Ssos"IHU\"0%9=*G4JUJM6K6+-JW<JUJ]>O8,.*'4NVK-FS:-.J7<NVK=NW9P$`\n"
3454272Ssos"`\n"
3554272Ssos"end\n"
3654272Ssos};
3754272Ssos
3854272Ssosstatic const char archive64[] = {
3954272Ssos"begin-base64 644 test_read_uu.Z\n"
4054272Ssos"H52QLgAIHEiwoMGDCBMqXMiwIUIYEG/UqAECAMQYEmFUvJhxI8SPIDXGgDFjBg0YNDDOsAECxsga\n"
4154272Ssos"NmIAAFHDoc2bOHPqBFBnDp0wcizCoJOmzc6ERI0ePRhSo1CQFZdKnUq1qtWrWLNq3cq1q9evYMOK\n"
4254272Ssos"HUu2rNmzaNOqXcu2rdu3ZwE=\n"
4354272Ssos"====\n"
4454272Ssos};
4554272Ssos
4654272Ssosstatic const char extradata[] = {
4754272Ssos"From uudecode@libarchive Mon Jun  2 03:03:31 2008\n"
4854272Ssos"Return-Path: <uudecode@libarchive>\n"
4954272Ssos"Received: from libarchive (localhost [127.0.0.1])\n"
5054272Ssos"        by libarchive (8.14.2/8.14.2) with ESMTP id m5233UT1006448\n"
5154272Ssos"        for <uudecode@libarchive>; Mon, 2 Jun 2008 03:03:31 GMT\n"
5254272Ssos"        (envelope-from uudecode@libarchive)\n"
5354272Ssos"Received: (from uudecode@localhost)\n"
5454272Ssos"        by libarchive (8.14.2/8.14.2/Submit) id m5233U3e006406\n"
5554272Ssos"        for uudecode; Mon, 2 Jun 2008 03:03:30 GMT\n"
5654272Ssos"        (envelope-from root)\n"
5754272Ssos"Date: Mon, 2 Jun 2008 03:03:30 GMT\n"
5854272Ssos"From: Libarchive Test <uudecode@libarchive>\n"
5954272Ssos"Message-Id: <200806020303.m5233U3e006406@libarchive>\n"
6054272Ssos"To: uudecode@libarchive\n"
6154272Ssos"Subject: Libarchive uudecode test\n"
6254272Ssos"\n"
6354272Ssos"* Redistribution and use in source and binary forms, with or without\n"
6454272Ssos"* modification, are permitted provided that the following conditions\n"
6554272Ssos"* are met:\n"
6654272Ssos"\n"
6754272Ssos"01234567890abcdeghijklmnopqrstuvwxyz\n"
6854272Ssos"01234567890ABCEFGHIJKLMNOPQRSTUVWXYZ\n"
6954272Ssos"\n"
7054272Ssos};
7154272Ssos
7254272Ssosstatic void
7354272Ssostest_read_uu_sub(const char *uudata, size_t uusize, int no_nl)
7454272Ssos{
7554272Ssos	struct archive_entry *ae;
7654272Ssos	struct archive *a;
7754272Ssos	char *buff;
7854272Ssos	char extradata_no_nl[sizeof(extradata)];
7954272Ssos	const char *extradata_ptr;
8054272Ssos	int extra;
8154272Ssos	size_t size;
8254272Ssos
8354272Ssos	if (no_nl) {
8454272Ssos		/* Remove '\n' from extra data to make a very long line. */
8560422Sken		char *p;
8660422Sken		memcpy(extradata_no_nl, extradata, sizeof(extradata));
8754272Ssos		extradata_ptr = extradata_no_nl;
8854272Ssos		for (p = extradata_no_nl;
8954272Ssos		    *p && (p = strchr(p, '\n')) != NULL; p++)
9054272Ssos			*p = ' ';/* Replace '\n' with ' ' a space character. */
9154272Ssos	} else
9254272Ssos		extradata_ptr = extradata;
9354272Ssos
9454272Ssos	assert(NULL != (buff = malloc(uusize + 1024 * 1024)));
9554272Ssos	if (buff == NULL)
9654272Ssos		return;
9754272Ssos	for (extra = 0; extra <= 64; extra = extra==0?1:extra*2) {
9854272Ssos		char *p = buff;
9954272Ssos
10054272Ssos		size = extra * 1024;
10154272Ssos		/* Add extra text size of which is from 1K bytes to
10254272Ssos		 * 64Kbytes before uuencoded data. */
10354272Ssos		while (size) {
10454272Ssos			if (size > sizeof(extradata)-1) {
10555522Ssos				memcpy(p, extradata_ptr, sizeof(extradata)-1);
10655522Ssos				p += sizeof(extradata)-1;
10755522Ssos				size -= sizeof(extradata)-1;
10855522Ssos			} else {
10955522Ssos				memcpy(p, extradata_ptr, size-1);
11055522Ssos				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
162DEFINE_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
171DEFINE_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