upnp_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package upnp
  7. import (
  8. "encoding/xml"
  9. "net/url"
  10. "testing"
  11. )
  12. func TestExternalIPParsing(t *testing.T) {
  13. soapResponse :=
  14. []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  15. <s:Body>
  16. <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  17. <NewExternalIPAddress>1.2.3.4</NewExternalIPAddress>
  18. </u:GetExternalIPAddressResponse>
  19. </s:Body>
  20. </s:Envelope>`)
  21. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  22. err := xml.Unmarshal(soapResponse, envelope)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. if envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress != "1.2.3.4" {
  27. t.Error("Parse of SOAP request failed.")
  28. }
  29. }
  30. func TestSoapFaultParsing(t *testing.T) {
  31. soapResponse :=
  32. []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  33. <s:Body>
  34. <s:Fault>
  35. <faultcode>s:Client</faultcode>
  36. <faultstring>UPnPError</faultstring>
  37. <detail>
  38. <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
  39. <errorCode>725</errorCode>
  40. <errorDescription>OnlyPermanentLeasesSupported</errorDescription></UPnPError>
  41. </detail>
  42. </s:Fault>
  43. </s:Body>
  44. </s:Envelope>`)
  45. envelope := &soapErrorResponse{}
  46. err := xml.Unmarshal(soapResponse, envelope)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. if envelope.ErrorCode != 725 {
  51. t.Error("Parse of SOAP request failed.", envelope)
  52. }
  53. }
  54. func TestControlURLParsing(t *testing.T) {
  55. rootURL := "http://192.168.243.1:80/igd.xml"
  56. u, _ := url.Parse(rootURL)
  57. subject := "/upnp?control=WANCommonIFC1"
  58. expected := "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  59. replaceRawPath(u, subject)
  60. if u.String() != expected {
  61. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  62. }
  63. u, _ = url.Parse(rootURL)
  64. subject = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  65. expected = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  66. replaceRawPath(u, subject)
  67. if u.String() != expected {
  68. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  69. }
  70. }