jdk/test/javax/security/sasl/Sasl/ClientServerTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 31724 f6f1365b416f
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31724
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     1
/*
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     4
 *
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     8
 *
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    13
 * accompanied this code).
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    14
 *
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    18
 *
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    21
 * questions.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    22
 */
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    23
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    24
import java.io.Closeable;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    25
import java.io.IOException;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    26
import java.io.ObjectInputStream;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    27
import java.io.ObjectOutputStream;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    28
import java.io.Serializable;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    29
import java.net.ServerSocket;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    30
import java.net.Socket;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    31
import java.net.UnknownHostException;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    32
import java.util.ArrayList;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    33
import java.util.Arrays;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    34
import java.util.HashMap;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    35
import java.util.Map;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    36
import java.util.StringJoiner;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    37
import javax.security.auth.callback.Callback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    38
import javax.security.auth.callback.CallbackHandler;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    39
import javax.security.auth.callback.NameCallback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    40
import javax.security.auth.callback.PasswordCallback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    41
import javax.security.auth.callback.UnsupportedCallbackException;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    42
import javax.security.sasl.AuthorizeCallback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    43
import javax.security.sasl.RealmCallback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    44
import javax.security.sasl.RealmChoiceCallback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    45
import javax.security.sasl.Sasl;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    46
import javax.security.sasl.SaslClient;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    47
import javax.security.sasl.SaslException;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    48
import javax.security.sasl.SaslServer;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    49
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    50
/*
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    51
 * @test
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    52
 * @bug 8049814
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    53
 * @summary JAVA SASL server and client tests with CRAM-MD5 and
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    54
 *          DIGEST-MD5 mechanisms. The tests try different QOP values on
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    55
 *          client and server side.
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    56
 * @modules java.security.sasl/javax.security.sasl
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    57
 */
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    58
public class ClientServerTest {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    59
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    60
    private static final int DELAY = 100;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    61
    private static final String LOCALHOST = "localhost";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    62
    private static final String DIGEST_MD5 = "DIGEST-MD5";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    63
    private static final String CRAM_MD5 = "CRAM-MD5";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    64
    private static final String PROTOCOL = "saslservice";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    65
    private static final String USER_ID = "sasltester";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    66
    private static final String PASSWD = "password";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    67
    private static final String QOP_AUTH = "auth";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    68
    private static final String QOP_AUTH_CONF = "auth-conf";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    69
    private static final String QOP_AUTH_INT = "auth-int";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    70
    private static final String AUTHID_SASL_TESTER = "sasl_tester";
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    71
    private static final ArrayList<String> SUPPORT_MECHS = new ArrayList<>();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    72
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    73
    static {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    74
        SUPPORT_MECHS.add(DIGEST_MD5);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    75
        SUPPORT_MECHS.add(CRAM_MD5);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    76
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    77
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    78
    public static void main(String[] args) throws Exception {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    79
        String[] allQops = { QOP_AUTH_CONF, QOP_AUTH_INT, QOP_AUTH };
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    80
        String[] twoQops = { QOP_AUTH_INT, QOP_AUTH };
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    81
        String[] authQop = { QOP_AUTH };
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    82
        String[] authIntQop = { QOP_AUTH_INT };
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    83
        String[] authConfQop = { QOP_AUTH_CONF };
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    84
        String[] emptyQop = {};
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    85
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    86
        boolean success = true;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    87
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    88
        success &= runTest("", CRAM_MD5, new String[] { QOP_AUTH },
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    89
                new String[] { QOP_AUTH }, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    90
        success &= runTest("", DIGEST_MD5, new String[] { QOP_AUTH },
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    91
                new String[] { QOP_AUTH }, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    92
        success &= runTest(AUTHID_SASL_TESTER, DIGEST_MD5,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    93
                new String[] { QOP_AUTH }, new String[] { QOP_AUTH }, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    94
        success &= runTest("", DIGEST_MD5, allQops, authQop, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    95
        success &= runTest("", DIGEST_MD5, allQops, authIntQop, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    96
        success &= runTest("", DIGEST_MD5, allQops, authConfQop, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    97
        success &= runTest("", DIGEST_MD5, twoQops, authQop, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    98
        success &= runTest("", DIGEST_MD5, twoQops, authIntQop, false);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
    99
        success &= runTest("", DIGEST_MD5, twoQops, authConfQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   100
        success &= runTest("", DIGEST_MD5, authIntQop, authQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   101
        success &= runTest("", DIGEST_MD5, authConfQop, authQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   102
        success &= runTest("", DIGEST_MD5, authConfQop, emptyQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   103
        success &= runTest("", DIGEST_MD5, authIntQop, emptyQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   104
        success &= runTest("", DIGEST_MD5, authQop, emptyQop, true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   105
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   106
        if (!success) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   107
            throw new RuntimeException("At least one test case failed");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   108
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   109
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   110
        System.out.println("Test passed");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   111
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   112
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   113
    private static boolean runTest(String authId, String mech,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   114
            String[] clientQops, String[] serverQops, boolean expectException)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   115
            throws Exception {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   116
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   117
        System.out.println("AuthId:" + authId
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   118
                + " mechanism:" + mech
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   119
                + " clientQops: " + Arrays.toString(clientQops)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   120
                + " serverQops: " + Arrays.toString(serverQops)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   121
                + " expect exception:" + expectException);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   122
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   123
        try (Server server = Server.start(LOCALHOST, authId, serverQops)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   124
            new Client(LOCALHOST, server.getPort(), mech, authId, clientQops)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   125
                    .run();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   126
            if (expectException) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   127
                System.out.println("Expected exception not thrown");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   128
                return false;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   129
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   130
        } catch (SaslException e) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   131
            if (!expectException) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   132
                System.out.println("Unexpected exception: " + e);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   133
                return false;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   134
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   135
            System.out.println("Expected exception: " + e);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   136
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   137
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   138
        return true;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   139
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   140
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   141
    static enum SaslStatus {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   142
        SUCCESS, FAILURE, CONTINUE
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   143
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   144
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   145
    static class Message implements Serializable {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   146
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   147
        private final SaslStatus status;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   148
        private final byte[] data;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   149
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   150
        public Message(SaslStatus status, byte[] data) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   151
            this.status = status;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   152
            this.data = data;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   153
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   154
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   155
        public SaslStatus getStatus() {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   156
            return status;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   157
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   158
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   159
        public byte[] getData() {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   160
            return data;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   161
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   162
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   163
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   164
    static class SaslPeer {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   165
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   166
        final String host;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   167
        final String mechanism;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   168
        final String qop;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   169
        final CallbackHandler callback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   170
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   171
        SaslPeer(String host, String authId, String... qops) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   172
            this(host, null, authId, qops);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   173
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   174
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   175
        SaslPeer(String host, String mechanism, String authId, String... qops) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   176
            this.host = host;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   177
            this.mechanism = mechanism;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   178
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   179
            StringJoiner sj = new StringJoiner(",");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   180
            for (String q : qops) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   181
                sj.add(q);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   182
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   183
            qop = sj.toString();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   184
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   185
            callback = new TestCallbackHandler(USER_ID, PASSWD, host, authId);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   186
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   187
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   188
        Message getMessage(Object ob) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   189
            if (!(ob instanceof Message)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   190
                throw new RuntimeException("Expected an instance of Message");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   191
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   192
            return (Message) ob;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   193
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   194
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   195
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   196
    static class Server extends SaslPeer implements Runnable, Closeable {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   197
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   198
        private volatile boolean ready = false;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   199
        private volatile ServerSocket ssocket;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   200
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   201
        static Server start(String host, String authId, String[] serverQops)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   202
                throws UnknownHostException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   203
            Server server = new Server(host, authId, serverQops);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   204
            Thread thread = new Thread(server);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   205
            thread.setDaemon(true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   206
            thread.start();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   207
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   208
            while (!server.ready) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   209
                try {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   210
                    Thread.sleep(DELAY);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   211
                } catch (InterruptedException e) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   212
                    throw new RuntimeException(e);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   213
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   214
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   215
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   216
            return server;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   217
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   218
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   219
        Server(String host, String authId, String... qops) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   220
            super(host, authId, qops);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   221
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   222
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   223
        int getPort() {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   224
            return ssocket.getLocalPort();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   225
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   226
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   227
        private void processConnection(SaslEndpoint endpoint)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   228
                throws SaslException, IOException, ClassNotFoundException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   229
            System.out.println("process connection");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   230
            endpoint.send(SUPPORT_MECHS);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   231
            Object o = endpoint.receive();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   232
            if (!(o instanceof String)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   233
                throw new RuntimeException("Received unexpected object: " + o);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   234
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   235
            String mech = (String) o;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   236
            SaslServer saslServer = createSaslServer(mech);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   237
            Message msg = getMessage(endpoint.receive());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   238
            while (!saslServer.isComplete()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   239
                byte[] data = processData(msg.getData(), endpoint,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   240
                        saslServer);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   241
                if (saslServer.isComplete()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   242
                    System.out.println("server is complete");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   243
                    endpoint.send(new Message(SaslStatus.SUCCESS, data));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   244
                } else {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   245
                    System.out.println("server continues");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   246
                    endpoint.send(new Message(SaslStatus.CONTINUE, data));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   247
                    msg = getMessage(endpoint.receive());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   248
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   249
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   250
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   251
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   252
        private byte[] processData(byte[] data, SaslEndpoint endpoint,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   253
                SaslServer server) throws SaslException, IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   254
            try {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   255
                return server.evaluateResponse(data);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   256
            } catch (SaslException e) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   257
                endpoint.send(new Message(SaslStatus.FAILURE, null));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   258
                System.out.println("Error while processing data");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   259
                throw e;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   260
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   261
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   262
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   263
        private SaslServer createSaslServer(String mechanism)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   264
                throws SaslException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   265
            Map<String, String> props = new HashMap<>();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   266
            props.put(Sasl.QOP, qop);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   267
            return Sasl.createSaslServer(mechanism, PROTOCOL, host, props,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   268
                    callback);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   269
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   270
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   271
        @Override
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   272
        public void run() {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   273
            try (ServerSocket ss = new ServerSocket(0)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   274
                ssocket = ss;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   275
                System.out.println("server started on port " + getPort());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   276
                ready = true;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   277
                Socket socket = ss.accept();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   278
                try (SaslEndpoint endpoint = new SaslEndpoint(socket)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   279
                    System.out.println("server accepted connection");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   280
                    processConnection(endpoint);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   281
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   282
            } catch (Exception e) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   283
                // ignore it for now, client will throw an exception
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   284
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   285
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   286
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   287
        @Override
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   288
        public void close() throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   289
            if (!ssocket.isClosed()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   290
                ssocket.close();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   291
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   292
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   293
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   294
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   295
    static class Client extends SaslPeer {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   296
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   297
        private final int port;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   298
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   299
        Client(String host, int port, String mech, String authId,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   300
                String... qops) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   301
            super(host, mech, authId, qops);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   302
            this.port = port;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   303
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   304
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   305
        public void run() throws Exception {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   306
            System.out.println("Host:" + host + " port: "
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   307
                    + port);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   308
            try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   309
                negotiateMechanism(endpoint);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   310
                SaslClient client = createSaslClient();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   311
                byte[] data = new byte[0];
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   312
                if (client.hasInitialResponse()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   313
                    data = client.evaluateChallenge(data);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   314
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   315
                endpoint.send(new Message(SaslStatus.CONTINUE, data));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   316
                Message msg = getMessage(endpoint.receive());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   317
                while (!client.isComplete()
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   318
                        && msg.getStatus() != SaslStatus.FAILURE) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   319
                    switch (msg.getStatus()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   320
                        case CONTINUE:
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   321
                            System.out.println("client continues");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   322
                            data = client.evaluateChallenge(msg.getData());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   323
                            endpoint.send(new Message(SaslStatus.CONTINUE,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   324
                                    data));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   325
                            msg = getMessage(endpoint.receive());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   326
                            break;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   327
                        case SUCCESS:
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   328
                            System.out.println("client succeeded");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   329
                            data = client.evaluateChallenge(msg.getData());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   330
                            if (data != null) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   331
                                throw new SaslException("data should be null");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   332
                            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   333
                            break;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   334
                        default:
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   335
                            throw new RuntimeException("Wrong status:"
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   336
                                    + msg.getStatus());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   337
                    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   338
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   339
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   340
                if (msg.getStatus() == SaslStatus.FAILURE) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   341
                    throw new RuntimeException("Status is FAILURE");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   342
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   343
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   344
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   345
            System.out.println("Done");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   346
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   347
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   348
        private SaslClient createSaslClient() throws SaslException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   349
            Map<String, String> props = new HashMap<>();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   350
            props.put(Sasl.QOP, qop);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   351
            return Sasl.createSaslClient(new String[] {mechanism}, USER_ID,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   352
                    PROTOCOL, host, props, callback);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   353
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   354
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   355
        private void negotiateMechanism(SaslEndpoint endpoint)
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   356
                throws ClassNotFoundException, IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   357
            Object o = endpoint.receive();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   358
            if (o instanceof ArrayList) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   359
                ArrayList list = (ArrayList) o;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   360
                if (!list.contains(mechanism)) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   361
                    throw new RuntimeException(
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   362
                            "Server does not support specified mechanism:"
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   363
                                    + mechanism);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   364
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   365
            } else {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   366
                throw new RuntimeException(
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   367
                        "Expected an instance of ArrayList, but received " + o);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   368
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   369
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   370
            endpoint.send(mechanism);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   371
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   372
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   373
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   374
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   375
    static class SaslEndpoint implements AutoCloseable {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   376
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   377
        private final Socket socket;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   378
        private ObjectInputStream input;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   379
        private ObjectOutputStream output;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   380
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   381
        static SaslEndpoint create(String host, int port) throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   382
            return new SaslEndpoint(new Socket(host, port));
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   383
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   384
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   385
        SaslEndpoint(Socket socket) throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   386
            this.socket = socket;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   387
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   388
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   389
        private ObjectInputStream getInput() throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   390
            if (input == null && socket != null) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   391
                input = new ObjectInputStream(socket.getInputStream());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   392
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   393
            return input;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   394
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   395
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   396
        private ObjectOutputStream getOutput() throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   397
            if (output == null && socket != null) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   398
                output = new ObjectOutputStream(socket.getOutputStream());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   399
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   400
            return output;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   401
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   402
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   403
        public Object receive() throws IOException, ClassNotFoundException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   404
            return getInput().readObject();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   405
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   406
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   407
        public void send(Object obj) throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   408
            getOutput().writeObject(obj);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   409
            getOutput().flush();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   410
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   411
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   412
        @Override
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   413
        public void close() throws IOException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   414
            if (socket != null && !socket.isClosed()) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   415
                socket.close();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   416
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   417
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   418
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   419
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   420
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   421
    static class TestCallbackHandler implements CallbackHandler {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   422
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   423
        private final String userId;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   424
        private final char[] passwd;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   425
        private final String realm;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   426
        private String authId;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   427
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   428
        TestCallbackHandler(String userId, String passwd, String realm,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   429
                String authId) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   430
            this.userId = userId;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   431
            this.passwd = passwd.toCharArray();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   432
            this.realm = realm;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   433
            this.authId = authId;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   434
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   435
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   436
        @Override
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   437
        public void handle(Callback[] callbacks) throws IOException,
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   438
                UnsupportedCallbackException {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   439
            for (Callback callback : callbacks) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   440
                if (callback instanceof NameCallback) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   441
                    System.out.println("NameCallback");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   442
                    ((NameCallback) callback).setName(userId);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   443
                } else if (callback instanceof PasswordCallback) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   444
                    System.out.println("PasswordCallback");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   445
                    ((PasswordCallback) callback).setPassword(passwd);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   446
                } else if (callback instanceof RealmCallback) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   447
                    System.out.println("RealmCallback");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   448
                    ((RealmCallback) callback).setText(realm);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   449
                } else if (callback instanceof RealmChoiceCallback) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   450
                    System.out.println("RealmChoiceCallback");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   451
                    RealmChoiceCallback choice = (RealmChoiceCallback) callback;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   452
                    if (realm == null) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   453
                        choice.setSelectedIndex(choice.getDefaultChoice());
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   454
                    } else {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   455
                        String[] choices = choice.getChoices();
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   456
                        for (int j = 0; j < choices.length; j++) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   457
                            if (realm.equals(choices[j])) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   458
                                choice.setSelectedIndex(j);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   459
                                break;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   460
                            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   461
                        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   462
                    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   463
                } else if (callback instanceof AuthorizeCallback) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   464
                    System.out.println("AuthorizeCallback");
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   465
                    ((AuthorizeCallback) callback).setAuthorized(true);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   466
                    if (authId == null || authId.trim().length() == 0) {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   467
                        authId = userId;
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   468
                    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   469
                    ((AuthorizeCallback) callback).setAuthorizedID(authId);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   470
                } else {
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   471
                    throw new UnsupportedCallbackException(callback);
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   472
                }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   473
            }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   474
        }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   475
    }
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   476
f6f1365b416f 8049814: Additional SASL client-server tests
asmotrak
parents:
diff changeset
   477
}