peerconnection_js_test.go 3.2 KB

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