tun_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package nebula
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "net"
  5. "testing"
  6. )
  7. func Test_parseRoutes(t *testing.T) {
  8. c := NewConfig()
  9. _, n, _ := net.ParseCIDR("10.0.0.0/24")
  10. // test no routes config
  11. routes, err := parseRoutes(c, n)
  12. assert.Nil(t, err)
  13. assert.Len(t, routes, 0)
  14. // not an array
  15. c.Settings["tun"] = map[interface{}]interface{}{"routes": "hi"}
  16. routes, err = parseRoutes(c, n)
  17. assert.Nil(t, routes)
  18. assert.EqualError(t, err, "tun.routes is not an array")
  19. // no routes
  20. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{}}
  21. routes, err = parseRoutes(c, n)
  22. assert.Nil(t, err)
  23. assert.Len(t, routes, 0)
  24. // weird route
  25. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{"asdf"}}
  26. routes, err = parseRoutes(c, n)
  27. assert.Nil(t, routes)
  28. assert.EqualError(t, err, "entry 1 in tun.routes is invalid")
  29. // no mtu
  30. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{}}}
  31. routes, err = parseRoutes(c, n)
  32. assert.Nil(t, routes)
  33. assert.EqualError(t, err, "entry 1.mtu in tun.routes is not present")
  34. // bad mtu
  35. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "nope"}}}
  36. routes, err = parseRoutes(c, n)
  37. assert.Nil(t, routes)
  38. assert.EqualError(t, err, "entry 1.mtu in tun.routes is not an integer: strconv.Atoi: parsing \"nope\": invalid syntax")
  39. // low mtu
  40. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "499"}}}
  41. routes, err = parseRoutes(c, n)
  42. assert.Nil(t, routes)
  43. assert.EqualError(t, err, "entry 1.mtu in tun.routes is below 500: 499")
  44. // missing route
  45. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "500"}}}
  46. routes, err = parseRoutes(c, n)
  47. assert.Nil(t, routes)
  48. assert.EqualError(t, err, "entry 1.route in tun.routes is not present")
  49. // unparsable route
  50. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "500", "route": "nope"}}}
  51. routes, err = parseRoutes(c, n)
  52. assert.Nil(t, routes)
  53. assert.EqualError(t, err, "entry 1.route in tun.routes failed to parse: invalid CIDR address: nope")
  54. // below network range
  55. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "500", "route": "1.0.0.0/8"}}}
  56. routes, err = parseRoutes(c, n)
  57. assert.Nil(t, routes)
  58. assert.EqualError(t, err, "entry 1.route in tun.routes is not contained within the network attached to the certificate; route: 1.0.0.0/8, network: 10.0.0.0/24")
  59. // above network range
  60. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{map[interface{}]interface{}{"mtu": "500", "route": "10.0.1.0/24"}}}
  61. routes, err = parseRoutes(c, n)
  62. assert.Nil(t, routes)
  63. assert.EqualError(t, err, "entry 1.route in tun.routes is not contained within the network attached to the certificate; route: 10.0.1.0/24, network: 10.0.0.0/24")
  64. // happy case
  65. c.Settings["tun"] = map[interface{}]interface{}{"routes": []interface{}{
  66. map[interface{}]interface{}{"mtu": "9000", "route": "10.0.0.0/29"},
  67. map[interface{}]interface{}{"mtu": "8000", "route": "10.0.0.1/32"},
  68. }}
  69. routes, err = parseRoutes(c, n)
  70. assert.Nil(t, err)
  71. assert.Len(t, routes, 2)
  72. tested := 0
  73. for _, r := range routes {
  74. if r.mtu == 8000 {
  75. assert.Equal(t, "10.0.0.1/32", r.route.String())
  76. tested++
  77. } else {
  78. assert.Equal(t, 9000, r.mtu)
  79. assert.Equal(t, "10.0.0.0/29", r.route.String())
  80. tested++
  81. }
  82. }
  83. if tested != 2 {
  84. t.Fatal("Did not see both routes")
  85. }
  86. }