1 Star 0 Fork 0

lwch / upnp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
device_test.go 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
lwch 提交于 2019-03-13 00:29 . support GetPortMapping
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)
}
}
1
https://gitee.com/lwch/upnp.git
git@gitee.com:lwch/upnp.git
lwch
upnp
upnp
master

搜索帮助