1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010 Tim Kientzle 5 * All rights reserved. 6 */ 7 #include "test.h" 8 9 DEFINE_TEST(test_option_C_upper) 10 { 11 int r; 12 13 assertMakeDir("d1", 0755); 14 assertMakeDir("d2", 0755); 15 assertMakeFile("d1/file1", 0644, "d1/file1"); 16 assertMakeFile("d1/file2", 0644, "d1/file2"); 17 assertMakeFile("d2/file1", 0644, "d2/file1"); 18 assertMakeFile("d2/file2", 0644, "d2/file2"); 19 20 /* 21 * Test 1: Basic use of -C 22 */ 23 assertMakeDir("test1", 0755); 24 assertChdir("test1"); 25 assertEqualInt(0, systemf("%s -cf archive.tar -C ../d1 file1 -C ../d2 file2", testprog)); 26 assertEqualInt(0, 27 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 28 assertFileContents("d1/file1", 8, "file1"); 29 assertFileContents("d2/file2", 8, "file2"); 30 assertEmptyFile("test.out"); 31 assertEmptyFile("test.err"); 32 assertChdir(".."); 33 34 35 /* 36 * Test 2: Multiple -C 37 */ 38 assertMakeDir("test2", 0755); 39 assertChdir("test2"); 40 assertEqualInt(0, systemf("%s -cf archive.tar -C .. -C d1 file1 -C .. -C d2 file2", testprog)); 41 assertEqualInt(0, 42 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 43 assertFileContents("d1/file1", 8, "file1"); 44 assertFileContents("d2/file2", 8, "file2"); 45 assertEmptyFile("test.out"); 46 assertEmptyFile("test.err"); 47 assertChdir(".."); 48 49 /* 50 * Test 3: -C fail 51 */ 52 assertMakeDir("test3", 0755); 53 assertChdir("test3"); 54 r = systemf("%s -cf archive.tar -C ../XXX file1 -C ../d2 file2 2>write.err", testprog); 55 assert(r != 0); 56 assertNonEmptyFile("write.err"); 57 assertEqualInt(0, 58 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 59 assertFileNotExists("file1"); 60 assertFileNotExists("file2"); 61 assertEmptyFile("test.out"); 62 assertEmptyFile("test.err"); 63 assertChdir(".."); 64 65 /* 66 * Test 4: Absolute -C 67 */ 68 assertMakeDir("test4", 0755); 69 assertChdir("test4"); 70 assertEqualInt(0, 71 systemf("%s -cf archive.tar -C %s/d1 file1", 72 testprog, testworkdir)); 73 assertEqualInt(0, 74 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 75 assertFileContents("d1/file1", 8, "file1"); 76 assertEmptyFile("test.out"); 77 assertEmptyFile("test.err"); 78 assertChdir(".."); 79 80 /* 81 * Test 5: Unnecessary -C ignored even if directory named doesn't exist 82 */ 83 assertMakeDir("test5", 0755); 84 assertChdir("test5"); 85 assertEqualInt(0, 86 systemf("%s -cf archive.tar -C XXX -C %s/d1 file1", 87 testprog, testworkdir)); 88 assertEqualInt(0, 89 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 90 assertFileContents("d1/file1", 8, "file1"); 91 assertEmptyFile("test.out"); 92 assertEmptyFile("test.err"); 93 assertChdir(".."); 94 95 /* 96 * Test 6: Necessary -C not ignored if directory doesn't exist 97 */ 98 assertMakeDir("test6", 0755); 99 assertChdir("test6"); 100 r = systemf("%s -cf archive.tar -C XXX -C ../d1 file1 2>write.err", 101 testprog); 102 assert(r != 0); 103 assertNonEmptyFile("write.err"); 104 assertEqualInt(0, 105 systemf("%s -xf archive.tar >test.out 2>test.err", testprog)); 106 assertEmptyFile("test.out"); 107 assertEmptyFile("test.err"); 108 assertChdir(".."); 109 110 /* 111 * Test 7: -C used without specifying directory 112 */ 113 assertMakeDir("test7", 0755); 114 assertChdir("test7"); 115 r = systemf("%s -cf archive.tar ../d1/file1 -C 2>write.err", testprog); 116 assert(r != 0); 117 assertNonEmptyFile("write.err"); 118 assertChdir(".."); 119 120 /* 121 * Test 8: -C used with meaningless option '' 122 */ 123 assertMakeDir("test8", 0755); 124 assertChdir("test8"); 125 r = systemf("%s -cf archive.tar ../d1/file1 -C \"\" 2>write.err", 126 testprog); 127 assert(r != 0); 128 assertNonEmptyFile("write.err"); 129 assertChdir(".."); 130 } 131