streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

sdp_test.go (3845B)


      1 package sdp
      2 
      3 import (
      4 	"net/mail"
      5 	"net/netip"
      6 	"net/url"
      7 	"os"
      8 	"reflect"
      9 	"testing"
     10 )
     11 
     12 func TestReadSession(t *testing.T) {
     13 	var cases = []struct {
     14 		name string
     15 		want Session
     16 		err  bool
     17 	}{
     18 		{
     19 			name: "good.sdp",
     20 			want: Session{
     21 				Name:   "Call to John Smith",
     22 				Origin: Origin{"jdoe", 3724394400, 3724394405, netip.MustParseAddr("198.51.100.1")},
     23 				Info:   "SDP Offer #1",
     24 				URI: &url.URL{
     25 					Scheme: "http",
     26 					Host:   "www.jdoe.example.com",
     27 					Path:   "/home.html",
     28 				},
     29 				Email: &mail.Address{
     30 					Name:    "Jane Doe",
     31 					Address: "jane@jdoe.example.com",
     32 				},
     33 				Phone: "+16175556011",
     34 				Connection: &ConnInfo{
     35 					Address: netip.MustParseAddr("198.51.100.1"),
     36 				},
     37 				Media: []Media{
     38 					Media{
     39 						Type:      MediaTypeAudio,
     40 						Port:      49170,
     41 						Transport: ProtoRTP,
     42 						Format:    []string{"0"},
     43 					},
     44 					Media{
     45 						Type:      MediaTypeAudio,
     46 						Port:      49180,
     47 						Transport: ProtoRTP,
     48 						Format:    []string{"0"},
     49 					},
     50 					Media{
     51 						Type:       MediaTypeVideo,
     52 						Port:       51372,
     53 						Transport:  ProtoRTP,
     54 						Format:     []string{"99"},
     55 						Connection: &ConnInfo{netip.MustParseAddr("2001:db8::2"), 0, 0},
     56 						Attributes: []string{"rtpmap:99", "h263-1998/90000"},
     57 					},
     58 				},
     59 			},
     60 		},
     61 		{
     62 			name: "some_optional.sdp",
     63 			want: Session{
     64 				Origin: Origin{"jdoe", 3724394400, 3724394405, netip.MustParseAddr("198.51.100.1")},
     65 				Name:   "Call to John Smith",
     66 				Email: &mail.Address{
     67 					Name:    "Jane Doe",
     68 					Address: "jane@jdoe.example.com",
     69 				},
     70 			},
     71 		},
     72 		{
     73 			name: "missing_origin.sdp",
     74 			want: Session{},
     75 			err:  true,
     76 		},
     77 		{
     78 			name: "out_of_order.sdp",
     79 			want: Session{},
     80 			err:  true,
     81 		},
     82 	}
     83 	for _, tt := range cases {
     84 		t.Run(tt.name, func(t *testing.T) {
     85 			f, err := os.Open("testdata/" + tt.name)
     86 			if err != nil {
     87 				t.Fatal(err)
     88 			}
     89 			defer f.Close()
     90 			session, err := ReadSession(f)
     91 			if err == nil && tt.err {
     92 				t.Fatal("unexpected nil error")
     93 			} else if err != nil && tt.err {
     94 				return
     95 			} else if err != nil && !tt.err {
     96 				t.Fatalf("read session: %v", err)
     97 			}
     98 			if !reflect.DeepEqual(*session, tt.want) {
     99 				t.Errorf("got %+v\nwant %+v\n", *session, tt.want)
    100 			}
    101 		})
    102 	}
    103 }
    104 
    105 func TestBandwidth(t *testing.T) {
    106 	var cases = []struct {
    107 		name    string
    108 		s       string
    109 		wantErr bool
    110 	}{
    111 		{"conference total", "CT:2048", false},
    112 		{"app specific", "AS:87654321", false},
    113 		{"custom", "69something:12345", false},
    114 		{"missing modifier", ":12345", true},
    115 		{"missing separator", "CT2048", true},
    116 	}
    117 	for _, tt := range cases {
    118 		t.Run(tt.name, func(t *testing.T) {
    119 			_, err := parseBandwidth(tt.s)
    120 			if err != nil && tt.wantErr {
    121 				// no worries, we got what we expected
    122 			} else if err != nil && !tt.wantErr {
    123 				t.Errorf("parseBandwidth(%q): unexpected error %v", tt.s, err)
    124 			} else if err == nil && tt.wantErr {
    125 				t.Errorf("parseBandwidth(%q): unexpected nil error", tt.s)
    126 			}
    127 		})
    128 	}
    129 }
    130 
    131 func TestConnInfo(t *testing.T) {
    132 	var cases = []struct {
    133 		name string
    134 		line string
    135 		want ConnInfo
    136 	}{
    137 		{"ipv4", "IN IP4 192.0.2.1", ConnInfo{netip.MustParseAddr("192.0.2.1"), 0, 0}},
    138 		{"ipv4 ttl", "IN IP4 233.252.0.1/127", ConnInfo{netip.MustParseAddr("233.252.0.1"), 127, 0}},
    139 		{"ipv4 ttl count", "IN IP4 233.252.0.1/127/3", ConnInfo{netip.MustParseAddr("233.252.0.1"), 127, 3}},
    140 		{"ipv6", "IN IP6 2001:db8::1", ConnInfo{netip.MustParseAddr("2001:db8::1"), 0, 0}},
    141 		{"ipv6 count", "IN IP6 ff00::db8:0:101/3", ConnInfo{netip.MustParseAddr("ff00::db8:0:101"), 0, 3}},
    142 	}
    143 
    144 	for _, tt := range cases {
    145 		t.Run(tt.name, func(t *testing.T) {
    146 			got, err := parseConnInfo(tt.line)
    147 			if err != nil {
    148 				t.Fatalf("parse %s: %v", tt.line, err)
    149 			}
    150 			if !reflect.DeepEqual(tt.want, got) {
    151 				t.Errorf("parseConnInfo(%q) = %+v, want %+v", tt.line, got, tt.want)
    152 			}
    153 		})
    154 	}
    155 }