Skip to content

Commit

Permalink
fixes ServiceName regex
Browse files Browse the repository at this point in the history
  • Loading branch information
octonato committed Feb 5, 2019
1 parent 9793e52 commit 2febf99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Expand Up @@ -206,15 +206,14 @@ case object Lookup {

private val SrvQuery = """^_(.+?)\._(.+?)\.(.+?)$""".r

private val ServiceName = "^((?!-)[A-Za-z0-9-]{1,63}(?<!-))(\\.+[A-Za-z]{2,6})*$".r
private val ServiceName = "^[^-_.]([A-Za-z0-9-]\\.{0,1})+[^-_.]$".r

/**
* Create a service Lookup from a string with format:
* _portName._protocol.serviceName.
* (as specified by https://www.ietf.org/rfc/rfc2782.txt)
*
* If the passed string conforms with this format, a SRV Lookup is returned.
* The serviceName part must be a valid domain name.
*
* The string is parsed and dismembered to build a Lookup as following:
* Lookup(serviceName).withPortName(portName).withProtocol(protocol)
Expand Down
39 changes: 25 additions & 14 deletions akka-discovery/src/test/scala/akka/discovery/LookupSpec.scala
Expand Up @@ -8,14 +8,19 @@ import org.scalatest.{ Matchers, OptionValues, WordSpec }

class LookupSpec extends WordSpec with Matchers with OptionValues {

// SRV strings with invalid domain names
// SRV strings with invalid service names
// should fail to build lookups
val srvWithInvalidDomainNames = List(
val srvWithInvalidServiceNames = List(
"_portName._protocol.service_name.local",
"_portName._protocol.servicename,local",
"_portName._protocol.servicename.local-",
"_portName._protocol.-servicename.local")

val srvWithValidServiceNames = List(
"_portName._protocol.service-name",
"_portName._protocol.service-name.local",
"_portName._protocol.service-name.svc.cluster.local")

// No SRV that should result in simple A/AAAA lookups
val noSrvLookups = List(
"portName.protocol.serviceName.local",
Expand All @@ -28,12 +33,14 @@ class LookupSpec extends WordSpec with Matchers with OptionValues {

"Lookup.parseSrv" should {

"generate a SRV Lookup from a SRV String with unqualified domain name" in {
val name = "_portName._protocol.serviceName"
val lookup = Lookup.parseSrv(name)
lookup.serviceName shouldBe "serviceName"
lookup.portName.value shouldBe "portName"
lookup.protocol.value shouldBe "protocol"
"generate a SRV Lookup from a valid SRV String" in {
srvWithValidServiceNames.foreach { str
withClue(s"parsing '$str'") {
val lookup = Lookup.parseSrv(str)
lookup.portName.value shouldBe "portName"
lookup.protocol.value shouldBe "protocol"
}
}
}

"generate a SRV Lookup from a SRV String" in {
Expand Down Expand Up @@ -66,8 +73,8 @@ class LookupSpec extends WordSpec with Matchers with OptionValues {
}
}

"throw an IllegalArgumentException for any SRV with invalid domain names" in {
srvWithInvalidDomainNames.foreach { str
"throw an IllegalArgumentException for any SRV with invalid service names" in {
srvWithInvalidServiceNames.foreach { str
withClue(s"parsing '$str'") {
assertThrows[IllegalArgumentException] {
Lookup.parseSrv(str)
Expand All @@ -92,8 +99,8 @@ class LookupSpec extends WordSpec with Matchers with OptionValues {
}
}

"return false if domain part in SRV String is an invalid domain name" in {
srvWithInvalidDomainNames.foreach { str
"return false if service name part in SRV String is an invalid service name" in {
srvWithInvalidServiceNames.foreach { str
withClue(s"checking '$str'") {
Lookup.isValidSrv(str) shouldBe false
}
Expand All @@ -108,8 +115,12 @@ class LookupSpec extends WordSpec with Matchers with OptionValues {
Lookup.isValidSrv(null) shouldBe false
}

"return true for a SRV with valid domain name" in {
Lookup.isValidSrv("_portName._protocol.serviceName.local") shouldBe true
"return true for a SRV with valid service name" in {
srvWithValidServiceNames.foreach { str
withClue(s"parsing '$str'") {
Lookup.isValidSrv(str) shouldBe true
}
}
}

}
Expand Down

0 comments on commit 2febf99

Please sign in to comment.