change parse to models.ListenPorts from string

This commit is contained in:
MaineK00n
2020-10-14 14:53:30 +09:00
parent ea820a248e
commit d9337eeb38
4 changed files with 57 additions and 4 deletions

View File

@@ -809,3 +809,11 @@ func (l *base) parseLsOf(stdout string) map[string]string {
}
return portPid
}
func (l *base) parseListenPorts(port string) models.ListenPorts {
sep := strings.LastIndex(port, ":")
if sep == -1 {
return models.ListenPorts{}
}
return models.ListenPorts{Address: port[:sep], Port: port[sep+1:]}
}

View File

@@ -12,6 +12,7 @@ import (
_ "github.com/aquasecurity/fanal/analyzer/library/poetry"
_ "github.com/aquasecurity/fanal/analyzer/library/yarn"
"github.com/future-architect/vuls/config"
"github.com/future-architect/vuls/models"
)
func TestParseDockerPs(t *testing.T) {
@@ -275,3 +276,47 @@ docker-pr 9135 root 4u IPv6 297133 0t0 TCP *:6379 (LISTEN)
})
}
}
func Test_base_parseListenPorts(t *testing.T) {
tests := []struct {
name string
args string
expect models.ListenPorts
}{{
name: "empty",
args: "",
expect: models.ListenPorts{
Address: "",
Port: "",
},
}, {
name: "normal",
args: "127.0.0.1:22",
expect: models.ListenPorts{
Address: "127.0.0.1",
Port: "22",
},
}, {
name: "asterisk",
args: "*:22",
expect: models.ListenPorts{
Address: "*",
Port: "22",
},
}, {
name: "ipv6_loopback",
args: "[::1]:22",
expect: models.ListenPorts{
Address: "[::1]",
Port: "22",
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := &base{}
if listenPort := l.parseListenPorts(tt.args); !reflect.DeepEqual(listenPort, tt.expect) {
t.Errorf("base.parseListenPorts() = %v, want %v", listenPort, tt.expect)
}
})
}
}

View File

@@ -1294,14 +1294,14 @@ func (o *debian) dpkgPs() error {
pidLoadedFiles[pid] = append(pidLoadedFiles[pid], ss...)
}
pidListenPorts := map[string][]string{}
pidListenPorts := map[string][]models.ListenPorts{}
stdout, err = o.lsOfListen()
if err != nil {
return xerrors.Errorf("Failed to ls of: %w", err)
}
portPid := o.parseLsOf(stdout)
for port, pid := range portPid {
pidListenPorts[pid] = append(pidListenPorts[pid], port)
pidListenPorts[pid] = append(pidListenPorts[pid], o.parseListenPorts(port))
}
for pid, loadedFiles := range pidLoadedFiles {

View File

@@ -491,14 +491,14 @@ func (o *redhatBase) yumPs() error {
pidLoadedFiles[pid] = append(pidLoadedFiles[pid], ss...)
}
pidListenPorts := map[string][]string{}
pidListenPorts := map[string][]models.ListenPorts{}
stdout, err = o.lsOfListen()
if err != nil {
return xerrors.Errorf("Failed to ls of: %w", err)
}
portPid := o.parseLsOf(stdout)
for port, pid := range portPid {
pidListenPorts[pid] = append(pidListenPorts[pid], port)
pidListenPorts[pid] = append(pidListenPorts[pid], o.parseListenPorts(port))
}
for pid, loadedFiles := range pidLoadedFiles {