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