github.com/pion/webrtc/v3@v3.2.24/peerconnection_js_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "encoding/json" 8 "syscall/js" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestValueToICECandidate(t *testing.T) { 15 testCases := []struct { 16 jsonCandidate string 17 expect ICECandidate 18 }{ 19 { 20 // Firefox-style ICECandidateInit: 21 `{"candidate":"1966762133 1 udp 2122260222 192.168.20.128 47298 typ srflx raddr 203.0.113.1 rport 5000"}`, 22 ICECandidate{ 23 Foundation: "1966762133", 24 Priority: 2122260222, 25 Address: "192.168.20.128", 26 Protocol: ICEProtocolUDP, 27 Port: 47298, 28 Typ: ICECandidateTypeSrflx, 29 Component: 1, 30 RelatedAddress: "203.0.113.1", 31 RelatedPort: 5000, 32 }, 33 }, { 34 // Chrome/Webkit-style ICECandidate: 35 `{"foundation":"1966762134", "component":"rtp", "protocol":"udp", "priority":2122260223, "address":"192.168.20.129", "port":47299, "type":"host", "relatedAddress":null}`, 36 ICECandidate{ 37 Foundation: "1966762134", 38 Priority: 2122260223, 39 Address: "192.168.20.129", 40 Protocol: ICEProtocolUDP, 41 Port: 47299, 42 Typ: ICECandidateTypeHost, 43 Component: 1, 44 RelatedAddress: "<null>", 45 RelatedPort: 0, 46 }, 47 }, { 48 // Both are present, Chrome/Webkit-style takes precedent: 49 `{"candidate":"1966762133 1 udp 2122260222 192.168.20.128 47298 typ srflx raddr 203.0.113.1 rport 5000", "foundation":"1966762134", "component":"rtp", "protocol":"udp", "priority":2122260223, "address":"192.168.20.129", "port":47299, "type":"host", "relatedAddress":null}`, 50 ICECandidate{ 51 Foundation: "1966762134", 52 Priority: 2122260223, 53 Address: "192.168.20.129", 54 Protocol: ICEProtocolUDP, 55 Port: 47299, 56 Typ: ICECandidateTypeHost, 57 Component: 1, 58 RelatedAddress: "<null>", 59 RelatedPort: 0, 60 }, 61 }, 62 } 63 64 for i, testCase := range testCases { 65 v := map[string]interface{}{} 66 err := json.Unmarshal([]byte(testCase.jsonCandidate), &v) 67 if err != nil { 68 t.Errorf("Case %d: bad test, got error: %v", i, err) 69 } 70 val := *valueToICECandidate(js.ValueOf(v)) 71 val.statsID = "" 72 assert.Equal(t, testCase.expect, val) 73 } 74 } 75 76 func TestValueToICEServer(t *testing.T) { 77 testCases := []ICEServer{ 78 { 79 URLs: []string{"turn:192.158.29.39?transport=udp"}, 80 Username: "unittest", 81 Credential: "placeholder", 82 CredentialType: ICECredentialTypePassword, 83 }, 84 { 85 URLs: []string{"turn:[2001:db8:1234:5678::1]?transport=udp"}, 86 Username: "unittest", 87 Credential: "placeholder", 88 CredentialType: ICECredentialTypePassword, 89 }, 90 { 91 URLs: []string{"turn:192.158.29.39?transport=udp"}, 92 Username: "unittest", 93 Credential: OAuthCredential{ 94 MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=", 95 AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA==", 96 }, 97 CredentialType: ICECredentialTypeOauth, 98 }, 99 } 100 101 for _, testCase := range testCases { 102 v := iceServerToValue(testCase) 103 s := valueToICEServer(v) 104 assert.Equal(t, testCase, s) 105 } 106 }