registry_test.go raw

   1  package registry
   2  
   3  import (
   4  	"encoding/json"
   5  	"strings"
   6  	"testing"
   7  	"time"
   8  
   9  	"git.smesh.lol/nostrino/pkg/nostr"
  10  )
  11  
  12  // TestATipBelongsToItsChain: a gateway that serves two chains must not publish one number that
  13  // reads as both chains' height. Two chains in a swap need not be a parent and a fork.
  14  func TestATipBelongsToItsChain(t *testing.T) {
  15  	info := Info{
  16  		Chains: []string{"btc", "blake"},
  17  		Tips:   map[string]int64{"btc": 972945, "blake": 978795},
  18  		Relays: []string{"wss://example"},
  19  	}
  20  
  21  	event, err := Event(info, [32]byte{1})
  22  
  23  	if err != nil {
  24  		t.Fatal(err)
  25  	}
  26  
  27  	var back Info
  28  
  29  	if err := json.Unmarshal([]byte(event.Content), &back); err != nil {
  30  		t.Fatal(err)
  31  	}
  32  
  33  	if back.Tip != 0 {
  34  		t.Fatalf("an aggregate tip of %d was published for two chains", back.Tip)
  35  	}
  36  
  37  	if back.Tips["btc"] != 972945 || back.Tips["blake"] != 978795 {
  38  		t.Fatalf("per-chain tips %v", back.Tips)
  39  	}
  40  }
  41  
  42  // TestAnAnnouncementExpires: a presence record that never expires is a client
  43  // connecting to a gateway that was decommissioned months ago.
  44  func TestAnAnnouncementExpires(t *testing.T) {
  45  	event, err := Event(Info{Chains: []string{"btc"}}, [32]byte{2})
  46  	if err != nil {
  47  		t.Fatal(err)
  48  	}
  49  
  50  	var tagExpiry int64
  51  	found := false
  52  
  53  	for _, tag := range event.Tags {
  54  		if len(tag) >= 2 && tag[0] == "expiration" {
  55  			found = true
  56  			tagExpiry = announcedExpiry(event)
  57  		}
  58  	}
  59  
  60  	if !found {
  61  		t.Fatal("the announcement carries no expiration tag")
  62  	}
  63  
  64  	var payload Info
  65  
  66  	if err := json.Unmarshal([]byte(event.Content), &payload); err != nil {
  67  		t.Fatal(err)
  68  	}
  69  
  70  	if payload.ExpiresAt != tagExpiry {
  71  		t.Fatalf("content expires_at %d, tag says %d: one value, two sources",
  72  			payload.ExpiresAt, tagExpiry)
  73  	}
  74  
  75  	if want := event.CreatedAt + int64(AnnouncementTTL/time.Second); tagExpiry != want {
  76  		t.Fatalf("expiry %d, want %d", tagExpiry, want)
  77  	}
  78  }
  79  
  80  // TestAnOldAnnouncementIsTimedFromItsCreation: a payload from before the tag
  81  // existed is bounded by its own created_at rather than trusted for ever.
  82  func TestAnOldAnnouncementIsTimedFromItsCreation(t *testing.T) {
  83  	event := &nostr.Event{Kind: KindGateway, CreatedAt: 1_700_000_000}
  84  
  85  	got := announcedExpiry(event)
  86  
  87  	if want := int64(1_700_000_000) + int64(AnnouncementTTL/time.Second); got != want {
  88  		t.Fatalf("expiry %d, want %d", got, want)
  89  	}
  90  }
  91  
  92  // TestOnIgnoresTheSchemesAndSlash: a relay URL is an identity of host and port,
  93  // so a gateway naming wss://relay.example/ is on the client's relay.example.
  94  func TestOnIgnoresTheSchemesAndSlash(t *testing.T) {
  95  	info := Info{Relays: []string{"wss://relay.example/"}}
  96  
  97  	if !info.On("ws://relay.example") {
  98  		t.Fatal("a gateway announcing wss://relay.example/ is not seen on ws://relay.example")
  99  	}
 100  
 101  	if info.On("wss://other.example") {
 102  		t.Fatal("a gateway is seen on a relay it does not name")
 103  	}
 104  
 105  	served := Info{ServedBy: "wss://relay.example"}
 106  
 107  	if !served.On("wss://relay.example/") {
 108  		t.Fatal("the relay that served the announcement is not a relay the gateway is on")
 109  	}
 110  }
 111  
 112  // TestTheAnnouncementContentIsNotHTMLEscaped: the URI carries an ampersand, and
 113  // Go's encoder would write it as \u0026. That is a different string, it differs
 114  // from what a browser produces for the same value, and the backslash it puts in
 115  // the content is the one case a canonical encoder can get wrong.
 116  func TestTheAnnouncementContentIsNotHTMLEscaped(t *testing.T) {
 117  	info := Info{
 118  		Chains: []string{"btc"},
 119  		URI:    "nostr+chaindata://abc?chains=btc%2Cblake&relay=wss%3A%2F%2Fexample&v=1",
 120  		Relays: []string{"wss://example"},
 121  		Tips:   map[string]int64{"btc": 1},
 122  	}
 123  	event, err := Event(info, [32]byte{1})
 124  	if err != nil {
 125  		t.Fatalf("event: %v", err)
 126  	}
 127  	if !strings.Contains(event.Content, "&relay=") {
 128  		t.Fatalf("content lost the ampersand: %s", event.Content)
 129  	}
 130  	if strings.Contains(event.Content, `\u00`) {
 131  		t.Fatalf("content carries a Go HTML escape: %s", event.Content)
 132  	}
 133  }
 134