github.com/slackhq/nebula@v1.9.0/service/service_test.go (about) 1 package service 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "net" 8 "testing" 9 "time" 10 11 "dario.cat/mergo" 12 "github.com/slackhq/nebula/cert" 13 "github.com/slackhq/nebula/config" 14 "github.com/slackhq/nebula/e2e" 15 "golang.org/x/sync/errgroup" 16 "gopkg.in/yaml.v2" 17 ) 18 19 type m map[string]interface{} 20 21 func newSimpleService(caCrt *cert.NebulaCertificate, caKey []byte, name string, udpIp net.IP, overrides m) *Service { 22 23 vpnIpNet := &net.IPNet{IP: make([]byte, len(udpIp)), Mask: net.IPMask{255, 255, 255, 0}} 24 copy(vpnIpNet.IP, udpIp) 25 26 _, _, myPrivKey, myPEM := e2e.NewTestCert(caCrt, caKey, "a", time.Now(), time.Now().Add(5*time.Minute), vpnIpNet, nil, []string{}) 27 caB, err := caCrt.MarshalToPEM() 28 if err != nil { 29 panic(err) 30 } 31 32 mc := m{ 33 "pki": m{ 34 "ca": string(caB), 35 "cert": string(myPEM), 36 "key": string(myPrivKey), 37 }, 38 //"tun": m{"disabled": true}, 39 "firewall": m{ 40 "outbound": []m{{ 41 "proto": "any", 42 "port": "any", 43 "host": "any", 44 }}, 45 "inbound": []m{{ 46 "proto": "any", 47 "port": "any", 48 "host": "any", 49 }}, 50 }, 51 "timers": m{ 52 "pending_deletion_interval": 2, 53 "connection_alive_interval": 2, 54 }, 55 "handshakes": m{ 56 "try_interval": "200ms", 57 }, 58 } 59 60 if overrides != nil { 61 err = mergo.Merge(&overrides, mc, mergo.WithAppendSlice) 62 if err != nil { 63 panic(err) 64 } 65 mc = overrides 66 } 67 68 cb, err := yaml.Marshal(mc) 69 if err != nil { 70 panic(err) 71 } 72 73 var c config.C 74 if err := c.LoadString(string(cb)); err != nil { 75 panic(err) 76 } 77 78 s, err := New(&c) 79 if err != nil { 80 panic(err) 81 } 82 return s 83 } 84 85 func TestService(t *testing.T) { 86 ca, _, caKey, _ := e2e.NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{}) 87 a := newSimpleService(ca, caKey, "a", net.IP{10, 0, 0, 1}, m{ 88 "static_host_map": m{}, 89 "lighthouse": m{ 90 "am_lighthouse": true, 91 }, 92 "listen": m{ 93 "host": "0.0.0.0", 94 "port": 4243, 95 }, 96 }) 97 b := newSimpleService(ca, caKey, "b", net.IP{10, 0, 0, 2}, m{ 98 "static_host_map": m{ 99 "10.0.0.1": []string{"localhost:4243"}, 100 }, 101 "lighthouse": m{ 102 "hosts": []string{"10.0.0.1"}, 103 "interval": 1, 104 }, 105 }) 106 107 ln, err := a.Listen("tcp", ":1234") 108 if err != nil { 109 t.Fatal(err) 110 } 111 var eg errgroup.Group 112 eg.Go(func() error { 113 conn, err := ln.Accept() 114 if err != nil { 115 return err 116 } 117 defer conn.Close() 118 119 t.Log("accepted connection") 120 121 if _, err := conn.Write([]byte("server msg")); err != nil { 122 return err 123 } 124 125 t.Log("server: wrote message") 126 127 data := make([]byte, 100) 128 n, err := conn.Read(data) 129 if err != nil { 130 return err 131 } 132 data = data[:n] 133 if !bytes.Equal(data, []byte("client msg")) { 134 return errors.New("got invalid message from client") 135 } 136 t.Log("server: read message") 137 return conn.Close() 138 }) 139 140 c, err := b.DialContext(context.Background(), "tcp", "10.0.0.1:1234") 141 if err != nil { 142 t.Fatal(err) 143 } 144 if _, err := c.Write([]byte("client msg")); err != nil { 145 t.Fatal(err) 146 } 147 148 data := make([]byte, 100) 149 n, err := c.Read(data) 150 if err != nil { 151 t.Fatal(err) 152 } 153 data = data[:n] 154 if !bytes.Equal(data, []byte("server msg")) { 155 t.Fatal("got invalid message from client") 156 } 157 158 if err := c.Close(); err != nil { 159 t.Fatal(err) 160 } 161 162 if err := eg.Wait(); err != nil { 163 t.Fatal(err) 164 } 165 }