2016-03-01 22:38:13 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-03-05 23:02:06 +08:00
|
|
|
"os"
|
2016-03-01 22:38:13 +08:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-03-05 22:57:18 +08:00
|
|
|
func TestGetProcInodesAll(t *testing.T) {
|
2016-03-05 23:02:06 +08:00
|
|
|
if os.Getenv("CIRCLECI") == "true" {
|
|
|
|
t.Skip("Skip CI")
|
|
|
|
}
|
|
|
|
|
2016-03-01 22:38:13 +08:00
|
|
|
root := common.HostProc("")
|
2016-03-05 22:57:18 +08:00
|
|
|
v, err := getProcInodesAll(root)
|
2016-03-04 22:42:22 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, v)
|
2016-03-01 22:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddrTest struct {
|
|
|
|
IP string
|
|
|
|
Port int
|
|
|
|
Error bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeAddress(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
addr := map[string]AddrTest{
|
|
|
|
"0500000A:0016": AddrTest{
|
|
|
|
IP: "10.0.0.5",
|
|
|
|
Port: 22,
|
|
|
|
},
|
|
|
|
"0100007F:D1C2": AddrTest{
|
|
|
|
IP: "127.0.0.1",
|
|
|
|
Port: 53698,
|
|
|
|
},
|
|
|
|
"11111:0035": AddrTest{
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
"0100007F:BLAH": AddrTest{
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
"0085002452100113070057A13F025401:0035": AddrTest{
|
|
|
|
IP: "2400:8500:1301:1052:a157:7:154:23f",
|
|
|
|
Port: 53,
|
|
|
|
},
|
|
|
|
"00855210011307F025401:0035": AddrTest{
|
|
|
|
Error: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for src, dst := range addr {
|
|
|
|
family := syscall.AF_INET
|
|
|
|
if len(src) > 13 {
|
|
|
|
family = syscall.AF_INET6
|
|
|
|
}
|
2016-03-04 22:42:22 +08:00
|
|
|
addr, err := decodeAddress(uint32(family), src)
|
2016-03-01 22:38:13 +08:00
|
|
|
if dst.Error {
|
|
|
|
assert.NotNil(err, src)
|
|
|
|
} else {
|
|
|
|
assert.Nil(err, src)
|
2016-03-04 22:42:22 +08:00
|
|
|
assert.Equal(dst.IP, addr.IP, src)
|
|
|
|
assert.Equal(dst.Port, int(addr.Port), src)
|
2016-03-01 22:38:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 21:32:16 +08:00
|
|
|
|
|
|
|
func TestReverse(t *testing.T) {
|
|
|
|
src := []byte{0x01, 0x02, 0x03}
|
|
|
|
assert.Equal(t, []byte{0x03, 0x02, 0x01}, Reverse(src))
|
|
|
|
}
|