Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"testing"
"net"
)

var test10ips = [...]string{ "10.0.0.1", "10.16.0.1", "10.22.0.1", "10.30.0.1", "11.200.0.1", "10.255.255.255" }
var _,aclass,_ = net.ParseCIDR("10.0.0.0/8")
var test172ips = [...]string{ "172.16.0.1", "172.22.0.1", "172.30.0.1", "172.200.0.1", "172.255.255.255" }
var _,bclass,_ = net.ParseCIDR("172.16.0.0/12")
var test192ips = [...]string{ "192.168.0.1", "192.168.22.1", "192.169.30.1", "191.168.200.25", "192.168.255.255" }
var _,cclass,_ = net.ParseCIDR("192.168.0.0/16")


func Test10(t *testing.T) {
for _,ip := range test10ips {
var theIP = net.ParseIP(ip)
if (aclass.Contains(theIP) != subnet10.MatchString(ip)) {
t.Errorf("Regex subnet10(%v) and net.Contains(%v) disagree about ip %v", subnet10.MatchString(ip), aclass.Contains(theIP), ip)
}
}
}

func Test172(t *testing.T) {
for _,ip := range test172ips {
var theIP = net.ParseIP(ip)
if (bclass.Contains(theIP) != subnet172.MatchString(ip)) {
t.Errorf("Regex subnet172(%v) and net.Contains(%v) disagree about ip %v", subnet172.MatchString(ip), bclass.Contains(theIP), ip)
}
}
}

func Test192(t *testing.T) {
for _,ip := range test192ips {
var theIP = net.ParseIP(ip)
if (cclass.Contains(theIP) != subnet192.MatchString(ip)) {
t.Errorf("Regex subnet192(%v) and net.Contains(%v) disagree about ip %v", subnet192.MatchString(ip), cclass.Contains(theIP), ip)
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var userAgentStrings []string

var subnet10 = regexp.MustCompile(`^10\..*`)
var subnet172 = regexp.MustCompile(`^172\.[1-3][678901].*`)
var subnet172 = regexp.MustCompile(`^172\.(1[6-9]|2[0-9]|3[0-1])\..*`)
var subnet192 = regexp.MustCompile(`^192\.168\..*`)
var localhostRegex = regexp.MustCompile(`^http://localhost`)
var loopbackRegex = regexp.MustCompile(`127.0.0.1`)
Expand Down