Skip to content

Commit

Permalink
Merge pull request #373 from bi-zone/master
Browse files Browse the repository at this point in the history
Add Inode field into netIPSocketLine structure
  • Loading branch information
SuperQ committed Jun 23, 2021
2 parents afad6d6 + fbd03f7 commit 8daa8ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
10 changes: 8 additions & 2 deletions net_ip_socket.go
Expand Up @@ -65,6 +65,7 @@ type (
TxQueue uint64
RxQueue uint64
UID uint64
Inode uint64
}
)

Expand Down Expand Up @@ -150,9 +151,9 @@ func parseIP(hexIP string) (net.IP, error) {
// parseNetIPSocketLine parses a single line, represented by a list of fields.
func parseNetIPSocketLine(fields []string) (*netIPSocketLine, error) {
line := &netIPSocketLine{}
if len(fields) < 8 {
if len(fields) < 10 {
return nil, fmt.Errorf(
"cannot parse net socket line as it has less then 8 columns %q",
"cannot parse net socket line as it has less then 10 columns %q",
strings.Join(fields, " "),
)
}
Expand Down Expand Up @@ -216,5 +217,10 @@ func parseNetIPSocketLine(fields []string) (*netIPSocketLine, error) {
return nil, fmt.Errorf("cannot parse uid value in socket line: %w", err)
}

// inode
if line.Inode, err = strconv.ParseUint(fields[9], 0, 64); err != nil {
return nil, fmt.Errorf("cannot parse inode value in socket line: %w", err)
}

return line, nil
}
27 changes: 17 additions & 10 deletions net_ip_socket_test.go
Expand Up @@ -28,7 +28,7 @@ func Test_parseNetIPSocketLine(t *testing.T) {
}{
{
name: "reading valid lines, no issue should happened",
fields: []string{"11:", "00000000:0000", "00000000:0000", "0A", "00000017:0000002A", "0:0", "0", "1000"},
fields: []string{"11:", "00000000:0000", "00000000:0000", "0A", "00000017:0000002A", "0:0", "0", "1000", "0", "39309"},
want: &netIPSocketLine{
Sl: 11,
LocalAddr: net.IP{0, 0, 0, 0},
Expand All @@ -39,53 +39,60 @@ func Test_parseNetIPSocketLine(t *testing.T) {
TxQueue: 23,
RxQueue: 42,
UID: 1000,
Inode: 39309,
},
},
{
name: "error case - invalid line - number of fields/columns < 8",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0:0", "0"},
name: "error case - invalid line - number of fields/columns < 10",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0:0", "0", "0"},
want: nil,
wantErr: true,
},
{
name: "error case - parse sl - not a valid uint",
fields: []string{"a:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0"},
fields: []string{"a:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse local_address - not a valid hex",
fields: []string{"1:", "0000000O:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0"},
fields: []string{"1:", "0000000O:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse rem_address - not a valid hex",
fields: []string{"1:", "00000000:0000", "0000000O:0000", "07", "00000000:00000001", "0:0", "0", "0"},
fields: []string{"1:", "00000000:0000", "0000000O:0000", "07", "00000000:00000001", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - cannot parse line - missing colon",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0000000000000001", "0:0", "0", "0"},
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "0000000000000001", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse tx_queue - not a valid hex",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "DEADCODE:00000001", "0:0", "0", "0"},
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "DEADCODE:00000001", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse rx_queue - not a valid hex",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:FEEDCODE", "0:0", "0", "0"},
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:FEEDCODE", "0:0", "0", "0", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse UID - not a valid uint",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10"},
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10", "0", "39309"},
want: nil,
wantErr: true,
},
{
name: "error case - parse Inode - not a valid uint",
fields: []string{"1:", "00000000:0000", "00000000:0000", "07", "00000000:00000001", "0:0", "0", "-10", "0", "-39309"},
want: nil,
wantErr: true,
},
Expand Down
5 changes: 5 additions & 0 deletions net_tcp_test.go
Expand Up @@ -40,6 +40,7 @@ func Test_newNetTCP(t *testing.T) {
TxQueue: 0,
RxQueue: 1,
UID: 0,
Inode: 2740,
},
&netIPSocketLine{
Sl: 1,
Expand All @@ -51,6 +52,7 @@ func Test_newNetTCP(t *testing.T) {
TxQueue: 1,
RxQueue: 0,
UID: 0,
Inode: 2740,
},
&netIPSocketLine{
Sl: 2,
Expand All @@ -62,6 +64,7 @@ func Test_newNetTCP(t *testing.T) {
TxQueue: 1,
RxQueue: 1,
UID: 0,
Inode: 2740,
},
},
wantErr: false,
Expand All @@ -80,6 +83,7 @@ func Test_newNetTCP(t *testing.T) {
TxQueue: 0,
RxQueue: 0,
UID: 981,
Inode: 21040,
},
&netIPSocketLine{
Sl: 6073,
Expand All @@ -91,6 +95,7 @@ func Test_newNetTCP(t *testing.T) {
TxQueue: 0,
RxQueue: 0,
UID: 1000,
Inode: 11337031,
},
},
wantErr: false,
Expand Down
5 changes: 5 additions & 0 deletions net_udp_test.go
Expand Up @@ -40,6 +40,7 @@ func Test_newNetUDP(t *testing.T) {
TxQueue: 0,
RxQueue: 1,
UID: 0,
Inode: 2740,
},
&netIPSocketLine{
Sl: 1,
Expand All @@ -51,6 +52,7 @@ func Test_newNetUDP(t *testing.T) {
TxQueue: 1,
RxQueue: 0,
UID: 0,
Inode: 2740,
},
&netIPSocketLine{
Sl: 2,
Expand All @@ -62,6 +64,7 @@ func Test_newNetUDP(t *testing.T) {
TxQueue: 1,
RxQueue: 1,
UID: 0,
Inode: 2740,
},
},
wantErr: false,
Expand All @@ -80,6 +83,7 @@ func Test_newNetUDP(t *testing.T) {
TxQueue: 0,
RxQueue: 0,
UID: 981,
Inode: 21040,
},
&netIPSocketLine{
Sl: 6073,
Expand All @@ -91,6 +95,7 @@ func Test_newNetUDP(t *testing.T) {
TxQueue: 0,
RxQueue: 0,
UID: 1000,
Inode: 11337031,
},
},
wantErr: false,
Expand Down

0 comments on commit 8daa8ba

Please sign in to comment.