test_option_c.c revision 302001
1262973Sbapt/*- 2262973Sbapt * Copyright (c) 2003-2007 Tim Kientzle 3262973Sbapt * All rights reserved. 4262973Sbapt * 5262973Sbapt * Redistribution and use in source and binary forms, with or without 6262973Sbapt * modification, are permitted provided that the following conditions 7268896Sbapt * are met: 8262973Sbapt * 1. Redistributions of source code must retain the above copyright 9262973Sbapt * notice, this list of conditions and the following disclaimer. 10262973Sbapt * 2. Redistributions in binary form must reproduce the above copyright 11262973Sbapt * notice, this list of conditions and the following disclaimer in the 12262973Sbapt * documentation and/or other materials provided with the distribution. 13262973Sbapt * 14262973Sbapt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15262973Sbapt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16262973Sbapt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17262973Sbapt * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18262973Sbapt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19262973Sbapt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20262973Sbapt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21262973Sbapt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22262973Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23262973Sbapt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24268896Sbapt */ 25268896Sbapt#include "test.h" 26268896Sbapt__FBSDID("$FreeBSD: stable/10/contrib/libarchive/cpio/test/test_option_c.c 302001 2016-06-17 22:40:10Z mm $"); 27268896Sbapt 28268896Sbaptstatic int 29268896Sbaptis_octal(const char *p, size_t l) 30268896Sbapt{ 31268896Sbapt while (l > 0) { 32268896Sbapt if (*p < '0' || *p > '7') 33268896Sbapt return (0); 34268896Sbapt --l; 35262973Sbapt ++p; 36262973Sbapt } 37262973Sbapt return (1); 38262973Sbapt} 39262973Sbapt 40262973Sbaptstatic int 41262973Sbaptfrom_octal(const char *p, size_t l) 42262973Sbapt{ 43262973Sbapt int r = 0; 44262973Sbapt 45262973Sbapt while (l > 0) { 46262973Sbapt r *= 8; 47262973Sbapt r += *p - '0'; 48262973Sbapt --l; 49262973Sbapt ++p; 50262973Sbapt } 51262973Sbapt return (r); 52262973Sbapt} 53262973Sbapt 54262973Sbapt#if !defined(_WIN32) || defined(__CYGWIN__) 55262973Sbaptstatic int 56268896Sbaptnlinks(const char *p) 57268896Sbapt{ 58262973Sbapt struct stat st; 59262973Sbapt assertEqualInt(0, stat(p, &st)); 60262973Sbapt return st.st_nlink; 61262973Sbapt} 62268896Sbapt#endif 63262973Sbapt 64262973SbaptDEFINE_TEST(test_option_c) 65262973Sbapt{ 66262973Sbapt FILE *filelist; 67262973Sbapt int r; 68262973Sbapt int uid = 1000; 69262973Sbapt int dev, ino, gid = 1000; 70262973Sbapt time_t t, now; 71262973Sbapt char *p, *e; 72262973Sbapt size_t s; 73262973Sbapt 74268896Sbapt assertUmask(0); 75262973Sbapt 76262973Sbapt /* 77268896Sbapt * Create an assortment of files. 78262973Sbapt * TODO: Extend this to cover more filetypes. 79262973Sbapt */ 80262973Sbapt filelist = fopen("filelist", "w"); 81268896Sbapt 82268896Sbapt /* "file" */ 83262973Sbapt assertMakeFile("file", 0644, "1234567890"); 84262973Sbapt fprintf(filelist, "file\n"); 85262973Sbapt 86262973Sbapt /* "symlink" */ 87262973Sbapt if (canSymlink()) { 88262973Sbapt assertMakeSymlink("symlink", "file"); 89262973Sbapt fprintf(filelist, "symlink\n"); 90262973Sbapt } 91262973Sbapt 92262973Sbapt /* "dir" */ 93 assertMakeDir("dir", 0775); 94 /* Record some facts about what we just created: */ 95 now = time(NULL); /* They were all created w/in last two seconds. */ 96 fprintf(filelist, "dir\n"); 97 98 /* Use the cpio program to create an archive. */ 99 fclose(filelist); 100 r = systemf("%s -R 1000:1000 -oc <filelist >basic.out 2>basic.err", testprog); 101 /* Verify that nothing went to stderr. */ 102 assertTextFileContents("1 block\n", "basic.err"); 103 104 /* Assert that the program finished. */ 105 failure("%s -oc crashed", testprog); 106 if (!assertEqualInt(r, 0)) 107 return; 108 109 /* Verify that stdout is a well-formed cpio file in "odc" format. */ 110 p = slurpfile(&s, "basic.out"); 111 assertEqualInt(s, 512); 112 e = p; 113 114 /* 115 * Some of these assertions could be stronger, but it's 116 * a little tricky because they depend on the local environment. 117 */ 118 119 /* First entry is "file" */ 120 assert(is_octal(e, 76)); /* Entire header is octal digits. */ 121 assertEqualMem(e + 0, "070707", 6); /* Magic */ 122 assert(is_octal(e + 6, 6)); /* dev */ 123 dev = from_octal(e + 6, 6); 124 assert(is_octal(e + 12, 6)); /* ino */ 125 ino = from_octal(e + 12, 6); 126#if defined(_WIN32) && !defined(__CYGWIN__) 127 /* Group members bits and others bits do not work. */ 128 assertEqualMem(e + 18, "100666", 6); /* Mode */ 129#else 130 assertEqualMem(e + 18, "100644", 6); /* Mode */ 131#endif 132 if (uid < 0) 133 uid = from_octal(e + 24, 6); 134 assertEqualInt(from_octal(e + 24, 6), uid); /* uid */ 135 assert(is_octal(e + 30, 6)); /* gid */ 136 gid = from_octal(e + 30, 6); 137 assertEqualMem(e + 36, "000001", 6); /* nlink */ 138 failure("file entries should not have rdev set (dev field was 0%o)", 139 dev); 140 assertEqualMem(e + 42, "000000", 6); /* rdev */ 141 t = from_octal(e + 48, 11); /* mtime */ 142 assert(t <= now); /* File wasn't created in future. */ 143 assert(t >= now - 2); /* File was created w/in last 2 secs. */ 144 assertEqualMem(e + 59, "000005", 6); /* Name size */ 145 assertEqualMem(e + 65, "00000000012", 11); /* File size */ 146 assertEqualMem(e + 76, "file\0", 5); /* Name contents */ 147 assertEqualMem(e + 81, "1234567890", 10); /* File contents */ 148 e += 91; 149 150 /* "symlink" pointing to "file" */ 151 if (canSymlink()) { 152 assert(is_octal(e, 76)); /* Entire header is octal digits. */ 153 assertEqualMem(e + 0, "070707", 6); /* Magic */ 154 assertEqualInt(dev, from_octal(e + 6, 6)); /* dev */ 155 assert(ino != from_octal(e + 12, 6)); /* ino */ 156#if !defined(_WIN32) || defined(__CYGWIN__) 157 /* On Windows, symbolic link and group members bits and 158 * others bits do not work. */ 159 assertEqualMem(e + 18, "120777", 6); /* Mode */ 160#endif 161 assertEqualInt(from_octal(e + 24, 6), uid); /* uid */ 162 assertEqualInt(gid, from_octal(e + 30, 6)); /* gid */ 163 assertEqualMem(e + 36, "000001", 6); /* nlink */ 164 failure("file entries should have rdev == 0 (dev was 0%o)", 165 from_octal(e + 6, 6)); 166 assertEqualMem(e + 42, "000000", 6); /* rdev */ 167 t = from_octal(e + 48, 11); /* mtime */ 168 assert(t <= now); /* File wasn't created in future. */ 169 assert(t >= now - 2); /* File was created w/in last 2 secs. */ 170 assertEqualMem(e + 59, "000010", 6); /* Name size */ 171 assertEqualMem(e + 65, "00000000004", 11); /* File size */ 172 assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */ 173 assertEqualMem(e + 84, "file", 4); /* Symlink target. */ 174 e += 88; 175 } 176 177 /* "dir" */ 178 assert(is_octal(e, 76)); 179 assertEqualMem(e + 0, "070707", 6); /* Magic */ 180 /* Dev should be same as first entry. */ 181 assert(is_octal(e + 6, 6)); /* dev */ 182 assertEqualInt(dev, from_octal(e + 6, 6)); 183 /* Ino must be different from first entry. */ 184 assert(is_octal(e + 12, 6)); /* ino */ 185 assert(ino != from_octal(e + 12, 6)); 186#if defined(_WIN32) && !defined(__CYGWIN__) 187 /* Group members bits and others bits do not work. */ 188 assertEqualMem(e + 18, "040777", 6); /* Mode */ 189#else 190 /* Accept 042775 to accommodate systems where sgid bit propagates. */ 191 if (memcmp(e + 18, "042775", 6) != 0) 192 assertEqualMem(e + 18, "040775", 6); /* Mode */ 193#endif 194 assertEqualInt(uid, from_octal(e + 24, 6)); /* uid */ 195 /* Gid should be same as first entry. */ 196 assert(is_octal(e + 30, 6)); /* gid */ 197 assertEqualInt(gid, from_octal(e + 30, 6)); 198 199#if !defined(_WIN32) || defined(__CYGWIN__) 200 assertEqualInt(nlinks("dir"), from_octal(e + 36, 6)); /* Nlink */ 201#endif 202 203 t = from_octal(e + 48, 11); /* mtime */ 204 assert(t <= now); /* File wasn't created in future. */ 205 assert(t >= now - 2); /* File was created w/in last 2 secs. */ 206 assertEqualMem(e + 59, "000004", 6); /* Name size */ 207 assertEqualMem(e + 65, "00000000000", 11); /* File size */ 208 assertEqualMem(e + 76, "dir\0", 4); /* name */ 209 e += 80; 210 211 /* TODO: Verify other types of entries. */ 212 213 /* Last entry is end-of-archive marker. */ 214 assert(is_octal(e, 76)); 215 assertEqualMem(e + 0, "070707", 6); /* Magic */ 216 assertEqualMem(e + 6, "000000", 6); /* dev */ 217 assertEqualMem(e + 12, "000000", 6); /* ino */ 218 assertEqualMem(e + 18, "000000", 6); /* Mode */ 219 assertEqualMem(e + 24, "000000", 6); /* uid */ 220 assertEqualMem(e + 30, "000000", 6); /* gid */ 221 assertEqualMem(e + 36, "000001", 6); /* Nlink */ 222 assertEqualMem(e + 42, "000000", 6); /* rdev */ 223 assertEqualMem(e + 48, "00000000000", 11); /* mtime */ 224 assertEqualMem(e + 59, "000013", 6); /* Name size */ 225 assertEqualMem(e + 65, "00000000000", 11); /* File size */ 226 assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */ 227 228 free(p); 229} 230