| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | package reverse
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"reflect"
 | 
					
						
							|  |  |  | 	"regexp"
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 	"testing"
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test converting from hostname to IP and back again to hostname
 | 
					
						
							|  |  |  | func TestNetworkConversion(t *testing.T) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, net4, _ := net.ParseCIDR("10.1.1.0/24")
 | 
					
						
							|  |  |  | 	_, net6, _ := net.ParseCIDR("fd01::/64")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	regexIP4, _ := regexp.Compile("^dns-" + regexMatchV4 + "\\.domain\\.internal\\.$")
 | 
					
						
							|  |  |  | 	regexIP6, _ := regexp.Compile("^dns-" + regexMatchV6 + "\\.domain\\.internal\\.$")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		network    network
 | 
					
						
							|  |  |  | 		resultHost string
 | 
					
						
							|  |  |  | 		resultIP   net.IP
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net4,
 | 
					
						
							|  |  |  | 				Template:     "dns-{ip}.domain.internal.",
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP4,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			"dns-10-1-1-23.domain.internal.",
 | 
					
						
							|  |  |  | 			net.ParseIP("10.1.1.23"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net6,
 | 
					
						
							|  |  |  | 				Template:     "dns-{ip}.domain.internal.",
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP6,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			"dns-fd01000000000000000000000000a32f.domain.internal.",
 | 
					
						
							|  |  |  | 			net.ParseIP("fd01::a32f"),
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							|  |  |  | 		resultIP := test.network.hostnameToIP(test.resultHost)
 | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(test.resultIP, resultIP) {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected %v, got %v", i, test.resultIP, resultIP)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		resultHost := test.network.ipToHostname(test.resultIP)
 | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(test.resultHost, resultHost) {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected %v, got %v", i, test.resultHost, resultHost)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestNetworkHostnameToIP(t *testing.T) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_, net4, _ := net.ParseCIDR("10.1.1.0/24")
 | 
					
						
							|  |  |  | 	_, net6, _ := net.ParseCIDR("fd01::/64")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	regexIP4, _ := regexp.Compile("^dns-" + regexMatchV4 + "\\.domain\\.internal\\.$")
 | 
					
						
							|  |  |  | 	regexIP6, _ := regexp.Compile("^dns-" + regexMatchV6 + "\\.domain\\.internal\\.$")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Test regex does NOT match
 | 
					
						
							|  |  |  | 	// All this test should return nil
 | 
					
						
							|  |  |  | 	testsNil := []struct {
 | 
					
						
							|  |  |  | 		network  network
 | 
					
						
							|  |  |  | 		hostname string
 | 
					
						
							|  |  |  | 	}{
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net4,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP4,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// domain does not match
 | 
					
						
							|  |  |  | 			"dns-10-1-1-23.domain.internals.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net4,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP4,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// IP does match / contain in subnet
 | 
					
						
							|  |  |  | 			"dns-200-1-1-23.domain.internals.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net4,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP4,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// template does not match
 | 
					
						
							|  |  |  | 			"dns-10-1-1-23-x.domain.internal.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net4,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP4,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// template does not match
 | 
					
						
							|  |  |  | 			"IP-dns-10-1-1-23.domain.internal.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net6,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP6,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// template does not match
 | 
					
						
							|  |  |  | 			"dnx-fd01000000000000000000000000a32f.domain.internal.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net6,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP6,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// no valid v6 (missing one 0, only 31 chars)
 | 
					
						
							|  |  |  | 			"dns-fd0100000000000000000000000a32f.domain.internal.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		{
 | 
					
						
							|  |  |  | 			network{
 | 
					
						
							| 
									
										
										
										
											2017-02-22 16:06:20 +00:00
										 |  |  | 				IPnet:        net6,
 | 
					
						
							|  |  |  | 				RegexMatchIP: regexIP6,
 | 
					
						
							| 
									
										
										
										
											2017-02-09 20:39:48 +01:00
										 |  |  | 			},
 | 
					
						
							|  |  |  | 			// IP does match / contain in subnet
 | 
					
						
							|  |  |  | 			"dns-ed01000000000000000000000000a32f.domain.internal.",
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for i, test := range testsNil {
 | 
					
						
							|  |  |  | 		resultIP := test.network.hostnameToIP(test.hostname)
 | 
					
						
							|  |  |  | 		if resultIP != nil {
 | 
					
						
							|  |  |  | 			t.Fatalf("Test %d expected nil, got %v", i, resultIP)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |