upnp_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package upnp
  16. import (
  17. "encoding/xml"
  18. "net/url"
  19. "testing"
  20. )
  21. func TestExternalIPParsing(t *testing.T) {
  22. soapResponse :=
  23. []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  24. <s:Body>
  25. <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  26. <NewExternalIPAddress>1.2.3.4</NewExternalIPAddress>
  27. </u:GetExternalIPAddressResponse>
  28. </s:Body>
  29. </s:Envelope>`)
  30. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  31. err := xml.Unmarshal(soapResponse, envelope)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. if envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress != "1.2.3.4" {
  36. t.Error("Parse of SOAP request failed.")
  37. }
  38. }
  39. func TestControlURLParsing(t *testing.T) {
  40. rootURL := "http://192.168.243.1:80/igd.xml"
  41. u, _ := url.Parse(rootURL)
  42. subject := "/upnp?control=WANCommonIFC1"
  43. expected := "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  44. replaceRawPath(u, subject)
  45. if u.String() != expected {
  46. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  47. }
  48. u, _ = url.Parse(rootURL)
  49. subject = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  50. expected = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  51. replaceRawPath(u, subject)
  52. if u.String() != expected {
  53. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  54. }
  55. }