Path: blob/main/contrib/libarchive/cpio/test/test_basic.c
39534 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003-2007 Tim Kientzle4* All rights reserved.5*/6#include "test.h"78static void9verify_files(const char *msg)10{11/*12* Verify unpacked files.13*/1415/* Regular file with 2 links. */16failure("%s", msg);17assertIsReg("file", 0644);18failure("%s", msg);19assertFileSize("file", 10);20failure("%s", msg);21assertFileNLinks("file", 2);2223/* Another name for the same file. */24failure("%s", msg);25assertIsHardlink("linkfile", "file");2627/* Symlink */28if (canSymlink())29assertIsSymlink("symlink", "file", 0);3031/* Another file with 1 link and different permissions. */32failure("%s", msg);33assertIsReg("file2", 0777);34failure("%s", msg);35assertFileSize("file2", 10);36failure("%s", msg);37assertFileNLinks("file2", 1);3839/* dir */40assertIsDir("dir", 0775);41}4243static void44basic_cpio(const char *target,45const char *pack_options,46const char *unpack_options,47const char *se, const char *se2)48{49int r;5051if (!assertMakeDir(target, 0775))52return;5354/* Use the cpio program to create an archive. */55r = systemf("%s -R 1000:1000 -o %s < filelist >%s/archive 2>%s/pack.err",56testprog, pack_options, target, target);57failure("Error invoking %s -o %s", testprog, pack_options);58assertEqualInt(r, 0);5960assertChdir(target);6162/* Verify stderr. */63failure("Expected: %s, options=%s", se, pack_options);64assertTextFileContents(se, "pack.err");6566/*67* Use cpio to unpack the archive into another directory.68*/69r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",70testprog, unpack_options);71failure("Error invoking %s -i %s", testprog, unpack_options);72assertEqualInt(r, 0);7374/* Verify stderr. */75failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);76assertTextFileContents(se2, "unpack.err");7778verify_files(pack_options);7980assertChdir("..");81}8283static void84passthrough(const char *target)85{86int r;8788if (!assertMakeDir(target, 0775))89return;9091/*92* Use cpio passthrough mode to copy files to another directory.93*/94r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",95testprog, target, target, target);96failure("Error invoking %s -p", testprog);97assertEqualInt(r, 0);9899assertChdir(target);100101/* Verify stderr. */102failure("Error invoking %s -p in dir %s",103testprog, target);104assertTextFileContents("1 block\n", "stderr");105106verify_files("passthrough");107assertChdir("..");108}109110DEFINE_TEST(test_basic)111{112FILE *filelist;113const char *msg;114char result[1024];115116assertUmask(0);117118/*119* Create an assortment of files on disk.120*/121filelist = fopen("filelist", "w");122memset(result, 0, sizeof(result));123124/* File with 10 bytes content. */125assertMakeFile("file", 0644, "1234567890");126fprintf(filelist, "file\n");127if (is_LargeInode("file")) {128strncat(result,129"bsdcpio: file: large inode number truncated: ",130sizeof(result) - strlen(result) -1);131strncat(result,132strerror(ERANGE),133sizeof(result) - strlen(result) -1);134strncat(result,135"\n",136sizeof(result) - strlen(result) -1);137}138139/* hardlink to above file. */140assertMakeHardlink("linkfile", "file");141fprintf(filelist, "linkfile\n");142if (is_LargeInode("linkfile")) {143strncat(result,144"bsdcpio: linkfile: large inode number truncated: ",145sizeof(result) - strlen(result) -1);146strncat(result,147strerror(ERANGE),148sizeof(result) - strlen(result) -1);149strncat(result,150"\n",151sizeof(result) - strlen(result) -1);152}153154/* Symlink to above file. */155if (canSymlink()) {156assertMakeSymlink("symlink", "file", 0);157fprintf(filelist, "symlink\n");158if (is_LargeInode("symlink")) {159strncat(result,160"bsdcpio: symlink: large inode number truncated: ",161sizeof(result) - strlen(result) -1);162strncat(result,163strerror(ERANGE),164sizeof(result) - strlen(result) -1);165strncat(result,166"\n",167sizeof(result) - strlen(result) -1);168}169}170171/* Another file with different permissions. */172assertMakeFile("file2", 0777, "1234567890");173fprintf(filelist, "file2\n");174if (is_LargeInode("file2")) {175strncat(result,176"bsdcpio: file2: large inode number truncated: ",177sizeof(result) - strlen(result) -1);178strncat(result,179strerror(ERANGE),180sizeof(result) - strlen(result) -1);181strncat(result,182"\n",183sizeof(result) - strlen(result) -1);184}185186/* Directory. */187assertMakeDir("dir", 0775);188fprintf(filelist, "dir\n");189if (is_LargeInode("dir")) {190strncat(result,191"bsdcpio: dir: large inode number truncated: ",192sizeof(result) - strlen(result) -1);193strncat(result,194strerror(ERANGE),195sizeof(result) - strlen(result) -1);196strncat(result,197"\n",198sizeof(result) - strlen(result) -1);199}200strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);201202/* All done. */203fclose(filelist);204205assertUmask(022);206207/* Archive/dearchive with a variety of options. */208msg = canSymlink() ? "2 blocks\n" : "1 block\n";209basic_cpio("copy", "", "", msg, msg);210basic_cpio("copy_odc", "--format=odc", "", msg, msg);211basic_cpio("copy_newc", "-H newc", "", result, "2 blocks\n");212basic_cpio("copy_cpio", "-H odc", "", msg, msg);213msg = "1 block\n";214basic_cpio("copy_bin", "-H bin", "", msg, msg);215msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";216basic_cpio("copy_ustar", "-H ustar", "", msg, msg);217218/* Copy in one step using -p */219passthrough("passthrough");220}221222223