1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2009 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$");
28
29static 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
38static 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
46static 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
72static void
73test_read_uu_sub(const char *uudata, size_t uusize)
74{
75	struct archive_entry *ae;
76	struct archive *a;
77	char *buff;
78	int extra;
79
80	assert(NULL != (buff = malloc(uusize + 64 * 1024)));
81	if (buff == NULL)
82		return;
83	for (extra = 0; extra <= 64; extra = extra==0?1:extra*2) {
84		size_t size = extra * 1024;
85		char *p = buff;
86
87		/* Add extra text size of which is from 1K bytes to
88		 * 64Kbytes before uuencoded data. */
89		while (size) {
90			if (size > sizeof(extradata)-1) {
91				memcpy(p, extradata, sizeof(extradata)-1);
92				p += sizeof(extradata)-1;
93				size -= sizeof(extradata)-1;
94			} else {
95				memcpy(p, extradata, size-1);
96				p += size-1;
97				*p++ = '\n';/* the last of extra text must have
98					     * '\n' character. */
99				break;
100			}
101		}
102		memcpy(p, uudata, uusize);
103		size = extra * 1024 + uusize;
104
105		assert((a = archive_read_new()) != NULL);
106		assertEqualIntA(a, ARCHIVE_OK,
107		    archive_read_support_compression_all(a));
108		assertEqualIntA(a, ARCHIVE_OK,
109		    archive_read_support_format_all(a));
110		assertEqualIntA(a, ARCHIVE_OK,
111		    read_open_memory(a, buff, size, 2));
112		assertEqualIntA(a, ARCHIVE_OK,
113		    archive_read_next_header(a, &ae));
114		failure("archive_compression_name(a)=\"%s\"",
115		    archive_compression_name(a));
116		assertEqualInt(archive_compression(a),
117		    ARCHIVE_COMPRESSION_COMPRESS);
118		failure("archive_format_name(a)=\"%s\"",
119		    archive_format_name(a));
120		assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_USTAR);
121		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
122		assertEqualInt(ARCHIVE_OK, archive_read_finish(a));
123	}
124	free(buff);
125}
126
127DEFINE_TEST(test_read_uu)
128{
129	/* Read the traditional uuencoded data. */
130	test_read_uu_sub(archive, sizeof(archive)-1);
131	/* Read the Base64 uuencoded data. */
132	test_read_uu_sub(archive64, sizeof(archive64)-1);
133}
134
135