1119691Smbr/*-
250479Speter * Copyright (c) 2011 Tim Kientzle
344024Sobrien * All rights reserved.
4119691Smbr *
5119691Smbr * Redistribution and use in source and binary forms, with or without
644024Sobrien * modification, are permitted provided that the following conditions
744024Sobrien * are met:
838581Sobrien * 1. Redistributions of source code must retain the above copyright
938581Sobrien *    notice, this list of conditions and the following disclaimer.
1038581Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11119691Smbr *    notice, this list of conditions and the following disclaimer in the
12119691Smbr *    documentation and/or other materials provided with the distribution.
1338581Sobrien *
14119691Smbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15119691Smbr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1638581Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17119691Smbr * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18119691Smbr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19119691Smbr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20119691Smbr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21174307Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22119691Smbr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23119691Smbr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24119691Smbr */
25119691Smbr
26119691Smbr#include "test.h"
27119691Smbr__FBSDID("$FreeBSD$");
28119691Smbr
29174307Sobrien#define should(__a, __code, __opts) \
30174307SobrienassertEqualInt(__code, archive_read_set_options(__a, __opts))
31174307Sobrien
32174307Sobrienstatic void
33119691Smbrtest(int pristine)
34119691Smbr{
35119691Smbr	struct archive* a = archive_read_new();
36119691Smbr	int halfempty_options_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
37119691Smbr	int known_option_rv = pristine ? ARCHIVE_FAILED : ARCHIVE_OK;
38119691Smbr
39119691Smbr	if (!pristine) {
4038581Sobrien		archive_read_support_filter_all(a);
4182817Sobrien		archive_read_support_format_all(a);
4238581Sobrien	}
4338581Sobrien
4482817Sobrien	/* NULL and "" denote `no option', so they're ok no matter
4538581Sobrien	 * what, if any, formats are registered */
4638581Sobrien	should(a, ARCHIVE_OK, NULL);
4782817Sobrien	should(a, ARCHIVE_OK, "");
4838581Sobrien
49119691Smbr	/* unknown modules and options */
50119691Smbr	should(a, ARCHIVE_FAILED, "fubar:snafu");
51119691Smbr	assertEqualString("Unknown module name: `fubar'",
5238581Sobrien	    archive_error_string(a));
5382817Sobrien	should(a, ARCHIVE_FAILED, "fubar:snafu=betcha");
5438581Sobrien	assertEqualString("Unknown module name: `fubar'",
5551307Sobrien	    archive_error_string(a));
5682817Sobrien
5738581Sobrien	/* unknown modules and options */
5838581Sobrien	should(a, ARCHIVE_FAILED, "snafu");
5982817Sobrien	assertEqualString("Undefined option: `snafu'",
6038581Sobrien	    archive_error_string(a));
6138581Sobrien	should(a, ARCHIVE_FAILED, "snafu=betcha");
6282817Sobrien	assertEqualString("Undefined option: `snafu'",
6338581Sobrien	    archive_error_string(a));
64119691Smbr
65119691Smbr	/* ARCHIVE_OK with iso9660 loaded, ARCHIVE_FAILED otherwise */
66119691Smbr	should(a, known_option_rv, "iso9660:joliet");
67119691Smbr	if (pristine) {
68119691Smbr		assertEqualString("Unknown module name: `iso9660'",
69119691Smbr		    archive_error_string(a));
7038581Sobrien	}
7182817Sobrien	should(a, known_option_rv, "iso9660:joliet");
7238581Sobrien	if (pristine) {
73119691Smbr		assertEqualString("Unknown module name: `iso9660'",
74119691Smbr		    archive_error_string(a));
7538581Sobrien	}
76119691Smbr	should(a, known_option_rv, "joliet");
77119691Smbr	if (pristine) {
7838581Sobrien		assertEqualString("Undefined option: `joliet'",
79119691Smbr		    archive_error_string(a));
80119691Smbr	}
8138581Sobrien	should(a, known_option_rv, "!joliet");
82174307Sobrien	if (pristine) {
83174307Sobrien		assertEqualString("Undefined option: `joliet'",
84174307Sobrien		    archive_error_string(a));
85174307Sobrien	}
86174307Sobrien
87174307Sobrien	should(a, ARCHIVE_OK, ",");
88119691Smbr	should(a, ARCHIVE_OK, ",,");
89119691Smbr
9038581Sobrien	should(a, halfempty_options_rv, ",joliet");
91119691Smbr	if (pristine) {
92119691Smbr		assertEqualString("Undefined option: `joliet'",
9338581Sobrien		    archive_error_string(a));
94119691Smbr	}
95119691Smbr	should(a, halfempty_options_rv, "joliet,");
9638581Sobrien	if (pristine) {
97119691Smbr		assertEqualString("Undefined option: `joliet'",
98119691Smbr		    archive_error_string(a));
9938581Sobrien	}
100119691Smbr
101119691Smbr	should(a, ARCHIVE_FAILED, "joliet,snafu");
10238581Sobrien	if (pristine) {
103119691Smbr		assertEqualString("Undefined option: `joliet'",
104119691Smbr		    archive_error_string(a));
10538581Sobrien	} else {
106174307Sobrien		assertEqualString("Undefined option: `snafu'",
107174307Sobrien		    archive_error_string(a));
108174307Sobrien	}
109174307Sobrien
110119691Smbr	should(a, ARCHIVE_FAILED, "iso9660:snafu");
11138581Sobrien	if (pristine) {
112174307Sobrien		assertEqualString("Unknown module name: `iso9660'",
113119691Smbr		    archive_error_string(a));
11438581Sobrien	} else {
115174307Sobrien		assertEqualString("Undefined option: `iso9660:snafu'",
116119691Smbr		    archive_error_string(a));
11738581Sobrien	}
118174307Sobrien
119119691Smbr	archive_read_free(a);
12038581Sobrien}
121174307Sobrien
122119691SmbrDEFINE_TEST(test_archive_read_set_options)
12338581Sobrien{
124174307Sobrien	test(1);
125119691Smbr	test(0);
12638581Sobrien}
127119691Smbr