github.com/pion/webrtc/v3@v3.2.24/iceserver_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !js
     5  // +build !js
     6  
     7  package webrtc
     8  
     9  import (
    10  	"encoding/json"
    11  	"testing"
    12  
    13  	"github.com/pion/stun"
    14  	"github.com/pion/webrtc/v3/pkg/rtcerr"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestICEServer_validate(t *testing.T) {
    19  	t.Run("Success", func(t *testing.T) {
    20  		testCases := []struct {
    21  			iceServer        ICEServer
    22  			expectedValidate bool
    23  		}{
    24  			{ICEServer{
    25  				URLs:           []string{"turn:192.158.29.39?transport=udp"},
    26  				Username:       "unittest",
    27  				Credential:     "placeholder",
    28  				CredentialType: ICECredentialTypePassword,
    29  			}, true},
    30  			{ICEServer{
    31  				URLs:           []string{"turn:[2001:db8:1234:5678::1]?transport=udp"},
    32  				Username:       "unittest",
    33  				Credential:     "placeholder",
    34  				CredentialType: ICECredentialTypePassword,
    35  			}, true},
    36  			{ICEServer{
    37  				URLs:     []string{"turn:192.158.29.39?transport=udp"},
    38  				Username: "unittest",
    39  				Credential: OAuthCredential{
    40  					MACKey:      "WmtzanB3ZW9peFhtdm42NzUzNG0=",
    41  					AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA==",
    42  				},
    43  				CredentialType: ICECredentialTypeOauth,
    44  			}, true},
    45  		}
    46  
    47  		for i, testCase := range testCases {
    48  			var iceServer ICEServer
    49  			jsonobj, err := json.Marshal(testCase.iceServer)
    50  			assert.NoError(t, err)
    51  			err = json.Unmarshal(jsonobj, &iceServer)
    52  			assert.NoError(t, err)
    53  			assert.Equal(t, iceServer, testCase.iceServer)
    54  			_, err = testCase.iceServer.urls()
    55  			assert.Nil(t, err, "testCase: %d %v", i, testCase)
    56  		}
    57  	})
    58  	t.Run("Failure", func(t *testing.T) {
    59  		testCases := []struct {
    60  			iceServer   ICEServer
    61  			expectedErr error
    62  		}{
    63  			{ICEServer{
    64  				URLs: []string{"turn:192.158.29.39?transport=udp"},
    65  			}, &rtcerr.InvalidAccessError{Err: ErrNoTurnCredentials}},
    66  			{ICEServer{
    67  				URLs:           []string{"turn:192.158.29.39?transport=udp"},
    68  				Username:       "unittest",
    69  				Credential:     false,
    70  				CredentialType: ICECredentialTypePassword,
    71  			}, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}},
    72  			{ICEServer{
    73  				URLs:           []string{"turn:192.158.29.39?transport=udp"},
    74  				Username:       "unittest",
    75  				Credential:     false,
    76  				CredentialType: ICECredentialTypeOauth,
    77  			}, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}},
    78  			{ICEServer{
    79  				URLs:           []string{"turn:192.158.29.39?transport=udp"},
    80  				Username:       "unittest",
    81  				Credential:     false,
    82  				CredentialType: Unknown,
    83  			}, &rtcerr.InvalidAccessError{Err: ErrTurnCredentials}},
    84  			{ICEServer{
    85  				URLs:           []string{"stun:google.de?transport=udp"},
    86  				Username:       "unittest",
    87  				Credential:     false,
    88  				CredentialType: ICECredentialTypeOauth,
    89  			}, &rtcerr.InvalidAccessError{Err: stun.ErrSTUNQuery}},
    90  		}
    91  
    92  		for i, testCase := range testCases {
    93  			_, err := testCase.iceServer.urls()
    94  			assert.EqualError(t,
    95  				err,
    96  				testCase.expectedErr.Error(),
    97  				"testCase: %d %v", i, testCase,
    98  			)
    99  		}
   100  	})
   101  	t.Run("JsonFailure", func(t *testing.T) {
   102  		testCases := [][]byte{
   103  			[]byte(`{"urls":"NOTAURL","username":"unittest","credential":"placeholder","credentialType":"password"}`),
   104  			[]byte(`{"urls":["turn:[2001:db8:1234:5678::1]?transport=udp"],"username":"unittest","credential":"placeholder","credentialType":"invalid"}`),
   105  			[]byte(`{"urls":["turn:[2001:db8:1234:5678::1]?transport=udp"],"username":6,"credential":"placeholder","credentialType":"password"}`),
   106  			[]byte(`{"urls":["turn:192.158.29.39?transport=udp"],"username":"unittest","credential":{"Bad Object": true},"credentialType":"oauth"}`),
   107  			[]byte(`{"urls":["turn:192.158.29.39?transport=udp"],"username":"unittest","credential":{"MACKey":"WmtzanB3ZW9peFhtdm42NzUzNG0=","AccessToken":null,"credentialType":"oauth"}`),
   108  			[]byte(`{"urls":["turn:192.158.29.39?transport=udp"],"username":"unittest","credential":{"MACKey":"WmtzanB3ZW9peFhtdm42NzUzNG0=","AccessToken":null,"credentialType":"password"}`),
   109  			[]byte(`{"urls":["turn:192.158.29.39?transport=udp"],"username":"unittest","credential":{"MACKey":1337,"AccessToken":"AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA=="},"credentialType":"oauth"}`),
   110  		}
   111  		for i, testCase := range testCases {
   112  			var tc ICEServer
   113  			err := json.Unmarshal(testCase, &tc)
   114  			assert.Error(t, err, "testCase: %d %v", i, string(testCase))
   115  		}
   116  	})
   117  }
   118  
   119  func TestICEServerZeroValue(t *testing.T) {
   120  	server := ICEServer{
   121  		URLs:       []string{"turn:galene.org:1195"},
   122  		Username:   "galene",
   123  		Credential: "secret",
   124  	}
   125  	assert.Equal(t, server.CredentialType, ICECredentialTypePassword)
   126  }