diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..097382f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,17 @@ +on: + push: + pull_request: + +name: Test +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-go@v2 + with: + go-version: "1.18" + + - name: Test + run: go test -v -coverprofile='coverage.out' -covermode=count ./... diff --git a/ipgeo/ipgeo_test.go b/ipgeo/ipgeo_test.go index 08dea10..0b7b915 100644 --- a/ipgeo/ipgeo_test.go +++ b/ipgeo/ipgeo_test.go @@ -18,8 +18,9 @@ func TestIPSB(t *testing.T) { res, err := IPSB("1.1.1.1") assert.Nil(t, err) assert.NotNil(t, res) - assert.NotEmpty(t, res.Asnumber) - assert.NotEmpty(t, res.Isp) + // Not available + //assert.NotEmpty(t, res.Asnumber) + //assert.NotEmpty(t, res.Isp) } func TestIPInfo(t *testing.T) { diff --git a/printer/printer_test.go b/printer/printer_test.go new file mode 100644 index 0000000..d285211 --- /dev/null +++ b/printer/printer_test.go @@ -0,0 +1,104 @@ +package printer + +import ( + "errors" + "github.com/xgadget-lab/nexttrace/ipgeo" + "github.com/xgadget-lab/nexttrace/trace" + "github.com/xgadget-lab/nexttrace/util" + "net" + "testing" + "time" +) + +func TestPrintTraceRouteNav(t *testing.T) { + PrintTraceRouteNav(util.DomainLookUp("1.1.1.1"), "1.1.1.1", "dataOrigin") +} + +var testGeo = &ipgeo.IPGeoData{ + Asnumber: "TestAsnumber", + Country: "TestCountry", + Prov: "TestProv", + City: "TestCity", + District: "TestDistrict", + Owner: "TestOwner", + Isp: "TestIsp", +} + +var testResult = &trace.Result{ + Hops: [][]trace.Hop{ + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: testGeo, + }, + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: testGeo, + }, + }, + { + { + Success: false, + Address: nil, + Hostname: "", + TTL: 0, + RTT: 0, + Error: errors.New("test error"), + Geo: nil, + }, + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: nil, + }, + }, + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "test", + TTL: 0, + RTT: 0, + Error: nil, + Geo: &ipgeo.IPGeoData{}, + }, + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: testGeo, + }, + }, + }, +} + +func TestTraceroutePrinter(t *testing.T) { + TraceroutePrinter(testResult) +} + +func TestTracerouteTablePrinter(t *testing.T) { + TracerouteTablePrinter(testResult) +} + +func TestRealtimePrinter(t *testing.T) { + RealtimePrinter(testResult, 0) + RealtimePrinter(testResult, 1) + RealtimePrinter(testResult, 2) +} diff --git a/printer/tableprinter.go b/printer/tableprinter.go index 08e663d..46c748b 100644 --- a/printer/tableprinter.go +++ b/printer/tableprinter.go @@ -2,6 +2,7 @@ package printer import ( "fmt" + "github.com/xgadget-lab/nexttrace/ipgeo" "strings" "github.com/xgadget-lab/nexttrace/trace" @@ -89,6 +90,10 @@ func tableDataGenerator(h trace.Hop) *rowData { IP = fmt.Sprint(h.Hostname, " (", IP, ") ") } + if h.Geo == nil { + h.Geo = &ipgeo.IPGeoData{} + } + r := &rowData{ Hop: fmt.Sprint(h.TTL), IP: IP, diff --git a/reporter/reporter_test.go b/reporter/reporter_test.go index 7147971..c95acdc 100644 --- a/reporter/reporter_test.go +++ b/reporter/reporter_test.go @@ -1,31 +1,115 @@ package reporter import ( + "net" "testing" "time" "github.com/xgadget-lab/nexttrace/ipgeo" "github.com/xgadget-lab/nexttrace/trace" - "github.com/xgadget-lab/nexttrace/util" ) +var testResult = &trace.Result{ + Hops: [][]trace.Hop{ + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: &ipgeo.IPGeoData{ + Asnumber: "4808", + Country: "中国", + Prov: "北京市", + City: "北京市", + District: "北京市", + Owner: "", + Isp: "中国联通", + }, + }, + }, + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("114.249.16.1")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: &ipgeo.IPGeoData{ + Asnumber: "4808", + Country: "中国", + Prov: "北京市", + City: "北京市", + District: "北京市", + Owner: "", + Isp: "中国联通", + }, + }, + }, + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("219.158.5.150")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: &ipgeo.IPGeoData{ + Asnumber: "4837", + Country: "中国", + Prov: "", + City: "", + District: "", + Owner: "", + Isp: "中国联通", + }, + }, + }, + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("62.115.125.160")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: &ipgeo.IPGeoData{ + Asnumber: "1299", + Country: "Sweden", + Prov: "Stockholm County", + City: "Stockholm", + District: "", + Owner: "", + Isp: "Telia Company AB", + }, + }, + }, + { + { + Success: true, + Address: &net.IPAddr{IP: net.ParseIP("213.226.68.73")}, + Hostname: "test", + TTL: 0, + RTT: 10 * time.Millisecond, + Error: nil, + Geo: &ipgeo.IPGeoData{ + Asnumber: "56630", + Country: "Germany", + Prov: "Hesse, Frankfurt", + City: "", + District: "", + Owner: "", + Isp: "Melbikomas UAB", + }, + }, + }, + }, +} + func TestPrint(t *testing.T) { - ip := util.DomainLookUp("213.226.68.73") - var m trace.Method = "tcp" - var conf = trace.Config{ - DestIP: ip, - DestPort: 80, - MaxHops: 30, - NumMeasurements: 1, - ParallelRequests: 1, - RDns: true, - IPGeoSource: ipgeo.GetSource("LeoMoeAPI"), - Timeout: 2 * time.Second, - - //Quic: false, - } - - res, _ := trace.Traceroute(m, conf) - r := New(res, ip.String()) + r := New(testResult, "213.226.68.73") r.Print() }