1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

remove unused code, add test cases

This commit is contained in:
Timothy 2017-10-27 14:34:17 +08:00
parent fad5b0fa20
commit 62ac21417e
4 changed files with 23 additions and 46 deletions

View File

@ -32,8 +32,6 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<-
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
fmt.Println(godns.IdentifyPanic())
log.Print(godns.IdentifyPanic())
panicChan <- *domain
}
}()

View File

@ -35,8 +35,6 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn
defer func() {
if err := recover(); err != nil {
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
fmt.Println(godns.IdentifyPanic())
log.Print(godns.IdentifyPanic())
panicChan <- *domain
}
}()

View File

@ -2,13 +2,9 @@ package godns
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime"
"strings"
"golang.org/x/net/proxy"
)
@ -31,10 +27,9 @@ func GetCurrentIP(configuration *Settings) (string, error) {
if configuration.Socks5Proxy != "" {
log.Println("use socks5 proxy:" + configuration.Socks5Proxy)
dialer, err := proxy.SOCKS5("tcp", configuration.Socks5Proxy, nil, proxy.Direct)
if err != nil {
fmt.Println("can't connect to the proxy:", err)
log.Println("can't connect to the proxy:", err)
return "", err
}
@ -56,41 +51,6 @@ func GetCurrentIP(configuration *Settings) (string, error) {
return string(body), nil
}
// IdentifyPanic identifies panic and output the detailed panic information
func IdentifyPanic() string {
var name, file string
var line int
var pc [16]uintptr
n := runtime.Callers(3, pc[:])
for _, pc := range pc[:n] {
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
file, line = fn.FileLine(pc)
name = fn.Name()
if !strings.HasPrefix(name, "runtime.") {
break
}
}
switch {
case name != "":
return fmt.Sprintf("%v:%v", name, line)
case file != "":
return fmt.Sprintf("%v:%v", file, line)
}
return fmt.Sprintf("pc:%x", pc)
}
// Usage prints the usage of GoDNS
func Usage() {
log.Println("[command] -c=[config file path]")
flag.PrintDefaults()
}
// CheckSettings check the format of settings
func CheckSettings(config *Settings) error {
if config.Provider == DNSPOD {

View File

@ -4,7 +4,7 @@ import (
"testing"
)
func testGetCurrentIP(t *testing.T) {
func TestGetCurrentIP(t *testing.T) {
conf := &Settings{IPUrl: "http://members.3322.org/dyndns/getip"}
ip, _ := GetCurrentIP(conf)
@ -14,3 +14,24 @@ func testGetCurrentIP(t *testing.T) {
t.Log("IP is:" + ip)
}
}
func TestCheckSettings(t *testing.T) {
settingError := &Settings{}
if err := CheckSettings(settingError); err == nil {
t.Error("setting is invalid, should return error")
}
settingDNSPod := &Settings{Provider: "DNSPod", LoginToken: "aaa"}
if err := CheckSettings(settingDNSPod); err == nil {
t.Log("setting with login token, passed")
} else {
t.Error("setting with login token, should be passed")
}
settingHE := &Settings{Provider: "HE", Password: ""}
if err := CheckSettings(settingHE); err != nil {
t.Log("HE setting without password, passed")
} else {
t.Error("HE setting without password, should be faild")
}
}