1299425Smm/*-
2299425Smm * Copyright (c) 2011 Tim Kientzle
3299425Smm * Copyright (c) 2014 Michihiro NAKAJIMA
4299425Smm * All rights reserved.
5299425Smm *
6299425Smm * Redistribution and use in source and binary forms, with or without
7299425Smm * modification, are permitted provided that the following conditions
8299425Smm * are met:
9299425Smm * 1. Redistributions of source code must retain the above copyright
10299425Smm *    notice, this list of conditions and the following disclaimer.
11299425Smm * 2. Redistributions in binary form must reproduce the above copyright
12299425Smm *    notice, this list of conditions and the following disclaimer in the
13299425Smm *    documentation and/or other materials provided with the distribution.
14299425Smm *
15299425Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16299425Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17299425Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18299425Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19299425Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20299425Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21299425Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22299425Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23299425Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24299425Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25299425Smm */
26299425Smm
27299425Smm#include "test.h"
28299425Smm__FBSDID("$FreeBSD$");
29299425Smm
30299425Smmstruct archive_write;
31299425Smmextern const char * __archive_write_get_passphrase(struct archive_write *);
32299425Smm
33299425Smmstatic void
34299425Smmtest(int pristine)
35299425Smm{
36299425Smm	struct archive* a = archive_write_new();
37299425Smm	struct archive_write* aw = (struct archive_write *)a;
38299425Smm
39299425Smm	if (!pristine) {
40299425Smm		archive_write_add_filter_gzip(a);
41299425Smm		archive_write_set_format_iso9660(a);
42299425Smm        }
43299425Smm
44299425Smm	assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass1"));
45299425Smm	/* An empty passphrase cannot be accepted. */
46299425Smm	assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, ""));
47299425Smm	/* NULL passphrases cannot be accepted. */
48299425Smm	assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, NULL));
49299425Smm	/* Check a passphrase. */
50299425Smm	assertEqualString("pass1", __archive_write_get_passphrase(aw));
51299425Smm	/* Change the passphrase. */
52299425Smm	assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass2"));
53299425Smm	assertEqualString("pass2", __archive_write_get_passphrase(aw));
54299425Smm
55299425Smm	archive_write_free(a);
56299425Smm}
57299425Smm
58299425SmmDEFINE_TEST(test_archive_write_set_passphrase)
59299425Smm{
60299425Smm	test(1);
61299425Smm	test(0);
62299425Smm}
63299425Smm
64299425Smm
65299425Smmstatic const char *
66299425Smmcallback1(struct archive *a, void *_client_data)
67299425Smm{
68299425Smm	int *cnt;
69299425Smm
70299425Smm	(void)a; /* UNUSED */
71299425Smm
72299425Smm	cnt = (int *)_client_data;
73299425Smm	*cnt += 1;
74299425Smm	return ("passCallBack");
75299425Smm}
76299425Smm
77299425SmmDEFINE_TEST(test_archive_write_set_passphrase_callback)
78299425Smm{
79299425Smm	struct archive* a = archive_write_new();
80299425Smm	struct archive_write* aw = (struct archive_write *)a;
81299425Smm	int cnt = 0;
82299425Smm
83299425Smm	archive_write_set_format_zip(a);
84299425Smm
85299425Smm	assertEqualInt(ARCHIVE_OK,
86299425Smm	    archive_write_set_passphrase_callback(a, &cnt, callback1));
87299425Smm	/* Check a passphrase. */
88299425Smm	assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
89299425Smm	assertEqualInt(1, cnt);
90299425Smm	/* Callback function should be called just once. */
91299425Smm	assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
92299425Smm	assertEqualInt(1, cnt);
93299425Smm
94299425Smm	archive_write_free(a);
95299425Smm}
96