control_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, &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(nil)
  45. remotes.unlockedPrependV4(0, NewIp4AndPort(remote1.IP, uint32(remote1.Port)))
  46. remotes.unlockedPrependV6(0, NewIp6AndPort(remote2.IP, uint32(remote2.Port)))
  47. hm.unlockedAddHostInfo(&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. relayState: RelayState{
  57. relays: map[iputil.VpnIp]struct{}{},
  58. relayForByIp: map[iputil.VpnIp]*Relay{},
  59. relayForByIdx: map[uint32]*Relay{},
  60. },
  61. }, &Interface{})
  62. hm.unlockedAddHostInfo(&HostInfo{
  63. remote: remote1,
  64. remotes: remotes,
  65. ConnectionState: &ConnectionState{
  66. peerCert: nil,
  67. },
  68. remoteIndexId: 200,
  69. localIndexId: 201,
  70. vpnIp: iputil.Ip2VpnIp(ipNet2.IP),
  71. relayState: RelayState{
  72. relays: map[iputil.VpnIp]struct{}{},
  73. relayForByIp: map[iputil.VpnIp]*Relay{},
  74. relayForByIdx: map[uint32]*Relay{},
  75. },
  76. }, &Interface{})
  77. c := Control{
  78. f: &Interface{
  79. hostMap: hm,
  80. },
  81. l: logrus.New(),
  82. }
  83. thi := c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet.IP), false)
  84. expectedInfo := ControlHostInfo{
  85. VpnIp: net.IPv4(1, 2, 3, 4).To4(),
  86. LocalIndex: 201,
  87. RemoteIndex: 200,
  88. RemoteAddrs: []*udp.Addr{remote2, remote1},
  89. Cert: crt.Copy(),
  90. MessageCounter: 0,
  91. CurrentRemote: udp.NewAddr(net.ParseIP("0.0.0.100"), 4444),
  92. CurrentRelaysToMe: []iputil.VpnIp{},
  93. CurrentRelaysThroughMe: []iputil.VpnIp{},
  94. }
  95. // Make sure we don't have any unexpected fields
  96. assertFields(t, []string{"VpnIp", "LocalIndex", "RemoteIndex", "RemoteAddrs", "Cert", "MessageCounter", "CurrentRemote", "CurrentRelaysToMe", "CurrentRelaysThroughMe"}, thi)
  97. test.AssertDeepCopyEqual(t, &expectedInfo, thi)
  98. // Make sure we don't panic if the host info doesn't have a cert yet
  99. assert.NotPanics(t, func() {
  100. thi = c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet2.IP), false)
  101. })
  102. }
  103. func assertFields(t *testing.T, expected []string, actualStruct interface{}) {
  104. val := reflect.ValueOf(actualStruct).Elem()
  105. fields := make([]string, val.NumField())
  106. for i := 0; i < val.NumField(); i++ {
  107. fields[i] = val.Type().Field(i).Name
  108. }
  109. assert.Equal(t, expected, fields)
  110. }