]> git.saurik.com Git - apt.git/blame - test/libapt/fileutl_test.cc
extend FileFd test behond basic permission tests
[apt.git] / test / libapt / fileutl_test.cc
CommitLineData
453b82a3
DK
1#include <config.h>
2
488011fa
MV
3#include <apt-pkg/error.h>
4#include <apt-pkg/fileutl.h>
f7feb041 5#include <apt-pkg/aptconfiguration.h>
488011fa 6
488011fa
MV
7#include <string>
8#include <vector>
488011fa 9#include <stdlib.h>
f22b65b4 10#include <sys/stat.h>
f7feb041 11#include <string.h>
488011fa 12
453b82a3 13#include "assert.h"
488011fa 14
f7feb041
DK
15static void assertStringEquals(char const * const expect, char const * const got, unsigned long const line) {
16 if (strncmp(expect, got, strlen(expect)) == 0)
17 return;
18 OutputAssertEqual(expect, "==", got, line);
19}
20#define strequals(x,y) assertStringEquals(x, y, __LINE__)
21
f22b65b4 22static bool
f7feb041 23TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission, unsigned int const filemode, APT::Configuration::Compressor const &compressor)
f22b65b4
MV
24{
25 FileFd f;
26 struct stat buf;
27 static const char* fname = "test.txt";
28
29 umask(a_umask);
f7feb041
DK
30 equals(f.Open(fname, filemode, compressor), true);
31 equals(f.IsOpen(), true);
32 equals(f.Failed(), false);
33 equals(umask(a_umask), a_umask);
34
35 std::string test = "This is a test!";
36 equals(f.Write(test.c_str(), test.size()), true);
37 equals(f.IsOpen(), true);
38 equals(f.Failed(), false);
39
f22b65b4 40 f.Close();
f7feb041
DK
41 equals(f.IsOpen(), false);
42 equals(f.Failed(), false);
43
44 equals(f.Open(fname, FileFd::ReadOnly, compressor), true);
45 equalsNot(f.FileSize(), 0);
46 equals(f.IsOpen(), true);
47 equals(f.Failed(), false);
48
49 char readback[20];
50 {
51 char const * const expect = "This";
52 equals(f.Read(readback, strlen(expect)), true);
53 equals(f.Failed(), false);
54 equals(f.Eof(), false);
55 strequals(expect, readback);
56 equals(strlen(expect), f.Tell());
57 }
58 {
59 char const * const expect = "test!";
60 equals(f.Skip((test.size() - f.Tell()) - strlen(expect)), true);
61 equals(f.Read(readback, strlen(expect)), true);
62 equals(f.Failed(), false);
63 equals(f.Eof(), false);
64 strequals(expect, readback);
65 equals(test.size(), f.Tell());
66 }
67
68 equals(f.Seek(0), true);
69 equals(f.Read(readback, 20, true), true);
70 equals(f.Failed(), false);
71 equals(f.Eof(), true);
72 equals(test, readback);
73 equals(test.size(), strlen(readback));
74 equals(f.Size(), f.Tell());
75
76 equals(f.Seek(0), true);
77 f.ReadLine(readback, 20);
78 equals(f.Failed(), false);
79 equals(f.Eof(), true);
80 equals(test, readback);
81 equals(test.size(), strlen(readback));
82 equals(f.Size(), f.Tell());
83
84 f.Close();
85 equals(f.IsOpen(), false);
86 equals(f.Failed(), false);
87
88 // regression test for permission bug LP: #1304657
f22b65b4
MV
89 if (stat(fname, &buf) < 0)
90 {
91 _error->Errno("stat", "failed to stat");
f22b65b4
MV
92 return false;
93 }
94 unlink(fname);
95 equals(buf.st_mode & 0777, ExpectedFilePermission);
96 return true;
97}
98
f7feb041 99static bool TestFileFd(unsigned int const filemode)
488011fa 100{
f7feb041
DK
101 std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
102 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin(); c != compressors.end(); ++c)
103 {
104 if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
105 (c->Name.empty() != true && c->Binary.empty() != true))
106 continue;
107 if (TestFileFd(0002, 0664, filemode, *c) == false ||
108 TestFileFd(0022, 0644, filemode, *c) == false ||
109 TestFileFd(0077, 0600, filemode, *c) == false ||
110 TestFileFd(0026, 0640, filemode, *c) == false)
111 {
112 _error->DumpErrors();
113 return false;
114 }
115 }
116 return true;
117}
488011fa 118
f7feb041
DK
119int main()
120{
121 if (TestFileFd(FileFd::WriteOnly | FileFd::Create) == false ||
122 TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty) == false ||
123 TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive) == false ||
124 TestFileFd(FileFd::WriteOnly | FileFd::Atomic) == false ||
125 TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic) == false ||
126 // short-hands for ReadWrite with these modes
127 TestFileFd(FileFd::WriteEmpty) == false ||
128 TestFileFd(FileFd::WriteAny) == false ||
129 TestFileFd(FileFd::WriteTemp) == false ||
130 TestFileFd(FileFd::WriteAtomic) == false)
f22b65b4
MV
131 {
132 return 1;
133 }
134
f7feb041 135 std::vector<std::string> files;
488011fa
MV
136 // normal match
137 files = Glob("*.lst");
138 if (files.size() != 1)
139 {
140 _error->DumpErrors();
141 return 1;
142 }
143
144 // not there
145 files = Glob("xxxyyyzzz");
146 if (files.size() != 0 || _error->PendingError())
147 {
148 _error->DumpErrors();
149 return 1;
150 }
151
152 // many matches (number is a bit random)
153 files = Glob("*.cc");
154 if (files.size() < 10)
155 {
156 _error->DumpErrors();
157 return 1;
158 }
159
a077861a
MV
160 // GetTempDir()
161 unsetenv("TMPDIR");
162 equals(GetTempDir(), "/tmp");
163
164 setenv("TMPDIR", "", 1);
165 equals(GetTempDir(), "/tmp");
166
167 setenv("TMPDIR", "/not-there-no-really-not", 1);
168 equals(GetTempDir(), "/tmp");
169
170 setenv("TMPDIR", "/usr", 1);
171 equals(GetTempDir(), "/usr");
172
488011fa
MV
173 return 0;
174}