View Javadoc
1   /*
2    * Copyright 2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.is;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.api.condition.DisabledOnOs;
15  import org.junit.jupiter.api.condition.OS;
16  
17  @DisabledOnOs(OS.WINDOWS)
18  class UserGroupInfoTest {
19  
20      @Test
21      void testGetUser() {
22          List<String> checkedUid = new ArrayList<>();
23          List<String> passwd = ExecutingCommand.runNative("getent passwd");
24          passwd.stream().map(s -> s.split(":")).filter(arr -> arr.length > 2).forEach(split -> {
25              String uid = split[2];
26              String userName = split[0];
27              if (!checkedUid.contains(uid)) {
28                  assertThat("Incorrect result for USER_ID_MAP", UserGroupInfo.getUser(uid), is(userName));
29                  checkedUid.add(uid);
30              }
31          });
32      }
33  
34      @Test
35      void testGetGroupName() {
36          List<String> checkedGid = new ArrayList<>();
37          List<String> group = ExecutingCommand.runNative("getent group");
38          group.stream().map(s -> s.split(":")).filter(arr -> arr.length > 2).forEach(split -> {
39              String gid = split[2];
40              String groupName = split[0];
41              if (!checkedGid.contains(gid)) {
42                  assertThat("Incorrect result for GROUPS_ID_MAP", UserGroupInfo.getGroupName(gid), is(groupName));
43                  checkedGid.add(gid);
44              }
45          });
46      }
47  }