github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/hostmatcher/hostmatcher_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package hostmatcher
     7  
     8  import (
     9  	"net"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestHostOrIPMatchesList(t *testing.T) {
    16  	type tc struct {
    17  		host     string
    18  		ip       net.IP
    19  		expected bool
    20  	}
    21  
    22  	// for IPv6: "::1" is loopback, "fd00::/8" is private
    23  
    24  	hl := ParseHostMatchList("", "private, External, *.myDomain.com, 169.254.1.0/24")
    25  
    26  	test := func(cases []tc) {
    27  		for _, c := range cases {
    28  			assert.Equalf(t, c.expected, hl.MatchHostOrIP(c.host, c.ip), "case domain=%s, ip=%v, expected=%v", c.host, c.ip, c.expected)
    29  		}
    30  	}
    31  
    32  	cases := []tc{
    33  		{"", net.IPv4zero, false},
    34  		{"", net.IPv6zero, false},
    35  
    36  		{"", net.ParseIP("127.0.0.1"), false},
    37  		{"127.0.0.1", nil, false},
    38  		{"", net.ParseIP("::1"), false},
    39  
    40  		{"", net.ParseIP("10.0.1.1"), true},
    41  		{"10.0.1.1", nil, true},
    42  		{"10.0.1.1:8080", nil, true},
    43  		{"", net.ParseIP("192.168.1.1"), true},
    44  		{"192.168.1.1", nil, true},
    45  		{"", net.ParseIP("fd00::1"), true},
    46  		{"fd00::1", nil, true},
    47  
    48  		{"", net.ParseIP("8.8.8.8"), true},
    49  		{"", net.ParseIP("1001::1"), true},
    50  
    51  		{"mydomain.com", net.IPv4zero, false},
    52  		{"sub.mydomain.com", net.IPv4zero, true},
    53  		{"sub.mydomain.com:8080", net.IPv4zero, true},
    54  
    55  		{"", net.ParseIP("169.254.1.1"), true},
    56  		{"169.254.1.1", nil, true},
    57  		{"", net.ParseIP("169.254.2.2"), false},
    58  		{"169.254.2.2", nil, false},
    59  	}
    60  	test(cases)
    61  
    62  	hl = ParseHostMatchList("", "loopback")
    63  	cases = []tc{
    64  		{"", net.IPv4zero, false},
    65  		{"", net.ParseIP("127.0.0.1"), true},
    66  		{"", net.ParseIP("10.0.1.1"), false},
    67  		{"", net.ParseIP("192.168.1.1"), false},
    68  		{"", net.ParseIP("8.8.8.8"), false},
    69  
    70  		{"", net.ParseIP("::1"), true},
    71  		{"", net.ParseIP("fd00::1"), false},
    72  		{"", net.ParseIP("1000::1"), false},
    73  
    74  		{"mydomain.com", net.IPv4zero, false},
    75  	}
    76  	test(cases)
    77  
    78  	hl = ParseHostMatchList("", "private")
    79  	cases = []tc{
    80  		{"", net.IPv4zero, false},
    81  		{"", net.ParseIP("127.0.0.1"), false},
    82  		{"", net.ParseIP("10.0.1.1"), true},
    83  		{"", net.ParseIP("192.168.1.1"), true},
    84  		{"", net.ParseIP("8.8.8.8"), false},
    85  
    86  		{"", net.ParseIP("::1"), false},
    87  		{"", net.ParseIP("fd00::1"), true},
    88  		{"", net.ParseIP("1000::1"), false},
    89  
    90  		{"mydomain.com", net.IPv4zero, false},
    91  	}
    92  	test(cases)
    93  
    94  	hl = ParseHostMatchList("", "external")
    95  	cases = []tc{
    96  		{"", net.IPv4zero, false},
    97  		{"", net.ParseIP("127.0.0.1"), false},
    98  		{"", net.ParseIP("10.0.1.1"), false},
    99  		{"", net.ParseIP("192.168.1.1"), false},
   100  		{"", net.ParseIP("8.8.8.8"), true},
   101  
   102  		{"", net.ParseIP("::1"), false},
   103  		{"", net.ParseIP("fd00::1"), false},
   104  		{"", net.ParseIP("1000::1"), true},
   105  
   106  		{"mydomain.com", net.IPv4zero, false},
   107  	}
   108  	test(cases)
   109  
   110  	hl = ParseHostMatchList("", "*")
   111  	cases = []tc{
   112  		{"", net.IPv4zero, true},
   113  		{"", net.ParseIP("127.0.0.1"), true},
   114  		{"", net.ParseIP("10.0.1.1"), true},
   115  		{"", net.ParseIP("192.168.1.1"), true},
   116  		{"", net.ParseIP("8.8.8.8"), true},
   117  
   118  		{"", net.ParseIP("::1"), true},
   119  		{"", net.ParseIP("fd00::1"), true},
   120  		{"", net.ParseIP("1000::1"), true},
   121  
   122  		{"mydomain.com", net.IPv4zero, true},
   123  	}
   124  	test(cases)
   125  
   126  	// built-in network names can be escaped (warping the first char with `[]`) to be used as a real host name
   127  	// this mechanism is reversed for internal usage only (maybe for some rare cases), it's not supposed to be used by end users
   128  	// a real user should never use loopback/private/external as their host names
   129  	hl = ParseHostMatchList("", "loopback, [p]rivate")
   130  	cases = []tc{
   131  		{"loopback", nil, false},
   132  		{"", net.ParseIP("127.0.0.1"), true},
   133  		{"private", nil, true},
   134  		{"", net.ParseIP("192.168.1.1"), false},
   135  	}
   136  	test(cases)
   137  
   138  	hl = ParseSimpleMatchList("", "loopback, *.domain.com")
   139  	cases = []tc{
   140  		{"loopback", nil, true},
   141  		{"", net.ParseIP("127.0.0.1"), false},
   142  		{"sub.domain.com", nil, true},
   143  		{"other.com", nil, false},
   144  		{"", net.ParseIP("1.1.1.1"), false},
   145  	}
   146  	test(cases)
   147  
   148  	hl = ParseSimpleMatchList("", "external")
   149  	cases = []tc{
   150  		{"", net.ParseIP("192.168.1.1"), false},
   151  		{"", net.ParseIP("1.1.1.1"), false},
   152  		{"external", nil, true},
   153  	}
   154  	test(cases)
   155  
   156  	hl = ParseSimpleMatchList("", "")
   157  	cases = []tc{
   158  		{"", net.ParseIP("192.168.1.1"), false},
   159  		{"", net.ParseIP("1.1.1.1"), false},
   160  		{"external", nil, false},
   161  	}
   162  	test(cases)
   163  }