mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	This fix adds unit tests for `ExtractAddressFromReverse`. It also updates the implementation of `ExtractAddressFromReverse` so that only a valid IPv4 or IPv6 address (in string format) is returned. If the addresses is not valid and can not be handled by golang's `net.ParseIP` then an empty string (`""`) will be returned. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
		
			
				
	
	
		
			52 lines
		
	
	
		
			837 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			837 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package dnsutil
 | |
| 
 | |
| import (
 | |
| 	"testing"
 | |
| )
 | |
| 
 | |
| func TestExtractAddressFromReverse(t *testing.T) {
 | |
| 	tests := []struct {
 | |
| 		reverseName     string
 | |
| 		expectedAddress string
 | |
| 	}{
 | |
| 		{
 | |
| 			"54.119.58.176.in-addr.arpa.",
 | |
| 			"176.58.119.54",
 | |
| 		},
 | |
| 		{
 | |
| 			".58.176.in-addr.arpa.",
 | |
| 			"",
 | |
| 		},
 | |
| 		{
 | |
| 			"b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.in-addr.arpa.",
 | |
| 			"",
 | |
| 		},
 | |
| 		{
 | |
| 			"b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.",
 | |
| 			"2001:db8::567:89ab",
 | |
| 		},
 | |
| 		{
 | |
| 			"d.0.1.0.0.2.ip6.arpa.",
 | |
| 			"",
 | |
| 		},
 | |
| 		{
 | |
| 			"54.119.58.176.ip6.arpa.",
 | |
| 			"",
 | |
| 		},
 | |
| 		{
 | |
| 			"NONAME",
 | |
| 			"",
 | |
| 		},
 | |
| 		{
 | |
| 			"",
 | |
| 			"",
 | |
| 		},
 | |
| 	}
 | |
| 	for i, test := range tests {
 | |
| 		got := ExtractAddressFromReverse(test.reverseName)
 | |
| 		if got != test.expectedAddress {
 | |
| 			t.Errorf("Test %d, expected '%s', got '%s'", i, test.expectedAddress, got)
 | |
| 		}
 | |
| 	}
 | |
| }
 |