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