1 /*- 2 * Copyright (C) 2014 Sebastian Freundt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "test.h" 27 __FBSDID("$FreeBSD$"); 28 29 static void test_read(struct archive *a, char *buff, size_t used, char *filedata) 30 { 31 struct archive_entry *ae; 32 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_none(a)); 33 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 34 35 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 36 assertEqualIntA(a, 9, archive_read_data(a, filedata, 10)); 37 assertEqualMem(filedata, "12345678", 9); 38 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 39 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 40 } 41 42 DEFINE_TEST(test_write_format_warc) 43 { 44 char filedata[64]; 45 struct archive *a; 46 struct archive_entry *ae; 47 const size_t bsiz = 2048U; 48 char *buff; 49 size_t used; 50 51 buff = malloc(bsiz); 52 53 /* Create a new archive in memory. */ 54 assert((a = archive_write_new()) != NULL); 55 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 56 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 57 assertEqualIntA(a, ARCHIVE_OK, 58 archive_write_open_memory(a, buff, bsiz, &used)); 59 60 /* 61 * Write a file to it. 62 */ 63 assert((ae = archive_entry_new()) != NULL); 64 archive_entry_set_pathname(ae, "test"); 65 archive_entry_set_filetype(ae, AE_IFREG); 66 archive_entry_set_size(ae, 9); 67 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 68 archive_entry_free(ae); 69 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 70 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 71 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 72 73 /* 74 * Read from it. 75 */ 76 assert((a = archive_read_new()) != NULL); 77 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_warc(a)); 78 test_read(a, buff, used, filedata); 79 80 assert((a = archive_read_new()) != NULL); 81 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_by_code(a, ARCHIVE_FORMAT_WARC)); 82 test_read(a, buff, used, filedata); 83 84 assert((a = archive_read_new()) != NULL); 85 assertEqualIntA(a, ARCHIVE_OK, archive_read_set_format(a, ARCHIVE_FORMAT_WARC)); 86 test_read(a, buff, used, filedata); 87 88 /* Create a new archive */ 89 assert((a = archive_write_new()) != NULL); 90 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 91 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 92 assertEqualIntA(a, ARCHIVE_OK, 93 archive_write_open_memory(a, buff, bsiz, &used)); 94 95 /* write first file: that should succeed */ 96 assert((ae = archive_entry_new()) != NULL); 97 archive_entry_set_pathname(ae, "test"); 98 archive_entry_set_filetype(ae, AE_IFREG); 99 archive_entry_set_size(ae, 9); 100 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 101 archive_entry_free(ae); 102 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 103 104 /* write second file: should succeed as well */ 105 assert((ae = archive_entry_new()) != NULL); 106 archive_entry_set_pathname(ae, "test2"); 107 archive_entry_set_filetype(ae, AE_IFREG); 108 archive_entry_set_size(ae, 9); 109 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 110 archive_entry_free(ae); 111 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 112 113 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 114 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 115 116 /* Create a new archive */ 117 assert((a = archive_write_new()) != NULL); 118 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 119 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 120 assertEqualIntA(a, ARCHIVE_OK, 121 archive_write_open_memory(a, buff, bsiz, &used)); 122 123 /* write a directory: this should fail */ 124 assert((ae = archive_entry_new()) != NULL); 125 archive_entry_copy_pathname(ae, "dir"); 126 archive_entry_set_filetype(ae, AE_IFDIR); 127 archive_entry_set_size(ae, 512); 128 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 129 archive_entry_free(ae); 130 131 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 132 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 133 134 /* test whether last archive is indeed empty */ 135 assert((a = archive_read_new()) != NULL); 136 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 137 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 138 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 139 140 /* Test EOF */ 141 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 142 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 143 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 144 145 free(buff); 146 } 147