test_option_L_upper.c revision 228753
1139823Simp/*-
21541Srgrimes * Copyright (c) 2003-2007 Tim Kientzle
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes *
141541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
151541Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161541Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171541Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
181541Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191541Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201541Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211541Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221541Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231541Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241541Srgrimes */
251541Srgrimes#include "test.h"
261541Srgrimes__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_L.c,v 1.2 2008/08/24 06:21:00 kientzle Exp $");
271541Srgrimes
281541Srgrimes/* This is a little pointless, as Windows doesn't support symlinks
2910939Swollman * (except for the seriously crippled CreateSymbolicLink API) so these
3050477Speter * tests won't run on Windows. */
311541Srgrimes#if defined(_WIN32) && !defined(__CYGWIN__)
321541Srgrimes#define CAT "type"
332169Spaul#else
342169Spaul#define CAT "cat"
352169Spaul#endif
367280Swollman
3784102SjlemonDEFINE_TEST(test_option_L_upper)
387280Swollman{
391541Srgrimes	FILE *filelist;
401541Srgrimes	int r;
4121666Swollman
421541Srgrimes	if (!canSymlink()) {
431541Srgrimes		skipping("Symlink tests");
441541Srgrimes		return;
451541Srgrimes	}
461541Srgrimes
471541Srgrimes	filelist = fopen("filelist", "w");
481541Srgrimes
491541Srgrimes	/* Create a file and a symlink to the file. */
501541Srgrimes	assertMakeFile("file", 0644, "1234567890");
511541Srgrimes	fprintf(filelist, "file\n");
521541Srgrimes
531541Srgrimes	/* Symlink to above file. */
541541Srgrimes	assertMakeSymlink("symlink", "file");
5584102Sjlemon	fprintf(filelist, "symlink\n");
5684102Sjlemon
571541Srgrimes	fclose(filelist);
581541Srgrimes
591541Srgrimes	r = systemf(CAT " filelist | %s -pd copy >copy.out 2>copy.err", testprog);
601541Srgrimes	assertEqualInt(r, 0);
611541Srgrimes	assertTextFileContents("1 block\n", "copy.err");
621541Srgrimes
631541Srgrimes	failure("Regular -p without -L should preserve symlinks.");
641541Srgrimes	assertIsSymlink("copy/symlink", NULL);
651541Srgrimes
661541Srgrimes	r = systemf(CAT " filelist | %s -pd -L copy-L >copy-L.out 2>copy-L.err", testprog);
671541Srgrimes	assertEqualInt(r, 0);
681541Srgrimes	assertEmptyFile("copy-L.out");
691541Srgrimes	assertTextFileContents("1 block\n", "copy-L.err");
701541Srgrimes	failure("-pdL should dereference symlinks and turn them into files.");
711541Srgrimes	assertIsReg("copy-L/symlink", -1);
721541Srgrimes
731541Srgrimes	r = systemf(CAT " filelist | %s -o >archive.out 2>archive.err", testprog);
743865Sswallace	failure("Error invoking %s -o ", testprog);
753865Sswallace	assertEqualInt(r, 0);
761541Srgrimes	assertTextFileContents("1 block\n", "archive.err");
771541Srgrimes
781541Srgrimes	assertMakeDir("unpack", 0755);
791541Srgrimes	assertChdir("unpack");
808876Srgrimes	r = systemf(CAT " ../archive.out | %s -i >unpack.out 2>unpack.err", testprog);
8155205Speter	failure("Error invoking %s -i", testprog);
827090Sbde	assertEqualInt(r, 0);
831541Srgrimes	assertTextFileContents("1 block\n", "unpack.err");
84133874Srwatson	assertChdir("..");
8584102Sjlemon
8684102Sjlemon	assertIsSymlink("unpack/symlink", NULL);
8784102Sjlemon
8884102Sjlemon	r = systemf(CAT " filelist | %s -oL >archive-L.out 2>archive-L.err", testprog);
8984102Sjlemon	failure("Error invoking %s -oL", testprog);
9084102Sjlemon	assertEqualInt(r, 0);
9184102Sjlemon	assertTextFileContents("1 block\n", "archive-L.err");
9284102Sjlemon
9384102Sjlemon	assertMakeDir("unpack-L", 0755);
9484102Sjlemon	assertChdir("unpack-L");
9584102Sjlemon	r = systemf(CAT " ../archive-L.out | %s -i >unpack-L.out 2>unpack-L.err", testprog);
9684102Sjlemon	failure("Error invoking %s -i < archive-L.out", testprog);
97162615Sbms	assertEqualInt(r, 0);
98162625Sbms	assertTextFileContents("1 block\n", "unpack-L.err");
99162615Sbms	assertChdir("..");
100162615Sbms	assertIsReg("unpack-L/symlink", -1);
101162615Sbms}
102162615Sbms