Repository: square/okhttp
File: okhttp/src/jvmTest/java/okhttp3/CallHandshakeTest.kt (Kotlin)

Code before:
    val platform = Platform.get()
    val platformDefaultCipherSuites =
      platform.newSslSocketFactory(platform.platformTrustManager()).defaultCipherSuites
    val cipherSuites =
      ConnectionSpec.RESTRICTED_TLS.effectiveCipherSuites(platformDefaultCipherSuites)

    if (cipherSuites.contains(TLS_CHACHA20_POLY1305_SHA256.javaName)) {
      assertThat(cipherSuites).containsExactlyElementsOf(listOf(
        TLS_AES_128_GCM_SHA256,
        TLS_AES_256_GCM_SHA384,
        TLS_CHACHA20_POLY1305_SHA256,
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
      ).map { it.javaName })
    } else {
      assertThat(cipherSuites).containsExactlyElementsOf(listOf(
        TLS_AES_128_GCM_SHA256,
        TLS_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
      ).map { it.javaName })
    }
  }

  private fun expectedConnectionCipherSuites(client: OkHttpClient): Set<String> {
    return client.connectionSpecs.first().cipherSuites!!.map { it.javaName }.intersect(defaultEnabledCipherSuites.toSet())
  }

  private fun makeClient(
    connectionSpec: ConnectionSpec? = null,
    tlsVersion: TlsVersion? = null,
    cipherSuites: List<CipherSuite>? = null
  ): OkHttpClient {
    return this.client.newBuilder()
      .apply {
        if (connectionSpec != null) {
          connectionSpecs(listOf(ConnectionSpec.Builder(connectionSpec)
            .apply {
              if (tlsVersion != null) {
                tlsVersions(tlsVersion)
              }
              if (cipherSuites != null) {
                cipherSuites(*cipherSuites.toTypedArray())
              }
            }

Review comment (suggestion):
... and this is the change to the test that matches the corresponding change to the production code. Makes sense.

Code after:
    if (cipherSuites.contains(TLS_CHACHA20_POLY1305_SHA256.javaName)) {
      assertThat(cipherSuites).containsExactlyElementsOf(listOf(
        TLS_AES_128_GCM_SHA256,
        TLS_AES_256_GCM_SHA384,
        TLS_CHACHA20_POLY1305_SHA256,
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
        TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
      ).map { it.javaName })
    } else {
      assertThat(cipherSuites).containsExactlyElementsOf(listOf(
        TLS_AES_128_GCM_SHA256,
        TLS_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
        TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
      ).map { it.javaName })
    }
  }

  private fun expectedConnectionCipherSuites(client: OkHttpClient): Set<String> {
    return client.connectionSpecs.first().cipherSuites!!.map { it.javaName }.intersect(defaultEnabledCipherSuites.toSet())
  }

  private fun makeClient(
    connectionSpec: ConnectionSpec? = null,
    tlsVersion: TlsVersion? = null,
    cipherSuites: List<CipherSuite>? = null
  ): OkHttpClient {
    return this.client.newBuilder()
      .apply {
        if (connectionSpec != null) {
          connectionSpecs(listOf(ConnectionSpec.Builder(connectionSpec)
            .apply {
              if (tlsVersion != null) {
                tlsVersions(tlsVersion)
              }
              if (cipherSuites != null) {
                cipherSuites(*cipherSuites.toTypedArray())
              }
            }
            .build()))
        }
      }
      .addNetworkInterceptor {
        val socket = it.connection()!!.socket() as SSLSocket