-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer_test.go
More file actions
145 lines (127 loc) · 3.27 KB
/
Copy pathrenderer_test.go
File metadata and controls
145 lines (127 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package uss
import (
"bytes"
"strings"
"testing"
)
func TestRenderHuman(t *testing.T) {
entries := []Entry{
{
Netid: "tcp", State: "LISTEN", RecvQ: 0, SendQ: 128,
Local: "0.0.0.0:22", Peer: "0.0.0.0:*",
LocalAddress: "0.0.0.0", LocalPort: "22",
PeerAddress: "0.0.0.0", PeerPort: "*",
ProcessRaw: "users:((\"sshd\",pid=1234,fd=3))",
},
{
Netid: "u_str", State: "LISTEN", RecvQ: 0, SendQ: 4096,
UnixPath: "/run/test.sock", UnixID: "12345", UnixPeer: "*",
ProcessRaw: "users:((\"test\",pid=5678,fd=7))",
},
}
var buf bytes.Buffer
err := RenderHuman(&buf, entries, false)
if err != nil {
t.Fatalf("RenderHuman failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "tcp") {
t.Errorf("Output should contain 'tcp'")
}
if !strings.Contains(output, "u_str") {
t.Errorf("Output should contain 'u_str'")
}
if !strings.Contains(output, "0.0.0.0:22") {
t.Errorf("Output should contain '0.0.0.0:22'")
}
if !strings.Contains(output, "/run/test.sock") {
t.Errorf("Output should contain '/run/test.sock'")
}
}
func TestRenderCSV(t *testing.T) {
uid := 1000
inode := uint64(12345)
entries := []Entry{
{
Netid: "tcp", State: "LISTEN", RecvQ: 0, SendQ: 128,
LocalAddress: "0.0.0.0", LocalPort: "22",
PeerAddress: "0.0.0.0", PeerPort: "*",
UID: &uid,
Inode: &inode,
Users: []User{{Name: "sshd", PID: 1234, FD: 3}},
},
}
var buf bytes.Buffer
err := RenderCSV(&buf, entries, false)
if err != nil {
t.Fatalf("RenderCSV failed: %v", err)
}
output := buf.String()
lines := strings.Split(strings.TrimSpace(output), "\n")
if len(lines) != 2 {
t.Fatalf("Expected 2 lines (header + 1 entry), got %d", len(lines))
}
// Check header
if !strings.Contains(lines[0], "Netid") {
t.Errorf("Header should contain 'Netid'")
}
// Check data
if !strings.Contains(lines[1], "tcp") {
t.Errorf("Data should contain 'tcp'")
}
if !strings.Contains(lines[1], "1000") {
t.Errorf("Data should contain UID '1000'")
}
}
func TestRenderJSON(t *testing.T) {
entries := []Entry{
{
Netid: "tcp", State: "LISTEN", RecvQ: 0, SendQ: 128,
LocalAddress: "0.0.0.0", LocalPort: "22",
},
}
var buf bytes.Buffer
err := RenderJSON(&buf, entries, false, false)
if err != nil {
t.Fatalf("RenderJSON failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "\"netid\":\"tcp\"") {
t.Errorf("JSON should contain '\"netid\":\"tcp\"'")
}
}
func TestRenderJSON_Pretty(t *testing.T) {
entries := []Entry{
{
Netid: "tcp", State: "LISTEN", RecvQ: 0, SendQ: 128,
LocalAddress: "0.0.0.0", LocalPort: "22",
},
}
var buf bytes.Buffer
err := RenderJSON(&buf, entries, true, false)
if err != nil {
t.Fatalf("RenderJSON (pretty) failed: %v", err)
}
output := buf.String()
// Pretty JSON should have indentation
if !strings.Contains(output, "\n ") {
t.Errorf("Pretty JSON should have indentation")
}
}
func TestRenderYAML(t *testing.T) {
entries := []Entry{
{
Netid: "tcp", State: "LISTEN", RecvQ: 0, SendQ: 128,
LocalAddress: "0.0.0.0", LocalPort: "22",
},
}
var buf bytes.Buffer
err := RenderYAML(&buf, entries, false)
if err != nil {
t.Fatalf("RenderYAML failed: %v", err)
}
output := buf.String()
if !strings.Contains(output, "netid: tcp") {
t.Errorf("YAML should contain 'netid: tcp'")
}
}