View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.servlet.spi;
8   
9   import java.util.Arrays;
10  import java.util.List;
11  
12  import javax.servlet.http.HttpServletResponse;
13  
14  import mockit.Mocked;
15  import mockit.Verifications;
16  
17  import org.junit.jupiter.api.Assertions;
18  import org.junit.jupiter.api.BeforeEach;
19  import org.junit.jupiter.api.Test;
20  
21  import waffle.windows.auth.IWindowsAuthProvider;
22  
23  /**
24   * Tests for {@link NegotiateSecurityFilterProvider}.
25   */
26  class NegotiateSecurityFilterProviderTest {
27  
28      /** The auth provider. */
29      @Mocked
30      private IWindowsAuthProvider auth;
31  
32      /** The response. */
33      @Mocked
34      private HttpServletResponse response;
35  
36      /** The provider. */
37      private NegotiateSecurityFilterProvider provider;
38  
39      /**
40       * Sets the up.
41       */
42      @BeforeEach
43      void setUp() {
44          this.provider = new NegotiateSecurityFilterProvider(this.auth);
45      }
46  
47      /**
48       * Test get protocols default.
49       */
50      @Test
51      void testGetProtocolsDefault() {
52          final List<String> protocols = this.provider.getProtocols();
53          Assertions.assertEquals(2, protocols.size());
54          Assertions.assertTrue(protocols.contains("Negotiate"));
55          Assertions.assertTrue(protocols.contains("NTLM"));
56      }
57  
58      /**
59       * Test set protocols.
60       */
61      @Test
62      void testSetProtocols() {
63          final List<String> newProtocols = Arrays.asList("NTLM");
64          this.provider.setProtocols(newProtocols);
65          Assertions.assertEquals(1, this.provider.getProtocols().size());
66          Assertions.assertTrue(this.provider.getProtocols().contains("NTLM"));
67      }
68  
69      /**
70       * Test is security package supported negotiate.
71       */
72      @Test
73      void testIsSecurityPackageSupportedNegotiate() {
74          Assertions.assertTrue(this.provider.isSecurityPackageSupported("Negotiate"));
75          Assertions.assertTrue(this.provider.isSecurityPackageSupported("negotiate"));
76          Assertions.assertTrue(this.provider.isSecurityPackageSupported("NEGOTIATE"));
77      }
78  
79      /**
80       * Test is security package supported ntlm.
81       */
82      @Test
83      void testIsSecurityPackageSupportedNtlm() {
84          Assertions.assertTrue(this.provider.isSecurityPackageSupported("NTLM"));
85          Assertions.assertTrue(this.provider.isSecurityPackageSupported("ntlm"));
86      }
87  
88      /**
89       * Test is security package supported unsupported.
90       */
91      @Test
92      void testIsSecurityPackageSupportedUnsupported() {
93          Assertions.assertFalse(this.provider.isSecurityPackageSupported("Basic"));
94          Assertions.assertFalse(this.provider.isSecurityPackageSupported("Bearer"));
95      }
96  
97      /**
98       * Test send unauthorized adds both negotiate and ntlm headers.
99       */
100     @Test
101     void testSendUnauthorized() {
102         this.provider.sendUnauthorized(this.response);
103         new Verifications() {
104             {
105                 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "Negotiate");
106                 this.times = 1;
107                 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "NTLM");
108                 this.times = 1;
109             }
110         };
111     }
112 
113     /**
114      * Test send unauthorized with custom protocols.
115      */
116     @Test
117     void testSendUnauthorizedWithCustomProtocols() {
118         this.provider.setProtocols(Arrays.asList("Negotiate"));
119         this.provider.sendUnauthorized(this.response);
120         new Verifications() {
121             {
122                 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "Negotiate");
123                 this.times = 1;
124                 NegotiateSecurityFilterProviderTest.this.response.addHeader("WWW-Authenticate", "NTLM");
125                 this.times = 0;
126             }
127         };
128     }
129 
130     /**
131      * Test init parameter protocols.
132      */
133     @Test
134     void testInitParameterProtocols() {
135         this.provider.initParameter("protocols", "NTLM");
136         Assertions.assertEquals(1, this.provider.getProtocols().size());
137         Assertions.assertTrue(this.provider.getProtocols().contains("NTLM"));
138     }
139 
140     /**
141      * Test init parameter protocols whitespace separated.
142      */
143     @Test
144     void testInitParameterProtocolsWhitespaceSeparated() {
145         this.provider.initParameter("protocols", "Negotiate NTLM");
146         Assertions.assertEquals(2, this.provider.getProtocols().size());
147     }
148 
149     /**
150      * Test init parameter unsupported protocol throws.
151      */
152     @Test
153     void testInitParameterUnsupportedProtocolThrows() {
154         Assertions.assertThrows(RuntimeException.class, () -> this.provider.initParameter("protocols", "Basic"));
155     }
156 
157     /**
158      * Test init parameter unknown parameter throws.
159      */
160     @Test
161     void testInitParameterUnknownParameterThrows() {
162         Assertions.assertThrows(Exception.class, () -> this.provider.initParameter("unknownParam", "value"));
163     }
164 }