ipmatch_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 rbacutils
  15. import "testing"
  16. func TestMatchIPStrings(t *testing.T) {
  17. cases := []struct {
  18. prefixes string
  19. ip string
  20. want bool
  21. }{
  22. {
  23. prefixes: "",
  24. ip: "127.0.0.1",
  25. want: true,
  26. },
  27. {
  28. prefixes: "10.0.0.0/8",
  29. ip: "10.8.0.1",
  30. want: true,
  31. },
  32. {
  33. prefixes: "10.0.0.0/8,192.168.0.0/16",
  34. ip: "172.16.0.23",
  35. want: false,
  36. },
  37. {
  38. prefixes: "10.0.0.0/8,192.168.0.0/16",
  39. ip: "10.16.0.23",
  40. want: true,
  41. },
  42. {
  43. prefixes: "10.0.0.0/8,192.168.0.0/16",
  44. ip: "192.168.0.23",
  45. want: true,
  46. },
  47. }
  48. for _, c := range cases {
  49. got := MatchIPStrings(c.prefixes, c.ip)
  50. if got != c.want {
  51. t.Errorf("prefix %s ip %s got %v want %v", c.prefixes, c.ip, got, c.want)
  52. }
  53. }
  54. }