-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87e1415
commit 287d18a
Showing
13 changed files
with
823 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/v-byte-cpu/sx/pkg/scan" | ||
) | ||
|
||
func TestParsePortRangeError(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
portsRange string | ||
}{ | ||
{ | ||
name: "EmptyPortRange", | ||
portsRange: "", | ||
}, | ||
{ | ||
name: "EmptyStartPort", | ||
portsRange: "-22", | ||
}, | ||
{ | ||
name: "EmptyEndPort", | ||
portsRange: "22-", | ||
}, | ||
{ | ||
name: "InvalidLargePort", | ||
portsRange: "65536", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := parsePortRange(tt.portsRange) | ||
require.Error(t, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestParsePortRange(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
portsRange string | ||
expected *scan.PortRange | ||
}{ | ||
{ | ||
name: "OnePort", | ||
portsRange: "22", | ||
expected: &scan.PortRange{ | ||
StartPort: 22, | ||
EndPort: 22, | ||
}, | ||
}, | ||
{ | ||
name: "TwoPorts", | ||
portsRange: "22-23", | ||
expected: &scan.PortRange{ | ||
StartPort: 22, | ||
EndPort: 23, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ports, err := parsePortRange(tt.portsRange) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expected, ports) | ||
}) | ||
} | ||
} | ||
|
||
func TestParsePortRanges(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
portsRange string | ||
expected []*scan.PortRange | ||
}{ | ||
{ | ||
name: "OneRangeOnePort", | ||
portsRange: "22", | ||
expected: []*scan.PortRange{ | ||
{ | ||
StartPort: 22, | ||
EndPort: 22, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "OneRangeTwoPorts", | ||
portsRange: "22-23", | ||
expected: []*scan.PortRange{ | ||
{ | ||
StartPort: 22, | ||
EndPort: 23, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TwoRangesOnePort", | ||
portsRange: "22,23", | ||
expected: []*scan.PortRange{ | ||
{ | ||
StartPort: 22, | ||
EndPort: 22, | ||
}, | ||
{ | ||
StartPort: 23, | ||
EndPort: 23, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TwoRangesTwoPorts", | ||
portsRange: "22-23,26-27", | ||
expected: []*scan.PortRange{ | ||
{ | ||
StartPort: 22, | ||
EndPort: 23, | ||
}, | ||
{ | ||
StartPort: 26, | ||
EndPort: 27, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ports, err := parsePortRanges(tt.portsRange) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.expected, ports) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.