1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.servlet;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.util.Arrays;
35
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39
40 import waffle.mock.MockWindowsSecurityContext;
41
42
43
44
45 class WindowsPrincipalTest {
46
47
48 private WindowsPrincipal windowsPrincipal;
49
50
51
52
53 @BeforeEach
54 void setUp() {
55 final MockWindowsSecurityContext ctx = new MockWindowsSecurityContext("Administrator");
56 this.windowsPrincipal = new WindowsPrincipal(ctx.getIdentity());
57 }
58
59
60
61
62
63
64
65
66
67 @Test
68 void testIsSerializable() throws IOException, ClassNotFoundException {
69
70 final ByteArrayOutputStream out = new ByteArrayOutputStream();
71 try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
72 oos.writeObject(this.windowsPrincipal);
73 }
74 assertThat(out.toByteArray()).isNotEmpty();
75
76 final InputStream in = new ByteArrayInputStream(out.toByteArray());
77 final ObjectInputStream ois = new ObjectInputStream(in);
78 final WindowsPrincipal copy = (WindowsPrincipal) ois.readObject();
79
80 Assertions.assertEquals(this.windowsPrincipal.getName(), copy.getName());
81 Assertions.assertEquals(this.windowsPrincipal.getRolesString(), copy.getRolesString());
82 Assertions.assertEquals(this.windowsPrincipal.getSidString(), copy.getSidString());
83 Assertions.assertTrue(Boolean.valueOf(Arrays.equals(this.windowsPrincipal.getSid(), copy.getSid())));
84 }
85
86
87
88
89 @Test
90 void testHasRole() {
91 Assertions.assertTrue(this.windowsPrincipal.hasRole("Administrator"));
92 Assertions.assertTrue(this.windowsPrincipal.hasRole("Users"));
93 Assertions.assertTrue(this.windowsPrincipal.hasRole("Everyone"));
94 Assertions.assertFalse(this.windowsPrincipal.hasRole("RoleDoesNotExist"));
95 }
96 }