webrtc_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "net"
  4. "strings"
  5. "testing"
  6. )
  7. func TestRemoteIPFromSDP(t *testing.T) {
  8. tests := []struct {
  9. sdp string
  10. expected net.IP
  11. }{
  12. // https://tools.ietf.org/html/rfc4566#section-5
  13. {`v=0
  14. o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
  15. s=SDP Seminar
  16. i=A Seminar on the session description protocol
  17. u=http://www.example.com/seminars/sdp.pdf
  18. e=j.doe@example.com (Jane Doe)
  19. c=IN IP4 224.2.17.12/127
  20. t=2873397496 2873404696
  21. a=recvonly
  22. m=audio 49170 RTP/AVP 0
  23. m=video 51372 RTP/AVP 99
  24. a=rtpmap:99 h263-1998/90000
  25. `, net.ParseIP("224.2.17.12")},
  26. // Missing c= line
  27. {`v=0
  28. o=jdoe 2890844526 2890842807 IN IP4 10.47.16.5
  29. s=SDP Seminar
  30. i=A Seminar on the session description protocol
  31. u=http://www.example.com/seminars/sdp.pdf
  32. e=j.doe@example.com (Jane Doe)
  33. t=2873397496 2873404696
  34. a=recvonly
  35. m=audio 49170 RTP/AVP 0
  36. m=video 51372 RTP/AVP 99
  37. a=rtpmap:99 h263-1998/90000
  38. `, nil},
  39. // Single line, IP address only
  40. {`c=IN IP4 224.2.1.1
  41. `, net.ParseIP("224.2.1.1")},
  42. // Same, with TTL
  43. {`c=IN IP4 224.2.1.1/127
  44. `, net.ParseIP("224.2.1.1")},
  45. // Same, with TTL and multicast addresses
  46. {`c=IN IP4 224.2.1.1/127/3
  47. `, net.ParseIP("224.2.1.1")},
  48. // IPv6, address only
  49. {`c=IN IP6 FF15::101
  50. `, net.ParseIP("ff15::101")},
  51. // Same, with multicast addresses
  52. {`c=IN IP6 FF15::101/3
  53. `, net.ParseIP("ff15::101")},
  54. // Multiple c= lines
  55. {`c=IN IP4 1.2.3.4
  56. c=IN IP4 5.6.7.8
  57. `, net.ParseIP("1.2.3.4")},
  58. // Modified from SDP sent by snowflake-client.
  59. {`v=0
  60. o=- 7860378660295630295 2 IN IP4 127.0.0.1
  61. s=-
  62. t=0 0
  63. a=group:BUNDLE data
  64. a=msid-semantic: WMS
  65. m=application 54653 DTLS/SCTP 5000
  66. c=IN IP4 1.2.3.4
  67. a=candidate:3581707038 1 udp 2122260223 192.168.0.1 54653 typ host generation 0 network-id 1 network-cost 50
  68. a=candidate:2617212910 1 tcp 1518280447 192.168.0.1 59673 typ host tcptype passive generation 0 network-id 1 network-cost 50
  69. a=candidate:2082671819 1 udp 1686052607 1.2.3.4 54653 typ srflx raddr 192.168.0.1 rport 54653 generation 0 network-id 1 network-cost 50
  70. a=ice-ufrag:IBdf
  71. a=ice-pwd:G3lTrrC9gmhQx481AowtkhYz
  72. a=fingerprint:sha-256 53:F8:84:D9:3C:1F:A0:44:AA:D6:3C:65:80:D3:CB:6F:23:90:17:41:06:F9:9C:10:D8:48:4A:A8:B6:FA:14:A1
  73. a=setup:actpass
  74. a=mid:data
  75. a=sctpmap:5000 webrtc-datachannel 1024
  76. `, net.ParseIP("1.2.3.4")},
  77. // Improper character within IPv4
  78. {`c=IN IP4 224.2z.1.1
  79. `, nil},
  80. // Improper character within IPv6
  81. {`c=IN IP6 ff15:g::101
  82. `, nil},
  83. // Bogus "IP7" addrtype
  84. {`c=IN IP7 1.2.3.4
  85. `, nil},
  86. }
  87. for _, test := range tests {
  88. // https://tools.ietf.org/html/rfc4566#section-5: "The sequence
  89. // CRLF (0x0d0a) is used to end a record, although parsers
  90. // SHOULD be tolerant and also accept records terminated with a
  91. // single newline character." We represent the test cases with
  92. // LF line endings for convenience, and test them both that way
  93. // and with CRLF line endings.
  94. lfSDP := test.sdp
  95. crlfSDP := strings.Replace(lfSDP, "\n", "\r\n", -1)
  96. ip := remoteIPFromSDP(lfSDP)
  97. if !ip.Equal(test.expected) {
  98. t.Errorf("expected %q, got %q from %q", test.expected, ip, lfSDP)
  99. }
  100. ip = remoteIPFromSDP(crlfSDP)
  101. if !ip.Equal(test.expected) {
  102. t.Errorf("expected %q, got %q from %q", test.expected, ip, crlfSDP)
  103. }
  104. }
  105. }