Skip to content

Commit

Permalink
Remove panics and fix lint
Browse files Browse the repository at this point in the history
Don't allow panics in library code, pion-WebRTC should never
cause a user's application to exit

Resolves #125
  • Loading branch information
Sean-Der committed Sep 12, 2018
1 parent 95ab1c9 commit 8f1e32a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/lint-disallowed-functions-in-library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -e
# Disallow usages of functions that cause the program to exit in the library code
SCRIPT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
EXCLUDE_DIRECTORIES="--exclude-dir=examples --exclude-dir=.git --exclude-dir=.github "
DISALLOWED_FUNCTIONS=('os.Exit(', 'panic(', 'Fatal(', 'Fatalf(', 'Fatalln(')
DISALLOWED_FUNCTIONS=('os.Exit(' 'panic(' 'Fatal(' 'Fatalf(' 'Fatalln(')


for disallowedFunction in "${DISALLOWED_FUNCTIONS[@]}"
Expand Down
12 changes: 6 additions & 6 deletions pkg/rtcp/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ func TestHeaderUnmarshal(t *testing.T) {

var got Header
if err := got.Unmarshal(data); err != nil {
t.Fatalf("Unmarshal: %v", err)
t.Errorf("Unmarshal: %v", err)
}

if !reflect.DeepEqual(got, want) {
t.Fatalf("Unmarshal: got %#v, want %#v", got, want)
t.Errorf("Unmarshal: got %#v, want %#v", got, want)
}
}

func TestHeaderUnmarshalNil(t *testing.T) {
var header Header
err := header.Unmarshal(nil)
if got, want := err, errHeaderTooShort; got != want {
t.Fatalf("unmarshal nil header: err = %v, want %v", got, want)
t.Errorf("unmarshal nil header: err = %v, want %v", got, want)
}
}
func TestHeaderRoundTrip(t *testing.T) {
Expand Down Expand Up @@ -80,19 +80,19 @@ func TestHeaderRoundTrip(t *testing.T) {
} {
data, err := test.Header.Marshal()
if got, want := err, test.WantError; got != want {
t.Fatalf("Marshal %q: err = %v, want %v", test.Name, got, want)
t.Errorf("Marshal %q: err = %v, want %v", test.Name, got, want)
}
if err != nil {
continue
}

var decoded Header
if err := decoded.Unmarshal(data); err != nil {
t.Fatalf("Unmarshal %q: %v", test.Name, err)
t.Errorf("Unmarshal %q: %v", test.Name, err)
}

if got, want := decoded, test.Header; !reflect.DeepEqual(got, want) {
t.Fatalf("%q header round trip: got %#v, want %#v", test.Name, got, want)
t.Errorf("%q header round trip: got %#v, want %#v", test.Name, got, want)
}
}
}
14 changes: 7 additions & 7 deletions rtccertificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

// RTCCertificate represents a x509Cert used to authenticate WebRTC communications.
type RTCCertificate struct {
secretKey crypto.PrivateKey
x509Cert *x509.Certificate
privateKey crypto.PrivateKey
x509Cert *x509.Certificate
}

// NewRTCCertificate generates a new x509 compliant RTCCertificate to be used
Expand Down Expand Up @@ -51,23 +51,23 @@ func NewRTCCertificate(key crypto.PrivateKey, tpl x509.Certificate) (*RTCCertifi
return nil, &rtcerr.UnknownError{Err: err}
}

return &RTCCertificate{secretKey: key, x509Cert: cert}, nil
return &RTCCertificate{privateKey: key, x509Cert: cert}, nil
}

// Equals determines if two certificates are identical by comparing both the
// secretKeys and x509Certificates.
func (c RTCCertificate) Equals(o RTCCertificate) bool {
switch cSK := c.secretKey.(type) {
switch cSK := c.privateKey.(type) {
case *rsa.PrivateKey:
if oSK, ok := o.secretKey.(*rsa.PrivateKey); ok {
if oSK, ok := o.privateKey.(*rsa.PrivateKey); ok {
if cSK.N.Cmp(oSK.N) != 0 {
return false
}
return c.x509Cert.Equal(o.x509Cert)
}
return false
case *ecdsa.PrivateKey:
if oSK, ok := o.secretKey.(*ecdsa.PrivateKey); ok {
if oSK, ok := o.privateKey.(*ecdsa.PrivateKey); ok {
if cSK.X.Cmp(oSK.X) != 0 || cSK.Y.Cmp(oSK.Y) != 0 {
return false
}
Expand All @@ -90,7 +90,7 @@ func (c RTCCertificate) Expires() time.Time {
// GetFingerprints returns the list of certificate fingerprints, one of which
// is computed with the digest algorithm used in the certificate signature.
func (c RTCCertificate) GetFingerprints() {
panic("not implemented yet.")
panic("not implemented yet.") // nolint
}

// GenerateCertificate causes the creation of an X.509 certificate and
Expand Down
8 changes: 4 additions & 4 deletions rtcpeerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (pc *RTCPeerConnection) CreateAnswer(options *RTCAnswerOptions) (RTCSession

// // SetLocalDescription sets the SessionDescription of the local peer
// func (pc *RTCPeerConnection) SetLocalDescription() {
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED nolint
// }

// LocalDescription returns PendingLocalDescription if it is not null and
Expand Down Expand Up @@ -497,7 +497,7 @@ func (pc *RTCPeerConnection) RemoteDescription() *RTCSessionDescription {
}

// func (pc *RTCPeerConnection) addIceCandidate() {
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED nolint
// }

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -575,11 +575,11 @@ func (pc *RTCPeerConnection) AddTrack(track *RTCTrack) (*RTCRtpSender, error) {
}

// func (pc *RTCPeerConnection) RemoveTrack() {
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED nolint
// }

// func (pc *RTCPeerConnection) AddTransceiver() RTCRtpTransceiver {
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED
// panic("not implemented yet") // FIXME NOT-IMPLEMENTED nolint
// }

// ------------------------------------------------------------------------
Expand Down
6 changes: 0 additions & 6 deletions rtcpeerconnection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,3 @@ func TestSetRemoteDescription(t *testing.T) {
}
}
}

func ExampleNew_default() {
if _, err := New(RTCConfiguration{}); err != nil {
panic(err)
}
}

0 comments on commit 8f1e32a

Please sign in to comment.