commit df15482535093988ec1b4514e5fc9e0f12c4de49
parent 69e6191557711b6dd82b524cd204458310fea746
Author: Oliver Lowe <o@olowe.co>
Date: Fri, 17 May 2024 17:10:57 +1000
scte35: check for accidental Command fields setting
The bug was that if we forget to set the Command.Type, the
command.type is SpliceNull. Encoding SpliceNull always results in an
empty byte slice for the command, even if we have set other fields of
Commmand that we want to be encoded.
Diffstat:
2 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/scte35/command.go b/scte35/command.go
@@ -54,6 +54,17 @@ func (t CommandType) String() string {
func encodeCommand(c *Command) ([]byte, error) {
switch c.Type {
case SpliceNull, BandwidthReservation:
+ // Since SpliceNull == 0 (default) check if we've
+ // accidentally set another field.
+ if c.Schedule != nil {
+ return nil, fmt.Errorf("command %s has non-nil schedule", c.Type)
+ } else if c.Insert != nil {
+ return nil, fmt.Errorf("command %s has non-nil Insert", c.Type)
+ } else if c.TimeSignal != nil {
+ return nil, fmt.Errorf("command %s has non-nil TimeSignal", c.Type)
+ } else if c.Private != nil {
+ return nil, fmt.Errorf("command %s has non-nil Private", c.Type)
+ }
return nil, nil
case SpliceSchedule:
b, err := packEvents(c.Schedule)
diff --git a/scte35/command_test.go b/scte35/command_test.go
@@ -0,0 +1,28 @@
+package scte35
+
+import (
+ "bytes"
+ "testing"
+)
+
+func TestEncodeInsert(t *testing.T) {
+ out := &Insert{
+ ID: 12345,
+ EventIDCompliance: true,
+ OutOfNetwork: true,
+ Immediate: true,
+ Duration: &BreakDuration{true, uint64(90000 * 2)},
+ }
+ in := &Insert{
+ ID: out.ID,
+ EventIDCompliance: true,
+ }
+
+ bout := encodeInsert(out)
+ bin := encodeInsert(in)
+ if bytes.Equal(bout, bin) {
+ t.Errorf("different inserts are equal when encoded")
+ t.Log(bout)
+ t.Log(bin)
+ }
+}