util_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package net
  14. import (
  15. "net"
  16. "testing"
  17. )
  18. func getIPNet(cidr string) *net.IPNet {
  19. _, ipnet, _ := net.ParseCIDR(cidr)
  20. return ipnet
  21. }
  22. func TestIPNetEqual(t *testing.T) {
  23. testCases := []struct {
  24. ipnet1 *net.IPNet
  25. ipnet2 *net.IPNet
  26. expect bool
  27. }{
  28. //null case
  29. {
  30. getIPNet("10.0.0.1/24"),
  31. getIPNet(""),
  32. false,
  33. },
  34. {
  35. getIPNet("10.0.0.0/24"),
  36. getIPNet("10.0.0.0/24"),
  37. true,
  38. },
  39. {
  40. getIPNet("10.0.0.0/24"),
  41. getIPNet("10.0.0.1/24"),
  42. true,
  43. },
  44. {
  45. getIPNet("10.0.0.0/25"),
  46. getIPNet("10.0.0.0/24"),
  47. false,
  48. },
  49. {
  50. getIPNet("10.0.1.0/24"),
  51. getIPNet("10.0.0.0/24"),
  52. false,
  53. },
  54. }
  55. for _, tc := range testCases {
  56. if tc.expect != IPNetEqual(tc.ipnet1, tc.ipnet2) {
  57. t.Errorf("Expect equality of %s and %s be to %v", tc.ipnet1.String(), tc.ipnet2.String(), tc.expect)
  58. }
  59. }
  60. }