3 // Copyright (C) 2005, 2007 Daniel Burrows
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.
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.
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.
20 #include <cppunit/extensions/HelperMacros.h>
22 #include <generic/util/temp.h>
23 #include <generic/util/util.h>
29 #include <sys/types.h>
32 #define ASSERT_STAT(s, buf) \
35 if(stat((s), (buf)) != 0) \
36 CPPUNIT_FAIL(ssprintf("Can't stat %s: %s", (s), sstrerror(errno).c_str())); \
40 class TempTest : public CppUnit::TestFixture
42 CPPUNIT_TEST_SUITE(TempTest);
44 CPPUNIT_TEST(testTempDir);
45 CPPUNIT_TEST(testTempName);
47 CPPUNIT_TEST_SUITE_END();
52 std::string d1name, d2name, d3name, d4name;
56 temp::dir d2("tmp", d1);
58 temp::dir d4("tmp", true);
60 d1name = d1.get_name();
61 d2name = d2.get_name();
62 d3name = d3.get_name();
63 d4name = d4.get_name();
65 int result = access(d1name.c_str(), F_OK);
67 CPPUNIT_FAIL(ssprintf("Unable to access %s: %s",
68 d1name.c_str(), sstrerror(errno).c_str()));
70 result = access(d2.get_name().c_str(), F_OK);
72 CPPUNIT_FAIL(ssprintf("Unable to access %s: %s",
73 d2name.c_str(), sstrerror(errno).c_str()));
75 char *d1namecopy = strdup(d1name.c_str());
76 std::string base1 = basename(d1namecopy);
80 CPPUNIT_ASSERT_EQUAL(std::string("tmp"), std::string(base1, 0, base1.size()-6));
82 char *d2namecopy = strdup(d2name.c_str());
83 std::string base2 = basename(d2namecopy);
87 CPPUNIT_ASSERT_EQUAL(std::string("tmp"), std::string(base2, 0, base2.size()-6));
91 ASSERT_STAT(d1.get_name().c_str(), &stbuf);
92 CPPUNIT_ASSERT(S_ISDIR(stbuf.st_mode));
94 ASSERT_STAT(d2.get_name().c_str(), &stbuf);
95 CPPUNIT_ASSERT(S_ISDIR(stbuf.st_mode));
97 close(creat((d3name + "/" + "foo").c_str(), 0644));
98 close(creat((d4name + "/" + "foo").c_str(), 0644));
101 int result = access(d1name.c_str(), F_OK);
102 CPPUNIT_ASSERT(result != 0);
103 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
105 result = access(d2name.c_str(), F_OK);
106 CPPUNIT_ASSERT(result != 0);
107 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
109 result = access(d3name.c_str(), F_OK);
110 CPPUNIT_ASSERT(result == 0);
112 CPPUNIT_ASSERT(aptitude::util::recursive_remdir(d3name));
114 result = access(d3name.c_str(), F_OK);
115 CPPUNIT_ASSERT(result != 0);
116 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
118 result = access(d4name.c_str(), F_OK);
119 CPPUNIT_ASSERT(result != 0);
120 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
131 temp::name f(d, "tmpf");
133 dname = d.get_name();
134 fname = f.get_name();
136 char *fnamecopy = strdup(fname.c_str());
137 std::string base = basename(fnamecopy);
141 CPPUNIT_ASSERT_EQUAL(std::string("tmpf"), std::string(base, 0, base.size()-6));
143 CPPUNIT_ASSERT(access(f.get_name().c_str(), F_OK) != 0);
144 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
147 int fd = open(fname.c_str(), O_EXCL | O_CREAT | O_WRONLY, 0700);
149 CPPUNIT_FAIL(ssprintf("Can't create \"%s\": %s",
151 sstrerror(errno).c_str()));
153 CPPUNIT_ASSERT_EQUAL(0, access(f.get_name().c_str(), F_OK));
158 CPPUNIT_ASSERT(access(fname.c_str(), F_OK) != 0);
159 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
161 CPPUNIT_ASSERT(access(dname.c_str(), F_OK) != 0);
162 CPPUNIT_ASSERT_EQUAL(ENOENT, errno);
166 CPPUNIT_TEST_SUITE_REGISTRATION(TempTest);