1 /*
   2  * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8230159
  27  * @library /test/lib
  28  * @summary Ensure that correct exceptions are being thrown in
  29  * BasicAuthenticator constructor
  30  * @run testng BasicAuthenticatorExceptionCheck
  31  */
  32 
  33 
  34 import java.nio.charset.Charset;
  35 import com.sun.net.httpserver.BasicAuthenticator;
  36 import org.testng.annotations.Test;
  37 
  38 import static org.testng.Assert.expectThrows;
  39 import static java.nio.charset.StandardCharsets.UTF_8;
  40 
  41 
  42 public class BasicAuthenticatorExceptionCheck {
  43     static final Class<NullPointerException> NPE = NullPointerException.class;
  44     static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
  45 
  46     static BasicAuthenticator createBasicAuthenticator(String realm, Charset charset) {
  47         return new BasicAuthenticator(realm, charset) {
  48             public boolean checkCredentials(String username, String pw) {
  49                 return true;
  50             }
  51         };
  52     }
  53     static BasicAuthenticator createBasicAuthenticator(String realm) {
  54         return new BasicAuthenticator(realm) {
  55             public boolean checkCredentials(String username, String pw) {
  56                 return true;
  57             }
  58         };
  59     }
  60 
  61     @Test
  62     public void testAuthenticationException() {
  63 
  64         Throwable ex = expectThrows(NPE, () ->
  65                 createBasicAuthenticator("/test", null));
  66         System.out.println("Valid realm and Null charset provided - " +
  67                 "NullPointerException thrown as expected: " + ex);
  68 
  69         ex = expectThrows(NPE, () ->
  70                 createBasicAuthenticator(null, UTF_8));
  71         System.out.println("Null realm and valid charset provided - " +
  72                 "NullPointerException thrown as expected: " + ex);
  73 
  74         ex = expectThrows(IAE, () ->
  75                 createBasicAuthenticator("", UTF_8));
  76         System.out.println("Empty string for realm and valid charset provided - " +
  77                 "IllegalArgumentException thrown as expected: " + ex);
  78 
  79         ex = expectThrows(NPE, () ->
  80                 createBasicAuthenticator(null));
  81         System.out.println("Null realm provided - " +
  82                 "NullPointerException thrown as expected: " + ex);
  83 
  84         ex = expectThrows(IAE, () ->
  85                 createBasicAuthenticator(""));
  86         System.out.println("Empty string for realm provided - " +
  87                 "IllegalArgumentException thrown as expected: " + ex);
  88     }
  89 }