You need to sign in or sign up before continuing.
提交 a533f45f 编写于 作者: R robm

Merge

/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -916,6 +916,9 @@ assertEquals("[x, y, z]", pb.command().toString());
* @throws NullPointerException if any argument is null
*/
public MethodHandle findConstructor(Class<?> refc, MethodType type) throws NoSuchMethodException, IllegalAccessException {
if (refc.isArray()) {
throw new NoSuchMethodException("no constructor for array class: " + refc.getName());
}
String name = "<init>";
MemberName ctor = resolveOrFail(REF_newInvokeSpecial, refc, name, type);
return getDirectConstructor(refc, ctor);
......
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -25,12 +25,16 @@
package java.security;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import sun.misc.JavaSecurityProtectionDomainAccess;
import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
import sun.security.util.Debug;
......@@ -456,25 +460,130 @@ public class ProtectionDomain {
/**
* Used for storing ProtectionDomains as keys in a Map.
*/
final class Key {}
static final class Key {}
static {
SharedSecrets.setJavaSecurityProtectionDomainAccess(
new JavaSecurityProtectionDomainAccess() {
@Override
public ProtectionDomainCache getProtectionDomainCache() {
return new ProtectionDomainCache() {
private final Map<Key, PermissionCollection> map =
Collections.synchronizedMap
(new WeakHashMap<Key, PermissionCollection>());
public void put(ProtectionDomain pd,
PermissionCollection pc) {
map.put((pd == null ? null : pd.key), pc);
return new PDCache();
}
});
}
/**
* A cache of ProtectionDomains and their Permissions.
*
* This class stores ProtectionDomains as weak keys in a ConcurrentHashMap
* with additional support for checking and removing weak keys that are no
* longer in use. There can be cases where the permission collection may
* have a chain of strong references back to the ProtectionDomain, which
* ordinarily would prevent the entry from being removed from the map. To
* address that, we wrap the permission collection in a SoftReference so
* that it can be reclaimed by the garbage collector due to memory demand.
*/
private static class PDCache implements ProtectionDomainCache {
private final ConcurrentHashMap<WeakProtectionDomainKey,
SoftReference<PermissionCollection>>
pdMap = new ConcurrentHashMap<>();
private final ReferenceQueue<Key> queue = new ReferenceQueue<>();
@Override
public void put(ProtectionDomain pd, PermissionCollection pc) {
processQueue(queue, pdMap);
WeakProtectionDomainKey weakPd =
new WeakProtectionDomainKey(pd, queue);
pdMap.put(weakPd, new SoftReference<>(pc));
}
@Override
public PermissionCollection get(ProtectionDomain pd) {
return pd == null ? map.get(null) : map.get(pd.key);
processQueue(queue, pdMap);
WeakProtectionDomainKey weakPd = new WeakProtectionDomainKey(pd);
SoftReference<PermissionCollection> sr = pdMap.get(weakPd);
return (sr == null) ? null : sr.get();
}
/**
* Removes weak keys from the map that have been enqueued
* on the reference queue and are no longer in use.
*/
private static void processQueue(ReferenceQueue<Key> queue,
ConcurrentHashMap<? extends
WeakReference<Key>, ?> pdMap) {
Reference<? extends Key> ref;
while ((ref = queue.poll()) != null) {
pdMap.remove(ref);
}
}
}
/**
* A weak key for a ProtectionDomain.
*/
private static class WeakProtectionDomainKey extends WeakReference<Key> {
/**
* Saved value of the referent's identity hash code, to maintain
* a consistent hash code after the referent has been cleared
*/
private final int hash;
/**
* A key representing a null ProtectionDomain.
*/
private static final Key NULL_KEY = new Key();
/**
* Create a new WeakProtectionDomain with the specified domain and
* registered with a queue.
*/
WeakProtectionDomainKey(ProtectionDomain pd, ReferenceQueue<Key> rq) {
this((pd == null ? NULL_KEY : pd.key), rq);
}
WeakProtectionDomainKey(ProtectionDomain pd) {
this(pd == null ? NULL_KEY : pd.key);
}
private WeakProtectionDomainKey(Key key, ReferenceQueue<Key> rq) {
super(key, rq);
hash = key.hashCode();
}
private WeakProtectionDomainKey(Key key) {
super(key);
hash = key.hashCode();
}
/**
* Returns the identity hash code of the original referent.
*/
@Override
public int hashCode() {
return hash;
}
/**
* Returns true if the given object is an identical
* WeakProtectionDomainKey instance, or, if this object's referent
* has not been cleared and the given object is another
* WeakProtectionDomainKey instance with an identical non-null
* referent as this one.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof WeakProtectionDomainKey) {
Object referent = get();
return (referent != null) &&
(referent == ((WeakProtectionDomainKey)obj).get());
} else {
return false;
}
};
}
});
}
}
......@@ -1044,40 +1044,6 @@ final class CipherBox {
return nonce;
}
/*
* Is this cipher available?
*
* This method can only be called by CipherSuite.BulkCipher.isAvailable()
* to test the availability of a cipher suites. Please DON'T use it in
* other places, otherwise, the behavior may be unexpected because we may
* initialize AEAD cipher improperly in the method.
*/
Boolean isAvailable() {
// We won't know whether a cipher for a particular key size is
// available until the cipher is successfully initialized.
//
// We do not initialize AEAD cipher in the constructor. Need to
// initialize the cipher to ensure that the AEAD mode for a
// particular key size is supported.
if (cipherType == AEAD_CIPHER) {
try {
Authenticator authenticator =
new Authenticator(protocolVersion);
byte[] nonce = authenticator.sequenceNumber();
byte[] iv = Arrays.copyOf(fixedIv,
fixedIv.length + nonce.length);
System.arraycopy(nonce, 0, iv, fixedIv.length, nonce.length);
GCMParameterSpec spec = new GCMParameterSpec(tagSize * 8, iv);
cipher.init(mode, key, spec, random);
} catch (Exception e) {
return Boolean.FALSE;
}
} // Otherwise, we have initialized the cipher in the constructor.
return Boolean.TRUE;
}
/**
* Sanity check the length of a fragment before decryption.
*
......
......@@ -75,12 +75,6 @@ final class CipherSuite implements Comparable<CipherSuite> {
// minimum priority for default enabled CipherSuites
final static int DEFAULT_SUITES_PRIORITY = 300;
// Flag indicating if CipherSuite availability can change dynamically.
// This is the case when we rely on a JCE cipher implementation that
// may not be available in the installed JCE providers.
// It is true because we might not have an ECC implementation.
final static boolean DYNAMIC_AVAILABILITY = true;
private final static boolean ALLOW_ECC = Debug.getBooleanProperty
("com.sun.net.ssl.enableECC", true);
......@@ -186,9 +180,6 @@ final class CipherSuite implements Comparable<CipherSuite> {
* Return whether this CipherSuite is available for use. A
* CipherSuite may be unavailable even if it is supported
* (i.e. allowed == true) if the required JCE cipher is not installed.
* In some configuration, this situation may change over time, call
* CipherSuiteList.clearAvailableCache() before this method to obtain
* the most current status.
*/
boolean isAvailable() {
return allowed && keyExchange.isAvailable() && cipher.isAvailable();
......@@ -404,10 +395,6 @@ final class CipherSuite implements Comparable<CipherSuite> {
*/
final static class BulkCipher {
// Map BulkCipher -> Boolean(available)
private final static Map<BulkCipher,Boolean> availableCache =
new HashMap<>(8);
// descriptive name including key size, e.g. AES/128
final String description;
......@@ -451,6 +438,9 @@ final class CipherSuite implements Comparable<CipherSuite> {
// The secure random used to detect the cipher availability.
private final static SecureRandom secureRandom;
// runtime availability
private final boolean isAvailable;
static {
try {
secureRandom = JsseJce.getSecureRandom();
......@@ -475,6 +465,17 @@ final class CipherSuite implements Comparable<CipherSuite> {
this.expandedKeySize = expandedKeySize;
this.exportable = true;
// availability of this bulk cipher
//
// Currently all supported ciphers except AES are always available
// via the JSSE internal implementations. We also assume AES/128 of
// CBC mode is always available since it is shipped with the SunJCE
// provider. However, AES/256 is unavailable when the default JCE
// policy jurisdiction files are installed because of key length
// restrictions.
this.isAvailable =
allowed ? isUnlimited(keySize, transformation) : false;
}
BulkCipher(String transformation, CipherType cipherType, int keySize,
......@@ -491,6 +492,17 @@ final class CipherSuite implements Comparable<CipherSuite> {
this.expandedKeySize = keySize;
this.exportable = false;
// availability of this bulk cipher
//
// Currently all supported ciphers except AES are always available
// via the JSSE internal implementations. We also assume AES/128 of
// CBC mode is always available since it is shipped with the SunJCE
// provider. However, AES/256 is unavailable when the default JCE
// policy jurisdiction files are installed because of key length
// restrictions.
this.isAvailable =
allowed ? isUnlimited(keySize, transformation) : false;
}
/**
......@@ -508,84 +520,27 @@ final class CipherSuite implements Comparable<CipherSuite> {
/**
* Test if this bulk cipher is available. For use by CipherSuite.
*
* Currently all supported ciphers except AES are always available
* via the JSSE internal implementations. We also assume AES/128 of
* CBC mode is always available since it is shipped with the SunJCE
* provider. However, AES/256 is unavailable when the default JCE
* policy jurisdiction files are installed because of key length
* restrictions, and AEAD is unavailable when the underlying providers
* do not support AEAD/GCM mode.
*/
boolean isAvailable() {
if (allowed == false) {
return false;
}
if ((this == B_AES_256) ||
(this.cipherType == CipherType.AEAD_CIPHER)) {
return isAvailable(this);
}
// always available
return true;
return this.isAvailable;
}
// for use by CipherSuiteList.clearAvailableCache();
static synchronized void clearAvailableCache() {
if (DYNAMIC_AVAILABILITY) {
availableCache.clear();
}
}
private static synchronized boolean isAvailable(BulkCipher cipher) {
Boolean b = availableCache.get(cipher);
if (b == null) {
int keySizeInBits = cipher.keySize * 8;
private static boolean isUnlimited(int keySize, String transformation) {
int keySizeInBits = keySize * 8;
if (keySizeInBits > 128) { // need the JCE unlimited
// strength jurisdiction policy
try {
if (Cipher.getMaxAllowedKeyLength(
cipher.transformation) < keySizeInBits) {
b = Boolean.FALSE;
}
} catch (Exception e) {
b = Boolean.FALSE;
}
}
transformation) < keySizeInBits) {
if (b == null) {
b = Boolean.FALSE; // may be reset to TRUE if
// the cipher is available
CipherBox temporary = null;
try {
SecretKey key = new SecretKeySpec(
new byte[cipher.expandedKeySize],
cipher.algorithm);
IvParameterSpec iv;
if (cipher.cipherType == CipherType.AEAD_CIPHER) {
iv = new IvParameterSpec(
new byte[cipher.fixedIvSize]);
} else {
iv = new IvParameterSpec(new byte[cipher.ivSize]);
}
temporary = cipher.newCipher(
ProtocolVersion.DEFAULT,
key, iv, secureRandom, true);
b = temporary.isAvailable();
} catch (NoSuchAlgorithmException e) {
// not available
} finally {
if (temporary != null) {
temporary.dispose();
}
return false;
}
} catch (Exception e) {
return false;
}
availableCache.put(cipher, b);
}
return b.booleanValue();
return true;
}
@Override
......
/*
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -74,25 +74,13 @@ final class CipherSuiteList {
throw new IllegalArgumentException("CipherSuites may not be null");
}
cipherSuites = new ArrayList<CipherSuite>(names.length);
// refresh available cache once if a CipherSuite is not available
// (maybe new JCE providers have been installed)
boolean refreshed = false;
for (int i = 0; i < names.length; i++) {
String suiteName = names[i];
CipherSuite suite = CipherSuite.valueOf(suiteName);
if (suite.isAvailable() == false) {
if (refreshed == false) {
// clear the cache so that the isAvailable() call below
// does a full check
clearAvailableCache();
refreshed = true;
}
// still missing?
if (suite.isAvailable() == false) {
throw new IllegalArgumentException("Cannot support "
+ suiteName + " with currently installed providers");
}
}
cipherSuites.add(suite);
}
}
......@@ -195,16 +183,4 @@ final class CipherSuiteList {
}
s.putBytes16(suiteBytes);
}
/**
* Clear cache of available ciphersuites. If we support all ciphers
* internally, there is no need to clear the cache and calling this
* method has no effect.
*/
static synchronized void clearAvailableCache() {
if (CipherSuite.DYNAMIC_AVAILABILITY) {
CipherSuite.BulkCipher.clearAvailableCache();
JsseJce.clearEcAvailable();
}
}
}
......@@ -55,11 +55,6 @@ final class JsseJce {
private final static ProviderList fipsProviderList;
// Flag indicating whether EC crypto is available.
// If null, then we have not checked yet.
// If yes, then all the EC based crypto we need is available.
private static Boolean ecAvailable;
// Flag indicating whether Kerberos crypto is available.
// If true, then all the Kerberos-based crypto we need is available.
private final static boolean kerberosAvailable;
......@@ -195,24 +190,8 @@ final class JsseJce {
// no instantiation of this class
}
synchronized static boolean isEcAvailable() {
if (ecAvailable == null) {
try {
JsseJce.getSignature(SIGNATURE_ECDSA);
JsseJce.getSignature(SIGNATURE_RAWECDSA);
JsseJce.getKeyAgreement("ECDH");
JsseJce.getKeyFactory("EC");
JsseJce.getKeyPairGenerator("EC");
ecAvailable = true;
} catch (Exception e) {
ecAvailable = false;
}
}
return ecAvailable;
}
synchronized static void clearEcAvailable() {
ecAvailable = null;
static boolean isEcAvailable() {
return EcAvailability.isAvailable;
}
static boolean isKerberosAvailable() {
......@@ -414,4 +393,27 @@ final class JsseJce {
}
}
// lazy initialization holder class idiom for static default parameters
//
// See Effective Java Second Edition: Item 71.
private static class EcAvailability {
// Is EC crypto available?
private final static boolean isAvailable;
static {
boolean mediator = true;
try {
JsseJce.getSignature(SIGNATURE_ECDSA);
JsseJce.getSignature(SIGNATURE_RAWECDSA);
JsseJce.getKeyAgreement("ECDH");
JsseJce.getKeyFactory("EC");
JsseJce.getKeyPairGenerator("EC");
} catch (Exception e) {
mediator = false;
}
isAvailable = mediator;
}
}
}
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8155106
* @run testng/othervm -ea -esa test.java.lang.invoke.ArrayConstructorTest
*/
package test.java.lang.invoke;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import static java.lang.invoke.MethodType.methodType;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
public class ArrayConstructorTest {
static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
@Test
public static void testFindConstructorArray() {
boolean caught = false;
try {
MethodHandle h = LOOKUP.findConstructor(Object[].class, methodType(void.class));
} catch (NoSuchMethodException nsme) {
assertEquals("no constructor for array class: [Ljava.lang.Object;", nsme.getMessage());
caught = true;
} catch (Exception e) {
throw new AssertionError("unexpected exception: " + e);
}
assertTrue(caught);
}
}
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -23,6 +23,7 @@
/* @test
* @summary unit tests for method handles which permute their arguments
* @library /lib/testlibrary/jsr292 /lib/testlibrary
* @run testng/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies -ea -esa -DPermuteArgsTest.MAX_ARITY=8 test.java.lang.invoke.PermuteArgsTest
*/
/* Examples of manual runs:
......@@ -36,6 +37,8 @@ package test.java.lang.invoke;
import org.testng.*;
import org.testng.annotations.*;
import com.oracle.testlibrary.jsr292.CodeCacheOverflowProcessor;
import java.util.*;
import java.lang.reflect.*;
......@@ -122,9 +125,15 @@ public class PermuteArgsTest {
}
new PermuteArgsTest().test();
}
static int testCases;
@Test
public void test() throws Throwable {
CodeCacheOverflowProcessor.runMHTest(this::test0);
}
public void test0() throws Throwable {
testCases = 0;
Lookup lookup = lookup();
for (Method m : lookup.lookupClass().getDeclaredMethods()) {
......
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
import java.io.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import org.testng.Assert;
import org.testng.annotations.Test;
/*
* @test
* @bug 8153781
* @run testng/othervm SkipDTDTest
* @summary Test if method skipDTD of class XMLDTDScannerImpl will correctly skip the DTD section,
* even if a call to XMLEntityScanner.scanData for skipping to the closing ']' returns true.
*/
public class SkipDTDTest {
public static int DOCTYPE_SECTION_LENGTH = XMLEntityManager.DEFAULT_BUFFER_SIZE * 2;
public static int DOCUMENT_LENGTH = DOCTYPE_SECTION_LENGTH + 4096;
public String createXMLDocument(int doctypeoffset) {
StringBuilder xmlcontentbuilder = new StringBuilder(DOCUMENT_LENGTH);
xmlcontentbuilder.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n");
xmlcontentbuilder.append("<!DOCTYPE dummy [\r\n");
xmlcontentbuilder.append(" <!ELEMENT dummy EMPTY>\r\n");
xmlcontentbuilder.append(" <!--\r\n");
int doctypelines = DOCTYPE_SECTION_LENGTH / 3;
for (int i = 0; i < doctypeoffset; i++)
xmlcontentbuilder.append('a');
for (int i = 0; i < doctypelines; i++)
xmlcontentbuilder.append("a\r\n");
xmlcontentbuilder.append(" -->\r\n");
xmlcontentbuilder.append(" ]\r\n");
xmlcontentbuilder.append(">\r\n");
xmlcontentbuilder.append("<dummy>\r\n");
xmlcontentbuilder.append("</dummy>\r\n");
System.out.println("Document length:" + xmlcontentbuilder.length());
return xmlcontentbuilder.toString();
}
public void runReader(XMLInputFactory factory, int offset) throws XMLStreamException {
StringReader stringReader = new StringReader(createXMLDocument(offset));
XMLEventReader reader = factory.createXMLEventReader(stringReader);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
System.out.println("Event Type: " + event.getEventType());
}
}
@Test
public void test() {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
for (int i = 0; i < 3; i++) {
runReader(factory, i);
}
} catch (XMLStreamException xe) {
xe.printStackTrace();
Assert.fail(xe.getMessage());
}
}
}
/*
* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -35,6 +35,7 @@ import java.io.*;
import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.security.KeyStore;
public class SSLCtxAccessToSessCtx {
......@@ -63,7 +64,7 @@ public class SSLCtxAccessToSessCtx {
/*
* Is the server ready to serve?
*/
volatile static boolean serverReady = false;
AtomicInteger serverReady = new AtomicInteger(1); // only one port now
/*
* Turn on SSL debugging?
......@@ -89,12 +90,13 @@ public class SSLCtxAccessToSessCtx {
SSLServerSocket sslServerSocket =
(SSLServerSocket) sslssf.createServerSocket(serverPort);
serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
int slot = createdPorts.getAndIncrement();
serverPorts[slot] = sslServerSocket.getLocalPort();
/*
* Signal Client, we're ready for his connect.
*/
serverReady = true;
serverReady.getAndDecrement();
int read = 0;
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
InputStream sslIS = sslSocket.getInputStream();
......@@ -121,7 +123,7 @@ public class SSLCtxAccessToSessCtx {
/*
* Wait for server to get started.
*/
while (!serverReady) {
while (serverReady.get() > 0) {
Thread.sleep(50);
}
/*
......@@ -151,8 +153,8 @@ public class SSLCtxAccessToSessCtx {
* The remainder is just support stuff
*/
volatile int serverPorts[] = new int[]{0};
volatile int createdPorts = 0;
int serverPorts[] = new int[]{0}; // only one port at present
AtomicInteger createdPorts = new AtomicInteger(0);
static SSLServerSocketFactory sslssf;
static SSLSocketFactory sslsf;
static SSLContext sslctx;
......@@ -255,14 +257,20 @@ public class SSLCtxAccessToSessCtx {
*/
System.err.println("Server died...");
e.printStackTrace();
serverReady = true;
serverReady.set(0);
serverException = e;
}
}
};
serverThread.start();
} else {
try {
doServerSide(port);
} catch (Exception e) {
serverException = e;
} finally {
serverReady.set(0);
}
}
}
......@@ -284,7 +292,11 @@ public class SSLCtxAccessToSessCtx {
};
clientThread.start();
} else {
try {
doClientSide();
} catch (Exception e) {
clientException = e;
}
}
}
}
/*
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -108,8 +108,14 @@ public class SessionCacheSizeTests {
void doServerSide(int serverPort, int serverConns) throws Exception {
SSLServerSocket sslServerSocket =
(SSLServerSocket) sslssf.createServerSocket(serverPort);
try (SSLServerSocket sslServerSocket =
(SSLServerSocket) sslssf.createServerSocket(serverPort)) {
// timeout to accept a connection
sslServerSocket.setSoTimeout(45000);
// make sure createdPorts++ is atomic
synchronized(serverPorts) {
serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
/*
......@@ -118,8 +124,10 @@ public class SessionCacheSizeTests {
if (createdPorts == serverPorts.length) {
serverReady = true;
}
}
int read = 0;
int nConnections = 0;
/*
* Divide the max connections among the available server ports.
* The use of more than one server port ensures creation of more
......@@ -129,17 +137,20 @@ public class SessionCacheSizeTests {
SSLSessionContext sessCtx = sslctx.getServerSessionContext();
while (nConnections < serverConns) {
SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
try (SSLSocket sslSocket =
(SSLSocket)sslServerSocket.accept()) {
sslSocket.setSoTimeout(90000); // timeout to read
InputStream sslIS = sslSocket.getInputStream();
OutputStream sslOS = sslSocket.getOutputStream();
read = sslIS.read();
sessions[nConnections] = sslSocket.getSession();
sslOS.write(85);
sslOS.flush();
sslSocket.close();
nConnections++;
}
}
}
}
/*
* Define the client side of the test.
......@@ -263,8 +274,8 @@ public class SessionCacheSizeTests {
* Using four ports (one per each connection), we are able to create
* alteast four sessions.
*/
volatile int serverPorts[] = new int[]{0, 0, 0, 0};
volatile int createdPorts = 0;
int serverPorts[] = new int[]{0, 0, 0, 0}; // MAX_ACTIVE_CONNECTIONS: 4
int createdPorts = 0;
static SSLServerSocketFactory sslssf;
static SSLSocketFactory sslsf;
static SSLContext sslctx;
......
/*
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -36,6 +36,7 @@ import java.net.*;
import javax.net.ssl.*;
import java.util.*;
import java.security.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Session reuse time-out tests cover the cases below:
......@@ -79,7 +80,7 @@ public class SessionTimeOutTests {
/*
* Is the server ready to serve?
*/
volatile static int serverReady = PORTS;
AtomicInteger serverReady = new AtomicInteger(PORTS);
/*
* Turn on SSL debugging?
......@@ -98,7 +99,7 @@ public class SessionTimeOutTests {
/*
* Define the server side of the test.
*
* If the server prematurely exits, serverReady will be set to true
* If the server prematurely exits, serverReady will be set to zero
* to avoid infinite hangs.
*/
......@@ -111,12 +112,13 @@ public class SessionTimeOutTests {
SSLServerSocket sslServerSocket =
(SSLServerSocket) sslssf.createServerSocket(serverPort);
serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
int slot = createdPorts.getAndIncrement();
serverPorts[slot] = sslServerSocket.getLocalPort();
/*
* Signal Client, we're ready for his connect.
*/
serverReady--;
serverReady.getAndDecrement();
int read = 0;
int nConnections = 0;
SSLSession sessions [] = new SSLSession [serverConns];
......@@ -137,7 +139,7 @@ public class SessionTimeOutTests {
/*
* Define the client side of the test.
*
* If the server prematurely exits, serverReady will be set to true
* If the server prematurely exits, serverReady will be set to zero
* to avoid infinite hangs.
*/
void doClientSide() throws Exception {
......@@ -145,7 +147,7 @@ public class SessionTimeOutTests {
/*
* Wait for server to get started.
*/
while (serverReady > 0) {
while (serverReady.get() > 0) {
Thread.sleep(50);
}
......@@ -287,8 +289,8 @@ public class SessionTimeOutTests {
* The remainder is just support stuff
*/
volatile int serverPorts[] = new int[PORTS];
volatile int createdPorts = 0;
int serverPorts[] = new int[PORTS];
AtomicInteger createdPorts = new AtomicInteger(0);
static SSLServerSocketFactory sslssf;
static SSLSocketFactory sslsf;
static SSLContext sslctx;
......@@ -447,7 +449,7 @@ public class SessionTimeOutTests {
*/
System.err.println("Server died...");
e.printStackTrace();
serverReady = 0;
serverReady.set(0);
serverException = e;
}
}
......@@ -459,7 +461,7 @@ public class SessionTimeOutTests {
} catch (Exception e) {
serverException = e;
} finally {
serverReady = 0;
serverReady.set(0);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册