1 /*-
2  * Copyright (c) 2011 Tim Kientzle
3  * Copyright (c) 2014 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 
27 #include "test.h"
28 __FBSDID("$FreeBSD$");
29 
30 struct archive_write;
31 extern const char * __archive_write_get_passphrase(struct archive_write *);
32 
33 static void
34 test(int pristine)
35 {
36 	struct archive* a = archive_write_new();
37 	struct archive_write* aw = (struct archive_write *)a;
38 
39 	if (!pristine) {
40 		archive_write_add_filter_gzip(a);
41 		archive_write_set_format_iso9660(a);
42         }
43 
44 	assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass1"));
45 	/* An empty passphrase cannot be accepted. */
46 	assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, ""));
47 	/* NULL passphrases cannot be accepted. */
48 	assertEqualInt(ARCHIVE_FAILED, archive_write_set_passphrase(a, NULL));
49 	/* Check a passphrase. */
50 	assertEqualString("pass1", __archive_write_get_passphrase(aw));
51 	/* Change the passphrase. */
52 	assertEqualInt(ARCHIVE_OK, archive_write_set_passphrase(a, "pass2"));
53 	assertEqualString("pass2", __archive_write_get_passphrase(aw));
54 
55 	archive_write_free(a);
56 }
57 
58 DEFINE_TEST(test_archive_write_set_passphrase)
59 {
60 	test(1);
61 	test(0);
62 }
63 
64 
65 static const char *
66 callback1(struct archive *a, void *_client_data)
67 {
68 	int *cnt;
69 
70 	(void)a; /* UNUSED */
71 
72 	cnt = (int *)_client_data;
73 	*cnt += 1;
74 	return ("passCallBack");
75 }
76 
77 DEFINE_TEST(test_archive_write_set_passphrase_callback)
78 {
79 	struct archive* a = archive_write_new();
80 	struct archive_write* aw = (struct archive_write *)a;
81 	int cnt = 0;
82 
83 	archive_write_set_format_zip(a);
84 
85 	assertEqualInt(ARCHIVE_OK,
86 	    archive_write_set_passphrase_callback(a, &cnt, callback1));
87 	/* Check a passphrase. */
88 	assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
89 	assertEqualInt(1, cnt);
90 	/* Callback function should be called just once. */
91 	assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
92 	assertEqualInt(1, cnt);
93 
94 	archive_write_free(a);
95 }
96