ipnet_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 flannel authors
  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 ip
  15. import (
  16. "encoding/json"
  17. "net"
  18. "testing"
  19. )
  20. func mkIP4Net(s string, plen uint) IP4Net {
  21. ip, err := ParseIP4(s)
  22. if err != nil {
  23. panic(err)
  24. }
  25. return IP4Net{ip, plen}
  26. }
  27. func mkIP4(s string) IP4 {
  28. ip, err := ParseIP4(s)
  29. if err != nil {
  30. panic(err)
  31. }
  32. return ip
  33. }
  34. func TestIP4(t *testing.T) {
  35. nip := net.ParseIP("1.2.3.4")
  36. ip := FromIP(nip)
  37. a, b, c, d := ip.Octets()
  38. if a != 1 || b != 2 || c != 3 || d != 4 {
  39. t.Error("FromIP failed")
  40. }
  41. ip, err := ParseIP4("1.2.3.4")
  42. if err != nil {
  43. t.Error("ParseIP4 failed with: ", err)
  44. } else {
  45. a, b, c, d := ip.Octets()
  46. if a != 1 || b != 2 || c != 3 || d != 4 {
  47. t.Error("ParseIP4 failed")
  48. }
  49. }
  50. if ip.ToIP().String() != "1.2.3.4" {
  51. t.Error("ToIP failed")
  52. }
  53. if ip.String() != "1.2.3.4" {
  54. t.Error("String failed")
  55. }
  56. if ip.StringSep("*") != "1*2*3*4" {
  57. t.Error("StringSep failed")
  58. }
  59. j, err := json.Marshal(ip)
  60. if err != nil {
  61. t.Error("Marshal of IP4 failed: ", err)
  62. } else if string(j) != `"1.2.3.4"` {
  63. t.Error("Marshal of IP4 failed with unexpected value: ", j)
  64. }
  65. }
  66. func TestIP4Net(t *testing.T) {
  67. n1 := mkIP4Net("1.2.3.0", 24)
  68. if n1.ToIPNet().String() != "1.2.3.0/24" {
  69. t.Error("ToIPNet failed")
  70. }
  71. if !n1.Overlaps(n1) {
  72. t.Errorf("%s does not overlap %s", n1, n1)
  73. }
  74. n2 := mkIP4Net("1.2.0.0", 16)
  75. if !n1.Overlaps(n2) {
  76. t.Errorf("%s does not overlap %s", n1, n2)
  77. }
  78. n2 = mkIP4Net("1.2.4.0", 24)
  79. if n1.Overlaps(n2) {
  80. t.Errorf("%s overlaps %s", n1, n2)
  81. }
  82. n2 = mkIP4Net("7.2.4.0", 22)
  83. if n1.Overlaps(n2) {
  84. t.Errorf("%s overlaps %s", n1, n2)
  85. }
  86. if !n1.Contains(mkIP4("1.2.3.0")) {
  87. t.Error("Contains failed")
  88. }
  89. if !n1.Contains(mkIP4("1.2.3.4")) {
  90. t.Error("Contains failed")
  91. }
  92. if n1.Contains(mkIP4("1.2.4.0")) {
  93. t.Error("Contains failed")
  94. }
  95. j, err := json.Marshal(n1)
  96. if err != nil {
  97. t.Error("Marshal of IP4Net failed: ", err)
  98. } else if string(j) != `"1.2.3.0/24"` {
  99. t.Error("Marshal of IP4Net failed with unexpected value: ", j)
  100. }
  101. n1.IncrementIP()
  102. if n1.String() != "1.2.3.1/24" {
  103. t.Error("IncrementIP() failed")
  104. }
  105. }