You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flatip_test.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package flatip
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "net"
  6. "testing"
  7. "time"
  8. )
  9. func easyParseIP(ipstr string) (result net.IP) {
  10. result = net.ParseIP(ipstr)
  11. if result == nil {
  12. panic(ipstr)
  13. }
  14. return
  15. }
  16. func easyParseFlat(ipstr string) (result IP) {
  17. x := easyParseIP(ipstr)
  18. return FromNetIP(x)
  19. }
  20. func easyParseIPNet(nipstr string) (result net.IPNet) {
  21. _, nip, err := net.ParseCIDR(nipstr)
  22. if err != nil {
  23. panic(err)
  24. }
  25. return *nip
  26. }
  27. func TestBasic(t *testing.T) {
  28. nip := easyParseIP("8.8.8.8")
  29. flatip := FromNetIP(nip)
  30. if flatip.String() != "8.8.8.8" {
  31. t.Errorf("conversions don't work")
  32. }
  33. }
  34. func TestLoopback(t *testing.T) {
  35. localhost_v4 := easyParseFlat("127.0.0.1")
  36. localhost_v4_again := easyParseFlat("127.2.3.4")
  37. google := easyParseFlat("8.8.8.8")
  38. loopback_v6 := easyParseFlat("::1")
  39. google_v6 := easyParseFlat("2607:f8b0:4006:801::2004")
  40. if !(localhost_v4.IsLoopback() && localhost_v4_again.IsLoopback() && loopback_v6.IsLoopback()) {
  41. t.Errorf("can't detect loopbacks")
  42. }
  43. if google_v6.IsLoopback() || google.IsLoopback() {
  44. t.Errorf("incorrectly detected loopbacks")
  45. }
  46. }
  47. func TestContains(t *testing.T) {
  48. nipnet := easyParseIPNet("8.8.0.0/16")
  49. flatipnet := FromNetIPNet(nipnet)
  50. nip := easyParseIP("8.8.8.8")
  51. flatip_ := FromNetIP(nip)
  52. if !flatipnet.Contains(flatip_) {
  53. t.Errorf("contains doesn't work")
  54. }
  55. }
  56. var testIPStrs = []string{
  57. "8.8.8.8",
  58. "127.0.0.1",
  59. "1.1.1.1",
  60. "128.127.65.64",
  61. "2001:0db8::1",
  62. "::1",
  63. "255.255.255.255",
  64. }
  65. func doMaskingTest(ip net.IP, t *testing.T) {
  66. flat := FromNetIP(ip)
  67. netLen := len(ip) * 8
  68. for i := 0; i < netLen; i++ {
  69. masked := flat.Mask(i, netLen)
  70. netMask := net.CIDRMask(i, netLen)
  71. netMasked := ip.Mask(netMask)
  72. if !bytes.Equal(masked[:], netMasked.To16()) {
  73. t.Errorf("Masking %s with %d/%d; expected %s, got %s", ip.String(), i, netLen, netMasked.String(), masked.String())
  74. }
  75. }
  76. }
  77. func TestMasking(t *testing.T) {
  78. for _, ipstr := range testIPStrs {
  79. doMaskingTest(easyParseIP(ipstr), t)
  80. }
  81. }
  82. func TestMaskingFuzz(t *testing.T) {
  83. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  84. buf := make([]byte, 4)
  85. for i := 0; i < 10000; i++ {
  86. r.Read(buf)
  87. doMaskingTest(net.IP(buf), t)
  88. }
  89. buf = make([]byte, 16)
  90. for i := 0; i < 10000; i++ {
  91. r.Read(buf)
  92. doMaskingTest(net.IP(buf), t)
  93. }
  94. }
  95. func BenchmarkMasking(b *testing.B) {
  96. ip := easyParseIP("2001:0db8::42")
  97. flat := FromNetIP(ip)
  98. b.ResetTimer()
  99. for i := 0; i < b.N; i++ {
  100. flat.Mask(64, 128)
  101. }
  102. }
  103. func BenchmarkMaskingLegacy(b *testing.B) {
  104. ip := easyParseIP("2001:0db8::42")
  105. mask := net.CIDRMask(64, 128)
  106. b.ResetTimer()
  107. for i := 0; i < b.N; i++ {
  108. ip.Mask(mask)
  109. }
  110. }
  111. func BenchmarkMaskingCached(b *testing.B) {
  112. i := easyParseIP("2001:0db8::42")
  113. flat := FromNetIP(i)
  114. mask := cidrMask(64, 128)
  115. b.ResetTimer()
  116. for i := 0; i < b.N; i++ {
  117. flat.applyMask(mask)
  118. }
  119. }
  120. func BenchmarkMaskingConstruct(b *testing.B) {
  121. for i := 0; i < b.N; i++ {
  122. cidrMask(69, 128)
  123. }
  124. }
  125. func BenchmarkContains(b *testing.B) {
  126. ip := easyParseIP("2001:0db8::42")
  127. flat := FromNetIP(ip)
  128. _, ipnet, err := net.ParseCIDR("2001:0db8::/64")
  129. if err != nil {
  130. panic(err)
  131. }
  132. flatnet := FromNetIPNet(*ipnet)
  133. b.ResetTimer()
  134. for i := 0; i < b.N; i++ {
  135. flatnet.Contains(flat)
  136. }
  137. }
  138. func BenchmarkContainsLegacy(b *testing.B) {
  139. ip := easyParseIP("2001:0db8::42")
  140. _, ipnetptr, err := net.ParseCIDR("2001:0db8::/64")
  141. if err != nil {
  142. panic(err)
  143. }
  144. ipnet := *ipnetptr
  145. b.ResetTimer()
  146. for i := 0; i < b.N; i++ {
  147. ipnet.Contains(ip)
  148. }
  149. }