You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1002 B

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package upnp
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. )
  7. /*
  8. This is a manual test.
  9. TODO: set up or find a service to probe open ports.
  10. */
  11. func TestUPNP(t *testing.T) {
  12. t.Log("hello!")
  13. nat, err := Discover()
  14. if err != nil {
  15. t.Fatalf("NAT upnp could not be discovered: %v", err)
  16. }
  17. t.Log("ourIP: ", nat.(*upnpNAT).ourIP)
  18. ext, err := nat.GetExternalAddress()
  19. if err != nil {
  20. t.Fatalf("External address error: %v", err)
  21. }
  22. t.Logf("External address: %v", ext)
  23. port, err := nat.AddPortMapping("tcp", 8001, 8001, "testing", 0)
  24. if err != nil {
  25. t.Fatalf("Port mapping error: %v", err)
  26. }
  27. t.Logf("Port mapping mapped: %v", port)
  28. // also run the listener, open for all remote addresses.
  29. listener, err := net.Listen("tcp", ":8001")
  30. if err != nil {
  31. panic(err)
  32. }
  33. // now sleep for 10 seconds
  34. time.Sleep(10 * time.Second)
  35. err = nat.DeletePortMapping("tcp", 8001, 8001)
  36. if err != nil {
  37. t.Fatalf("Port mapping delete error: %v", err)
  38. }
  39. t.Logf("Port mapping deleted")
  40. listener.Close()
  41. }