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 DEFINE_TEST(test_write_format_warc) 30 { 31 char filedata[64]; 32 struct archive *a; 33 struct archive_entry *ae; 34 const size_t bsiz = 2048U; 35 char *buff; 36 size_t used; 37 38 buff = malloc(bsiz); 39 40 /* Create a new archive in memory. */ 41 assert((a = archive_write_new()) != NULL); 42 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 43 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 44 assertEqualIntA(a, ARCHIVE_OK, 45 archive_write_open_memory(a, buff, bsiz, &used)); 46 47 /* 48 * Write a file to it. 49 */ 50 assert((ae = archive_entry_new()) != NULL); 51 archive_entry_set_pathname(ae, "test"); 52 archive_entry_set_filetype(ae, AE_IFREG); 53 archive_entry_set_size(ae, 9); 54 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 55 archive_entry_free(ae); 56 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 57 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 58 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 59 60 /* 61 * Read from it. 62 */ 63 assert((a = archive_read_new()) != NULL); 64 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_warc(a)); 65 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_none(a)); 66 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 67 68 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 69 assertEqualIntA(a, 9, archive_read_data(a, filedata, 10)); 70 assertEqualMem(filedata, "12345678", 9); 71 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 72 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 73 74 /* Create a new archive */ 75 assert((a = archive_write_new()) != NULL); 76 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 77 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 78 assertEqualIntA(a, ARCHIVE_OK, 79 archive_write_open_memory(a, buff, bsiz, &used)); 80 81 /* write first file: that should succeed */ 82 assert((ae = archive_entry_new()) != NULL); 83 archive_entry_set_pathname(ae, "test"); 84 archive_entry_set_filetype(ae, AE_IFREG); 85 archive_entry_set_size(ae, 9); 86 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 87 archive_entry_free(ae); 88 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 89 90 /* write second file: should succeed as well */ 91 assert((ae = archive_entry_new()) != NULL); 92 archive_entry_set_pathname(ae, "test2"); 93 archive_entry_set_filetype(ae, AE_IFREG); 94 archive_entry_set_size(ae, 9); 95 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 96 archive_entry_free(ae); 97 assertEqualIntA(a, 9, archive_write_data(a, "12345678", 9)); 98 99 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 100 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 101 102 /* Create a new archive */ 103 assert((a = archive_write_new()) != NULL); 104 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_warc(a)); 105 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 106 assertEqualIntA(a, ARCHIVE_OK, 107 archive_write_open_memory(a, buff, bsiz, &used)); 108 109 /* write a directory: this should fail */ 110 assert((ae = archive_entry_new()) != NULL); 111 archive_entry_copy_pathname(ae, "dir"); 112 archive_entry_set_filetype(ae, AE_IFDIR); 113 archive_entry_set_size(ae, 512); 114 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 115 archive_entry_free(ae); 116 117 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 118 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 119 120 /* test whether last archive is indeed empty */ 121 assert((a = archive_read_new()) != NULL); 122 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 123 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 124 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used)); 125 126 /* Test EOF */ 127 assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 128 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 129 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 130 131 free(buff); 132 } 133