1/*- 2 * Copyright (c) 2007 Tim Kientzle 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 * in this position and unchanged. 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/* 31 * A basic exercise of compress reading and writing. 32 * 33 * TODO: Add a reference file and make sure we can decompress that. 34 */ 35 36DEFINE_TEST(test_write_compress) 37{ 38 struct archive_entry *ae; 39 struct archive* a; 40 char *buff, *data; 41 size_t buffsize, datasize; 42 char path[16]; 43 size_t used; 44 int i; 45 46 buffsize = 1000000; 47 assert(NULL != (buff = (char *)malloc(buffsize))); 48 49 datasize = 10000; 50 assert(NULL != (data = (char *)malloc(datasize))); 51 memset(data, 0, datasize); 52 53 assert((a = archive_write_new()) != NULL); 54 assertA(0 == archive_write_set_format_ustar(a)); 55 assertA(0 == archive_write_set_compression_compress(a)); 56 assertA(0 == archive_write_open_memory(a, buff, buffsize, &used)); 57 58 for (i = 0; i < 100; i++) { 59 sprintf(path, "file%03d", i); 60 assert((ae = archive_entry_new()) != NULL); 61 archive_entry_copy_pathname(ae, path); 62 archive_entry_set_size(ae, datasize); 63 archive_entry_set_filetype(ae, AE_IFREG); 64 assertA(0 == archive_write_header(a, ae)); 65 assertA(datasize == (size_t)archive_write_data(a, data, datasize)); 66 archive_entry_free(ae); 67 } 68 69 70 archive_write_close(a); 71#if ARCHIVE_VERSION_NUMBER < 2000000 72 archive_write_finish(a); 73#else 74 assert(0 == archive_write_finish(a)); 75#endif 76 77 /* 78 * Now, read the data back. 79 */ 80 assert((a = archive_read_new()) != NULL); 81 assertA(0 == archive_read_support_format_all(a)); 82 assertA(0 == archive_read_support_compression_all(a)); 83 assertA(0 == archive_read_open_memory(a, buff, used)); 84 85 86 for (i = 0; i < 100; i++) { 87 sprintf(path, "file%03d", i); 88 if (!assertEqualInt(0, archive_read_next_header(a, &ae))) 89 break; 90 assertEqualString(path, archive_entry_pathname(ae)); 91 assertEqualInt((int)datasize, archive_entry_size(ae)); 92 } 93 assert(0 == archive_read_close(a)); 94#if ARCHIVE_VERSION_NUMBER < 2000000 95 archive_read_finish(a); 96#else 97 assert(0 == archive_read_finish(a)); 98#endif 99 100 free(data); 101 free(buff); 102} 103