summaryrefslogtreecommitdiff
path: root/test/libapt/fileutl_test.cc
blob: 1d1a1a1b8ddcc39fd81b270ec549b0f3ffccf64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <config.h>

#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>

#include <string>
#include <vector>
#include <stdlib.h>
#include <sys/stat.h>

#include "assert.h"

// regression test for permission bug LP: #1304657
static bool
TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission)
{
   FileFd f;
   struct stat buf;
   static const char* fname = "test.txt";

   umask(a_umask);
   f.Open(fname, FileFd::ReadWrite|FileFd::Atomic);
   f.Close();
   if (stat(fname, &buf) < 0)
   {
      _error->Errno("stat", "failed to stat");
      _error->DumpErrors();
      return false;
   }
   unlink(fname);
   equals(buf.st_mode & 0777, ExpectedFilePermission);
   return true;
}

int main()
{
   std::vector<std::string> files;

   if (TestFileFdOpenPermissions(0002, 0664) == false ||
       TestFileFdOpenPermissions(0022, 0644) == false ||
       TestFileFdOpenPermissions(0077, 0600) == false ||
       TestFileFdOpenPermissions(0026, 0640) == false)
   {
      return 1;
   }

   // normal match
   files = Glob("*.lst");
   if (files.size() != 1)
   {
      _error->DumpErrors();
      return 1;
   }

   // not there
   files = Glob("xxxyyyzzz");
   if (files.size() != 0 || _error->PendingError())
   {
      _error->DumpErrors();
      return 1;
   }

   // many matches (number is a bit random)
   files = Glob("*.cc");
   if (files.size() < 10)
   {
      _error->DumpErrors();
      return 1;
   }

   // GetTempDir()
   unsetenv("TMPDIR");
   equals(GetTempDir(), "/tmp");

   setenv("TMPDIR", "", 1);
   equals(GetTempDir(), "/tmp");

   setenv("TMPDIR", "/not-there-no-really-not", 1);
   equals(GetTempDir(), "/tmp");

   setenv("TMPDIR", "/usr", 1);
   equals(GetTempDir(), "/usr");

   return 0;
}