github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/internal/enforcer/applicationproxy/servicecache/servicecache_test.go (about)

     1  // +build !windows
     2  
     3  package servicecache
     4  
     5  import (
     6  	"net"
     7  	"testing"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  	"go.aporeto.io/enforcerd/trireme-lib/common"
    11  	"go.aporeto.io/enforcerd/trireme-lib/utils/portspec"
    12  )
    13  
    14  func TestEntries(t *testing.T) {
    15  	Convey("Given an entry list", t, func() {
    16  
    17  		Convey("If I delete the last element, I should the right data", func() {
    18  			e := entryList{
    19  				{
    20  					id: "1",
    21  				},
    22  				{
    23  					id: "2",
    24  				},
    25  				{
    26  					id: "3",
    27  				},
    28  				{
    29  					id: "4",
    30  				},
    31  			}
    32  			new := e.Delete(3)
    33  			So(len(new), ShouldEqual, 3)
    34  			So(new[0], ShouldResemble, &entry{id: "1"})
    35  			So(new[1], ShouldResemble, &entry{id: "2"})
    36  			So(new[2], ShouldResemble, &entry{id: "3"})
    37  		})
    38  
    39  		Convey("If I delete the first element in the list, I should get the right data", func() {
    40  			e := entryList{
    41  				{
    42  					id: "1",
    43  				},
    44  				{
    45  					id: "2",
    46  				},
    47  				{
    48  					id: "3",
    49  				},
    50  			}
    51  			new := e.Delete(0)
    52  			So(len(new), ShouldEqual, 2)
    53  			So(new[0], ShouldResemble, &entry{id: "2"})
    54  			So(new[1], ShouldResemble, &entry{id: "3"})
    55  		})
    56  
    57  		Convey("If I try to delete out of bounds, the list should not be modified", func() {
    58  			e := entryList{
    59  				{
    60  					id: "1",
    61  				},
    62  				{
    63  					id: "3",
    64  				},
    65  			}
    66  			new := e.Delete(4)
    67  			So(len(new), ShouldEqual, 2)
    68  			So(new[0], ShouldResemble, &entry{id: "1"})
    69  			So(new[1], ShouldResemble, &entry{id: "3"})
    70  		})
    71  		Convey("If I delete the last element list I should get an empty list", func() {
    72  			e := entryList{
    73  				{
    74  					id: "1",
    75  				},
    76  			}
    77  			new := e.Delete(0)
    78  			So(len(new), ShouldEqual, 0)
    79  		})
    80  	})
    81  }
    82  
    83  func createServices() (*common.Service, *common.Service, *common.Service) {
    84  	n1 := "172.17.1.0/24"
    85  	n2 := "192.168.0.0/16"
    86  	n3 := "20.1.1.1/32"
    87  
    88  	s1 := &common.Service{
    89  		Ports: &portspec.PortSpec{
    90  			Min: uint16(0),
    91  			Max: uint16(100),
    92  		},
    93  		Protocol:  6,
    94  		Addresses: map[string]struct{}{n1: struct{}{}, n2: struct{}{}, n3: struct{}{}},
    95  		FQDNs:     []string{"host1", "host2", "host3"},
    96  	}
    97  
    98  	n4 := "10.1.1.0/28"
    99  
   100  	s2 := &common.Service{
   101  		Ports: &portspec.PortSpec{
   102  			Min: uint16(150),
   103  			Max: uint16(200),
   104  		},
   105  		Protocol:  6,
   106  		Addresses: map[string]struct{}{n4: struct{}{}},
   107  		FQDNs:     []string{"host4"},
   108  	}
   109  
   110  	s3 := &common.Service{
   111  		Ports: &portspec.PortSpec{
   112  			Min: uint16(1000),
   113  			Max: uint16(2000),
   114  		},
   115  		Protocol:  6,
   116  		Addresses: map[string]struct{}{},
   117  	}
   118  
   119  	return s1, s2, s3
   120  }
   121  func TestServiceCache(t *testing.T) {
   122  	Convey("Given a new cache", t, func() {
   123  		c := NewTable()
   124  		Convey("When I add a set of entries, I should succeed", func() {
   125  
   126  			s1, s2, s3 := createServices()
   127  
   128  			cerr := c.Add(s1, "1", "first data", true)
   129  			So(cerr, ShouldBeNil)
   130  			So(c.local, ShouldNotBeNil)
   131  			So(c.localHosts, ShouldNotBeNil)
   132  			So(len(c.localHosts), ShouldEqual, 3)
   133  
   134  			cerr = c.Add(s2, "2", "second data", true)
   135  			So(cerr, ShouldBeNil)
   136  			So(c.local, ShouldNotBeNil)
   137  			So(c.localHosts, ShouldNotBeNil)
   138  			So(len(c.localHosts), ShouldEqual, 4)
   139  
   140  			cerr = c.Add(s3, "3", "third data", true)
   141  			So(cerr, ShouldBeNil)
   142  			So(len(c.localHosts), ShouldEqual, 4)
   143  
   144  		})
   145  
   146  		Convey("If I try to add overlapping ports for a given prefix, I should get error", func() {
   147  			s1, s2, s3 := createServices()
   148  			cerr := c.Add(s1, "1", "first data", true)
   149  			So(cerr, ShouldBeNil)
   150  			cerr = c.Add(s2, "2", "second data", true)
   151  			So(cerr, ShouldBeNil)
   152  			cerr = c.Add(s3, "3", "third data", true)
   153  			So(cerr, ShouldBeNil)
   154  
   155  			n5 := "10.1.1.0/28"
   156  			s4 := &common.Service{
   157  				Ports: &portspec.PortSpec{
   158  					Min: uint16(100),
   159  					Max: uint16(300),
   160  				},
   161  				Protocol:  6,
   162  				Addresses: map[string]struct{}{n5: struct{}{}},
   163  			}
   164  			cerr = c.Add(s4, "4", "failed data", true)
   165  			So(cerr, ShouldNotBeNil)
   166  		})
   167  
   168  		Convey("If I try to add overlapping ports for a given host, I should get error", func() {
   169  			s1, s2, s3 := createServices()
   170  			cerr := c.Add(s1, "1", "first data", true)
   171  			So(cerr, ShouldBeNil)
   172  			cerr = c.Add(s2, "2", "second data", true)
   173  			So(cerr, ShouldBeNil)
   174  			cerr = c.Add(s3, "3", "third data", true)
   175  			So(cerr, ShouldBeNil)
   176  
   177  			s4 := &common.Service{
   178  				Ports: &portspec.PortSpec{
   179  					Min: uint16(100),
   180  					Max: uint16(300),
   181  				},
   182  				Protocol:  6,
   183  				Addresses: nil,
   184  				FQDNs:     []string{"host4"},
   185  			}
   186  			cerr = c.Add(s4, "4", "failed data", true)
   187  			So(cerr, ShouldNotBeNil)
   188  		})
   189  
   190  		Convey("When I search for valid entries, I should get the right responses", func() {
   191  			s1, s2, s3 := createServices()
   192  			cerr := c.Add(s1, "1", "first data", true)
   193  			So(cerr, ShouldBeNil)
   194  			cerr = c.Add(s2, "2", "second data", true)
   195  			So(cerr, ShouldBeNil)
   196  			cerr = c.Add(s3, "3", "third data", true)
   197  			So(cerr, ShouldBeNil)
   198  
   199  			data := c.Find(net.ParseIP("10.1.1.1").To4(), 175, "", true)
   200  			So(data, ShouldNotBeNil)
   201  			So(data.(string), ShouldResemble, "second data")
   202  
   203  			data = c.Find(net.ParseIP("192.168.1.1").To4(), 50, "", true)
   204  			So(data, ShouldNotBeNil)
   205  			So(data.(string), ShouldResemble, "first data")
   206  
   207  			data = c.Find(net.ParseIP("50.50.50.50").To4(), 1001, "", true)
   208  			So(data, ShouldNotBeNil)
   209  			So(data.(string), ShouldResemble, "third data")
   210  
   211  			data = c.Find(nil, 50, "host2", true)
   212  			So(data, ShouldNotBeNil)
   213  			So(data.(string), ShouldResemble, "first data")
   214  
   215  			data = c.Find(nil, 150, "host4", true)
   216  			So(data, ShouldNotBeNil)
   217  			So(data.(string), ShouldResemble, "second data")
   218  		})
   219  
   220  		Convey("When I search for a good IP, but invalid port, I should get nil ", func() {
   221  			s1, s2, s3 := createServices()
   222  			cerr := c.Add(s1, "1", "first data", true)
   223  			So(cerr, ShouldBeNil)
   224  			cerr = c.Add(s2, "2", "second data", true)
   225  			So(cerr, ShouldBeNil)
   226  			cerr = c.Add(s3, "3", "third data", true)
   227  			So(cerr, ShouldBeNil)
   228  
   229  			data := c.Find(net.ParseIP("10.1.1.1").To4(), 50, "", true)
   230  			So(data, ShouldBeNil)
   231  		})
   232  
   233  		Convey("When I search for a good IP, but uknown host, I should get nil ", func() {
   234  			s1, s2, s3 := createServices()
   235  			cerr := c.Add(s1, "1", "first data", true)
   236  			So(cerr, ShouldBeNil)
   237  			cerr = c.Add(s2, "2", "second data", true)
   238  			So(cerr, ShouldBeNil)
   239  			cerr = c.Add(s3, "3", "third data", true)
   240  			So(cerr, ShouldBeNil)
   241  
   242  			data := c.Find(nil, 50, "uknown", true)
   243  			So(data, ShouldBeNil)
   244  		})
   245  
   246  		Convey("When I search for a good IP, but invalid host, I should get nil ", func() {
   247  			s1, s2, s3 := createServices()
   248  			cerr := c.Add(s1, "1", "first data", true)
   249  			So(cerr, ShouldBeNil)
   250  			cerr = c.Add(s2, "2", "second data", true)
   251  			So(cerr, ShouldBeNil)
   252  			cerr = c.Add(s3, "3", "third data", true)
   253  			So(cerr, ShouldBeNil)
   254  
   255  			data := c.Find(nil, 50, "host4", true)
   256  			So(data, ShouldBeNil)
   257  		})
   258  
   259  		Convey("When I search for a good exact IP, and valid port, I should get the data ", func() {
   260  			s1, s2, s3 := createServices()
   261  			cerr := c.Add(s1, "1", "first data", true)
   262  			So(cerr, ShouldBeNil)
   263  			cerr = c.Add(s2, "2", "second data", true)
   264  			So(cerr, ShouldBeNil)
   265  			cerr = c.Add(s3, "3", "third data", true)
   266  			So(cerr, ShouldBeNil)
   267  
   268  			data := c.Find(net.ParseIP("20.1.1.1").To4(), 50, "", true)
   269  			So(data, ShouldNotBeNil)
   270  			So(data.(string), ShouldResemble, "first data")
   271  		})
   272  	})
   273  }
   274  
   275  func TestDelete(t *testing.T) {
   276  	Convey("When I delete the first of entries, I should not be able to find them any more", t, func() {
   277  		c := NewTable()
   278  		s1, s2, s3 := createServices()
   279  		cerr := c.Add(s1, "1", "first data", true)
   280  		So(cerr, ShouldBeNil)
   281  		cerr = c.Add(s2, "2", "second data", true)
   282  		So(cerr, ShouldBeNil)
   283  		cerr = c.Add(s3, "3", "third data", false)
   284  		So(cerr, ShouldBeNil)
   285  
   286  		c.DeleteByID("1", true)
   287  		data := c.Find(net.ParseIP("192.168.1.1").To4(), 50, "", true)
   288  		So(data, ShouldBeNil)
   289  	})
   290  }
   291  
   292  func TestFindExistingServices(t *testing.T) {
   293  	Convey("Given a table with entries", t, func() {
   294  		c := NewTable()
   295  		s1, s2, s3 := createServices()
   296  		cerr := c.Add(s1, "1", "first data", true)
   297  		So(cerr, ShouldBeNil)
   298  		cerr = c.Add(s2, "2", "second data", true)
   299  		So(cerr, ShouldBeNil)
   300  		cerr = c.Add(s3, "3", "third data", true)
   301  		So(cerr, ShouldBeNil)
   302  
   303  		Convey("When I retrieve the service list from the local, it should be correct", func() {
   304  			data, spec := c.FindListeningServicesForPU("3")
   305  			So(data, ShouldNotBeNil)
   306  			So(data, ShouldResemble, "third data")
   307  			So(spec, ShouldNotBeNil)
   308  		})
   309  	})
   310  }