<commit_before>package io.buoyant.linkerd
package protocol

import com.twitter.conversions.time._
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Failure, Service}
import io.buoyant.linkerd.protocol.TlsUtils._
import io.buoyant.test.Awaits
import io.l5d.FsInitializer
import io.l5d.clientTls.BoundPathInitializer
import java.io.File
import org.scalatest.FunSuite
import scala.sys.process._

class TlsBoundPathTest extends FunSuite with Awaits {

  override val defaultWait = 2.seconds

  test("tls router + 2 tls backends") {

    withCerts("bill.buoyant.io", "ted.buoyant.io") { certs =>
      val bill = Downstream
        .constTls(
          "bill",
          "whoa",
          certs.serviceCerts("bill.buoyant.io").cert,
          certs.serviceCerts("bill.buoyant.io").key
        )
      val ted = Downstream
        .constTls(
          "ted",
          "dude",
          certs.serviceCerts("ted.buoyant.io").cert,
          certs.serviceCerts("ted.buoyant.io").key
        )
      try {
        withDisco(bill, ted) { disco =>

          val linkerConfig = s"""
             |namers:
             |- kind: io.l5d.fs
             |  rootDir: ${disco.getPath}
             |
             |routers:
             |- protocol: http
             |  baseDtab: |
             |    /http/1.1/GET => /srv ;
             |    /srv => /io.l5d.fs
             |
             |  servers:
             |  - port: 0
             |  client:
             |    tls:
             |      kind: io.l5d.clientTls.boundPath
             |      caCertPath: ${certs.caCert.getPath}
             |      names:
             |      - prefix: "/io.l5d.fs/{host}"
             |        commonNamePattern: "{host}.buoyant.io"
             |""".
              stripMargin
          withLinkerdClient(linkerConfig) { client =>
            val billRsp = {
              val req = Request()
              req.host = "bill"
              await(client(req))
            }
            assert(billRsp.contentString == "whoa")

            val tedRsp = {
              val req = Request()
              req.host = "ted"
              await(client(req))
            }
            assert(tedRsp.contentString == "dude")
          }
        }
      } finally (await(bill.server.close().join(ted.server.close())))
    }
  }

  test("mix of tls and plain backends") {

    withCerts("bill.buoyant.io") { certs =>
      val bill = Downstream
        .constTls(
          "bill",
          "whoa",
          certs.serviceCerts("bill.buoyant.io").cert,
          certs.serviceCerts("bill.buoyant.io").key
        )
      val ted = Downstream
        .const("ted", "dude")

      try {
        withDisco(bill, ted) { disco =>

          val linkerConfig = s"""
            |namers:
            |- kind: io.l5d.fs
            |  rootDir: ${disco.getPath}
            |
            |routers:
            |- protocol: http
            |  baseDtab: |
            |    /http/1.1/GET => /srv ;
            |    /srv => /io.l5d.fs
            |
            |  servers:
            |  - port: 0
            |  client:
            |    tls:
            |      kind: io.l5d.clientTls.boundPath
            |      caCertPath: ${certs.caCert.getPath}
            |      names:
            |      - prefix: "/io.l5d.fs/bill"
            |        commonNamePattern: "bill.buoyant.io"
            |""".
            stripMargin
          withLinkerdClient(linkerConfig) { client =>
            val billRsp = {
              val req = Request()
              req.host = "bill"<commit_msg>Fix tls integration test. (#269)<commit_after>package io.buoyant.linkerd
package protocol

import com.twitter.conversions.time._
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Failure, Service}
import io.buoyant.linkerd.protocol.TlsUtils._
import io.buoyant.test.Awaits
import io.l5d.FsInitializer
import io.l5d.clientTls.BoundPathInitializer
import java.io.File
import org.scalatest.FunSuite
import scala.sys.process._

class TlsBoundPathTest extends FunSuite with Awaits {

  override val defaultWait = 2.seconds

  test("tls router + 2 tls backends") {

    withCerts("bill.buoyant.io", "ted.buoyant.io") { certs =>
      val bill = Downstream
        .constTls(
          "bill",
          "whoa",
          certs.serviceCerts("bill.buoyant.io").cert,
          certs.serviceCerts("bill.buoyant.io").key
        )
      val ted = Downstream
        .constTls(
          "ted",
          "dude",
          certs.serviceCerts("ted.buoyant.io").cert,
          certs.serviceCerts("ted.buoyant.io").key
        )
      try {
        withDisco(bill, ted) { disco =>

          val linkerConfig = s"""
             |namers:
             |- kind: io.l5d.fs
             |  rootDir: ${disco.getPath}
             |
             |routers:
             |- protocol: http
             |  baseDtab: |
             |    /http/1.1/GET => /srv ;
             |    /srv => /io.l5d.fs
             |
             |  servers:
             |  - port: 0
             |  client:
             |    tls:
             |      kind: io.l5d.clientTls.boundPath
             |      caCertPath: ${certs.caCert.getPath}
             |      names:
             |      - prefix: "/io.l5d.fs/{host}"
             |        commonNamePattern: "{host}.buoyant.io"
             |""".
              stripMargin
          withLinkerdClient(linkerConfig) { client =>
            val billRsp = {
              val req = Request()
              req.host = "bill"
              await(client(req))
            }
            assert(billRsp.contentString == "whoa")

            val tedRsp = {
              val req = Request()
              req.host = "ted"
              await(client(req))
            }
            assert(tedRsp.contentString == "dude")
          }
        }
      } finally (await(bill.server.close().join(ted.server.close())))
    }
  }

  test("mix of tls and plain backends") {

    withCerts("bill.buoyant.io") { certs =>
      val bill = Downstream
        .constTls(
          "bill",
          "whoa",
          certs.serviceCerts("bill.buoyant.io").cert,
          certs.serviceCerts("bill.buoyant.io").key
        )
      val ted = Downstream
        .const("ted", "dude")

      try {
        withDisco(bill, ted) { disco =>

          val linkerConfig = s"""
            |namers:
            |- kind: io.l5d.fs
            |  rootDir: ${disco.getPath}
            |
            |routers:
            |- protocol: http
            |  baseDtab: |
            |    /http/1.1/GET => /srv ;
            |    /srv => /io.l5d.fs
            |
            |  servers:
            |  - port: 0
            |  client:
            |    tls:
            |      kind: io.l5d.clientTls.boundPath
            |      caCertPath: ${certs.caCert.getPath}
            |      strict: false
            |      names:
            |      - prefix: "/io.l5d.fs/bill"
            |        commonNamePattern: "bill.buoyant.io"
            |""".
            stripMargin
          withLinkerdClient(linkerConfig) { client =>
            val billRsp = {
              val req = Request()
              req.host = "bill"
{'fraction_non_alphanumeric': 0.1235015333147477, 'fraction_numerical': 0.005436297741845553, 'mean_word_length': 1.3085585585585586, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2658876', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2234, 'n_words': 373}