From bd1b88226edb8b088996c522a80b96068d5570ee Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 7 May 2021 10:44:00 +0200 Subject: [PATCH] track: add negative tests --- track.go | 24 ++-- track_test.go | 298 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 309 insertions(+), 13 deletions(-) diff --git a/track.go b/track.go index a0e63faf..93012c6c 100644 --- a/track.go +++ b/track.go @@ -80,12 +80,12 @@ func (t *Track) IsH264() bool { func (t *Track) ExtractDataH264() ([]byte, []byte, error) { v, ok := t.Media.Attribute("fmtp") if !ok { - return nil, nil, fmt.Errorf("unable to find fmtp") + return nil, nil, fmt.Errorf("fmtp attribute is missing") } tmp := strings.SplitN(v, " ", 2) if len(tmp) != 2 { - return nil, nil, fmt.Errorf("unable to parse fmtp (%v)", v) + return nil, nil, fmt.Errorf("invalid fmtp attribute (%v)", v) } var sps []byte @@ -100,30 +100,30 @@ func (t *Track) ExtractDataH264() ([]byte, []byte, error) { tmp := strings.SplitN(kv, "=", 2) if len(tmp) != 2 { - return nil, nil, fmt.Errorf("unable to parse fmtp (%v)", v) + return nil, nil, fmt.Errorf("invalid fmtp attribute (%v)", v) } if tmp[0] == "sprop-parameter-sets" { tmp := strings.SplitN(tmp[1], ",", 2) if len(tmp) != 2 { - return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) + return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v) } var err error sps, err = base64.StdEncoding.DecodeString(tmp[0]) if err != nil { - return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) + return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v) } pps, err = base64.StdEncoding.DecodeString(tmp[1]) if err != nil { - return nil, nil, fmt.Errorf("unable to parse sprop-parameter-sets (%v)", v) + return nil, nil, fmt.Errorf("invalid sprop-parameter-sets (%v)", v) } } } if sps == nil || pps == nil { - return nil, nil, fmt.Errorf("unable to find SPS or PPS (%v)", v) + return nil, nil, fmt.Errorf("sprop-parameter-sets is missing (%v)", v) } return sps, pps, nil @@ -189,12 +189,12 @@ func (t *Track) IsAAC() bool { func (t *Track) ExtractDataAAC() ([]byte, error) { v, ok := t.Media.Attribute("fmtp") if !ok { - return nil, fmt.Errorf("unable to find fmtp") + return nil, fmt.Errorf("fmtp attribute is missing") } tmp := strings.SplitN(v, " ", 2) if len(tmp) != 2 { - return nil, fmt.Errorf("unable to parse fmtp (%v)", v) + return nil, fmt.Errorf("invalid fmtp (%v)", v) } var config []byte @@ -208,21 +208,21 @@ func (t *Track) ExtractDataAAC() ([]byte, error) { tmp := strings.SplitN(kv, "=", 2) if len(tmp) != 2 { - return nil, fmt.Errorf("unable to parse fmtp (%v)", v) + return nil, fmt.Errorf("invalid fmtp (%v)", v) } if tmp[0] == "config" { var err error config, err = hex.DecodeString(tmp[1]) if err != nil { - return nil, fmt.Errorf("unable to parse config (%v)", v) + return nil, fmt.Errorf("invalid config (%v)", v) } break } } if config == nil { - return nil, fmt.Errorf("unable to find config (%v)", v) + return nil, fmt.Errorf("config is missing (%v)", v) } return config, nil diff --git a/track_test.go b/track_test.go index f185abd1..e406ae20 100644 --- a/track_test.go +++ b/track_test.go @@ -281,7 +281,7 @@ func TestTrackH264Extract(t *testing.T) { }, { Key: "fmtp", - Value: "96 96 packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,aOvjyyLA;", + Value: "96 packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,aOvjyyLA;", }, }, }, @@ -306,6 +306,177 @@ func TestTrackH264Extract(t *testing.T) { } } +func TestTrackH264ExtractError(t *testing.T) { + for _, ca := range []struct { + name string + track *Track + err string + }{ + { + "missing fmtp", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + }, + }, + }, + "fmtp attribute is missing", + }, + { + "invalid fmtp", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96", + }, + }, + }, + }, + "invalid fmtp attribute (96)", + }, + { + "fmtp without key", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96 packetization-mode", + }, + }, + }, + }, + "invalid fmtp attribute (96 packetization-mode)", + }, + { + "missing sprop-parameter-set", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96 packetization-mode=1", + }, + }, + }, + }, + "sprop-parameter-sets is missing (96 packetization-mode=1)", + }, + { + "invalid sprop-parameter-set 1", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96 sprop-parameter-sets=aaaaaa", + }, + }, + }, + }, + "invalid sprop-parameter-sets (96 sprop-parameter-sets=aaaaaa)", + }, + { + "invalid sprop-parameter-set 2", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96 sprop-parameter-sets=aaaaaa,bbb", + }, + }, + }, + }, + "invalid sprop-parameter-sets (96 sprop-parameter-sets=aaaaaa,bbb)", + }, + { + "invalid sprop-parameter-set 3", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "video", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 H264/90000", + }, + { + Key: "fmtp", + Value: "96 sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,bbb", + }, + }, + }, + }, + "invalid sprop-parameter-sets (96 sprop-parameter-sets=Z2QAH6zZQFAFuwFsgAAAAwCAAAAeB4wYyw==,bbb)", + }, + } { + t.Run(ca.name, func(t *testing.T) { + _, _, err := ca.track.ExtractDataH264() + require.Equal(t, ca.err, err.Error()) + }) + } +} + func TestTrackAACNew(t *testing.T) { tr, err := NewTrackAAC(96, []byte{17, 144}) require.NoError(t, err) @@ -390,3 +561,128 @@ func TestTrackAACExtract(t *testing.T) { }) } } + +func TestTrackAACExtractError(t *testing.T) { + for _, ca := range []struct { + name string + track *Track + err string + }{ + { + "missing fmtp", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 MPEG4-GENERIC/48000/2", + }, + }, + }, + }, + "fmtp attribute is missing", + }, + { + "invalid fmtp", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 MPEG4-GENERIC/48000/2", + }, + { + Key: "fmtp", + Value: "96", + }, + }, + }, + }, + "invalid fmtp (96)", + }, + { + "fmtp without key", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 MPEG4-GENERIC/48000/2", + }, + { + Key: "fmtp", + Value: "96 profile-level-id", + }, + }, + }, + }, + "invalid fmtp (96 profile-level-id)", + }, + { + "missing config", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 MPEG4-GENERIC/48000/2", + }, + { + Key: "fmtp", + Value: "96 profile-level-id=1", + }, + }, + }, + }, + "config is missing (96 profile-level-id=1)", + }, + { + "invalid config", + &Track{ + Media: &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"96"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "96 MPEG4-GENERIC/48000/2", + }, + { + Key: "fmtp", + Value: "96 profile-level-id=1; config=zz", + }, + }, + }, + }, + "invalid config (96 profile-level-id=1; config=zz)", + }, + } { + t.Run(ca.name, func(t *testing.T) { + _, err := ca.track.ExtractDataAAC() + require.Equal(t, ca.err, err.Error()) + }) + } +}