You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ph...@apache.org on 2016/12/06 00:17:00 UTC

[2/4] zookeeper git commit: ZOOKEEPER-1045: Support Quorum Peer mutual authentication via SASL (rakeshr via phunt)

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java b/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java
new file mode 100644
index 0000000..6b6c0b4
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/QuorumCnxManagerTest.java
@@ -0,0 +1,925 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import javax.security.sasl.SaslException;
+
+import org.apache.jute.BinaryOutputArchive;
+import org.apache.zookeeper.PortAssignment;
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.server.ServerCnxn;
+import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.apache.zookeeper.server.ZKDatabase;
+import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
+import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
+import org.apache.zookeeper.server.quorum.auth.NullQuorumAuthLearner;
+import org.apache.zookeeper.server.quorum.auth.NullQuorumAuthServer;
+import org.apache.zookeeper.server.quorum.auth.QuorumAuth;
+import org.apache.zookeeper.server.quorum.auth.QuorumAuthLearner;
+import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer;
+import org.apache.zookeeper.server.quorum.auth.SaslQuorumAuthLearner;
+import org.apache.zookeeper.server.quorum.auth.SaslQuorumAuthServer;
+import org.apache.zookeeper.server.quorum.flexible.QuorumMaj;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class QuorumCnxManagerTest extends ZKTestCase {
+    private static final Logger LOG = LoggerFactory.getLogger(QuorumCnxManagerTest.class);
+    private int count;
+    private HashMap<Long,QuorumServer> peers;
+    private int peerQuorumPort[];
+    private int peerClientPort[];
+    private ThreadPoolExecutor executor;
+    /**
+     * The maximum number of threads to allow in the connectionExecutors thread
+     * pool which will be used to initiate quorum server connections. Defaulting to 20.
+     * TODO: Need to tune this param.
+     */
+    private final int quorumCnxnThreadsSize = 20;
+    private Set<String> authzHosts;
+
+    private static File saslConfigFile = null;
+
+    @BeforeClass
+    public static void setupSasl() throws Exception {
+        String jaasEntries = new String(""
+                + "QuorumServer {\n"
+                + "       org.apache.zookeeper.server.auth.DigestLoginModule required\n"
+                + "       user_test=\"mypassword\";\n"
+                + "};\n"
+                + "QuorumLearner {\n"
+                + "       org.apache.zookeeper.server.auth.DigestLoginModule required\n"
+                + "       username=\"test\"\n"
+                + "       password=\"mypassword\";\n"
+                + "};\n"
+                + "QuorumLearnerInvalid {\n"
+                + "       org.apache.zookeeper.server.auth.DigestLoginModule required\n"
+                + "       username=\"test\"\n"
+                + "       password=\"invalid\";\n"
+                + "};\n");
+
+        saslConfigFile = File.createTempFile("jaas.", ".conf");
+        FileWriter fwriter = new FileWriter(saslConfigFile);
+        fwriter.write(jaasEntries);
+        fwriter.close();
+        System.setProperty("java.security.auth.login.config",
+                           saslConfigFile.getAbsolutePath());
+    }
+
+    @AfterClass
+    public static void cleanupSasl() throws Exception {
+        if (saslConfigFile != null) {
+            saslConfigFile.delete();
+        }
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        this.count = 3;
+        this.peers = new HashMap<Long,QuorumServer>(count);
+        peerQuorumPort = new int[count];
+        peerClientPort = new int[count];
+        authzHosts = new HashSet<String>();
+        for(int i = 0; i < count; i++) {
+            peerQuorumPort[i] = PortAssignment.unique();
+            peerClientPort[i] = PortAssignment.unique();
+            QuorumServer qs = new QuorumServer(i, "0.0.0.0",
+                    peerQuorumPort[i], PortAssignment.unique(), null);
+            peers.put(Long.valueOf(i), qs);
+            authzHosts.add(qs.hostname);
+        }
+        executor = new ThreadPoolExecutor(3, 10,
+                60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (executor != null) {
+            executor.shutdownNow();
+        }
+    }
+
+    @Test(timeout = 30000)
+    public void testNoAuthConnection() throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0);
+        QuorumCnxManager peer1 = createAndStartManager(1);
+
+        peer0.connectOne(1);
+        assertEventuallyConnected(peer0, 1);
+    }
+
+    @Test(timeout = 30000)
+    public void testAuthConnection() throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        peer0.connectOne(1);
+        assertEventuallyConnected(peer0, 1);
+    }
+
+    /**
+     * Peer0 has no auth configured, Peer1 has auth configured.
+     * Peer1 connects to peer0, because null auth server sees an auth packet and connection succeeds.
+     * Peer0 connects to peer1, but connection isn't initiated because
+     * peer0's sid is lower than peer1's
+     */
+    @Test(timeout = 30000)
+    public void testClientAuthAgainstNoAuthServerWithLowerSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                                                       "QuorumLearner", false, false);
+        peer1.connectOne(0);
+        peer0.connectOne(1);
+        assertEventuallyConnected(peer0, 1);
+    }
+
+    /**
+     * Peer0 has auth configured, Peer1 has no auth configured.
+     * Peer1 connects to peer0, but is disconnected, because peer1's sid is
+     * higher than peer0.
+     * Peer0 connects to peer1, but is disconnected, because peer1 cannot
+     * handle auth.
+     */
+    @Test(timeout = 30000)
+    public void testClientAuthAgainstNoAuthServerWithHigherSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer",
+                                                       "QuorumLearner", false, false);
+        QuorumCnxManager peer1 = createAndStartManager(1);
+        peer0.connectOne(1);
+        peer1.connectOne(0);
+        assertEventuallyConnected(peer0, 1);
+    }
+
+    /**
+     * No auth learner connects to a server that requires auth, when the server
+     * has a higher sid.
+     * The connection should fail in both directions.
+     */
+    @Test(timeout = 30000)
+    public void testNoAuthLearnerConnectToAuthRequiredServerWithLowerSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        QuorumCnxManager peer1 = createAndStartManager(1);
+        peer0.connectOne(1);
+        peer1.connectOne(0);
+        assertEventuallyNotConnected(peer0, 1);
+    }
+
+    /**
+     * No auth learner connects to a server that requires auth, when the server
+     * has a higher sid.
+     * The connection should fail in both directions.
+     */
+    @Test(timeout = 30000)
+    public void testNoAuthLearnerConnectToAuthRequiredServerWithHigherSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        peer0.connectOne(1);
+        peer1.connectOne(0);
+        assertEventuallyNotConnected(peer0, 1);
+    }
+
+    /**
+     * An auth learner connects to a auth server, but the credentials are bad.
+     * The peer with the higher sid has the bad credentials.
+     * The connection will be denied.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerBadCredToAuthRequiredServerWithLowerSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0,  "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                                                       "QuorumLearnerInvalid", true, true);
+        peer0.connectOne(1);
+        peer1.connectOne(0);
+
+        assertEventuallyNotConnected(peer0, 1);
+    }
+
+    /**
+     * An auth learner connects to a auth server, but the credentials are bad.
+     * The peer with the lower sid has the bad credentials.
+     * The connection will work, because peer1 is connecting to peer0.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerBadCredToAuthRequiredServerWithHigherSid()
+            throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0,  "QuorumServer",
+                                                       "QuorumLearnerInvalid", true, true);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                                                       "QuorumLearner", true, true);
+        peer0.connectOne(1);
+        peer1.connectOne(0);
+        assertEventuallyConnected(peer0, 1);
+        assertEventuallyConnected(peer1, 0);
+    }
+
+    /**
+     * An auth learner connects to a auth server, but the credentials are bad.
+     * The connection should fail in both directions.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerBadCredToNoAuthServerWithHigherSid() throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer",
+                "QuorumLearner", false, false);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                "QuorumLearnerInvalid", true, true);
+        peer1.connectOne(0);
+        assertEventuallyNotConnected(peer1, 0);
+    }
+
+    /**
+     * An auth learner connects to a auth server, but the credentials are bad.
+     * The peer with the lower sid has the bad credentials.
+     * The connection will work, because peer0 is connecting to peer1 and peer1
+     * server doesn't require sasl
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerBadCredToNoAuthServerWithLowerSid() throws Exception {
+        QuorumCnxManager peer0 = createAndStartManager(0, "QuorumServer",
+                "QuorumLearnerInvalid", true, true);
+        QuorumCnxManager peer1 = createAndStartManager(1, "QuorumServer",
+                "QuorumLearner", false, true);
+        peer0.connectOne(1);
+        assertEventuallyConnected(peer0, 1);
+        assertEventuallyConnected(peer1, 0);
+    }
+
+    /**
+     * Test verifies that the LearnerHandler should authenticate the connecting
+     * quorumpeer. Here its simulating authentication failure and it should throw
+     * SaslException
+     */
+    @Test(timeout = 30000)
+    public void testLearnerHandlerAuthFailed() throws Exception {
+        File testData = ClientBase.createTmpDir();
+        Socket leaderSocket = getSocketPair();
+        File tmpDir = File.createTempFile("test", ".dir", testData);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, true, false, true,
+                "QuorumLearner", "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        leader = createLeader(tmpDir, peer);
+        peer.leader = leader;
+
+        // authentication failed as qpserver didn't get auth packet from qpclient.
+        try {
+            new LearnerHandler(leaderSocket,
+                    new BufferedInputStream(leaderSocket.getInputStream()), leader);
+            Assert.fail("Must throw exception as there is an authentication failure");
+        } catch (SaslException e){
+            Assert.assertEquals("Mistakely added to learners", 0,
+                    leader.getLearners().size());
+        }
+        ClientBase.recursiveDelete(testData);
+    }
+
+    /**
+     * Test verifies that the Leader should authenticate the connecting learner
+     * quorumpeer. After the successful authentication it should add this
+     * learner to the learnerHandler list.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerConnectsToServerWithAuthRequired()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true,
+                "QuorumLearner", "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, true, true, true, "QuorumLearner",
+                "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+        String hostname = getLeaderHostname(peer);
+        sl.connectToLeader(peer.getQuorumAddress(), hostname);
+        // wait till leader socket soTimeout period
+        Assert.assertTrue("Leader should accept the auth learner connection",
+                learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
+                        TimeUnit.MILLISECONDS));
+        Assert.assertEquals("Failed to added the learner", 1,
+                leader.getLearners().size());
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    private String getLeaderHostname(QuorumPeer peer) {
+        String hostname = null;
+        for (QuorumServer p : peer.getView().values()) {
+            if (p.id == peer.getId()) {
+                hostname = p.hostname;
+                break;
+            }
+        }
+        Assert.assertNotNull("Didn't find leader", hostname);
+        return hostname;
+    }
+
+    /**
+     * Test verifies that the Leader should authenticate the connecting learner
+     * quorumpeer. After the successful authentication it should add this
+     * learner to the learnerHandler list.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerConnectsToServerWithAuthNotRequired()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true,
+                "QuorumLearner", "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, true, true, false, "QuorumLearner",
+                "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+        String hostname = getLeaderHostname(peer);
+        sl.connectToLeader(peer.getQuorumAddress(), hostname);
+        // wait till leader socket soTimeout period
+        Assert.assertTrue("Leader should accept the auth learner connection",
+                learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
+                        TimeUnit.MILLISECONDS));
+        Assert.assertEquals("Failed to added the learner", 1,
+                leader.getLearners().size());
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    private void startLearnerCnxAcceptorThread(Leader leader)
+            throws InterruptedException {
+        final CountDownLatch cnxAcceptorWatcher = new CountDownLatch(1);
+        leader.cnxAcceptor = leader.new LearnerCnxAcceptor(){
+            @Override
+            public void run() {
+                cnxAcceptorWatcher.countDown();
+                super.run();
+            }
+        };
+        leader.cnxAcceptor.start();
+        // waiting to start the thread
+        Assert.assertTrue("Failed to start leader.cnxAcceptor thread!",
+                cnxAcceptorWatcher.await(15, TimeUnit.SECONDS));
+        LOG.info("Started leader.cnxAcceptor:{} thread, state:{}",
+                leader.cnxAcceptor.getName(), leader.cnxAcceptor.getState());
+    }
+
+    /**
+     * Test verifies that the Auth enabled Learner is connecting to a Null Auth
+     * Leader server. Learner is failing to get an auth response from Null Auth
+     * Leader and fails the connection establishment.
+     */
+    @Test(timeout = 30000)
+    public void testAuthLearnerConnectsToNullAuthServer()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, true, true,
+                "QuorumLearner", "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, false, false, false,
+                "QuorumLearner", "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+
+        try {
+            String hostname = getLeaderHostname(peer);
+            sl.connectToLeader(peer.getQuorumAddress(), hostname);
+            Assert.fail("Must throw exception as server doesn't supports authentication");
+        } catch (IOException e) {
+            // expected
+            Assert.assertTrue("Leader should accept the auth learner connection",
+                    learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 500,
+                            TimeUnit.MILLISECONDS));
+        }
+
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    /**
+     * Test verifies that the No Auth enabled Learner is connecting to a No Auth
+     * Leader server. Learner should be able to establish a connection with
+     * Leader as auth is not required.
+     */
+    @Test(timeout = 30000)
+    public void testNoAuthLearnerConnectsToServerWithAuthNotRequired()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false,
+                "QuorumLearner", "QuorumServer", "");
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, true, false, false, "QuorumLearner",
+                "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+        String hostname = getLeaderHostname(peer);
+        sl.connectToLeader(peer.getQuorumAddress(), hostname);
+
+        Assert.assertTrue("Leader should accept no auth learner connection",
+                learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
+                        TimeUnit.MILLISECONDS));
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    /**
+     * Test verifies that the No Auth enabled Learner is connecting to a No Auth
+     * Leader server. Learner shouldn't be able to establish a connection with
+     * Leader as auth as auth is required.
+     */
+    @Test(timeout = 30000)
+    public void testNoAuthLearnerConnectsToServerWithAuthRequired()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false,
+                "QuorumLearner", "QuorumServer", "");
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, true, true, true, "QuorumLearner",
+                "QuorumServer",
+                QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE);
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+        String hostname = getLeaderHostname(peer);
+        sl.connectToLeader(peer.getQuorumAddress(), hostname);
+        Assert.assertFalse("Leader shouldn't accept no auth learner connection",
+                learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
+                        TimeUnit.MILLISECONDS));
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    /**
+     * Test verifies that the No Auth enabled Learner is connecting to a No Auth
+     * Leader server. Learner should be able to establish a connection with
+     * Leader as auth is not required.
+     */
+    @Test(timeout = 30000)
+    public void testNoAuthLearnerConnectsToNullAuthServer()
+            throws Exception {
+        File testDataLearner = ClientBase.createTmpDir();
+        File tmpDir = File.createTempFile("test", ".dir", testDataLearner);
+        tmpDir.delete();
+        FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpDir, tmpDir);
+        QuorumPeer learnerPeer = createQuorumPeer(tmpDir, true, false, false,
+                "QuorumLearner", "QuorumServer", "");
+        SimpleLearner sl = new SimpleLearner(ftsl, learnerPeer);
+
+        File testDataLeader = ClientBase.createTmpDir();
+        tmpDir = File.createTempFile("test", ".dir", testDataLeader);
+        tmpDir.delete();
+        tmpDir.mkdir();
+        Leader leader = null;
+        QuorumPeer peer = createQuorumPeer(tmpDir, false, false, false, "", "",
+                "");
+        CountDownLatch learnerLatch = new CountDownLatch(1);
+        leader = createSimpleLeader(tmpDir, peer, learnerLatch);
+        peer.leader = leader;
+
+        startLearnerCnxAcceptorThread(leader);
+        LOG.info("Start establishing a connection with the Leader");
+        String hostname = getLeaderHostname(peer);
+        sl.connectToLeader(peer.getQuorumAddress(), hostname);
+
+        Assert.assertTrue("Leader should accept no auth learner connection",
+                learnerLatch.await(leader.self.tickTime * leader.self.initLimit + 1000,
+                        TimeUnit.MILLISECONDS));
+        ClientBase.recursiveDelete(testDataLearner);
+        ClientBase.recursiveDelete(testDataLeader);
+    }
+
+    /**
+     * SaslQuorumAuthServer throws exception on receiving an invalid quorum
+     * auth packet.
+     */
+    @Test(timeout = 30000)
+    public void testSaslQuorumAuthServerWithInvalidQuorumAuthPacket()
+            throws Exception {
+        Socket socket = getSocketPair();
+        DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
+        BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout);
+        BinaryOutputArchive boa = BinaryOutputArchive
+                .getArchive(bufferedOutput);
+        QuorumAuthPacket authPacket = QuorumAuth
+                .createPacket(QuorumAuth.Status.IN_PROGRESS, null);
+        authPacket.setMagic(Long.MAX_VALUE); // invalid magic number
+        boa.writeRecord(authPacket, null);
+        bufferedOutput.flush();
+        QuorumAuthServer authServer = new SaslQuorumAuthServer(true,
+                "QuorumServer", authzHosts);
+        BufferedInputStream is = new BufferedInputStream(
+                socket.getInputStream());
+        try {
+            authServer.authenticate(socket, new DataInputStream(is));
+            Assert.fail("Must throw exception as QuorumAuthPacket is invalid");
+        } catch (SaslException e) {
+            // expected
+        }
+    }
+
+    /**
+     * NullQuorumAuthServer should return true when no auth quorum packet
+     * received and timed out.
+     */
+    @Test(timeout = 30000)
+    public void testNullQuorumAuthServerShouldReturnTrue()
+            throws Exception {
+        Socket socket = getSocketPair();
+        QuorumAuthServer authServer = new NullQuorumAuthServer();
+        BufferedInputStream is = new BufferedInputStream(
+                socket.getInputStream());
+        // It will throw exception and fail the
+        // test if any unexpected error. Not adding any extra assertion.
+        authServer.authenticate(socket, new DataInputStream(is));
+    }
+
+    /**
+     * NullQuorumAuthServer should return true on receiving a valid quorum auth
+     * packet.
+     */
+    @Test(timeout = 30000)
+    public void testNullQuorumAuthServerWithValidQuorumAuthPacket()
+            throws Exception {
+        Socket socket = getSocketPair();
+        DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
+        BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout);
+        BinaryOutputArchive boa = BinaryOutputArchive
+                .getArchive(bufferedOutput);
+        QuorumAuthPacket authPacket = QuorumAuth
+                .createPacket(QuorumAuth.Status.IN_PROGRESS, null);
+        boa.writeRecord(authPacket, null);
+        bufferedOutput.flush();
+        QuorumAuthServer authServer = new NullQuorumAuthServer();
+        BufferedInputStream is = new BufferedInputStream(
+                socket.getInputStream());
+        // It will throw exception and fail the
+        // test if any unexpected error. Not adding any extra assertion.
+        authServer.authenticate(socket, new DataInputStream(is));
+    }
+
+    private QuorumCnxManager createAndStartManager(long sid) {
+        QuorumCnxManager peer = new QuorumCnxManager(sid, peers,
+                new NullQuorumAuthServer(), new NullQuorumAuthLearner(), 10000,
+                false, quorumCnxnThreadsSize, false);
+        executor.submit(peer.listener);
+        InetSocketAddress electionAddr = peer.view.get(sid).electionAddr;
+        waitForElectionAddrBinding(electionAddr, 15);
+        return peer;
+    }
+
+    private QuorumCnxManager createAndStartManager(long sid,
+                                                   String serverLoginContext,
+                                                   String learnerLoginContext,
+                                                   boolean serverRequireSasl,
+                                                   boolean learnerRequireSasl)
+            throws Exception {
+        QuorumAuthLearner authClient = new SaslQuorumAuthLearner(learnerRequireSasl,
+                "NOT_USING_KRB_PRINCIPAL", learnerLoginContext);
+        QuorumAuthServer authServer = new SaslQuorumAuthServer(serverRequireSasl,
+                serverLoginContext, authzHosts);
+        QuorumCnxManager peer = new QuorumCnxManager(sid, peers,
+                authServer, authClient, 10000, false, quorumCnxnThreadsSize, true);
+        executor.submit(peer.listener);
+        InetSocketAddress electionAddr = peer.view.get(sid).electionAddr;
+        waitForElectionAddrBinding(electionAddr, 15);
+        return peer;
+    }
+
+    private void waitForElectionAddrBinding(InetSocketAddress electionAddr,
+            int retries) {
+        boolean success = false;
+        while (retries > 0) {
+            Socket sock = new Socket();
+            try {
+                sock.setTcpNoDelay(true);
+                sock.setSoTimeout(5000);
+                sock.connect(electionAddr, 5000);
+                success = true;
+            } catch (IOException e) {
+                LOG.error("IOException while checking election addr", e);
+            } finally {
+                cleanup(sock);
+            }
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                // ignore
+            }
+            retries--;
+        }
+        Assert.assertTrue("Did not connect to election port", success);
+    }
+
+    private void cleanup(Socket sock) {
+        try {
+            sock.close();
+        } catch (IOException ie) {
+            LOG.error("Exception while closing socket", ie);
+        }
+    }
+
+    private void assertEventuallyConnected(QuorumCnxManager peer, long sid)
+            throws Exception {
+        for (int i = 0; i < 20 && !peer.connectedToPeer(sid); i++) {
+            Thread.sleep(1000);
+        }
+        Assert.assertTrue("Not connected to peer", peer.connectedToPeer(sid));
+    }
+
+    private void assertEventuallyNotConnected(QuorumCnxManager peer, long sid)
+            throws Exception {
+        for (int i = 0; i < 3 && !peer.connectedToPeer(sid); i++) {
+            Thread.sleep(1000);
+        }
+        Assert.assertFalse("Connected to peer (shouldn't be)",
+                           peer.connectedToPeer(sid));
+    }
+
+    private QuorumPeer createQuorumPeer(File tmpDir,
+            boolean isQuorumAuthEnabled, boolean isQuorumLearnerAuthRequired,
+            boolean isQuorumServerAuthRequired, String quorumLearnerLoginContext,
+            String quorumServerLoginContext, String quorumServicePrincipal)
+                    throws IOException, FileNotFoundException {
+        QuorumPeer peer = QuorumPeer.testingQuorumPeer();
+        peer.syncLimit = 2;
+        peer.initLimit = 2;
+        peer.tickTime = 2000;
+        peer.quorumPeers = new HashMap<Long, QuorumServer>();
+        peer.quorumPeers.put(0L,
+                new QuorumServer(0, "0.0.0.0", PortAssignment.unique(), null, null));
+        peer.quorumPeers.put(1L,
+                new QuorumServer(1, "0.0.0.0", PortAssignment.unique(), null, null));
+        peer.setQuorumVerifier(new QuorumMaj(3));
+        peer.setCnxnFactory(new NullServerCnxnFactory());
+        // auth
+        if (isQuorumAuthEnabled) {
+            peer.authServer = new SaslQuorumAuthServer(
+                    isQuorumServerAuthRequired, quorumServerLoginContext, authzHosts);
+            peer.authLearner = new SaslQuorumAuthLearner(
+                    isQuorumLearnerAuthRequired, quorumServicePrincipal,
+                    quorumLearnerLoginContext);
+        }
+        File version2 = new File(tmpDir, "version-2");
+        version2.mkdir();
+        FileOutputStream fos;
+        fos = new FileOutputStream(new File(version2, "currentEpoch"));
+        fos.write("0\n".getBytes());
+        fos.close();
+        fos = new FileOutputStream(new File(version2, "acceptedEpoch"));
+        fos.write("0\n".getBytes());
+        fos.close();
+        return peer;
+    }
+
+    private static final class NullServerCnxnFactory extends ServerCnxnFactory {
+        public void startup(ZooKeeperServer zkServer)
+                throws IOException, InterruptedException {
+        }
+
+        public void start() {
+        }
+
+        public void shutdown() {
+        }
+
+        public void setMaxClientCnxnsPerHost(int max) {
+        }
+
+        public void join() throws InterruptedException {
+        }
+
+        public int getMaxClientCnxnsPerHost() {
+            return 0;
+        }
+
+        public int getLocalPort() {
+            return 0;
+        }
+
+        public InetSocketAddress getLocalAddress() {
+            return null;
+        }
+
+        public Iterable<ServerCnxn> getConnections() {
+            return null;
+        }
+
+        public void configure(InetSocketAddress addr, int maxClientCnxns)
+                throws IOException {
+        }
+
+        public void closeSession(long sessionId) {
+        }
+
+        public void closeAll() {
+        }
+
+        @Override
+        public int getNumAliveConnections() {
+            return 0;
+        }
+    }
+
+    private static Socket getSocketPair() throws IOException {
+        ServerSocket ss = new ServerSocket();
+        ss.bind(null);
+        InetSocketAddress endPoint = (InetSocketAddress) ss
+                .getLocalSocketAddress();
+        Socket s = new Socket(endPoint.getAddress(), endPoint.getPort());
+        s.setSoTimeout(5000);
+        return s;
+    }
+
+    private Leader createLeader(File tmpDir, QuorumPeer peer) throws IOException,
+                    NoSuchFieldException, IllegalAccessException {
+        LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer);
+        return new Leader(peer, zk);
+    }
+
+    private Leader createSimpleLeader(File tmpDir, QuorumPeer peer,
+            CountDownLatch learnerLatch) throws IOException,
+                    NoSuchFieldException, IllegalAccessException {
+        LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer);
+        return new SimpleLeader(peer, zk, learnerLatch);
+    }
+
+    class SimpleLeader extends Leader {
+        final CountDownLatch learnerLatch;
+
+        SimpleLeader(QuorumPeer self, LeaderZooKeeperServer zk,
+                CountDownLatch latch) throws IOException {
+            super(self, zk);
+            this.learnerLatch = latch;
+        }
+
+        @Override
+        void addLearnerHandler(LearnerHandler learner) {
+            super.addLearnerHandler(learner);
+            learnerLatch.countDown();
+        }
+    }
+
+    private LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer)
+            throws IOException, NoSuchFieldException, IllegalAccessException {
+        FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir);
+        peer.setTxnFactory(logFactory);
+        Field addrField = peer.getClass().getDeclaredField("myQuorumAddr");
+        addrField.setAccessible(true);
+        addrField.set(peer, new InetSocketAddress(PortAssignment.unique()));
+        ZKDatabase zkDb = new ZKDatabase(logFactory);
+        LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer,
+                new ZooKeeperServer.BasicDataTreeBuilder(), zkDb);
+        return zk;
+    }
+
+    class SimpleLearnerZooKeeperServer extends LearnerZooKeeperServer {
+        boolean startupCalled;
+
+        public SimpleLearnerZooKeeperServer(FileTxnSnapLog ftsl,
+                QuorumPeer self) throws IOException {
+            super(ftsl, 2000, 2000, 2000, null, new ZKDatabase(ftsl), self);
+        }
+
+        Learner learner;
+
+        @Override
+        public Learner getLearner() {
+            return learner;
+        }
+
+        @Override
+        public void startup() {
+            startupCalled = true;
+        }
+    }
+
+    class SimpleLearner extends Learner {
+        SimpleLearner(FileTxnSnapLog ftsl, QuorumPeer learner)
+                throws IOException {
+            self = learner;
+            zk = new SimpleLearnerZooKeeperServer(ftsl, self);
+            ((SimpleLearnerZooKeeperServer) zk).learner = this;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java
index 85817b2..6d5eb47 100644
--- a/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java
+++ b/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java
@@ -24,6 +24,10 @@ package org.apache.zookeeper.server.quorum;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 
 import org.apache.zookeeper.WatchedEvent;
@@ -42,6 +46,8 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher {
     protected static final Logger LOG = LoggerFactory
             .getLogger(QuorumPeerTestBase.class);
 
+    public static final int TIMEOUT = 5000;
+
     public void process(WatchedEvent event) {
         // ignore for this test
     }
@@ -60,20 +66,29 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher {
         volatile TestQPMain main;
         final File dataDir;
         CountDownLatch mainFailed;
-
-        public MainThread(int myid, int clientPort, String quorumCfgSection)
-                throws IOException {
-            File tmpDir = ClientBase.createTmpDir();
-            LOG.info("id = " + myid + " tmpDir = " + tmpDir + " clientPort = "
+        File baseDir;
+        private final int myid;
+        private final int clientPort;
+        private final String quorumCfgSection;
+        private final Map<String, String> otherConfigs;
+
+        public MainThread(int myid, int clientPort, String quorumCfgSection,
+                Map<String, String> otherConfigs) throws IOException {
+            baseDir = ClientBase.createTmpDir();
+            this.myid = myid;
+            this.clientPort = clientPort;
+            this.quorumCfgSection = quorumCfgSection;
+            this.otherConfigs = otherConfigs;
+            LOG.info("id = " + myid + " tmpDir = " + baseDir + " clientPort = "
                     + clientPort);
-            confFile = new File(tmpDir, "zoo.cfg");
+            confFile = new File(baseDir, "zoo.cfg");
 
             FileWriter fwriter = new FileWriter(confFile);
             fwriter.write("tickTime=4000\n");
             fwriter.write("initLimit=10\n");
             fwriter.write("syncLimit=5\n");
 
-            dataDir = new File(tmpDir, "data");
+            dataDir = new File(baseDir, "data");
             if (!dataDir.mkdir()) {
                 throw new IOException("Unable to mkdir " + dataDir);
             }
@@ -87,6 +102,13 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher {
             fwriter.write("dataDir=" + dir + "\n");
 
             fwriter.write("clientPort=" + clientPort + "\n");
+
+            // write extra configurations
+            Set<Entry<String, String>> entrySet = otherConfigs.entrySet();
+            for (Entry<String, String> entry : entrySet) {
+                fwriter.write(entry.getKey() + "=" + entry.getValue() + "\n");
+            }
+
             fwriter.write(quorumCfgSection + "\n");
             fwriter.flush();
             fwriter.close();
@@ -98,6 +120,12 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher {
             fwriter.close();
         }
 
+        public MainThread(int myid, int clientPort, String quorumCfgSection)
+                throws IOException {
+            this(myid, clientPort, quorumCfgSection,
+                    new HashMap<String, String>());
+        }
+
         Thread currentThread;
 
         synchronized public void start() {
@@ -151,5 +179,28 @@ public class QuorumPeerTestBase extends ZKTestCase implements Watcher {
             return main.quorumPeer;
         }
 
+        public void deleteBaseDir() {
+            ClientBase.recursiveDelete(baseDir);
+        }
+
+        public int getMyid() {
+            return myid;
+        }
+
+        public int getClientPort() {
+            return clientPort;
+        }
+
+        public String getQuorumCfgSection() {
+            return quorumCfgSection;
+        }
+
+        public Map<String, String> getOtherConfigs() {
+            return otherConfigs;
+        }
+
+        public File getConfFile() {
+            return confFile;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java b/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java
index ab8ce42..52e7d27 100644
--- a/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java
+++ b/src/java/test/org/apache/zookeeper/server/quorum/Zab1_0Test.java
@@ -20,6 +20,7 @@ package org.apache.zookeeper.server.quorum;
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -367,7 +368,9 @@ public class Zab1_0Test {
                 Thread.sleep(20);
             }
             
-            LearnerHandler lh = new LearnerHandler(leaderSocket, leader);
+            LearnerHandler lh = new LearnerHandler(leaderSocket,
+                    new BufferedInputStream(leaderSocket.getInputStream()),
+                    leader);
             lh.start();
             leaderSocket.setSoTimeout(4000);
 
@@ -435,8 +438,10 @@ public class Zab1_0Test {
             while(leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
                 Thread.sleep(20);
             }
-            
-            LearnerHandler lh = new LearnerHandler(leaderSocket, leader);
+
+            LearnerHandler lh = new LearnerHandler(leaderSocket,
+                    new BufferedInputStream(leaderSocket.getInputStream()),
+                    leader);
             lh.start();
             leaderSocket.setSoTimeout(4000);
 
@@ -458,7 +463,6 @@ public class Zab1_0Test {
         }
     }
     
-    
     public void testFollowerConversation(FollowerConversation conversation) throws Exception {
         File tmpDir = File.createTempFile("test", "dir", testData);
         tmpDir.delete();
@@ -473,7 +477,9 @@ public class Zab1_0Test {
             
             ServerSocket ss = new ServerSocket();
             ss.bind(null);
-            follower.setLeaderSocketAddress((InetSocketAddress)ss.getLocalSocketAddress());
+            QuorumServer leaderQS = new QuorumServer(1,
+                    (InetSocketAddress) ss.getLocalSocketAddress());
+            follower.setLeaderQuorumServer(leaderQS);
             final Follower followerForThread = follower;
             
             followerThread = new Thread() {
@@ -526,7 +532,9 @@ public class Zab1_0Test {
 
             ServerSocket ss = new ServerSocket();
             ss.bind(null);
-            observer.setLeaderSocketAddress((InetSocketAddress)ss.getLocalSocketAddress());
+            QuorumServer leaderQS = new QuorumServer(1,
+                    (InetSocketAddress) ss.getLocalSocketAddress());
+            observer.setLeaderQuorumServer(leaderQS);
             final Observer observerForThread = observer;
 
             observerThread = new Thread() {
@@ -1338,14 +1346,14 @@ public class Zab1_0Test {
             super(self, zk);
         }
 
-        InetSocketAddress leaderAddr;
-        public void setLeaderSocketAddress(InetSocketAddress addr) {
-            leaderAddr = addr;
+        QuorumServer leaderQuorumServer;
+        public void setLeaderQuorumServer(QuorumServer quorumServer) {
+            leaderQuorumServer = quorumServer;
         }
         
         @Override
-        protected InetSocketAddress findLeader() {
-            return leaderAddr;
+        protected QuorumServer findLeader() {
+            return leaderQuorumServer;
         }
     }
     private ConversableFollower createFollower(File tmpDir, QuorumPeer peer)
@@ -1364,14 +1372,14 @@ public class Zab1_0Test {
             super(self, zk);
         }
         
-        InetSocketAddress leaderAddr;
-        public void setLeaderSocketAddress(InetSocketAddress addr) {
-            leaderAddr = addr;
+        QuorumServer leaderQuorumServer;
+        public void setLeaderQuorumServer(QuorumServer quorumServer) {
+            leaderQuorumServer = quorumServer;
         }
         
         @Override
-        protected InetSocketAddress findLeader() {
-            return leaderAddr;
+        protected QuorumServer findLeader() {
+            return leaderQuorumServer;
         }
     }
         
@@ -1389,7 +1397,7 @@ public class Zab1_0Test {
     
     private QuorumPeer createQuorumPeer(File tmpDir) throws IOException,
             FileNotFoundException {
-        QuorumPeer peer = new QuorumPeer();
+        QuorumPeer peer = QuorumPeer.testingQuorumPeer();
         peer.syncLimit = SYNC_LIMIT;
         peer.initLimit = 2;
         peer.tickTime = 2000;
@@ -1435,7 +1443,7 @@ public class Zab1_0Test {
             logFactory.append(req);
             logFactory.commit();
             ZKDatabase zkDb = new ZKDatabase(logFactory);
-            QuorumPeer peer = new QuorumPeer();
+            QuorumPeer peer = QuorumPeer.testingQuorumPeer();
             peer.setZKDatabase(zkDb);
             peer.setTxnFactory(logFactory);
             peer.getLastLoggedZxid();

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java
new file mode 100644
index 0000000..9617c70
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java
@@ -0,0 +1,120 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.zookeeper.server.quorum.auth;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+
+/*
+ * This code is originally from HDFS, see the similarly named file there
+ * in case of bug fixing, history, etc.
+ *
+ * Branch : trunk
+ * Github Revision: 1d1ab587e4e92ce3aea4cb144811f69145cb3b33
+ */
+
+/**
+ * KerberosSecurityTestcase provides a base class for using MiniKdc with other
+ * test cases. KerberosSecurityTestcase starts the MiniKdc (@Before) before
+ * running tests, and stop the MiniKdc (@After) after the testcases, using
+ * default settings (working dir and kdc configurations).
+ * <p>
+ * Users can directly inherit this class and implement their own test functions
+ * using the default settings, or override functions getTestDir() and
+ * createMiniKdcConf() to provide new settings.
+ */
+public class KerberosSecurityTestcase extends QuorumAuthTestBase {
+    private static MiniKdc kdc;
+    private static File workDir;
+    private static Properties conf;
+
+    @BeforeClass
+    public static void setUpSasl() throws Exception {
+        startMiniKdc();
+    }
+
+    @AfterClass
+    public static void tearDownSasl() throws Exception {
+        stopMiniKdc();
+        FileUtils.deleteQuietly(workDir);
+    }
+
+    public static void startMiniKdc() throws Exception {
+        createTestDir();
+        createMiniKdcConf();
+
+        kdc = new MiniKdc(conf, workDir);
+        kdc.start();
+    }
+
+    /**
+     * Create a working directory, it should be the build directory. Under this
+     * directory an ApacheDS working directory will be created, this directory
+     * will be deleted when the MiniKdc stops.
+     *
+     * @throws IOException
+     */
+    public static void createTestDir() throws IOException {
+        workDir = createTmpDir(
+                new File(System.getProperty("build.test.dir", "build")));
+    }
+
+    static File createTmpDir(File parentDir) throws IOException {
+        File tmpFile = File.createTempFile("test", ".junit", parentDir);
+        // don't delete tmpFile - this ensures we don't attempt to create
+        // a tmpDir with a duplicate name
+        File tmpDir = new File(tmpFile + ".dir");
+        // never true if tmpfile does it's job
+        Assert.assertFalse(tmpDir.exists());
+        Assert.assertTrue(tmpDir.mkdirs());
+        return tmpDir;
+    }
+
+    /**
+     * Create a Kdc configuration
+     */
+    public static void createMiniKdcConf() {
+        conf = MiniKdc.createConf();
+    }
+
+    public static void stopMiniKdc() {
+        if (kdc != null) {
+            kdc.stop();
+        }
+    }
+
+    public static MiniKdc getKdc() {
+        return kdc;
+    }
+
+    public static File getWorkDir() {
+        return workDir;
+    }
+
+    public static Properties getConf() {
+        return conf;
+    }
+}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java
new file mode 100644
index 0000000..4a75f83
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum.auth;
+
+import java.io.File;
+import java.util.UUID;
+
+import org.apache.zookeeper.util.SecurityUtils;
+
+public class KerberosTestUtils {
+    private static String keytabFile = new File(System.getProperty("test.dir", "build"), UUID.randomUUID().toString())
+            .getAbsolutePath();
+
+    public static String getRealm() {
+        return "EXAMPLE.COM";
+    }
+
+    public static String getLearnerPrincipal() {
+        return "learner@EXAMPLE.COM";
+    }
+
+    public static String getServerPrincipal() {
+        return "zkquorum/localhost@EXAMPLE.COM";
+    }
+
+    public static String getHostLearnerPrincipal() {
+        return "learner/_HOST@EXAMPLE.COM";
+    }
+
+    public static String getHostServerPrincipal() {
+        return "zkquorum/_HOST@EXAMPLE.COM";
+    }
+
+    public static String getHostNamedLearnerPrincipal(String myHostname) {
+        return "learner/" + myHostname + "@EXAMPLE.COM";
+    }
+
+    public static String getKeytabFile() {
+        return keytabFile;
+    }
+
+    public static String replaceHostPattern(String principal) {
+        String[] components = principal.split("[/@]");
+        if (components == null || components.length < 2
+                || !components[1].equals(SecurityUtils.QUORUM_HOSTNAME_PATTERN)) {
+            return principal;
+        } else {
+            return replacePattern(components, "localhost");
+        }
+    }
+
+    public static String replacePattern(String[] components, String hostname) {
+        if (components.length == 3) {
+            return components[0] + "/" + hostname.toLowerCase() + "@"
+                    + components[2];
+        } else {
+            return components[0] + "/" + hostname.toLowerCase();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java
new file mode 100644
index 0000000..4afef41
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdc.java
@@ -0,0 +1,418 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum.auth;
+import org.apache.commons.io.Charsets;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.server.KdcConfigKey;
+import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer;
+import org.apache.kerby.util.IOUtil;
+import org.apache.kerby.util.NetworkUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+/**
+ * Mini KDC based on Apache Directory Server that can be embedded in testcases
+ * or used from command line as a standalone KDC.
+ * <p>
+ * <b>From within testcases:</b>
+ * <p>
+ * MiniKdc sets one System property when started and un-set when stopped:
+ * <ul>
+ *   <li>sun.security.krb5.debug: set to the debug value provided in the
+ *   configuration</li>
+ * </ul>
+ * Because of this, multiple MiniKdc instances cannot be started in parallel.
+ * For example, running testcases in parallel that start a KDC each. To
+ * accomplish this a single MiniKdc should be used for all testcases running
+ * in parallel.
+ * <p>
+ * MiniKdc default configuration values are:
+ * <ul>
+ *   <li>org.name=EXAMPLE (used to create the REALM)</li>
+ *   <li>org.domain=COM (used to create the REALM)</li>
+ *   <li>kdc.bind.address=localhost</li>
+ *   <li>kdc.port=0 (ephemeral port)</li>
+ *   <li>instance=DefaultKrbServer</li>
+ *   <li>max.ticket.lifetime=86400000 (1 day)</li>
+ *   <li>max.renewable.lifetime=604800000 (7 days)</li>
+ *   <li>transport=TCP</li>
+ *   <li>debug=false</li>
+ * </ul>
+ * The generated krb5.conf forces TCP connections.
+ */
+/*
+ * This code is originally from HDFS, see the file name MiniKdc there
+ * in case of bug fixing, history, etc.
+ *
+ * Branch : trunk
+ * Github Revision: 916140604ffef59466ba30832478311d3e6249bd
+ */
+public class MiniKdc {
+
+    public static final String JAVA_SECURITY_KRB5_CONF =
+            "java.security.krb5.conf";
+    public static final String SUN_SECURITY_KRB5_DEBUG =
+            "sun.security.krb5.debug";
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 4) {
+            System.out.println("Arguments: <WORKDIR> <MINIKDCPROPERTIES> " +
+                    "<KEYTABFILE> [<PRINCIPALS>]+");
+            System.exit(1);
+        }
+        File workDir = new File(args[0]);
+        if (!workDir.exists()) {
+            throw new RuntimeException("Specified work directory does not exists: "
+                    + workDir.getAbsolutePath());
+        }
+        Properties conf = createConf();
+        File file = new File(args[1]);
+        if (!file.exists()) {
+            throw new RuntimeException("Specified configuration does not exists: "
+                    + file.getAbsolutePath());
+        }
+        Properties userConf = new Properties();
+        InputStreamReader r = null;
+        try {
+            r = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8);
+            userConf.load(r);
+        } finally {
+            if (r != null) {
+                r.close();
+            }
+        }
+        for (Map.Entry<?, ?> entry : userConf.entrySet()) {
+            conf.put(entry.getKey(), entry.getValue());
+        }
+        final MiniKdc miniKdc = new MiniKdc(conf, workDir);
+        miniKdc.start();
+        File krb5conf = new File(workDir, "krb5.conf");
+        if (miniKdc.getKrb5conf().renameTo(krb5conf)) {
+            File keytabFile = new File(args[2]).getAbsoluteFile();
+            String[] principals = new String[args.length - 3];
+            System.arraycopy(args, 3, principals, 0, args.length - 3);
+            miniKdc.createPrincipal(keytabFile, principals);
+            System.out.println();
+            System.out.println("Standalone MiniKdc Running");
+            System.out.println("---------------------------------------------------");
+            System.out.println("  Realm           : " + miniKdc.getRealm());
+            System.out.println("  Running at      : " + miniKdc.getHost() + ":" +
+                    miniKdc.getHost());
+            System.out.println("  krb5conf        : " + krb5conf);
+            System.out.println();
+            System.out.println("  created keytab  : " + keytabFile);
+            System.out.println("  with principals : " + Arrays.asList(principals));
+            System.out.println();
+            System.out.println(" Do <CTRL-C> or kill <PID> to stop it");
+            System.out.println("---------------------------------------------------");
+            System.out.println();
+            Runtime.getRuntime().addShutdownHook(new Thread() {
+                @Override
+                public void run() {
+                    miniKdc.stop();
+                }
+            });
+        } else {
+            throw new RuntimeException("Cannot rename KDC's krb5conf to "
+                    + krb5conf.getAbsolutePath());
+        }
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(MiniKdc.class);
+
+    public static final String ORG_NAME = "org.name";
+    public static final String ORG_DOMAIN = "org.domain";
+    public static final String KDC_BIND_ADDRESS = "kdc.bind.address";
+    public static final String KDC_PORT = "kdc.port";
+    public static final String INSTANCE = "instance";
+    public static final String MAX_TICKET_LIFETIME = "max.ticket.lifetime";
+    public static final String MAX_RENEWABLE_LIFETIME = "max.renewable.lifetime";
+    public static final String TRANSPORT = "transport";
+    public static final String DEBUG = "debug";
+
+    private static final Set<String> PROPERTIES = new HashSet<String>();
+    private static final Properties DEFAULT_CONFIG = new Properties();
+
+    static {
+        PROPERTIES.add(ORG_NAME);
+        PROPERTIES.add(ORG_DOMAIN);
+        PROPERTIES.add(KDC_BIND_ADDRESS);
+        PROPERTIES.add(KDC_BIND_ADDRESS);
+        PROPERTIES.add(KDC_PORT);
+        PROPERTIES.add(INSTANCE);
+        PROPERTIES.add(TRANSPORT);
+        PROPERTIES.add(MAX_TICKET_LIFETIME);
+        PROPERTIES.add(MAX_RENEWABLE_LIFETIME);
+
+        DEFAULT_CONFIG.setProperty(KDC_BIND_ADDRESS, "localhost");
+        DEFAULT_CONFIG.setProperty(KDC_PORT, "0");
+        DEFAULT_CONFIG.setProperty(INSTANCE, "DefaultKrbServer");
+        DEFAULT_CONFIG.setProperty(ORG_NAME, "EXAMPLE");
+        DEFAULT_CONFIG.setProperty(ORG_DOMAIN, "COM");
+        DEFAULT_CONFIG.setProperty(TRANSPORT, "TCP");
+        DEFAULT_CONFIG.setProperty(MAX_TICKET_LIFETIME, "86400000");
+        DEFAULT_CONFIG.setProperty(MAX_RENEWABLE_LIFETIME, "604800000");
+        DEFAULT_CONFIG.setProperty(DEBUG, "false");
+    }
+
+    /**
+     * Convenience method that returns MiniKdc default configuration.
+     * <p>
+     * The returned configuration is a copy, it can be customized before using
+     * it to create a MiniKdc.
+     * @return a MiniKdc default configuration.
+     */
+    public static Properties createConf() {
+        return (Properties) DEFAULT_CONFIG.clone();
+    }
+
+    private Properties conf;
+    private SimpleKdcServer simpleKdc;
+    private int port;
+    private String realm;
+    private File workDir;
+    private File krb5conf;
+    private String transport;
+    private boolean krb5Debug;
+
+    public void setTransport(String transport) {
+        this.transport = transport;
+    }
+    /**
+     * Creates a MiniKdc.
+     *
+     * @param conf MiniKdc configuration.
+     * @param workDir working directory, it should be the build directory. Under
+     * this directory an ApacheDS working directory will be created, this
+     * directory will be deleted when the MiniKdc stops.
+     * @throws Exception thrown if the MiniKdc could not be created.
+     */
+    public MiniKdc(Properties conf, File workDir) throws Exception {
+        if (!conf.keySet().containsAll(PROPERTIES)) {
+            Set<String> missingProperties = new HashSet<String>(PROPERTIES);
+            missingProperties.removeAll(conf.keySet());
+            throw new IllegalArgumentException("Missing configuration properties: "
+                    + missingProperties);
+        }
+        this.workDir = new File(workDir, Long.toString(System.currentTimeMillis()));
+        if (!this.workDir.exists()
+                && !this.workDir.mkdirs()) {
+            throw new RuntimeException("Cannot create directory " + this.workDir);
+        }
+        LOG.info("Configuration:");
+        LOG.info("---------------------------------------------------------------");
+        for (Map.Entry<?, ?> entry : conf.entrySet()) {
+            LOG.info("  {}: {}", entry.getKey(), entry.getValue());
+        }
+        LOG.info("---------------------------------------------------------------");
+        this.conf = conf;
+        port = Integer.parseInt(conf.getProperty(KDC_PORT));
+        String orgName= conf.getProperty(ORG_NAME);
+        String orgDomain = conf.getProperty(ORG_DOMAIN);
+        realm = orgName.toUpperCase(Locale.ENGLISH) + "."
+                + orgDomain.toUpperCase(Locale.ENGLISH);
+    }
+
+    /**
+     * Returns the port of the MiniKdc.
+     *
+     * @return the port of the MiniKdc.
+     */
+    public int getPort() {
+        return port;
+    }
+
+    /**
+     * Returns the host of the MiniKdc.
+     *
+     * @return the host of the MiniKdc.
+     */
+    public String getHost() {
+        return conf.getProperty(KDC_BIND_ADDRESS);
+    }
+
+    /**
+     * Returns the realm of the MiniKdc.
+     *
+     * @return the realm of the MiniKdc.
+     */
+    public String getRealm() {
+        return realm;
+    }
+
+    public File getKrb5conf() {
+        krb5conf = new File(System.getProperty(JAVA_SECURITY_KRB5_CONF));
+        return krb5conf;
+    }
+
+    /**
+     * Starts the MiniKdc.
+     *
+     * @throws Exception thrown if the MiniKdc could not be started.
+     */
+    public synchronized void start() throws Exception {
+        if (simpleKdc != null) {
+            throw new RuntimeException("Already started");
+        }
+        simpleKdc = new SimpleKdcServer();
+        prepareKdcServer();
+        simpleKdc.init();
+        resetDefaultRealm();
+        simpleKdc.start();
+        LOG.info("MiniKdc stated.");
+    }
+
+    private void resetDefaultRealm() throws IOException {
+        InputStream templateResource = new FileInputStream(
+                getKrb5conf().getAbsolutePath());
+        String content = IOUtil.readInput(templateResource);
+        content = content.replaceAll("default_realm = .*\n",
+                "default_realm = " + getRealm() + "\n");
+        IOUtil.writeFile(content, getKrb5conf());
+    }
+
+    private void prepareKdcServer() throws Exception {
+        // transport
+        simpleKdc.setWorkDir(workDir);
+        simpleKdc.setKdcHost(getHost());
+        simpleKdc.setKdcRealm(realm);
+        if (transport == null) {
+            transport = conf.getProperty(TRANSPORT);
+        }
+        if (port == 0) {
+            port = NetworkUtil.getServerPort();
+        }
+        if (transport != null) {
+            if (transport.trim().equals("TCP")) {
+                simpleKdc.setKdcTcpPort(port);
+                simpleKdc.setAllowUdp(false);
+            } else if (transport.trim().equals("UDP")) {
+                simpleKdc.setKdcUdpPort(port);
+                simpleKdc.setAllowTcp(false);
+            } else {
+                throw new IllegalArgumentException("Invalid transport: " + transport);
+            }
+        } else {
+            throw new IllegalArgumentException("Need to set transport!");
+        }
+        simpleKdc.getKdcConfig().setString(KdcConfigKey.KDC_SERVICE_NAME,
+                conf.getProperty(INSTANCE));
+        if (conf.getProperty(DEBUG) != null) {
+            krb5Debug = getAndSet(SUN_SECURITY_KRB5_DEBUG, conf.getProperty(DEBUG));
+        }
+    }
+
+    /**
+     * Stops the MiniKdc
+     */
+    public synchronized void stop() {
+        if (simpleKdc != null) {
+            try {
+                simpleKdc.stop();
+            } catch (KrbException e) {
+                e.printStackTrace();
+            } finally {
+                if(conf.getProperty(DEBUG) != null) {
+                    System.setProperty(SUN_SECURITY_KRB5_DEBUG,
+                            Boolean.toString(krb5Debug));
+                }
+            }
+        }
+        delete(workDir);
+        try {
+            // Will be fixed in next Kerby version.
+            Thread.sleep(1000);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        LOG.info("MiniKdc stopped.");
+    }
+
+    private void delete(File f) {
+        if (f.isFile()) {
+            if (! f.delete()) {
+                LOG.warn("WARNING: cannot delete file " + f.getAbsolutePath());
+            }
+        } else {
+            for (File c: f.listFiles()) {
+                delete(c);
+            }
+            if (! f.delete()) {
+                LOG.warn("WARNING: cannot delete directory " + f.getAbsolutePath());
+            }
+        }
+    }
+
+    /**
+     * Creates a principal in the KDC with the specified user and password.
+     *
+     * @param principal principal name, do not include the domain.
+     * @param password password.
+     * @throws Exception thrown if the principal could not be created.
+     */
+    public synchronized void createPrincipal(String principal, String password)
+            throws Exception {
+        simpleKdc.createPrincipal(principal, password);
+    }
+
+    /**
+     * Creates multiple principals in the KDC and adds them to a keytab file.
+     *
+     * @param keytabFile keytab file to add the created principals.
+     * @param principals principals to add to the KDC, do not include the domain.
+     * @throws Exception thrown if the principals or the keytab file could not be
+     * created.
+     */
+    public synchronized void createPrincipal(File keytabFile,
+                                             String ... principals)
+            throws Exception {
+        simpleKdc.createPrincipals(principals);
+        if (keytabFile.exists() && !keytabFile.delete()) {
+            LOG.error("Failed to delete keytab file: " + keytabFile);
+        }
+        for (String principal : principals) {
+            simpleKdc.getKadmin().exportKeytab(keytabFile, principal);
+        }
+    }
+
+    /**
+     * Set the System property; return the old value for caching.
+     *
+     * @param sysprop property
+     * @param debug true or false
+     * @return the previous value
+     */
+    private boolean getAndSet(String sysprop, String debug) {
+        boolean old = Boolean.getBoolean(sysprop);
+        System.setProperty(sysprop, debug);
+        return old;
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java
new file mode 100644
index 0000000..a7bbf7f
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java
@@ -0,0 +1,184 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum.auth;
+
+import org.apache.kerby.kerberos.kerb.keytab.Keytab;
+import org.apache.kerby.kerberos.kerb.type.base.PrincipalName;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.security.auth.Subject;
+import javax.security.auth.kerberos.KerberosPrincipal;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginContext;
+import java.io.File;
+import java.security.Principal;
+import java.util.List;
+import java.util.Set;
+import java.util.Map;
+import java.util.HashSet;
+import java.util.HashMap;
+import java.util.Arrays;
+
+/*
+ * This code is originally from HDFS, see the file name TestMiniKdc there
+ * in case of bug fixing, history, etc.
+ *
+ * Branch : trunk
+ * Github Revision: 916140604ffef59466ba30832478311d3e6249bd
+ */
+public class MiniKdcTest extends KerberosSecurityTestcase {
+    private static final boolean IBM_JAVA = System.getProperty("java.vendor")
+            .contains("IBM");
+
+    @Test(timeout = 60000)
+    public void testMiniKdcStart() {
+        MiniKdc kdc = getKdc();
+        Assert.assertNotSame(0, kdc.getPort());
+    }
+
+    @Test(timeout = 60000)
+    public void testKeytabGen() throws Exception {
+        MiniKdc kdc = getKdc();
+        File workDir = getWorkDir();
+
+        kdc.createPrincipal(new File(workDir, "keytab"), "foo/bar", "bar/foo");
+        List<PrincipalName> principalNameList =
+                Keytab.loadKeytab(new File(workDir, "keytab")).getPrincipals();
+
+        Set<String> principals = new HashSet<String>();
+        for (PrincipalName principalName : principalNameList) {
+          principals.add(principalName.getName());
+        }
+
+        Assert.assertEquals(new HashSet<String>(Arrays.asList(
+                "foo/bar@" + kdc.getRealm(), "bar/foo@" + kdc.getRealm())),
+                principals);
+      }
+
+    private static class KerberosConfiguration extends Configuration {
+        private String principal;
+        private String keytab;
+        private boolean isInitiator;
+
+        private KerberosConfiguration(String principal, File keytab,
+                boolean client) {
+            this.principal = principal;
+            this.keytab = keytab.getAbsolutePath();
+            this.isInitiator = client;
+        }
+
+        public static Configuration createClientConfig(String principal,
+                File keytab) {
+            return new KerberosConfiguration(principal, keytab, true);
+        }
+
+        public static Configuration createServerConfig(String principal,
+                File keytab) {
+            return new KerberosConfiguration(principal, keytab, false);
+        }
+
+        private static String getKrb5LoginModuleName() {
+            return System.getProperty("java.vendor").contains("IBM")
+                    ? "com.ibm.security.auth.module.Krb5LoginModule"
+                    : "com.sun.security.auth.module.Krb5LoginModule";
+        }
+
+        @Override
+        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
+            Map<String, String> options = new HashMap<String, String>();
+            options.put("principal", principal);
+            options.put("refreshKrb5Config", "true");
+            if (IBM_JAVA) {
+                options.put("useKeytab", keytab);
+                options.put("credsType", "both");
+            } else {
+                options.put("keyTab", keytab);
+                options.put("useKeyTab", "true");
+                options.put("storeKey", "true");
+                options.put("doNotPrompt", "true");
+                options.put("useTicketCache", "true");
+                options.put("renewTGT", "true");
+                options.put("isInitiator", Boolean.toString(isInitiator));
+            }
+            String ticketCache = System.getenv("KRB5CCNAME");
+            if (ticketCache != null) {
+                options.put("ticketCache", ticketCache);
+            }
+            options.put("debug", "true");
+
+            return new AppConfigurationEntry[] {
+                    new AppConfigurationEntry(getKrb5LoginModuleName(),
+                            AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
+                            options) };
+        }
+    }
+
+    @Test(timeout = 60000)
+    public void testKerberosLogin() throws Exception {
+        MiniKdc kdc = getKdc();
+        File workDir = getWorkDir();
+        LoginContext loginContext = null;
+        try {
+            String principal = "foo";
+            File keytab = new File(workDir, "foo.keytab");
+            kdc.createPrincipal(keytab, principal);
+
+            Set<Principal> principals = new HashSet<Principal>();
+            principals.add(new KerberosPrincipal(principal));
+
+            // client login
+            Subject subject = new Subject(false, principals,
+                    new HashSet<Object>(), new HashSet<Object>());
+            loginContext = new LoginContext("", subject, null,
+                    KerberosConfiguration.createClientConfig(principal,
+                            keytab));
+            loginContext.login();
+            subject = loginContext.getSubject();
+            Assert.assertEquals(1, subject.getPrincipals().size());
+            Assert.assertEquals(KerberosPrincipal.class,
+                    subject.getPrincipals().iterator().next().getClass());
+            Assert.assertEquals(principal + "@" + kdc.getRealm(),
+                    subject.getPrincipals().iterator().next().getName());
+            loginContext.logout();
+
+            // server login
+            subject = new Subject(false, principals, new HashSet<Object>(),
+                    new HashSet<Object>());
+            loginContext = new LoginContext("", subject, null,
+                    KerberosConfiguration.createServerConfig(principal,
+                            keytab));
+            loginContext.login();
+            subject = loginContext.getSubject();
+            Assert.assertEquals(1, subject.getPrincipals().size());
+            Assert.assertEquals(KerberosPrincipal.class,
+                    subject.getPrincipals().iterator().next().getClass());
+            Assert.assertEquals(principal + "@" + kdc.getRealm(),
+                    subject.getPrincipals().iterator().next().getName());
+            loginContext.logout();
+
+        } finally {
+            if (loginContext != null) {
+                loginContext.logout();
+            }
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8a06bd1c/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java
----------------------------------------------------------------------
diff --git a/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java
new file mode 100644
index 0000000..8978d17
--- /dev/null
+++ b/src/java/test/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java
@@ -0,0 +1,146 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.server.quorum.auth;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.zookeeper.PortAssignment;
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread;
+import org.apache.zookeeper.test.ClientBase;
+import org.junit.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * QuorumAuthTestBase provides a base class for testing quorum peer mutual
+ * authentication using SASL mechanisms.
+ */
+public class QuorumAuthTestBase extends ZKTestCase {
+    protected static final Logger LOG = LoggerFactory.getLogger(QuorumAuthTestBase.class);
+    protected List<MainThread> mt = new ArrayList<MainThread>();
+    protected static File jaasConfigDir;
+
+    public static void setupJaasConfig(String jaasEntries) {
+        try {
+            jaasConfigDir = ClientBase.createTmpDir();
+            File saslConfFile = new File(jaasConfigDir, "jaas.conf");
+            FileWriter fwriter = new FileWriter(saslConfFile);
+            fwriter.write(jaasEntries);
+            fwriter.close();
+            System.setProperty("java.security.auth.login.config",
+                    saslConfFile.getAbsolutePath());
+        } catch (IOException ioe) {
+            LOG.error("Failed to create tmp directory to hold JAAS conf file", ioe);
+            // could not create tmp directory to hold JAAS conf file : test will
+            // fail now.
+        }
+    }
+
+    public static void cleanupJaasConfig() {
+        if (jaasConfigDir != null) {
+            FileUtils.deleteQuietly(jaasConfigDir);
+        }
+    }
+
+    protected String startQuorum(final int serverCount,
+            Map<String, String> authConfigs, int authServerCount) throws IOException {
+        StringBuilder connectStr = new StringBuilder();
+        final int[] clientPorts = startQuorum(serverCount, connectStr,
+                authConfigs, authServerCount);
+        for (int i = 0; i < serverCount; i++) {
+            Assert.assertTrue("waiting for server " + i + " being up",
+                    ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i],
+                            ClientBase.CONNECTION_TIMEOUT));
+        }
+        return connectStr.toString();
+    }
+
+    protected int[] startQuorum(final int serverCount, StringBuilder connectStr,
+            Map<String, String> authConfigs, int authServerCount) throws IOException {
+        final int clientPorts[] = new int[serverCount];
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < serverCount; i++) {
+            clientPorts[i] = PortAssignment.unique();
+            String server = String.format(
+                    "server.%d=localhost:%d:%d:participant", i,
+                    PortAssignment.unique(), PortAssignment.unique());
+            sb.append(server + "\n");
+            connectStr.append("127.0.0.1:" + clientPorts[i]);
+            if (i < serverCount - 1) {
+                connectStr.append(",");
+            }
+        }
+        String quorumCfg = sb.toString();
+        // servers with authentication interfaces configured
+        int i = 0;
+        for (; i < authServerCount; i++) {
+            startServer(authConfigs, clientPorts, quorumCfg, i);
+        }
+        // servers without any authentication configured
+        for (int j = 0; j < serverCount - authServerCount; j++, i++) {
+            MainThread mthread = new MainThread(i, clientPorts[i], quorumCfg);
+            mt.add(mthread);
+            mthread.start();
+        }
+        return clientPorts;
+    }
+
+    private void startServer(Map<String, String> authConfigs,
+            final int[] clientPorts, String quorumCfg, int i)
+                    throws IOException {
+        MainThread mthread = new MainThread(i, clientPorts[i], quorumCfg,
+                authConfigs);
+        mt.add(mthread);
+        mthread.start();
+    }
+
+    protected void startServer(MainThread restartPeer,
+            Map<String, String> authConfigs) throws IOException {
+        MainThread mthread = new MainThread(restartPeer.getMyid(),
+                restartPeer.getClientPort(), restartPeer.getQuorumCfgSection(),
+                authConfigs);
+        mt.add(mthread);
+        mthread.start();
+    }
+
+    void shutdownAll() {
+        for (int i = 0; i < mt.size(); i++) {
+            shutdown(i);
+        }
+    }
+
+    MainThread shutdown(int index) {
+        MainThread mainThread = mt.get(index);
+        try {
+            mainThread.shutdown();
+        } catch (InterruptedException e) {
+        } finally {
+            mt.remove(index);
+        }
+        mainThread.deleteBaseDir();
+        return mainThread;
+    }
+}