]> git.saurik.com Git - apt.git/blob - test/libapt/file-helpers.cc
Merge remote-tracking branch 'donkult/debian/sid' into debian/experimental
[apt.git] / test / libapt / file-helpers.cc
1 #include <apt-pkg/fileutl.h>
2
3 #include <string>
4
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9
10 #include <gtest/gtest.h>
11
12 #include "file-helpers.h"
13
14 void helperCreateTemporaryDirectory(std::string const &id, std::string &dir)
15 {
16 std::string const strtempdir = GetTempDir().append("/apt-tests-").append(id).append(".XXXXXX");
17 char * tempdir = strdup(strtempdir.c_str());
18 ASSERT_STREQ(tempdir, mkdtemp(tempdir));
19 dir = tempdir;
20 free(tempdir);
21 }
22 void helperRemoveDirectory(std::string const &dir)
23 {
24 // basic sanity check to avoid removing random directories based on earlier failures
25 if (dir.find("/apt-tests-") == std::string::npos || dir.find_first_of("*?") != std::string::npos)
26 FAIL() << "Directory '" << dir << "' seems invalid. It is therefore not removed!";
27 else
28 ASSERT_EQ(0, system(std::string("rm -rf ").append(dir).c_str()));
29 }
30 void helperCreateFile(std::string const &dir, std::string const &name)
31 {
32 std::string file = dir;
33 file.append("/");
34 file.append(name);
35 int const fd = creat(file.c_str(), 0600);
36 ASSERT_NE(-1, fd);
37 close(fd);
38 }
39 void helperCreateDirectory(std::string const &dir, std::string const &name)
40 {
41 std::string file = dir;
42 file.append("/");
43 file.append(name);
44 ASSERT_TRUE(CreateDirectory(dir, file));
45 }
46 void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname)
47 {
48 std::string target = dir;
49 target.append("/");
50 target.append(targetname);
51 std::string link = dir;
52 link.append("/");
53 link.append(linkname);
54 ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
55 }
56 void helperCreateTemporaryFile(std::string const &id, FileFd &fd, char * * const filename, char const * const content)
57 {
58 std::string name("apt-test-");
59 name.append(id).append(".XXXXXXXX");
60 char * tempfile = strdup(name.c_str());
61 int tempfile_fd = mkstemp(tempfile);
62 ASSERT_NE(-1, tempfile_fd);
63 if (filename != NULL)
64 *filename = tempfile;
65 else
66 {
67 unlink(tempfile);
68 free(tempfile);
69 }
70
71 EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite));
72 if (content != NULL)
73 {
74 ASSERT_TRUE(fd.Write(content, strlen(content)));
75 fd.Seek(0);
76 }
77 }