Skip to content

Commit

Permalink
Remame Entries() to Connections() to be more symetrical
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Oct 16, 2018
1 parent 976b412 commit 73dd025
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Package netstat helps you query open network connections.
import "github.com/bastjan/netstat"

// Query open tcp sockets
netstat.TCP.Entries()
netstat.TCP.Connections()

// Query open udp sockets for ipv6 connections
netstat.UDP6.Entries()
netstat.UDP6.Connections()
```

## Development Status: Work in Progress
Expand Down
10 changes: 5 additions & 5 deletions netstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ var (
procFdLinkParseType2 = regexp.MustCompile(`^\[0000\]:(\d+)$`)
)

// Entries queries the given /proc/net file and returns the found entries.
// Connections queries the given /proc/net file and returns the found connections.
// Returns an error if the /proc/net file can't be read.
func (n Netstat) Entries() ([]Connection, error) {
func (n Netstat) Connections() ([]Connection, error) {
inodeToPid := make(chan map[uint64]int)

go func() {
Expand All @@ -83,12 +83,12 @@ func (n Netstat) Entries() ([]Connection, error) {
return nil, err
}

entries := procNetToEntries(lines, <-inodeToPid)
connections := procNetToConnections(lines, <-inodeToPid)

return entries, nil
return connections, nil
}

func procNetToEntries(lines [][]string, inodeToPid map[uint64]int) []Connection {
func procNetToConnections(lines [][]string, inodeToPid map[uint64]int) []Connection {
connections := make([]Connection, 0, len(lines))
for _, line := range lines {
localIPPort := strings.Split(line[1], ":")
Expand Down
18 changes: 9 additions & 9 deletions netstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ func init() {
netstat.ProcRoot = "./test/proc"
}

func TestEntries(t *testing.T) {
entries, err := netstat.TCP.Entries()
func TestConnections(t *testing.T) {
connections, err := netstat.TCP.Connections()
assert.NilError(t, err)
assert.DeepEqual(t, entries, []netstat.Connection{tcpConnection})
assert.DeepEqual(t, connections, []netstat.Connection{tcpConnection})

entries, err = netstat.TCP6.Entries()
connections, err = netstat.TCP6.Connections()
assert.NilError(t, err)
assert.DeepEqual(t, entries, []netstat.Connection{tcp6Connection})
assert.DeepEqual(t, connections, []netstat.Connection{tcp6Connection})
}

func TestEntriesProcNetNotFound(t *testing.T) {
_, err := netstat.Netstat("./nothere").Entries()
func TestConnectionsProcNetNotFound(t *testing.T) {
_, err := netstat.Netstat("./nothere").Connections()
assert.ErrorContains(t, err, "can't open proc file")
assert.ErrorContains(t, err, "test/proc/nothere")
}

func TestEntriesEmptyFileDoesNotCrashNetstat(t *testing.T) {
_, err := netstat.Netstat("net/empty").Entries()
func TestConnectionsEmptyFileDoesNotCrashNetstat(t *testing.T) {
_, err := netstat.Netstat("net/empty").Connections()
assert.ErrorContains(t, err, "net/empty has no content")
}

0 comments on commit 73dd025

Please sign in to comment.