ip6net_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 mkIP6Net(s string, plen uint) IP6Net {
  21. ip, err := ParseIP6(s)
  22. if err != nil {
  23. panic(err)
  24. }
  25. return IP6Net{ip, plen}
  26. }
  27. func mkIP6(s string) *IP6 {
  28. ip, err := ParseIP6(s)
  29. if err != nil {
  30. panic(err)
  31. }
  32. return ip
  33. }
  34. func TestIP6(t *testing.T) {
  35. nip := net.ParseIP("fc00::1")
  36. ip := FromIP6(nip)
  37. ipStr := ip.String()
  38. if ipStr != "fc00::1" {
  39. t.Error("FromIP6 failed")
  40. }
  41. ip, err := ParseIP6("fc00::1")
  42. if err != nil {
  43. t.Error("ParseIP6 failed with: ", err)
  44. } else {
  45. ipStr := ip.String()
  46. if ipStr != "fc00::1" {
  47. t.Error("ParseIP6 failed")
  48. }
  49. }
  50. if ip.ToIP().String() != "fc00::1" {
  51. t.Error("ToIP failed")
  52. }
  53. j, err := json.Marshal(ip)
  54. if err != nil {
  55. t.Error("Marshal of IP6 failed: ", err)
  56. } else if string(j) != `"fc00::1"` {
  57. t.Error("Marshal of IP6 failed with unexpected value: ", j)
  58. }
  59. }
  60. func TestIP6Net(t *testing.T) {
  61. n1 := mkIP6Net("fc00:1::", 64)
  62. if n1.ToIPNet().String() != "fc00:1::/64" {
  63. t.Error("ToIPNet failed")
  64. }
  65. if !n1.Overlaps(n1) {
  66. t.Errorf("%s does not overlap %s", n1, n1)
  67. }
  68. n2 := mkIP6Net("fc00::", 16)
  69. if !n1.Overlaps(n2) {
  70. t.Errorf("%s does not overlap %s", n1, n2)
  71. }
  72. n2 = mkIP6Net("fc00:2::", 64)
  73. if n1.Overlaps(n2) {
  74. t.Errorf("%s overlaps %s", n1, n2)
  75. }
  76. n2 = mkIP6Net("fb00:2::", 48)
  77. if n1.Overlaps(n2) {
  78. t.Errorf("%s overlaps %s", n1, n2)
  79. }
  80. if !n1.Contains(mkIP6("fc00:1::")) {
  81. t.Error("Contains failed")
  82. }
  83. if !n1.Contains(mkIP6("fc00:1::1")) {
  84. t.Error("Contains failed")
  85. }
  86. if n1.Contains(mkIP6("fc00:2::")) {
  87. t.Error("Contains failed")
  88. }
  89. j, err := json.Marshal(n1)
  90. if err != nil {
  91. t.Error("Marshal of IP6Net failed: ", err)
  92. } else if string(j) != `"fc00:1::/64"` {
  93. t.Error("Marshal of IP6Net failed with unexpected value: ", j)
  94. }
  95. n1.IncrementIP()
  96. if n1.String() != "fc00:1::1/64" {
  97. t.Error("IncrementIP() failed")
  98. }
  99. }