Maemo 0.4.11.11-1
[aptitude] / tests / test_temp.cc
1 // test_temp.cc
2 //
3 //   Copyright (C) 2005, 2007 Daniel Burrows
4 //
5 //   This program is free software; you can redistribute it and/or
6 //   modify it under the terms of the GNU General Public License as
7 //   published by the Free Software Foundation; either version 2 of
8 //   the License, or (at your option) any later version.
9 //
10 //   This program is distributed in the hope that it will be useful,
11 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //   General Public License for more details.
14 //
15 //   You should have received a copy of the GNU General Public License
16 //   along with this program; see the file COPYING.  If not, write to
17 //   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 //   Boston, MA 02111-1307, USA.
19
20 #include <cppunit/extensions/HelperMacros.h>
21
22 #include <generic/util/temp.h>
23 #include <generic/util/util.h>
24
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #define ASSERT_STAT(s, buf) \
33   do \
34   { \
35     if(stat((s), (buf)) != 0) \
36        CPPUNIT_FAIL(ssprintf("Can't stat %s: %s", (s), sstrerror(errno).c_str())); \
37   } while(0)
38                                                     
39
40 class TempTest : public CppUnit::TestFixture
41 {
42   CPPUNIT_TEST_SUITE(TempTest);
43
44   CPPUNIT_TEST(testTempDir);
45   CPPUNIT_TEST(testTempName);
46
47   CPPUNIT_TEST_SUITE_END();
48
49 public:
50   void testTempDir()
51   {
52     std::string d1name, d2name, d3name, d4name;
53
54     {
55       temp::dir d1("tmp");
56       temp::dir d2("tmp", d1);
57       temp::dir d3("tmp");
58       temp::dir d4("tmp", true);
59
60       d1name = d1.get_name();
61       d2name = d2.get_name();
62       d3name = d3.get_name();
63       d4name = d4.get_name();
64
65       int result = access(d1name.c_str(), F_OK);
66       if(result != 0)
67         CPPUNIT_FAIL(ssprintf("Unable to access %s: %s",
68                               d1name.c_str(), sstrerror(errno).c_str()));
69
70       result = access(d2.get_name().c_str(), F_OK);
71       if(result != 0)
72         CPPUNIT_FAIL(ssprintf("Unable to access %s: %s",
73                               d2name.c_str(), sstrerror(errno).c_str()));
74
75       char *d1namecopy = strdup(d1name.c_str());
76       std::string base1 = basename(d1namecopy);
77       free(d1namecopy);
78       d1namecopy = NULL;
79
80       CPPUNIT_ASSERT_EQUAL(std::string("tmp"), std::string(base1, 0, base1.size()-6));
81
82       char *d2namecopy = strdup(d2name.c_str());
83       std::string base2 = basename(d2namecopy);
84       free(d2namecopy);
85       d2namecopy = NULL;
86
87       CPPUNIT_ASSERT_EQUAL(std::string("tmp"), std::string(base2, 0, base2.size()-6));
88
89       struct stat stbuf;
90
91       ASSERT_STAT(d1.get_name().c_str(), &stbuf);
92       CPPUNIT_ASSERT(S_ISDIR(stbuf.st_mode));
93
94       ASSERT_STAT(d2.get_name().c_str(), &stbuf);
95       CPPUNIT_ASSERT(S_ISDIR(stbuf.st_mode));
96
97       close(creat((d3name + "/" + "foo").c_str(), 0644));
98       close(creat((d4name + "/" + "foo").c_str(), 0644));
99     }
100
101     int result = access(d1name.c_str(), F_OK);
102     CPPUNIT_ASSERT(result != 0);
103     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
104
105     result = access(d2name.c_str(), F_OK);
106     CPPUNIT_ASSERT(result != 0);
107     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
108
109     result = access(d3name.c_str(), F_OK);
110     CPPUNIT_ASSERT(result == 0);
111
112     CPPUNIT_ASSERT(aptitude::util::recursive_remdir(d3name));
113
114     result = access(d3name.c_str(), F_OK);
115     CPPUNIT_ASSERT(result != 0);
116     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
117
118     result = access(d4name.c_str(), F_OK);
119     CPPUNIT_ASSERT(result != 0);
120     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
121   }
122
123   void testTempName()
124   {
125     std::string dname;
126     std::string fname;
127
128     {
129       temp::dir d("tmp");
130
131       temp::name f(d, "tmpf");
132
133       dname = d.get_name();
134       fname = f.get_name();
135
136       char *fnamecopy = strdup(fname.c_str());
137       std::string base = basename(fnamecopy);
138       free(fnamecopy);
139       fnamecopy = NULL;
140
141       CPPUNIT_ASSERT_EQUAL(std::string("tmpf"), std::string(base, 0, base.size()-6));
142
143       CPPUNIT_ASSERT(access(f.get_name().c_str(), F_OK) != 0);
144       CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
145
146       // Create it.
147       int fd = open(fname.c_str(), O_EXCL | O_CREAT | O_WRONLY, 0700);
148       if(fd == -1)
149         CPPUNIT_FAIL(ssprintf("Can't create \"%s\": %s",
150                               fname.c_str(),
151                               sstrerror(errno).c_str()));
152
153       CPPUNIT_ASSERT_EQUAL(0, access(f.get_name().c_str(), F_OK));
154
155       close(fd);
156     }
157
158     CPPUNIT_ASSERT(access(fname.c_str(), F_OK) != 0);
159     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
160
161     CPPUNIT_ASSERT(access(dname.c_str(), F_OK) != 0);
162     CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
163   }
164 };
165
166 CPPUNIT_TEST_SUITE_REGISTRATION(TempTest);