helpers_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 dhcp
  15. import (
  16. "fmt"
  17. "testing"
  18. "yunion.io/x/onecloud/pkg/util/netutils2"
  19. )
  20. func compareBytes(b1, b2 []byte) error {
  21. if len(b1) != len(b2) {
  22. return fmt.Errorf("length not match")
  23. }
  24. for i := range b1 {
  25. if b1[i] != b2[i] {
  26. return fmt.Errorf("%dth not equal", i)
  27. }
  28. }
  29. return nil
  30. }
  31. func TestGetClasslessRoutePack(t *testing.T) {
  32. cases := []struct {
  33. net string
  34. gw string
  35. want []byte
  36. }{
  37. {
  38. net: "10.0.0.0/8",
  39. gw: "10.168.120.1",
  40. want: []byte{
  41. 8, 10, 10, 168, 120, 1,
  42. },
  43. },
  44. {
  45. net: "192.168.0.0/16",
  46. gw: "10.168.120.1",
  47. want: []byte{
  48. 16, 192, 168, 10, 168, 120, 1,
  49. },
  50. },
  51. {
  52. net: "172.16.0.0/12",
  53. gw: "10.168.222.1",
  54. want: []byte{
  55. 12, 172, 16, 10, 168, 222, 1,
  56. },
  57. },
  58. }
  59. for _, c := range cases {
  60. routeInfo, err := netutils2.ParseRouteInfo([]string{c.net, c.gw})
  61. if err != nil {
  62. t.Errorf("parse route %s error: %v", c.net, err)
  63. continue
  64. }
  65. got := getClasslessRoutePack(*routeInfo)
  66. if err := compareBytes(c.want, got); err != nil {
  67. t.Errorf("net: %s gw: %s want: %#v got: %#v", c.net, c.gw, c.want, got)
  68. }
  69. }
  70. }