Compare commits
No commits in common. "b45f8181aa139c52222b467bdbf61bae73a014c9" and "54af4469a48d9dc384121b181316d6f2f6f641f6" have entirely different histories.
b45f8181aa
...
54af4469a4
@ -2,7 +2,6 @@ package dockerCompose
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -560,7 +559,7 @@ func (s *Service) ExistsLabel(name string) bool {
|
|||||||
// ExistsPort returns true if the port definition is already present.
|
// ExistsPort returns true if the port definition is already present.
|
||||||
func (s *Service) ExistsPort(src string, dest string, protocol string) bool {
|
func (s *Service) ExistsPort(src string, dest string, protocol string) bool {
|
||||||
for _, port := range s.Ports {
|
for _, port := range s.Ports {
|
||||||
s, d, p := splitStringInPortMapping(port)
|
s, d, p := splitStringInPort(port)
|
||||||
if s == src && d == dest && p == protocol {
|
if s == src && d == dest && p == protocol {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -572,7 +571,7 @@ func (s *Service) ExistsPort(src string, dest string, protocol string) bool {
|
|||||||
// ExistsDestinationPort returns true if the destination port is already used.
|
// ExistsDestinationPort returns true if the destination port is already used.
|
||||||
func (s *Service) ExistsDestinationPort(dest string) bool {
|
func (s *Service) ExistsDestinationPort(dest string) bool {
|
||||||
for _, port := range s.Ports {
|
for _, port := range s.Ports {
|
||||||
_, d, _ := splitStringInPortMapping(port)
|
_, d, _ := splitStringInPort(port)
|
||||||
if d == dest {
|
if d == dest {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -584,7 +583,7 @@ func (s *Service) ExistsDestinationPort(dest string) bool {
|
|||||||
// ExistsSourcePort returns true if the source port is already used.
|
// ExistsSourcePort returns true if the source port is already used.
|
||||||
func (s *Service) ExistsSourcePort(src string) bool {
|
func (s *Service) ExistsSourcePort(src string) bool {
|
||||||
for _, port := range s.Ports {
|
for _, port := range s.Ports {
|
||||||
s, _, _ := splitStringInPortMapping(port)
|
s, _, _ := splitStringInPort(port)
|
||||||
if s == src {
|
if s == src {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -861,7 +860,7 @@ func (s *Service) mergeExistingWinPorts(ports []string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
src, dest, protocol := splitStringInPortMapping(port)
|
src, dest, protocol := splitStringInPort(port)
|
||||||
if !s.ExistsDestinationPort(dest) {
|
if !s.ExistsDestinationPort(dest) {
|
||||||
s.SetPort(src, dest, protocol)
|
s.SetPort(src, dest, protocol)
|
||||||
}
|
}
|
||||||
@ -1057,7 +1056,7 @@ func (s *Service) mergeLastWinPorts(ports []string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
src, dest, protocol := splitStringInPortMapping(port)
|
src, dest, protocol := splitStringInPort(port)
|
||||||
s.SetPort(src, dest, protocol)
|
s.SetPort(src, dest, protocol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1139,7 +1138,7 @@ func (s *Service) RemoveLabel(name string) {
|
|||||||
func (s *Service) RemovePort(dest string) {
|
func (s *Service) RemovePort(dest string) {
|
||||||
ports := make([]string, 0)
|
ports := make([]string, 0)
|
||||||
for _, port := range s.Ports {
|
for _, port := range s.Ports {
|
||||||
srcPort, destPort, protocol := splitStringInPortMapping(port)
|
srcPort, destPort, protocol := splitStringInPort(port)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case destPort == dest && len(protocol) <= 0:
|
case destPort == dest && len(protocol) <= 0:
|
||||||
@ -1898,7 +1897,7 @@ func (v *Volume) MergeLastWin(volume *Volume) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Volume) mergeExistingWinExternal(_ bool) {
|
func (v *Volume) mergeExistingWinExternal(external bool) {
|
||||||
if v.External {
|
if v.External {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1951,33 +1950,17 @@ func splitStringInKeyValue(s, sep string) (string, string) {
|
|||||||
return key, value
|
return key, value
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitStringInPortMapping parses a string and returns the src, dest port including an optional protocol.
|
func splitStringInPort(s string) (string, string, string) {
|
||||||
//
|
parts := strings.Split(s, portDelimiter)
|
||||||
// // Example
|
src := parts[0]
|
||||||
// s, d, p := splitStringInPortMapping("80:80/tcp")
|
rest := parts[1]
|
||||||
// // Output: "80" "80" "tcp"
|
|
||||||
// s, d, p := splitStringInPortMapping("0.0.0.0:80:80/tcp")
|
|
||||||
// // Output: "0.0.0.0:80" "80" "tcp"
|
|
||||||
func splitStringInPortMapping(s string) (string, string, string) {
|
|
||||||
p := port(s)
|
|
||||||
|
|
||||||
var src string
|
parts = strings.Split(rest, portProtocolDelimiter)
|
||||||
switch {
|
if len(parts) == 2 {
|
||||||
case p.existsSrcIP() && p.existsSrcPort():
|
return src, parts[0], parts[1]
|
||||||
src = fmt.Sprintf("%s:%s", p.getSrcIP(), p.getSrcPort())
|
|
||||||
case !p.existsSrcIP():
|
|
||||||
src = p.getSrcPort()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var dst string
|
return src, parts[0], ""
|
||||||
switch {
|
|
||||||
case p.existsDstIP() && p.existsDstPort():
|
|
||||||
dst = fmt.Sprintf("%s:%s", p.getDstIP(), p.getDstPort())
|
|
||||||
case !p.existsDstIP():
|
|
||||||
dst = p.getDstPort()
|
|
||||||
}
|
|
||||||
|
|
||||||
return src, dst, p.getProtocol()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitStringInVolume(s string) (string, string, string) {
|
func splitStringInVolume(s string) (string, string, string) {
|
||||||
@ -1990,131 +1973,3 @@ func splitStringInVolume(s string) (string, string, string) {
|
|||||||
}
|
}
|
||||||
return src, dest, ""
|
return src, dest, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
regExpPort = regexp.MustCompile(`^((?<srcIP>([\d]{1,3}\.){3}[\d]{1,3}):)?(?<srcPort>[\d]{1,5}):((?<dstIP>([\d]{1,3}\.){3}[\d]{1,3}):)?(?<dstPort>[\d]{1,5})(\/(?<protocol>[a-z]*))?$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
type port string
|
|
||||||
|
|
||||||
// existsDstPort returns true, if the port string contains a trailing destination port definition.
|
|
||||||
func (p port) existsDstPort() bool {
|
|
||||||
return len(p.getDstPort()) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// existsDstIP returns true, if the port string contains a trailing destination ip definition.
|
|
||||||
func (p port) existsDstIP() bool {
|
|
||||||
return len(p.getDstIP()) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// existsProtocol returns true, if the port string contains a protocol definition.
|
|
||||||
func (p port) existsProtocol() bool {
|
|
||||||
return len(p.getProtocol()) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// existsSrcIP returns true, if the port string contains a leading src ip definition.
|
|
||||||
func (p port) existsSrcIP() bool {
|
|
||||||
return len(p.getSrcIP()) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// existsSrcPort returns true, if the port string contains a leading src port definition.
|
|
||||||
func (p port) existsSrcPort() bool {
|
|
||||||
return len(p.getSrcPort()) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// getDst returns the concatenation of the destination ip and port. If the destination ip is empty, only the port will
|
|
||||||
// be returned.
|
|
||||||
func (p port) getDst() string {
|
|
||||||
switch {
|
|
||||||
case p.existsDstIP():
|
|
||||||
return fmt.Sprintf("%s%s%s", p.getDstIP(), portDelimiter, p.getDstPort())
|
|
||||||
default:
|
|
||||||
return p.getDstPort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSrcIP returns the destination ip, if the port string contains a destination ip definition.
|
|
||||||
func (p port) getDstIP() string {
|
|
||||||
matches := regExpPort.FindStringSubmatch(string(p))
|
|
||||||
i := regExpPort.SubexpIndex("dstIP")
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(matches) <= 0:
|
|
||||||
return ""
|
|
||||||
case i < 0:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSrcPort returns the destination port, if the port string contains an destination port definition.
|
|
||||||
func (p port) getDstPort() string {
|
|
||||||
matches := regExpPort.FindStringSubmatch(string(p))
|
|
||||||
i := regExpPort.SubexpIndex("dstPort")
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(matches) <= 0:
|
|
||||||
return ""
|
|
||||||
case i < 0:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// getProtocol returns the protocol, if the port string contains a protocol definition.
|
|
||||||
func (p port) getProtocol() string {
|
|
||||||
matches := regExpPort.FindStringSubmatch(string(p))
|
|
||||||
i := regExpPort.SubexpIndex("protocol")
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(matches) <= 0:
|
|
||||||
return ""
|
|
||||||
case i < 0:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSrc returns the concatenation of the source ip and port. If the source ip is empty, only the port will be
|
|
||||||
// returned.
|
|
||||||
func (p port) getSrc() string {
|
|
||||||
switch {
|
|
||||||
case p.existsSrcIP():
|
|
||||||
return fmt.Sprintf("%s%s%s", p.getSrcIP(), portDelimiter, p.getSrcPort())
|
|
||||||
default:
|
|
||||||
return p.getSrcPort()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSrcIP returns the source ip, if the port string contains an src ip definition.
|
|
||||||
func (p port) getSrcIP() string {
|
|
||||||
matches := regExpPort.FindStringSubmatch(string(p))
|
|
||||||
i := regExpPort.SubexpIndex("srcIP")
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(matches) <= 0:
|
|
||||||
return ""
|
|
||||||
case i < 0:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSrcPort returns the source port, if the port string contains an src port definition.
|
|
||||||
func (p port) getSrcPort() string {
|
|
||||||
matches := regExpPort.FindStringSubmatch(string(p))
|
|
||||||
i := regExpPort.SubexpIndex("srcPort")
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case len(matches) <= 0:
|
|
||||||
return ""
|
|
||||||
case i < 0:
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches[i]
|
|
||||||
}
|
|
||||||
|
@ -1,418 +0,0 @@
|
|||||||
package dockerCompose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Test_splitStringInPortMapping(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedSrc string
|
|
||||||
expectedDst string
|
|
||||||
expectedProtocol string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedSrc: "53",
|
|
||||||
expectedDst: "53",
|
|
||||||
expectedProtocol: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53",
|
|
||||||
expectedSrc: "0.0.0.0:53",
|
|
||||||
expectedDst: "53",
|
|
||||||
expectedProtocol: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:10.11.12.13:53",
|
|
||||||
expectedSrc: "0.0.0.0:53",
|
|
||||||
expectedDst: "10.11.12.13:53",
|
|
||||||
expectedProtocol: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:10.11.12.13:53/tcp",
|
|
||||||
expectedSrc: "0.0.0.0:53",
|
|
||||||
expectedDst: "10.11.12.13:53",
|
|
||||||
expectedProtocol: "tcp",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
actualSrc, actualDst, actualProtocol := splitStringInPortMapping(testCase.s)
|
|
||||||
require.Equal(testCase.expectedSrc, actualSrc, "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedDst, actualDst, "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedProtocol, actualProtocol, "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPort_DstIP(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedBool bool
|
|
||||||
expectedString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/tcp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/udp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
p := port(testCase.s)
|
|
||||||
require.Equal(testCase.expectedBool, p.existsDstIP(), "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedString, p.getDstIP(), "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPort_DstPort(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedBool bool
|
|
||||||
expectedString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:0.0.0.0:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:10.11.12.13:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
p := port(testCase.s)
|
|
||||||
require.Equal(testCase.expectedBool, p.existsDstPort(), "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedString, p.getDstPort(), "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPort_Protocol(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedBool bool
|
|
||||||
expectedString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "0",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53/tcp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53/udp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "tcp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "udp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "tcp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "udp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "tcp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:11.12.13.14:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "tcp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:11.12.13.14:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "udp",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
p := port(testCase.s)
|
|
||||||
require.Equal(testCase.expectedBool, p.existsProtocol(), "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedString, p.getProtocol(), "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPort_SrcIP(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedBool bool
|
|
||||||
expectedString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/tcp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/udp",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "0.0.0.0",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "10.11.12.13",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
p := port(testCase.s)
|
|
||||||
require.Equal(testCase.expectedBool, p.existsSrcIP(), "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedString, p.getSrcIP(), "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPort_SrcPort(t *testing.T) {
|
|
||||||
require := require.New(t)
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
s string
|
|
||||||
expectedBool bool
|
|
||||||
expectedString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
s: "",
|
|
||||||
expectedBool: false,
|
|
||||||
expectedString: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "0.0.0.0:53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53/tcp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
s: "10.11.12.13:53:53/udp",
|
|
||||||
expectedBool: true,
|
|
||||||
expectedString: "53",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
|
||||||
p := port(testCase.s)
|
|
||||||
require.Equal(testCase.expectedBool, p.existsSrcPort(), "TestCase %v", i)
|
|
||||||
require.Equal(testCase.expectedString, p.getSrcPort(), "TestCase %v", i)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user