github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/pvl/dns_stub.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package pvl
     5  
     6  import (
     7  	"fmt"
     8  )
     9  
    10  type stubDNSEngine struct {
    11  	canned cannedDNS
    12  	ok     bool
    13  }
    14  
    15  // Map from domain to list of records.
    16  // A string "ERROR" will cause an error to be returned.
    17  type cannedDNS map[string]([]string)
    18  
    19  func newStubDNSEngine(canned cannedDNS) *stubDNSEngine {
    20  	return &stubDNSEngine{
    21  		canned: canned,
    22  		ok:     true,
    23  	}
    24  }
    25  
    26  func (e *stubDNSEngine) LookupTXT(domain string) ([]string, error) {
    27  	defaultres := []string{}
    28  	txts, ok := e.canned[domain]
    29  	if !ok {
    30  		e.ok = false
    31  		return defaultres, nil
    32  	}
    33  	if len(txts) == 1 && txts[0] == "ERROR" {
    34  		return defaultres, fmt.Errorf("fake dns error")
    35  	}
    36  	return txts, nil
    37  }
    38  
    39  func (e *stubDNSEngine) IsOk() bool {
    40  	return e.ok
    41  }