control_test.go 2.9 KB

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