Class TestUtils


  • public class TestUtils
    extends java.lang.Object
    Internal utility functions useful for writing tests.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TestUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void installConscryptIfAvailable()
      Add Conscrypt to the list of security providers, if it is available.
      static java.io.File loadCert​(java.lang.String name)
      Saves a file from the classpath resources in src/main/resources/certs as a file on the filesystem.
      static java.security.cert.X509Certificate loadX509Cert​(java.lang.String fileName)
      Loads an X.509 certificate from the classpath resources in src/main/resources/certs.
      static javax.net.ssl.SSLSocketFactory newSslSocketFactoryForCa​(java.security.Provider provider, java.io.File certChainFile)
      Creates an SSLSocketFactory which contains certChainFile as its only root certificate.
      static java.net.InetSocketAddress testServerAddress​(java.lang.String host, int port)
      Creates a new InetSocketAddress that overrides the host with TEST_SERVER_HOST.
      static java.net.InetSocketAddress testServerAddress​(java.net.InetSocketAddress originalSockAddr)
      Creates a new InetSocketAddress on localhost that overrides the host with TEST_SERVER_HOST.
      static java.util.List<java.net.InetSocketAddress> testServerAddresses​(java.net.InetSocketAddress... originalSockAddr)
      Creates a new list of InetSocketAddress on localhost that overrides the host with TEST_SERVER_HOST.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TEST_SERVER_HOST

        public static final java.lang.String TEST_SERVER_HOST
        See Also:
        Constant Field Values
      • conscryptInstallAttempted

        private static boolean conscryptInstallAttempted
    • Constructor Detail

      • TestUtils

        private TestUtils()
    • Method Detail

      • testServerAddress

        public static java.net.InetSocketAddress testServerAddress​(java.lang.String host,
                                                                   int port)
        Creates a new InetSocketAddress that overrides the host with TEST_SERVER_HOST.
      • testServerAddress

        public static java.net.InetSocketAddress testServerAddress​(java.net.InetSocketAddress originalSockAddr)
        Creates a new InetSocketAddress on localhost that overrides the host with TEST_SERVER_HOST.
      • testServerAddresses

        public static java.util.List<java.net.InetSocketAddress> testServerAddresses​(java.net.InetSocketAddress... originalSockAddr)
        Creates a new list of InetSocketAddress on localhost that overrides the host with TEST_SERVER_HOST.
      • loadCert

        public static java.io.File loadCert​(java.lang.String name)
                                     throws java.io.IOException
        Saves a file from the classpath resources in src/main/resources/certs as a file on the filesystem.
        Parameters:
        name - name of a file in src/main/resources/certs.
        Throws:
        java.io.IOException
      • loadX509Cert

        public static java.security.cert.X509Certificate loadX509Cert​(java.lang.String fileName)
                                                               throws java.security.cert.CertificateException,
                                                                      java.io.IOException
        Loads an X.509 certificate from the classpath resources in src/main/resources/certs.
        Parameters:
        fileName - name of a file in src/main/resources/certs.
        Throws:
        java.security.cert.CertificateException
        java.io.IOException
      • installConscryptIfAvailable

        public static void installConscryptIfAvailable()
        Add Conscrypt to the list of security providers, if it is available. If it appears to be available but fails to load, this method will throw an exception. Since the list of security providers is static, this method does nothing if the provider is not available or succeeded previously.
      • newSslSocketFactoryForCa

        public static javax.net.ssl.SSLSocketFactory newSslSocketFactoryForCa​(java.security.Provider provider,
                                                                              java.io.File certChainFile)
                                                                       throws java.lang.Exception
        Creates an SSLSocketFactory which contains certChainFile as its only root certificate.
        Throws:
        java.lang.Exception