github.com/mholt/caddy-l4@v0.0.0-20241104153248-ec8fae209322/layer4/matchers_test.go (about)

     1  package layer4
     2  
     3  import (
     4  	"net"
     5  	"testing"
     6  
     7  	"github.com/caddyserver/caddy/v2"
     8  	"go.uber.org/zap"
     9  )
    10  
    11  var _ net.Conn = &dummyConn{}
    12  var _ net.Addr = dummyAddr{}
    13  
    14  type dummyAddr struct {
    15  	ip      string
    16  	network string
    17  }
    18  
    19  // Network implements net.Addr.
    20  func (da dummyAddr) Network() string {
    21  	return da.network
    22  }
    23  
    24  // String implements net.Addr.
    25  func (da dummyAddr) String() string {
    26  	return da.ip
    27  }
    28  
    29  type dummyConn struct {
    30  	net.Conn
    31  	localAddr  net.Addr
    32  	remoteAddr net.Addr
    33  }
    34  
    35  // LocalAddr implements net.Conn.
    36  func (dc *dummyConn) LocalAddr() net.Addr {
    37  	return dc.localAddr
    38  }
    39  
    40  // RemoteAddr implements net.Conn.
    41  func (dc *dummyConn) RemoteAddr() net.Addr {
    42  	return dc.remoteAddr
    43  }
    44  
    45  type provisionableMatcher interface {
    46  	caddy.Provisioner
    47  	ConnMatcher
    48  }
    49  
    50  func provision(in provisionableMatcher) ConnMatcher {
    51  	_ = in.Provision(caddy.Context{})
    52  	return in
    53  }
    54  func TestNotMatcher(t *testing.T) {
    55  	for i, tc := range []struct {
    56  		cx        *Connection
    57  		matcher   MatchNot
    58  		match     bool
    59  		expectErr bool
    60  	}{
    61  		{
    62  			matcher:   MatchNot{},
    63  			match:     true,
    64  			expectErr: false,
    65  		},
    66  		{
    67  			cx: &Connection{
    68  				Conn: &dummyConn{
    69  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
    70  					remoteAddr: dummyAddr{ip: "127.0.0.1", network: "tcp"},
    71  				},
    72  				Logger: zap.NewNop(),
    73  			},
    74  			matcher: MatchNot{
    75  				MatcherSets: []MatcherSet{
    76  					{
    77  						provision(&MatchRemoteIP{Ranges: []string{"127.0.0.1"}}),
    78  					},
    79  				},
    80  			},
    81  			match:     false,
    82  			expectErr: false,
    83  		},
    84  		{
    85  			cx: &Connection{
    86  				Conn: &dummyConn{
    87  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
    88  					remoteAddr: dummyAddr{ip: "192.168.0.1", network: "tcp"},
    89  				},
    90  				Logger: zap.NewNop(),
    91  			},
    92  			matcher: MatchNot{
    93  				MatcherSets: []MatcherSet{
    94  					{
    95  						provision(&MatchRemoteIP{Ranges: []string{"127.0.0.1"}}),
    96  					},
    97  				},
    98  			},
    99  			match:     true,
   100  			expectErr: false,
   101  		},
   102  		{
   103  			cx: &Connection{
   104  				Conn: &dummyConn{
   105  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
   106  					remoteAddr: dummyAddr{ip: "192.168.0.1", network: "tcp"},
   107  				},
   108  				Logger: zap.NewNop(),
   109  			},
   110  			matcher: MatchNot{
   111  				MatcherSets: []MatcherSet{
   112  					{
   113  						provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}),
   114  					},
   115  					{
   116  						provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}),
   117  					},
   118  				},
   119  			},
   120  			match:     false,
   121  			expectErr: false,
   122  		},
   123  		{
   124  			cx: &Connection{
   125  				Conn: &dummyConn{
   126  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
   127  					remoteAddr: dummyAddr{ip: "172.16.0.1", network: "tcp"},
   128  				},
   129  				Logger: zap.NewNop(),
   130  			},
   131  			matcher: MatchNot{
   132  				MatcherSets: []MatcherSet{
   133  					{
   134  						provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}),
   135  					},
   136  					{
   137  						provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}),
   138  					},
   139  				},
   140  			},
   141  			match:     false,
   142  			expectErr: false,
   143  		},
   144  		{
   145  			cx: &Connection{
   146  				Conn: &dummyConn{
   147  					localAddr:  dummyAddr{ip: "192.168.0.1", network: "tcp"},
   148  					remoteAddr: dummyAddr{ip: "192.168.0.1", network: "tcp"},
   149  				},
   150  				Logger: zap.NewNop(),
   151  			},
   152  			matcher: MatchNot{
   153  				MatcherSets: []MatcherSet{
   154  					{
   155  						provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}),
   156  					},
   157  					{
   158  						provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}),
   159  					},
   160  				},
   161  			},
   162  			match:     true,
   163  			expectErr: false,
   164  		},
   165  		{
   166  			cx: &Connection{
   167  				Conn: &dummyConn{
   168  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
   169  					remoteAddr: dummyAddr{ip: "172.16.0.1", network: "tcp"},
   170  				},
   171  				Logger: zap.NewNop(),
   172  			},
   173  			matcher: MatchNot{
   174  				MatcherSets: []MatcherSet{
   175  					{
   176  						provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}),
   177  						provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}),
   178  					},
   179  				},
   180  			},
   181  			match:     false,
   182  			expectErr: false,
   183  		},
   184  		{
   185  			cx: &Connection{
   186  				Conn: &dummyConn{
   187  					localAddr:  dummyAddr{ip: "127.0.0.1", network: "tcp"},
   188  					remoteAddr: dummyAddr{ip: "192.168.0.1", network: "tcp"},
   189  				},
   190  				Logger: zap.NewNop(),
   191  			},
   192  			matcher: MatchNot{
   193  				MatcherSets: []MatcherSet{
   194  					{
   195  						provision(&MatchRemoteIP{Ranges: []string{"172.16.0.1"}}),
   196  						provision(&MatchLocalIP{Ranges: []string{"127.0.0.1"}}),
   197  					},
   198  				},
   199  			},
   200  			match:     true,
   201  			expectErr: false,
   202  		},
   203  	} {
   204  		actual, err := tc.matcher.Match(tc.cx)
   205  		if err != nil && !tc.expectErr {
   206  			t.Errorf("Test %d %+v: Expected no error, got: %v", i, tc.matcher, err)
   207  			continue
   208  		}
   209  		if actual != tc.match {
   210  			t.Errorf("Test %d %+v: Expected %t, got %t for: Connection=%+v,", i, tc.matcher, tc.match, actual, tc.cx)
   211  			continue
   212  		}
   213  	}
   214  }