routeinfo_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package netutils2
  15. import (
  16. "net"
  17. "testing"
  18. )
  19. func TestParseRouteInfo(t *testing.T) {
  20. cases := []struct {
  21. net string
  22. gw string
  23. want *SRouteInfo
  24. }{
  25. {
  26. net: "10.0.0.0/8",
  27. gw: "10.168.120.1",
  28. want: &SRouteInfo{
  29. SPrefixInfo: SPrefixInfo{
  30. Prefix: net.ParseIP("10.0.0.0"),
  31. PrefixLen: 8,
  32. },
  33. Gateway: net.ParseIP("10.168.120.1"),
  34. },
  35. },
  36. {
  37. net: "::/0",
  38. gw: "3ffe:3200:fe::1",
  39. want: &SRouteInfo{
  40. SPrefixInfo: SPrefixInfo{
  41. Prefix: net.ParseIP("::"),
  42. PrefixLen: 0,
  43. },
  44. Gateway: net.ParseIP("3ffe:3200:fe::1"),
  45. },
  46. },
  47. {
  48. net: "fd00:ec2::254/128",
  49. gw: "::",
  50. want: &SRouteInfo{
  51. SPrefixInfo: SPrefixInfo{
  52. Prefix: net.ParseIP("fd00:ec2::254"),
  53. PrefixLen: 128,
  54. },
  55. Gateway: net.ParseIP("::"),
  56. },
  57. },
  58. {
  59. net: "fe80::a9fe:a9fe/128",
  60. gw: "::",
  61. want: &SRouteInfo{
  62. SPrefixInfo: SPrefixInfo{
  63. Prefix: net.ParseIP("fe80::a9fe:a9fe"),
  64. PrefixLen: 128,
  65. },
  66. Gateway: net.ParseIP("::"),
  67. },
  68. },
  69. }
  70. for _, c := range cases {
  71. routeInfo, err := ParseRouteInfo([]string{c.net, c.gw})
  72. if err != nil {
  73. t.Errorf("parse route %s error: %v", c.net, err)
  74. continue
  75. }
  76. if routeInfo.String() != c.want.String() {
  77. t.Errorf("parse route expect %s got: %s", c.want.String(), routeInfo.String())
  78. }
  79. }
  80. }