View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * The Class WindowsPrincipalTest.
44   */
45  class WindowsPrincipalTest {
46  
47      /** The windows principal. */
48      private WindowsPrincipal windowsPrincipal;
49  
50      /**
51       * Sets the up.
52       */
53      @BeforeEach
54      void setUp() {
55          final MockWindowsSecurityContext ctx = new MockWindowsSecurityContext("Administrator");
56          this.windowsPrincipal = new WindowsPrincipal(ctx.getIdentity());
57      }
58  
59      /**
60       * Test is serializable.
61       *
62       * @throws IOException
63       *             Signals that an I/O exception has occurred.
64       * @throws ClassNotFoundException
65       *             the class not found exception
66       */
67      @Test
68      void testIsSerializable() throws IOException, ClassNotFoundException {
69          // serialize
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          // deserialize
76          final InputStream in = new ByteArrayInputStream(out.toByteArray());
77          final ObjectInputStream ois = new ObjectInputStream(in);
78          final WindowsPrincipal copy = (WindowsPrincipal) ois.readObject();
79          // test
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       * Test has role.
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  }