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.apache;
25  
26  import java.io.IOException;
27  
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.catalina.connector.Request;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   * Waffle Authenticator Base Test.
38   */
39  class WaffleAuthenticatorBaseTest {
40  
41      /** The waffle authenticator base. */
42      private WaffleAuthenticatorBase waffleAuthenticatorBase;
43  
44      /**
45       * Inits the.
46       */
47      @BeforeEach
48      void init() {
49          this.waffleAuthenticatorBase = new WaffleAuthenticatorBase() {
50              {
51                  this.log = LoggerFactory.getLogger(WaffleAuthenticatorBaseTest.class);
52              }
53  
54              @Override
55              public boolean authenticate(final Request request, final HttpServletResponse response) throws IOException {
56                  return false;
57              }
58  
59              @Override
60              protected boolean doAuthenticate(final Request request, final HttpServletResponse response)
61                      throws IOException {
62                  return false;
63              }
64          };
65      }
66  
67      /**
68       * Should_accept_both_protocols.
69       */
70      @Test
71      void should_accept_both_protocols() {
72          this.waffleAuthenticatorBase.setProtocols("  NTLM , , Negotiate   ");
73  
74          Assertions.assertEquals(2, this.waffleAuthenticatorBase.protocols.size(), "Two protocols added");
75          Assertions.assertTrue(this.waffleAuthenticatorBase.protocols.contains("NTLM"), "NTLM has been added");
76          Assertions.assertTrue(this.waffleAuthenticatorBase.protocols.contains("Negotiate"), "Negotiate has been added");
77      }
78  
79      /**
80       * Should_accept_ negotiate_protocol.
81       */
82      @Test
83      void should_accept_Negotiate_protocol() {
84          this.waffleAuthenticatorBase.setProtocols(" Negotiate  ");
85  
86          Assertions.assertEquals(1, this.waffleAuthenticatorBase.protocols.size(), "One protocol added");
87          Assertions.assertEquals("Negotiate", this.waffleAuthenticatorBase.protocols.iterator().next());
88      }
89  
90      /**
91       * Should_accept_ ntl m_protocol.
92       */
93      @Test
94      void should_accept_NTLM_protocol() {
95          this.waffleAuthenticatorBase.setProtocols("  NTLM ");
96  
97          Assertions.assertEquals(1, this.waffleAuthenticatorBase.protocols.size(), "One protocol added");
98          Assertions.assertEquals("NTLM", this.waffleAuthenticatorBase.protocols.iterator().next());
99      }
100 
101     /**
102      * Should_refuse_other_protocol.
103      */
104     @Test
105     void should_refuse_other_protocol() {
106         Assertions.assertThrows(RuntimeException.class, () -> {
107             this.waffleAuthenticatorBase.setProtocols("  NTLM , OTHER, Negotiate   ");
108         });
109     }
110 
111 }