Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 81774d1f authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Fix android.net.SSLTest

The test is failing because of lack of visibility (IllegalAccessError).
Also rename it to match the tested class, migrate to JUnit4, and remove
an old test that is unused (suppressed), relies on third-party servers
and is not a proper unit test (would try to make real network requests).

Test: test now passes
Change-Id: I5032a056df83d4f572d89860b52bd85a1993d9dc
parent 6fa8d06c
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -19,11 +19,13 @@ package android.net;
import android.os.SystemProperties;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.RoSystemProperties;
import com.android.org.conscrypt.Conscrypt;
import com.android.org.conscrypt.OpenSSLContextImpl;
import com.android.org.conscrypt.OpenSSLSocketImpl;
import com.android.org.conscrypt.SSLClientSessionCache;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
@@ -31,6 +33,7 @@ import java.net.SocketException;
import java.security.KeyManagementException;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
@@ -306,8 +309,10 @@ public class SSLCertificateSocketFactory extends SSLSocketFactory {
    /**
     * Returns an array containing the concatenation of length-prefixed byte
     * strings.
     * @hide
     */
    static byte[] toLengthPrefixedList(byte[]... items) {
    @VisibleForTesting
    public static byte[] toLengthPrefixedList(byte[]... items) {
        if (items.length == 0) {
            throw new IllegalArgumentException("items.length == 0");
        }
+12 −29
Original line number Diff line number Diff line
@@ -16,39 +16,19 @@

package android.net;

import android.test.suitebuilder.annotation.Suppress;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import junit.framework.TestCase;

public class SSLTest extends TestCase {
    //This test relies on network resources.
    @Suppress
    public void testCertificate() throws Exception {
        // test www.fortify.net/sslcheck.html
        Socket ssl = SSLCertificateSocketFactory.getDefault().createSocket("www.fortify.net",443);
        assertNotNull(ssl);

        OutputStream out = ssl.getOutputStream();
        assertNotNull(out);

        InputStream in = ssl.getInputStream();
        assertNotNull(in);
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

        String get = "GET /sslcheck.html HTTP/1.1\r\nHost: 68.178.217.222\r\n\r\n";
import android.support.test.runner.AndroidJUnit4;

        // System.out.println("going for write...");
        out.write(get.getBytes());
import org.junit.Test;
import org.junit.runner.RunWith;

        byte[] b = new byte[1024];
        // System.out.println("going for read...");
        int ret = in.read(b);

        // System.out.println(new String(b));
    }
import java.util.Arrays;

@RunWith(AndroidJUnit4.class)
public class SSLCertificateSocketFactoryTest {
    @Test
    public void testStringsToLengthPrefixedBytes() {
        byte[] expected = {
                6, 's', 'p', 'd', 'y', '/', '2',
@@ -59,6 +39,7 @@ public class SSLTest extends TestCase {
                new byte[] { 'h', 't', 't', 'p', '/', '1', '.', '1' })));
    }

    @Test
    public void testStringsToLengthPrefixedBytesEmptyArray() {
        try {
            SSLCertificateSocketFactory.toLengthPrefixedList();
@@ -67,6 +48,7 @@ public class SSLTest extends TestCase {
        }
    }

    @Test
    public void testStringsToLengthPrefixedBytesEmptyByteArray() {
        try {
            SSLCertificateSocketFactory.toLengthPrefixedList(new byte[0]);
@@ -75,6 +57,7 @@ public class SSLTest extends TestCase {
        }
    }

    @Test
    public void testStringsToLengthPrefixedBytesOversizedInput() {
        try {
            SSLCertificateSocketFactory.toLengthPrefixedList(new byte[256]);