Add unit tests for stream validation
This commit is contained in:
committed by
Brendan LE GLAUNEC
parent
055dc69158
commit
4109a4405d
+164
@@ -341,6 +341,170 @@ func TestAttackRoute(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateStreams(t *testing.T) {
|
||||
validStream1 := Stream{
|
||||
Device: "fakeDevice",
|
||||
Address: "fakeAddress",
|
||||
Port: 1337,
|
||||
Available: true,
|
||||
}
|
||||
|
||||
validStream2 := Stream{
|
||||
Device: "fakeDevice",
|
||||
Address: "differentFakeAddress",
|
||||
Port: 1337,
|
||||
Available: true,
|
||||
}
|
||||
|
||||
unavailableStream := Stream{
|
||||
Device: "fakeDevice",
|
||||
Available: false,
|
||||
}
|
||||
|
||||
fakeTargets := []Stream{validStream1, validStream2}
|
||||
unavailableTargets := []Stream{unavailableStream}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
|
||||
targets []Stream
|
||||
timeout time.Duration
|
||||
log bool
|
||||
|
||||
status int
|
||||
|
||||
performErr error
|
||||
getInfoErr error
|
||||
|
||||
expectedStreams []Stream
|
||||
expectedErrMsg string
|
||||
}{
|
||||
// Route found
|
||||
{
|
||||
desc: "route found",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
status: 403,
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// Route found
|
||||
{
|
||||
desc: "route found",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
status: 401,
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// Camera accessed
|
||||
{
|
||||
desc: "camera accessed",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
status: 200,
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// Unavailable stream
|
||||
{
|
||||
desc: "unavailable stream",
|
||||
|
||||
targets: unavailableTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
status: 400,
|
||||
|
||||
expectedStreams: unavailableTargets,
|
||||
},
|
||||
// curl perform fails
|
||||
{
|
||||
desc: "curl perform fails",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
performErr: errors.New("dummy error"),
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// curl getinfo fails
|
||||
{
|
||||
desc: "curl getinfo fails",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
|
||||
getInfoErr: errors.New("dummy error"),
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// Logs disabled
|
||||
{
|
||||
desc: "logs disabled",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
log: false,
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
// Logs enabled
|
||||
{
|
||||
desc: "logs enabled",
|
||||
|
||||
targets: fakeTargets,
|
||||
timeout: 1 * time.Millisecond,
|
||||
log: true,
|
||||
|
||||
expectedStreams: fakeTargets,
|
||||
},
|
||||
}
|
||||
for i, tC := range testCases {
|
||||
t.Run(tC.desc, func(t *testing.T) {
|
||||
curlerMock := &CurlerMock{}
|
||||
|
||||
curlerMock.On("Setopt", mock.Anything, mock.Anything).Return(nil)
|
||||
curlerMock.On("Perform").Return(tC.performErr)
|
||||
if tC.performErr == nil {
|
||||
curlerMock.On("Getinfo", mock.Anything).Return(tC.status, tC.getInfoErr)
|
||||
}
|
||||
|
||||
results, err := ValidateStreams(curlerMock, tC.targets, tC.timeout, tC.log)
|
||||
|
||||
if len(tC.expectedErrMsg) > 0 {
|
||||
if err == nil {
|
||||
fmt.Printf("unexpected success in ValidateStream test, iteration %d. expected error: %s\n", i, tC.expectedErrMsg)
|
||||
os.Exit(1)
|
||||
}
|
||||
assert.Contains(t, err.Error(), tC.expectedErrMsg, "wrong error message")
|
||||
} else {
|
||||
if err != nil {
|
||||
fmt.Printf("unexpected error in ValidateStream test, iteration %d: %v\n", i, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
for _, stream := range tC.expectedStreams {
|
||||
foundStream := false
|
||||
for _, result := range results {
|
||||
if result.Address == stream.Address && result.Device == stream.Device && result.Port == stream.Port {
|
||||
foundStream = true
|
||||
}
|
||||
}
|
||||
assert.Equal(t, true, foundStream, "wrong streams parsed")
|
||||
}
|
||||
}
|
||||
assert.Equal(t, len(tC.expectedStreams), len(results), "wrong streams parsed")
|
||||
curlerMock.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDotWrite(t *testing.T) {
|
||||
assert.Equal(t, true, doNotWrite(nil, nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user