github.com/pion/webrtc/v3@v3.2.24/pkg/media/rtpdump/rtpdump_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package rtpdump 5 6 import ( 7 "errors" 8 "net" 9 "reflect" 10 "testing" 11 "time" 12 ) 13 14 func TestHeaderRoundTrip(t *testing.T) { 15 for _, test := range []struct { 16 Header Header 17 }{ 18 { 19 Header: Header{ 20 Start: time.Unix(0, 0).UTC(), 21 Source: net.IPv4(0, 0, 0, 0), 22 Port: 0, 23 }, 24 }, 25 { 26 Header: Header{ 27 Start: time.Date(2019, 3, 25, 1, 1, 1, 0, time.UTC), 28 Source: net.IPv4(1, 2, 3, 4), 29 Port: 8080, 30 }, 31 }, 32 } { 33 d, err := test.Header.Marshal() 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 var hdr Header 39 if err := hdr.Unmarshal(d); err != nil { 40 t.Fatal(err) 41 } 42 43 if got, want := hdr, test.Header; !reflect.DeepEqual(got, want) { 44 t.Fatalf("Unmarshal(%v.Marshal()) = %v, want identical", got, want) 45 } 46 } 47 } 48 49 func TestMarshalHeader(t *testing.T) { 50 for _, test := range []struct { 51 Name string 52 Header Header 53 Want []byte 54 WantErr error 55 }{ 56 { 57 Name: "nil source", 58 Header: Header{ 59 Start: time.Unix(0, 0).UTC(), 60 Source: nil, 61 Port: 0, 62 }, 63 Want: []byte{ 64 0x00, 0x00, 0x00, 0x00, 65 0x00, 0x00, 0x00, 0x00, 66 0x00, 0x00, 0x00, 0x00, 67 0x00, 0x00, 0x00, 0x00, 68 }, 69 }, 70 } { 71 data, err := test.Header.Marshal() 72 if got, want := err, test.WantErr; !errors.Is(got, want) { 73 t.Fatalf("Marshal(%q) err=%v, want %v", test.Name, got, want) 74 } 75 76 if got, want := data, test.Want; !reflect.DeepEqual(got, want) { 77 t.Fatalf("Marshal(%q) = %v, want %v", test.Name, got, want) 78 } 79 } 80 } 81 82 func TestPacketRoundTrip(t *testing.T) { 83 for _, test := range []struct { 84 Packet Packet 85 }{ 86 { 87 Packet: Packet{ 88 Offset: 0, 89 IsRTCP: false, 90 Payload: []byte{0}, 91 }, 92 }, 93 { 94 Packet: Packet{ 95 Offset: 0, 96 IsRTCP: true, 97 Payload: []byte{0}, 98 }, 99 }, 100 { 101 Packet: Packet{ 102 Offset: 123 * time.Millisecond, 103 IsRTCP: false, 104 Payload: []byte{1, 2, 3, 4}, 105 }, 106 }, 107 } { 108 d, err := test.Packet.Marshal() 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 var pkt Packet 114 if err := pkt.Unmarshal(d); err != nil { 115 t.Fatal(err) 116 } 117 118 if got, want := pkt, test.Packet; !reflect.DeepEqual(got, want) { 119 t.Fatalf("Unmarshal(%v.Marshal()) = %v, want identical", got, want) 120 } 121 } 122 }