Merge "Move apache HTTP tests out of tests/net" am: 1cdfeec47d am: bc83be307e

Original change: https://android-review.googlesource.com/c/platform/cts/+/1471587

Change-Id: I9cef4699702f109f7edab8c781e137db3d532069
This commit is contained in:
Remi NGUYEN VAN
2020-10-27 07:45:36 +00:00
committed by Automerger Merge Worker
8 changed files with 0 additions and 1001 deletions

View File

@@ -21,7 +21,6 @@ java_defaults {
libs: [
"voip-common",
"org.apache.http.legacy",
"android.test.base",
],
@@ -40,14 +39,11 @@ java_defaults {
static_libs: [
"FrameworksNetCommonTests",
"TestNetworkStackLib",
"compatibility-device-util-axt",
"core-tests-support",
"cts-net-utils",
"ctstestrunner-axt",
"ctstestserver",
"junit",
"junit-params",
"mockwebserver",
"net-utils-framework-common",
"truth-prebuilt",
],

View File

@@ -1,95 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.net.Uri;
import android.platform.test.annotations.AppModeFull;
import android.test.AndroidTestCase;
import android.webkit.cts.CtsTestServer;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@AppModeFull(reason = "Socket cannot bind in instant app mode")
public class ApacheHttpClientTest extends AndroidTestCase {
private static final int NUM_DOWNLOADS = 20;
private static final int SMALL_DOWNLOAD_SIZE = 100 * 1024;
private CtsTestServer mWebServer;
@Override
protected void setUp() throws Exception {
super.setUp();
mWebServer = new CtsTestServer(mContext);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
mWebServer.shutdown();
}
public void testExecute() throws Exception {
downloadMultipleFiles();
}
private void downloadMultipleFiles() throws ClientProtocolException, IOException {
List<HttpResponse> responses = new ArrayList<HttpResponse>();
for (int i = 0; i < NUM_DOWNLOADS; i++) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(getSmallDownloadUrl(i).toString());
HttpResponse response = httpClient.execute(request);
responses.add(response);
}
for (int i = 0; i < NUM_DOWNLOADS; i++) {
assertDownloadResponse("Download " + i, SMALL_DOWNLOAD_SIZE, responses.get(i));
}
}
private Uri getSmallDownloadUrl(int index) {
return Uri.parse(mWebServer.getTestDownloadUrl("cts-small-download-" + index,
SMALL_DOWNLOAD_SIZE));
}
private void assertDownloadResponse(String message, int expectedNumBytes, HttpResponse response)
throws IllegalStateException, IOException {
byte[] buffer = new byte[4096];
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream stream = response.getEntity().getContent();
int numBytes = 0;
while (true) {
int bytesRead = stream.read(buffer);
if (bytesRead < 0) {
break;
} else {
numBytes += bytesRead;
}
}
assertEquals(message, expectedNumBytes, numBytes);
}
}

View File

@@ -1,162 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import com.google.mockwebserver.MockResponse;
import com.google.mockwebserver.MockWebServer;
import junit.framework.TestCase;
import android.net.http.HttpResponseCache;
import android.platform.test.annotations.AppModeFull;
import com.android.compatibility.common.util.FileUtils;
import java.io.File;
import java.io.InputStream;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public final class HttpResponseCacheTest extends TestCase {
private File cacheDir;
private MockWebServer server;
@Override public void setUp() throws Exception {
super.setUp();
server = new MockWebServer();
String tmp = System.getProperty("java.io.tmpdir");
cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID());
cacheDir.mkdirs();
// Make the cache directory read / writable.
FileUtils.setPermissions(cacheDir.getPath(), 0777);
}
@Override protected void tearDown() throws Exception {
ResponseCache.setDefault(null);
server.shutdown();
super.tearDown();
}
public void testInstall() throws Exception {
HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
assertNotNull(installed);
assertSame(installed, ResponseCache.getDefault());
assertSame(installed, HttpResponseCache.getDefault());
}
public void testSecondEquivalentInstallDoesNothing() throws Exception {
HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
assertSame(first, another);
}
public void testInstallClosesPreviouslyInstalled() throws Exception {
HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
initializeCache(first);
HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024);
initializeCache(first);
assertNotSame(first, another);
try {
first.flush();
fail();
} catch (IllegalStateException expected) {
}
}
public void testGetInstalledWithWrongTypeInstalled() {
ResponseCache.setDefault(new ResponseCache() {
@Override public CacheResponse get(URI uri, String requestMethod,
Map<String, List<String>> requestHeaders) {
return null;
}
@Override public CacheRequest put(URI uri, URLConnection connection) {
return null;
}
});
assertNull(HttpResponseCache.getInstalled());
}
public void testCloseCloses() throws Exception {
HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
initializeCache(cache);
cache.close();
try {
cache.flush();
fail();
} catch (IllegalStateException expected) {
}
}
public void testCloseUninstalls() throws Exception {
HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
cache.close();
assertNull(ResponseCache.getDefault());
}
public void testDeleteUninstalls() throws Exception {
HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
cache.delete();
assertNull(ResponseCache.getDefault());
}
/**
* Make sure that statistics tracking are wired all the way through the
* wrapper class. http://code.google.com/p/android/issues/detail?id=25418
*/
@AppModeFull(reason = "Socket cannot bind in instant app mode")
public void testStatisticsTracking() throws Exception {
HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
server.enqueue(new MockResponse()
.addHeader("Cache-Control: max-age=60")
.setBody("A"));
server.play();
URLConnection c1 = server.getUrl("/").openConnection();
InputStream inputStream1 = c1.getInputStream();
assertEquals('A', inputStream1.read());
inputStream1.close();
assertEquals(1, cache.getRequestCount());
assertEquals(1, cache.getNetworkCount());
assertEquals(0, cache.getHitCount());
URLConnection c2 = server.getUrl("/").openConnection();
assertEquals('A', c2.getInputStream().read());
URLConnection c3 = server.getUrl("/").openConnection();
assertEquals('A', c3.getInputStream().read());
assertEquals(3, cache.getRequestCount());
assertEquals(1, cache.getNetworkCount());
assertEquals(2, cache.getHitCount());
}
private void initializeCache(HttpResponseCache cache) {
// Ensure the cache is initialized, otherwise various methods are no-ops.
cache.size();
}
}

View File

@@ -1,248 +0,0 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Principal;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.util.Date;
import java.util.Set;
import junit.framework.TestCase;
import android.net.http.SslCertificate;
import android.net.http.SslCertificate.DName;
import android.os.Bundle;
public class SslCertificateTest extends TestCase {
public void testConstructor() {
// new the SslCertificate instance
String date = DateFormat.getInstance().format(new Date());
new SslCertificate("c=129", "e=weji", date, date);
// new the SslCertificate instance
new SslCertificate(new MockX509Certificate());
}
class MockX509Certificate extends X509Certificate {
@Override
public void checkValidity() throws CertificateExpiredException,
CertificateNotYetValidException {
}
@Override
public void checkValidity(Date date) throws CertificateExpiredException,
CertificateNotYetValidException {
}
@Override
public int getBasicConstraints() {
return 0;
}
@Override
public Principal getIssuerDN() {
return new MockPrincipal();
}
@Override
public boolean[] getIssuerUniqueID() {
return null;
}
@Override
public boolean[] getKeyUsage() {
return null;
}
@Override
public Date getNotAfter() {
return new Date(System.currentTimeMillis());
}
@Override
public Date getNotBefore() {
return new Date(System.currentTimeMillis() - 1000);
}
@Override
public BigInteger getSerialNumber() {
return null;
}
@Override
public String getSigAlgName() {
return null;
}
@Override
public String getSigAlgOID() {
return null;
}
@Override
public byte[] getSigAlgParams() {
return null;
}
@Override
public byte[] getSignature() {
return null;
}
@Override
public Principal getSubjectDN() {
return new MockPrincipal();
}
class MockPrincipal implements Principal {
public String getName() {
return null;
}
}
@Override
public boolean[] getSubjectUniqueID() {
return null;
}
@Override
public byte[] getTBSCertificate() throws CertificateEncodingException {
return null;
}
@Override
public int getVersion() {
return 0;
}
@Override
public byte[] getEncoded() throws CertificateEncodingException {
return null;
}
@Override
public PublicKey getPublicKey() {
return null;
}
@Override
public String toString() {
return null;
}
@Override
public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException,
InvalidKeyException, NoSuchProviderException, SignatureException {
}
@Override
public void verify(PublicKey key, String sigProvider) throws CertificateException,
NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException,
SignatureException {
}
public Set<String> getCriticalExtensionOIDs() {
return null;
}
public byte[] getExtensionValue(String oid) {
return null;
}
public Set<String> getNonCriticalExtensionOIDs() {
return null;
}
public boolean hasUnsupportedCriticalExtension() {
return false;
}
}
public void testState() {
// set the expected value
Date date1 = new Date(System.currentTimeMillis() - 1000);
Date date2 = new Date(System.currentTimeMillis());
SslCertificate ssl = new SslCertificate("c=129", "e=weji", date1, date2);
Bundle saved = SslCertificate.saveState(ssl);
assertTrue(saved.size() == 4);
assertNotNull(saved.getString("issued-to"));
assertNotNull(saved.getString("issued-by"));
assertNotNull(saved.getString("valid-not-before"));
assertNotNull(saved.getString("valid-not-after"));
assertNull(SslCertificate.saveState(null));
SslCertificate restored = SslCertificate.restoreState(saved);
assertEquals(ssl.getValidNotAfter(), restored.getValidNotAfter());
assertEquals(ssl.getValidNotBefore(), restored.getValidNotBefore());
}
public void testSslCertificate() {
final String TO = "c=ccc,o=testOName,ou=testUName,cn=testCName";
final String BY = "e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName";
// new the SslCertificate instance
Date date1 = new Date(System.currentTimeMillis() - 1000);
Date date2 = new Date(System.currentTimeMillis());
SslCertificate ssl = new SslCertificate(TO, BY, date1, date2);
DName issuedTo = ssl.getIssuedTo();
DName issuedBy = ssl.getIssuedBy();
assertEquals("testCName", issuedTo.getCName());
assertEquals(TO, issuedTo.getDName());
assertEquals("testOName", issuedTo.getOName());
assertEquals("testUName", issuedTo.getUName());
assertEquals("testCName", issuedBy.getCName());
assertEquals(BY, issuedBy.getDName());
assertEquals("testOName", issuedBy.getOName());
assertEquals("testUName", issuedBy.getUName());
assertEquals(date1, ssl.getValidNotBeforeDate());
assertEquals(date2, ssl.getValidNotAfterDate());
final String EXPECTED = "Issued to: c=ccc,o=testOName,ou=testUName,cn=testCName;\n"
+ "Issued by: e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName;\n";
assertEquals(EXPECTED, ssl.toString());
assertNull(ssl.getX509Certificate());
}
public void testGetX509Certificate() {
final String TO = "c=ccc,o=testOName,ou=testUName,cn=testCName";
final String BY = "e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName";
Date validNotBefore = new Date(System.currentTimeMillis() - 1000);
Date validNotAfter = new Date(System.currentTimeMillis());
SslCertificate ssl = new SslCertificate(TO, BY, validNotBefore, validNotAfter);
assertNull(ssl.getX509Certificate());
X509Certificate cert = new MockX509Certificate();
ssl = new SslCertificate(cert);
assertSame(cert, ssl.getX509Certificate());
}
}

View File

@@ -1,46 +0,0 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import java.text.DateFormat;
import java.util.Date;
import junit.framework.TestCase;
import android.net.http.SslCertificate;
import android.net.http.SslCertificate.DName;
public class SslCertificate_DNameTest extends TestCase {
public void testDName() {
final String TO = "c=ccc,o=testOName,ou=testUName,cn=testCName";
final String BY = "e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName";
// new the SslCertificate instance
Date date1 = new Date(System.currentTimeMillis() - 1000);
Date date2 = new Date(System.currentTimeMillis());
SslCertificate ssl = new SslCertificate(TO, BY, DateFormat.getInstance().format(
date1), DateFormat.getInstance().format(date2));
DName issuedTo = ssl.getIssuedTo();
assertNotNull(issuedTo);
assertEquals("testCName", issuedTo.getCName());
assertEquals(TO, issuedTo.getDName());
assertEquals("testOName", issuedTo.getOName());
assertEquals("testUName", issuedTo.getUName());
}
}

View File

@@ -1,85 +0,0 @@
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import android.net.http.SslCertificate;
import android.net.http.SslError;
import java.util.Date;
import junit.framework.TestCase;
public class SslErrorTest extends TestCase {
private SslCertificate mCertificate;
@Override
protected void setUp() throws Exception {
super.setUp();
mCertificate = new SslCertificate("foo", "bar", new Date(42), new Date(43));
}
public void testHasError() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
assertTrue(error.hasError(SslError.SSL_EXPIRED));
assertFalse(error.hasError(SslError.SSL_UNTRUSTED));
}
public void testAddError() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
assertFalse(error.hasError(SslError.SSL_UNTRUSTED));
error.addError(SslError.SSL_UNTRUSTED);
assertTrue(error.hasError(SslError.SSL_UNTRUSTED));
}
public void testAddErrorIgnoresInvalidValues() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
error.addError(42);
assertFalse(error.hasError(42));
}
public void testConstructorIgnoresInvalidValues() {
SslError error = new SslError(42, mCertificate);
assertFalse(error.hasError(42));
}
public void testGetPrimaryError() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
error.addError(SslError.SSL_UNTRUSTED);
assertEquals(error.getPrimaryError(), SslError.SSL_UNTRUSTED);
}
public void testGetPrimaryErrorWithEmptySet() {
SslError error = new SslError(42, mCertificate);
assertEquals(error.getPrimaryError(), -1);
}
public void testGetUrl() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate, "foo");
assertEquals(error.getUrl(), "foo");
}
public void testGetUrlWithDeprecatedConstructor() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
assertEquals(error.getUrl(), "");
}
public void testGetCertificate() {
SslError error = new SslError(SslError.SSL_EXPIRED, mCertificate);
assertEquals(mCertificate, error.getCertificate());
}
}

View File

@@ -1,55 +0,0 @@
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed 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 android.net.http.cts;
import android.net.http.X509TrustManagerExtensions;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import junit.framework.TestCase;
public class X509TrustManagerExtensionsTest extends TestCase {
private static X509TrustManager getFirstX509TrustManager(TrustManagerFactory tmf)
throws Exception {
for (TrustManager trustManager : tmf.getTrustManagers()) {
if (trustManager instanceof X509TrustManager) {
return (X509TrustManager) trustManager;
}
}
fail("Unable to find X509TrustManager");
return null;
}
public void testIsUserAddedCertificateDefaults() throws Exception {
final TrustManagerFactory tmf =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
X509TrustManager tm = getFirstX509TrustManager(tmf);
X509TrustManagerExtensions xtm = new X509TrustManagerExtensions(tm);
// Verify that all the default system provided CAs are not marked as user added.
for (Certificate cert : tm.getAcceptedIssuers()) {
assertFalse(xtm.isUserAddedCertificate((X509Certificate) cert));
}
}
}

View File

@@ -1,306 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed 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.http.conn.ssl.cts;
import javax.security.auth.x500.X500Principal;
import junit.framework.TestCase;
import org.apache.http.conn.ssl.AbstractVerifier;
import java.lang.Override;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Principal;
import java.security.PublicKey;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateExpiredException;
import java.security.cert.CertificateNotYetValidException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Date;
import java.util.Set;
/**
* See also {@link libcore.javax.security.auth.x500.X500PrincipalTest} as it shows some cases
* we are not checking as they are not allowed by the X500 principal in the first place.
*/
public final class AbstractVerifierTest extends TestCase {
public void testGetCns() {
assertCns("");
assertCns("ou=xxx");
assertCns("ou=xxx,cn=xxx", "xxx");
assertCns("ou=xxx+cn=yyy,cn=zzz+cn=abc", "yyy", "zzz", "abc");
assertCns("cn=a,cn=b", "a", "b");
assertCns("cn=a c,cn=b", "a c", "b");
assertCns("cn=a ,cn=b", "a", "b");
assertCns("cn=Cc,cn=Bb,cn=Aa", "Cc", "Bb", "Aa");
assertCns("cn=imap.gmail.com", "imap.gmail.com");
assertCns("l=\"abcn=a,b\", cn=c", "c");
assertCns("l=\"abcn=a,b\", cn=c", "c");
assertCns("l=\"abcn=a,b\", cn= c", "c");
assertCns("cn=<", "<");
assertCns("cn=>", ">");
assertCns("cn= >", ">");
assertCns("cn=a b", "a b");
assertCns("cn =a b", "a b");
assertCns("Cn=a b", "a b");
assertCns("cN=a b", "a b");
assertCns("CN=a b", "a b");
assertCns("cn=a#b", "a#b");
assertCns("cn=#130161", "a");
assertCns("l=q\t+cn=p", "p");
assertCns("l=q\n+cn=p", "p");
assertCns("l=q\n,cn=p", "p");
assertCns("l=,cn=p", "p");
assertCns("l=\tq\n,cn=\tp", "\tp");
}
/** A cn=, generates an empty value, unless it's at the very end */
public void testEmptyValues() {
assertCns("l=,cn=+cn=q", "", "q");
assertCns("l=,cn=,cn=q", "", "q");
assertCns("l=,cn=");
assertCns("l=,cn=q,cn= ", "q");
assertCns("l=,cn=q ,cn= ", "q");
assertCns("l=,cn=\"\"");
assertCns("l=,cn=\" \",cn=\" \"", " ", " ");
assertCns("l=,cn= ,cn= ","");
assertCns("l=,cn=,cn= ,cn= ", "", "");
}
public void testGetCns_escapedChars() {
assertCns("cn=\\,", ",");
assertCns("cn=\\#", "#");
assertCns("cn=\\+", "+");
assertCns("cn=\\\"", "\"");
assertCns("cn=\\\\", "\\");
assertCns("cn=\\<", "<");
assertCns("cn=\\>", ">");
assertCns("cn=\\;", ";");
assertCns("cn=\\+", "+");
assertCns("cn=\"\\+\"", "+");
assertCns("cn=\"\\,\"", ",");
assertCns("cn= a =", "a =");
assertCns("cn==", "=");
}
public void testGetCns_whitespace() {
assertCns("cn= p", "p");
assertCns("cn=\np", "p");
assertCns("cn=\tp", "\tp");
}
public void testGetCnsWithOid() {
assertCns("2.5.4.3=a,ou=xxx", "a");
assertCns("2.5.4.3=\" a \",ou=xxx", " a ");
assertCns("2.5.5.3=a,ou=xxx,cn=b", "b");
}
public void testGetCnsWithQuotedStrings() {
assertCns("cn=\"\\\" a ,=<>#;\"", "\" a ,=<>#;");
assertCns("cn=abc\\,def", "abc,def");
assertCns("cn=\"\\\" a ,\\=<>\\#;\"", "\" a ,=<>#;");
}
public void testGetCnsWithUtf8() {
assertCns("cn=\"Lu\\C4\\8Di\\C4\\87\"", "\u004c\u0075\u010d\u0069\u0107");
assertCns("cn=Lu\\C4\\8Di\\C4\\87", "\u004c\u0075\u010d\u0069\u0107");
assertCns("cn=Lu\\C4\\8di\\c4\\87", "\u004c\u0075\u010d\u0069\u0107");
assertCns("cn=\"Lu\\C4\\8di\\c4\\87\"", "\u004c\u0075\u010d\u0069\u0107");
assertCns("cn=\u004c\u0075\u010d\u0069\u0107", "\u004c\u0075\u010d\u0069\u0107");
// \63=c
assertExceptionInPrincipal("\\63n=ab");
assertExceptionInPrincipal("cn=\\a");
}
public void testGetCnsWithWhitespace() {
assertCns("ou=a, cn= a b ,o=x", "a b");
assertCns("cn=\" a b \" ,o=x", " a b ");
}
private static void assertCns(String dn, String... expected) {
String[] result = AbstractVerifier.getCNs(createStubCertificate(dn));
if (expected.length == 0) {
assertNull(result);
} else {
assertNotNull(dn, result);
assertEquals(dn, Arrays.asList(expected), Arrays.asList(result));
}
}
private static void assertExceptionInPrincipal(String dn) {
try {
X500Principal principal = new X500Principal(dn);
fail("Expected " + IllegalArgumentException.class.getName()
+ " because of incorrect input name");
} catch (IllegalArgumentException e) {
// Expected.
}
}
private static X509Certificate createStubCertificate(final String subjectName) {
return new X509Certificate() {
@Override
public X500Principal getSubjectX500Principal() {
return new X500Principal(subjectName);
}
@Override
public Set<String> getCriticalExtensionOIDs() {
return null;
}
@Override
public byte[] getExtensionValue(String oid) {
return new byte[0];
}
@Override
public Set<String> getNonCriticalExtensionOIDs() {
return null;
}
@Override
public boolean hasUnsupportedCriticalExtension() {
return false;
}
@Override
public byte[] getEncoded() throws CertificateEncodingException {
return new byte[0];
}
@Override
public void verify(PublicKey key)
throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
NoSuchProviderException, SignatureException {
}
@Override
public void verify(PublicKey key, String sigProvider)
throws CertificateException, NoSuchAlgorithmException, InvalidKeyException,
NoSuchProviderException, SignatureException {
}
@Override
public String toString() {
return null;
}
@Override
public PublicKey getPublicKey() {
return null;
}
@Override
public void checkValidity()
throws CertificateExpiredException, CertificateNotYetValidException {
}
@Override
public void checkValidity(Date date)
throws CertificateExpiredException, CertificateNotYetValidException {
}
@Override
public int getVersion() {
return 0;
}
@Override
public BigInteger getSerialNumber() {
return null;
}
@Override
public Principal getIssuerDN() {
return null;
}
@Override
public Principal getSubjectDN() {
return null;
}
@Override
public Date getNotBefore() {
return null;
}
@Override
public Date getNotAfter() {
return null;
}
@Override
public byte[] getTBSCertificate() throws CertificateEncodingException {
return new byte[0];
}
@Override
public byte[] getSignature() {
return new byte[0];
}
@Override
public String getSigAlgName() {
return null;
}
@Override
public String getSigAlgOID() {
return null;
}
@Override
public byte[] getSigAlgParams() {
return new byte[0];
}
@Override
public boolean[] getIssuerUniqueID() {
return new boolean[0];
}
@Override
public boolean[] getSubjectUniqueID() {
return new boolean[0];
}
@Override
public boolean[] getKeyUsage() {
return new boolean[0];
}
@Override
public int getBasicConstraints() {
return 0;
}
};
}
}