代码拉取完成,页面将自动刷新
package upnp
import (
"fmt"
"testing"
)
const testDevIndex = 0
const localIP = "192.168.1.200"
func TestSearch(t *testing.T) {
var c Client
devs, err := c.SearchRoot(5)
if err != nil {
panic(err)
}
fmt.Printf("Found %d devices\n", len(devs))
for _, dev := range devs {
fmt.Println(dev.descLocation)
}
}
func findDevice(t *testing.T) *Device {
var c Client
devs, err := c.SearchRoot(1)
if err != nil {
panic(err)
}
if len(devs) <= testDevIndex {
t.Errorf("No devices found")
}
return devs[testDevIndex]
}
func TestParse(t *testing.T) {
dev := findDevice(t)
if err := dev.parse(); err != nil {
panic(err)
}
}
func TestServiceParse(t *testing.T) {
dev := findDevice(t)
if err := dev.parse(); err != nil {
panic(err)
}
for _, s := range dev.serviceList {
fmt.Printf("Parsing service %s...\n", s.ID)
if err := s.parse(); err != nil {
panic(err)
}
}
}
func TestGetExtIP(t *testing.T) {
dev := findDevice(t)
actions, err := dev.GetExtIPActions()
if err != nil {
panic(err)
}
fmt.Printf("GetExternalIPAddress: %d actions found\n", len(actions))
resp, err := actions[0].(*GetExtIPAction).Do(0)
if err != nil {
panic(err)
}
fmt.Printf("ExtIP: %s\n", resp.Data.(string))
}
func TestGetPortMapping(t *testing.T) {
dev := findDevice(t)
actions, err := dev.GetPortMappingActions()
if err != nil {
panic(err)
}
fmt.Printf("GetPortMappingActions: %d actions found\n", len(actions))
i := 0
for {
resp, err := actions[0].(*GetPortMappingAction).Do(i)
if err != nil {
if IsNotfound(err) {
break
}
panic(err)
}
pm := resp.Data.(PortMapping)
fmt.Printf("%s %s:%d => %d\n", pm.Protocol,
pm.LocalIP, pm.LocalPort, pm.ExtPort)
i++
}
}
func TestListPortMapping(t *testing.T) {
dev := findDevice(t)
for _, pm := range dev.ListPortMapping() {
fmt.Printf("%s %s:%d => %d\n", pm.Protocol,
pm.LocalIP, pm.LocalPort, pm.ExtPort)
}
}
func TestAddMapping(t *testing.T) {
dev := findDevice(t)
actions, err := dev.GetAddMappingActions()
if err != nil {
panic(err)
}
fmt.Printf("GetAddMappingActions: %d actions found\n", len(actions))
_, err = actions[0].(*AddPortMappingAction).Do("TCP", localIP, 8080, 8080, 60)
if err != nil {
panic(err)
}
}
func TestDeleteMapping(t *testing.T) {
TestAddMapping(t)
dev := findDevice(t)
actions, err := dev.GetDeleteMappingActions()
if err != nil {
panic(err)
}
fmt.Printf("GetDeleteMappingActions: %d actions found\n", len(actions))
_, err = actions[0].(*DeletePortMappingAction).Do("TCP", 8080)
if err != nil {
panic(err)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。