upnp_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 := []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  14. <s:Body>
  15. <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  16. <NewExternalIPAddress>1.2.3.4</NewExternalIPAddress>
  17. </u:GetExternalIPAddressResponse>
  18. </s:Body>
  19. </s:Envelope>`)
  20. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  21. err := xml.Unmarshal(soapResponse, envelope)
  22. if err != nil {
  23. t.Error(err)
  24. }
  25. if envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress != "1.2.3.4" {
  26. t.Error("Parse of SOAP request failed.")
  27. }
  28. }
  29. func TestSoapFaultParsing(t *testing.T) {
  30. soapResponse := []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  31. <s:Body>
  32. <s:Fault>
  33. <faultcode>s:Client</faultcode>
  34. <faultstring>UPnPError</faultstring>
  35. <detail>
  36. <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
  37. <errorCode>725</errorCode>
  38. <errorDescription>OnlyPermanentLeasesSupported</errorDescription></UPnPError>
  39. </detail>
  40. </s:Fault>
  41. </s:Body>
  42. </s:Envelope>`)
  43. envelope := &soapErrorResponse{}
  44. err := xml.Unmarshal(soapResponse, envelope)
  45. if err != nil {
  46. t.Error(err)
  47. }
  48. if envelope.ErrorCode != 725 {
  49. t.Error("Parse of SOAP request failed.", envelope)
  50. }
  51. }
  52. func TestControlURLParsing(t *testing.T) {
  53. rootURL := "http://192.168.243.1:80/igd.xml"
  54. u, _ := url.Parse(rootURL)
  55. subject := "/upnp?control=WANCommonIFC1"
  56. expected := "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  57. replaceRawPath(u, subject)
  58. if u.String() != expected {
  59. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  60. }
  61. u, _ = url.Parse(rootURL)
  62. subject = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  63. expected = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  64. replaceRawPath(u, subject)
  65. if u.String() != expected {
  66. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  67. }
  68. }