summaryrefslogtreecommitdiffstats
path: root/tests/test_identity.cpp
blob: 340a8a4fd1d4fb57e0efdbff6423467ac6978c84 (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

#include <pwd.h>
#include <tqstring.h>
#include "core/polkit-tqt-identity.h"

#define TEST_PASSED 0
#define TEST_FAILED 1

using namespace PolkitTQt;

int main(void)
{
  int test_result = TEST_PASSED;

  // Get real name and id of current user and group
  struct passwd *userinfo = getpwuid(getuid());
  TQString userName = userinfo->pw_name;
  unsigned int userId = userinfo->pw_uid;
  unsigned int groupId = userinfo->pw_gid;

  // Create generic Identity from UnixUser via string representation
  UnixUserIdentity user(userName);
  if (!user.identity())
  {
    return TEST_FAILED;
  }
  Identity id = Identity::fromString(user.toString());
  if (static_cast<UnixUserIdentity*>(&id)->uid() != userId)
  {
    return TEST_FAILED;
  }

  UnixGroupIdentity group(groupId);
  if (!group.identity())
  {
    return TEST_FAILED;
  }
  id = Identity::fromString(group.toString());
  if (static_cast<UnixGroupIdentity*>(&id)->gid() != groupId)
  {
    return TEST_FAILED;
  }

  // Test setting gid to another value
  group.setGid(9999U);
  id = Identity::fromString(group.toString());
  if (static_cast<UnixGroupIdentity*>(&id)->gid() != 9999U)
  {
    return TEST_FAILED;
  }

  return TEST_PASSED;
}