safely stage video frames
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package renderer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/christerso/vulkan-go/vk"
|
||||
)
|
||||
|
||||
func TestValidateFramePayload(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payloadLen int
|
||||
width uint32
|
||||
height uint32
|
||||
stride uint32
|
||||
wantSize vk.DeviceSize
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid frame",
|
||||
payloadLen: 5120 * 1080,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
stride: 5120,
|
||||
wantSize: vk.DeviceSize(5120 * 1080),
|
||||
},
|
||||
{
|
||||
name: "payload may be larger than frame",
|
||||
payloadLen: 5120*1080 + 128,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
stride: 5120,
|
||||
wantSize: vk.DeviceSize(5120 * 1080),
|
||||
},
|
||||
{
|
||||
name: "zero width",
|
||||
payloadLen: 100,
|
||||
height: 10,
|
||||
stride: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "zero height",
|
||||
payloadLen: 100,
|
||||
width: 10,
|
||||
stride: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "zero stride",
|
||||
payloadLen: 100,
|
||||
width: 10,
|
||||
height: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "payload is too small",
|
||||
payloadLen: 99,
|
||||
width: 10,
|
||||
height: 10,
|
||||
stride: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "negative payload length",
|
||||
payloadLen: -1,
|
||||
width: 10,
|
||||
height: 10,
|
||||
stride: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := validateFramePayload(
|
||||
tt.payloadLen,
|
||||
tt.width,
|
||||
tt.height,
|
||||
tt.stride,
|
||||
)
|
||||
|
||||
if tt.wantErr {
|
||||
if !errors.Is(err, ErrInvalidVideoFrame) {
|
||||
t.Fatalf("validateFramePayload() error = %v, want %v", err, ErrInvalidVideoFrame)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("validateFramePayload() error = %v, want nil", err)
|
||||
}
|
||||
if got != tt.wantSize {
|
||||
t.Errorf("validateFramePayload() size = %d, want %d", got, tt.wantSize)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateFramePayloadUses64BitSize(t *testing.T) {
|
||||
if strconv.IntSize < 64 {
|
||||
t.Skip("test requires a 64-bit int")
|
||||
}
|
||||
|
||||
stride := ^uint32(0)
|
||||
height := uint32(2)
|
||||
required := uint64(stride) * uint64(height)
|
||||
|
||||
got, err := validateFramePayload(
|
||||
int(required),
|
||||
1,
|
||||
height,
|
||||
stride,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("validateFramePayload() error = %v, want nil", err)
|
||||
}
|
||||
if uint64(got) != required {
|
||||
t.Fatalf("validateFramePayload() size = %d, want %d", got, required)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user