pull/18/head
Hugo Landau 10 years ago
parent ce564859ad
commit d8c033a4df

@ -45,6 +45,10 @@ type Config struct {
// should resolve to. This should be the public IP of the nameserver serving the
// zone expressed by this backend.
SelfIP string
// Map names (like "d/example") to strings containing JSON values. Used to provide
// fake names for testing purposes. You don't need to use this.
FakeNames map[string]string
}
// Creates a new Namecoin backend.
@ -61,6 +65,10 @@ func New(cfg *Config) (backend *Backend, err error) {
b.cache.MaxEntries = defaultMaxEntries
}
if b.cfg.FakeNames == nil {
b.cfg.FakeNames = map[string]string{}
}
backend = b
return
@ -109,10 +117,25 @@ func (b *Backend) addNamecoinEntryToCache(name string, d *domain) {
b.cache.Add(name, d)
}
func (b *Backend) getNamecoinEntryLL(name string) (*domain, error) {
func (b *Backend) resolveName(name string) (jsonValue string, err error) {
if fv, ok := b.cfg.FakeNames[name]; ok {
if fv == "NX" {
return "", merr.ErrNoSuchDomain
}
return fv, nil
}
v, err := b.nc.Query(name)
if err != nil {
log.Infoe(err, "namecoin query failed: ", err)
return "", err
}
return v, nil
}
func (b *Backend) getNamecoinEntryLL(name string) (*domain, error) {
v, err := b.resolveName(name)
if err != nil {
return nil, err
}
@ -141,13 +164,7 @@ func (b *Backend) jsonToDomain(name, jsonValue string) (*domain, error) {
}
func (b *Backend) resolveExtraName(name string) (jsonValue string, err error) {
v, err := b.nc.Query(name)
if err != nil {
log.Infoe(err, "namecoin subquery failed: ", err)
return "", err
}
return v, nil
return b.resolveName(name)
}
type btx struct {

@ -1,169 +1,15 @@
package ncdomain_test
import "github.com/hlandau/ncdns/ncdomain"
import "github.com/hlandau/ncdns/testutil"
import _ "github.com/hlandau/nctestsuite"
import "testing"
import "fmt"
import "strings"
import "os"
import "path/filepath"
import "bufio"
import "io"
import "sort"
import "unicode"
import "unicode/utf8"
//import "github.com/miekg/dns"
//import "net"
type testItem struct {
ID string
Names map[string]string
Records string
}
func stripTag(L string) string {
if len(L) < 3 {
return L
}
r, _ := utf8.DecodeRuneInString(L)
if unicode.IsUpper(r) {
x := strings.IndexRune(L, ' ')
L = L[x+1:]
}
return L
}
func openFileFromGOPATH(fn string) (f *os.File, err error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = "."
}
for _, p := range strings.Split(gopath, string(os.PathListSeparator)) {
f, err = os.Open(filepath.Join(p, fn))
if err == nil {
return
}
}
return
}
func suiteReader(t *testing.T) <-chan testItem {
testItemChan := make(chan testItem, 20)
fpath := "src/github.com/hlandau/nctestsuite/testsuite.txt"
f, err := openFileFromGOPATH(fpath)
if err != nil {
t.Fatalf("Error: Couldn't open %s: %+v", fpath, err)
}
go func() {
defer f.Close()
lineChan := make(chan string, 20)
syncChan := make(chan struct{})
go func() {
reissue := false
var L string
var ok bool
for {
if reissue {
reissue = false
} else {
L, ok = <-lineChan
}
if !ok {
break
}
m := map[string]string{}
if L != "IN" && !strings.HasPrefix(L, "IN ") {
t.Fatalf("invalid test suite file")
}
id := ""
if len(L) > 2 {
id = L[3:]
}
for {
name := <-lineChan
value := <-lineChan
m[name] = value
L = <-lineChan
if L != "IN" {
break
}
}
if L != "OUT" {
t.Fatalf("invalid test suite file")
}
records := []string{}
for {
L, ok = <-lineChan
if !ok || L == "IN" || strings.HasPrefix(L, "IN ") {
reissue = true
break
}
L = stripTag(L)
records = append(records, L)
}
sort.Strings(records)
// process records
ti := testItem{
ID: id,
Names: m,
Records: strings.Join(records, "\n"),
}
testItemChan <- ti
}
close(testItemChan)
close(syncChan)
}()
r := bufio.NewReader(f)
for {
L, err := r.ReadString('\n')
if err != nil {
if err != io.EOF {
t.Errorf("error while reading line: %+v", err)
}
break
}
L = strings.Trim(L, " \t\r\n")
if L == "" || (len(L) > 0 && L[0] == '#') {
continue
}
lineChan <- L
}
close(lineChan)
<-syncChan
}()
return testItemChan
}
func TestSuite(t *testing.T) {
items := suiteReader(t)
items := testutil.SuiteReader(t)
for ti := range items {
resolve := func(name string) (string, error) {
v, ok := ti.Names[name]

@ -0,0 +1,157 @@
package testutil
import "os"
import "sort"
import "unicode"
import "unicode/utf8"
import "strings"
import "path/filepath"
import "io"
import "bufio"
import "testing"
type TestItem struct {
ID string
Names map[string]string
Records string
}
func stripTag(L string) string {
if len(L) < 3 {
return L
}
r, _ := utf8.DecodeRuneInString(L)
if unicode.IsUpper(r) {
x := strings.IndexRune(L, ' ')
L = L[x+1:]
}
return L
}
func openFileFromGOPATH(fn string) (f *os.File, err error) {
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = "."
}
for _, p := range strings.Split(gopath, string(os.PathListSeparator)) {
f, err = os.Open(filepath.Join(p, fn))
if err == nil {
return
}
}
return
}
func SuiteReader(t *testing.T) <-chan TestItem {
testItemChan := make(chan TestItem, 20)
fpath := "src/github.com/hlandau/nctestsuite/testsuite.txt"
f, err := openFileFromGOPATH(fpath)
if err != nil {
t.Fatalf("Error: Couldn't open %s: %+v", fpath, err)
}
go func() {
defer f.Close()
lineChan := make(chan string, 20)
syncChan := make(chan struct{})
go func() {
reissue := false
var L string
var ok bool
for {
if reissue {
reissue = false
} else {
L, ok = <-lineChan
}
if !ok {
break
}
m := map[string]string{}
if L != "IN" && !strings.HasPrefix(L, "IN ") {
t.Fatalf("invalid test suite file")
}
id := ""
if len(L) > 2 {
id = L[3:]
}
for {
name := <-lineChan
value := <-lineChan
m[name] = value
L = <-lineChan
if L != "IN" {
break
}
}
if L != "OUT" {
t.Fatalf("invalid test suite file")
}
records := []string{}
for {
L, ok = <-lineChan
if !ok || L == "IN" || strings.HasPrefix(L, "IN ") {
reissue = true
break
}
L = stripTag(L)
records = append(records, L)
}
sort.Strings(records)
// process records
ti := TestItem{
ID: id,
Names: m,
Records: strings.Join(records, "\n"),
}
testItemChan <- ti
}
close(testItemChan)
close(syncChan)
}()
r := bufio.NewReader(f)
for {
L, err := r.ReadString('\n')
if err != nil {
if err != io.EOF {
t.Errorf("error while reading line: %+v", err)
}
break
}
L = strings.Trim(L, " \t\r\n")
if L == "" || (len(L) > 0 && L[0] == '#') {
continue
}
lineChan <- L
}
close(lineChan)
<-syncChan
}()
return testItemChan
}
Loading…
Cancel
Save