control_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package nebula
  2. import (
  3. "net"
  4. "reflect"
  5. "testing"
  6. "time"
  7. "github.com/sirupsen/logrus"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/iputil"
  10. "github.com/slackhq/nebula/test"
  11. "github.com/slackhq/nebula/udp"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestControl_GetHostInfoByVpnIp(t *testing.T) {
  15. l := test.NewLogger()
  16. // Special care must be taken to re-use all objects provided to the hostmap and certificate in the expectedInfo object
  17. // To properly ensure we are not exposing core memory to the caller
  18. hm := NewHostMap(l, "test", &net.IPNet{}, make([]*net.IPNet, 0))
  19. remote1 := udp.NewAddr(net.ParseIP("0.0.0.100"), 4444)
  20. remote2 := udp.NewAddr(net.ParseIP("1:2:3:4:5:6:7:8"), 4444)
  21. ipNet := net.IPNet{
  22. IP: net.IPv4(1, 2, 3, 4),
  23. Mask: net.IPMask{255, 255, 255, 0},
  24. }
  25. ipNet2 := net.IPNet{
  26. IP: net.ParseIP("1:2:3:4:5:6:7:8"),
  27. Mask: net.IPMask{255, 255, 255, 0},
  28. }
  29. crt := &cert.NebulaCertificate{
  30. Details: cert.NebulaCertificateDetails{
  31. Name: "test",
  32. Ips: []*net.IPNet{&ipNet},
  33. Subnets: []*net.IPNet{},
  34. Groups: []string{"default-group"},
  35. NotBefore: time.Unix(1, 0),
  36. NotAfter: time.Unix(2, 0),
  37. PublicKey: []byte{5, 6, 7, 8},
  38. IsCA: false,
  39. Issuer: "the-issuer",
  40. InvertedGroups: map[string]struct{}{"default-group": {}},
  41. },
  42. Signature: []byte{1, 2, 1, 2, 1, 3},
  43. }
  44. remotes := NewRemoteList()
  45. remotes.unlockedPrependV4(0, NewIp4AndPort(remote1.IP, uint32(remote1.Port)))
  46. remotes.unlockedPrependV6(0, NewIp6AndPort(remote2.IP, uint32(remote2.Port)))
  47. hm.Add(iputil.Ip2VpnIp(ipNet.IP), &HostInfo{
  48. remote: remote1,
  49. remotes: remotes,
  50. ConnectionState: &ConnectionState{
  51. peerCert: crt,
  52. },
  53. remoteIndexId: 200,
  54. localIndexId: 201,
  55. vpnIp: iputil.Ip2VpnIp(ipNet.IP),
  56. })
  57. hm.Add(iputil.Ip2VpnIp(ipNet2.IP), &HostInfo{
  58. remote: remote1,
  59. remotes: remotes,
  60. ConnectionState: &ConnectionState{
  61. peerCert: nil,
  62. },
  63. remoteIndexId: 200,
  64. localIndexId: 201,
  65. vpnIp: iputil.Ip2VpnIp(ipNet2.IP),
  66. })
  67. c := Control{
  68. f: &Interface{
  69. hostMap: hm,
  70. },
  71. l: logrus.New(),
  72. }
  73. thi := c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet.IP), false)
  74. expectedInfo := ControlHostInfo{
  75. VpnIp: net.IPv4(1, 2, 3, 4).To4(),
  76. LocalIndex: 201,
  77. RemoteIndex: 200,
  78. RemoteAddrs: []*udp.Addr{remote2, remote1},
  79. CachedPackets: 0,
  80. Cert: crt.Copy(),
  81. MessageCounter: 0,
  82. CurrentRemote: udp.NewAddr(net.ParseIP("0.0.0.100"), 4444),
  83. }
  84. // Make sure we don't have any unexpected fields
  85. assertFields(t, []string{"VpnIp", "LocalIndex", "RemoteIndex", "RemoteAddrs", "CachedPackets", "Cert", "MessageCounter", "CurrentRemote"}, thi)
  86. test.AssertDeepCopyEqual(t, &expectedInfo, thi)
  87. // Make sure we don't panic if the host info doesn't have a cert yet
  88. assert.NotPanics(t, func() {
  89. thi = c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet2.IP), false)
  90. })
  91. }
  92. func assertFields(t *testing.T, expected []string, actualStruct interface{}) {
  93. val := reflect.ValueOf(actualStruct).Elem()
  94. fields := make([]string, val.NumField())
  95. for i := 0; i < val.NumField(); i++ {
  96. fields[i] = val.Type().Field(i).Name
  97. }
  98. assert.Equal(t, expected, fields)
  99. }