summaryrefslogtreecommitdiff
path: root/test/sanitizer_common/TestCases/Posix/getpw_getgr.cc
blob: b4bc9b43d0d04002adfdcfd89dd965dcfc1f8efb (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// RUN: %clangxx %s -o %t && %run %t
// UNSUPPORTED: ios

#include <assert.h>
#include <grp.h>
#include <memory>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

std::string any_group;
const int N = 123456;

void Check(const char *str) {
  if (!str)
    return;
  assert(strlen(str) != N);
}

void Check(const passwd *result) {
  Check(result->pw_name);
  Check(result->pw_passwd);
  assert(result->pw_uid != N);
  assert(result->pw_gid != N);
#if !defined(__ANDROID__)
  Check(result->pw_gecos);
#endif
  Check(result->pw_dir);

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  assert(result->pw_change != N);
  Check(result->pw_class);
  assert(result->pw_expire != N);
#endif

#if defined(__FreeBSD__)
  assert(result->pw_fields != N);
#endif

  // SunOS also has pw_age and pw_comment which are documented as unused.
}

void Check(const group *result) {
  Check(result->gr_name);
  Check(result->gr_passwd);
  assert(result->gr_gid != N);
  for (char **mem = result->gr_mem; *mem; ++mem)
    Check(*mem);
  if (any_group.empty())
    any_group = result->gr_name;
}

template <class T, class Fn, class... Args>
void test(Fn f, Args... args) {
  T *result = f(args...);
  Check(result);
}

template <class T, class Fn, class... Args>
void test_r(Fn f, Args... args) {
  T gr;
  T *result;
  char buff[10000];
  assert(!f(args..., &gr, buff, sizeof(buff), &result));
  Check(&gr);
  Check(result);
}

int main(int argc, const char *argv[]) {
  test<passwd>(&getpwuid, 0);
  test<passwd>(&getpwnam, "root");
  test<group>(&getgrgid, 0);
  test<group>(&getgrnam, any_group.c_str());

#if !defined(__ANDROID__)
  setpwent();
  test<passwd>(&getpwent);
  setgrent();
  test<group>(&getgrent);

#if !defined(__APPLE__)
  setpwent();
  test_r<passwd>(&getpwent_r);
  setgrent();
  test_r<group>(&getgrent_r);
#endif

  test_r<passwd>(&getpwuid_r, 0);
  test_r<passwd>(&getpwnam_r, "root");

  test_r<group>(&getgrgid_r, 0);
  test_r<group>(&getgrnam_r, any_group.c_str());

#if defined(__linux__)
  auto pwd_file = [] {
    return std::unique_ptr<FILE, decltype(&fclose)>(fopen("/etc/passwd", "r"),
                                                    &fclose);
  };
  auto gr_file = [] {
    return std::unique_ptr<FILE, decltype(&fclose)>(fopen("/etc/group", "r"),
                                                    &fclose);
  };
  test<passwd>(&fgetpwent, pwd_file().get());
  test<group>(&fgetgrent, gr_file().get());
  test_r<passwd>(&fgetpwent_r, pwd_file().get());
  test_r<group>(&fgetgrent_r, gr_file().get());
#endif

#endif // __ANDROID__

  return 0;
}