fileutl_test.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <config.h>
  2. #include <apt-pkg/error.h>
  3. #include <apt-pkg/fileutl.h>
  4. #include <string>
  5. #include <vector>
  6. #include <stdlib.h>
  7. #include <sys/stat.h>
  8. #include "assert.h"
  9. // regression test for permission bug LP: #1304657
  10. static bool
  11. TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission)
  12. {
  13. FileFd f;
  14. struct stat buf;
  15. static const char* fname = "test.txt";
  16. umask(a_umask);
  17. f.Open(fname, FileFd::ReadWrite|FileFd::Atomic);
  18. f.Close();
  19. if (stat(fname, &buf) < 0)
  20. {
  21. _error->Errno("stat", "failed to stat");
  22. _error->DumpErrors();
  23. return false;
  24. }
  25. unlink(fname);
  26. equals(buf.st_mode & 0777, ExpectedFilePermission);
  27. return true;
  28. }
  29. int main()
  30. {
  31. std::vector<std::string> files;
  32. if (TestFileFdOpenPermissions(0002, 0664) == false ||
  33. TestFileFdOpenPermissions(0022, 0644) == false ||
  34. TestFileFdOpenPermissions(0077, 0600) == false ||
  35. TestFileFdOpenPermissions(0026, 0640) == false)
  36. {
  37. return 1;
  38. }
  39. // normal match
  40. files = Glob("*.lst");
  41. if (files.size() != 1)
  42. {
  43. _error->DumpErrors();
  44. return 1;
  45. }
  46. // not there
  47. files = Glob("xxxyyyzzz");
  48. if (files.size() != 0 || _error->PendingError())
  49. {
  50. _error->DumpErrors();
  51. return 1;
  52. }
  53. // many matches (number is a bit random)
  54. files = Glob("*.cc");
  55. if (files.size() < 10)
  56. {
  57. _error->DumpErrors();
  58. return 1;
  59. }
  60. // GetTempDir()
  61. unsetenv("TMPDIR");
  62. equals(GetTempDir(), "/tmp");
  63. setenv("TMPDIR", "", 1);
  64. equals(GetTempDir(), "/tmp");
  65. setenv("TMPDIR", "/not-there-no-really-not", 1);
  66. equals(GetTempDir(), "/tmp");
  67. setenv("TMPDIR", "/usr", 1);
  68. equals(GetTempDir(), "/usr");
  69. return 0;
  70. }