rental_test.go raw

   1  package rental
   2  
   3  import (
   4  	"context"
   5  	"sync"
   6  	"testing"
   7  	"time"
   8  
   9  	"git.smesh.lol/orly/pkg/nostr/crypto/keys"
  10  	"git.smesh.lol/orly/pkg/nostr/encoders/hex"
  11  	"git.smesh.lol/orly/pkg/nostr/interfaces/signer"
  12  	"git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k"
  13  )
  14  
  15  type memStore struct {
  16  	mu sync.Mutex
  17  	m  map[string][]byte
  18  }
  19  
  20  func (s *memStore) GetMarker(k string) ([]byte, error) {
  21  	s.mu.Lock()
  22  	defer s.mu.Unlock()
  23  	return s.m[k], nil
  24  }
  25  
  26  func (s *memStore) SetMarker(k string, v []byte) error {
  27  	s.mu.Lock()
  28  	defer s.mu.Unlock()
  29  	if s.m == nil {
  30  		s.m = make(map[string][]byte)
  31  	}
  32  	s.m[k] = v
  33  	return nil
  34  }
  35  
  36  func buyerSigner(t *testing.T) signer.I {
  37  	t.Helper()
  38  	sk, err := keys.GenerateSecretKey()
  39  	if err != nil {
  40  		t.Fatal(err)
  41  	}
  42  	s, err := p8k.New()
  43  	if err != nil {
  44  		t.Fatal(err)
  45  	}
  46  	if err = s.InitSec(sk); err != nil {
  47  		t.Fatal(err)
  48  	}
  49  	return s
  50  }
  51  
  52  func fakeAsset() string {
  53  	var b [32]byte
  54  	for i := range b {
  55  		b[i] = byte(i + 3)
  56  	}
  57  	id, _ := hexAsset(b[:])
  58  	return id
  59  }
  60  
  61  func hexAsset(b []byte) (string, error) {
  62  	return hex.Enc(b), nil
  63  }
  64  
  65  func TestWindowExpires(t *testing.T) {
  66  	buyer := buyerSigner(t)
  67  	asset := fakeAsset()
  68  	m := New(&memStore{}, time.Minute)
  69  	ctx := context.Background()
  70  
  71  	creq, err := m.Challenge(asset, 50, "https://mint.example.com")
  72  	if err != nil {
  73  		t.Fatal(err)
  74  	}
  75  	if creq[:5] != "creqA" {
  76  		t.Fatalf("challenge is not NUT-18: %q", creq)
  77  	}
  78  	if m.Valid(ctx, buyer.Pub(), asset) {
  79  		t.Fatal("access before payment")
  80  	}
  81  	w, err := m.Open(ctx, buyer.Pub(), asset, time.Hour, 50)
  82  	if err != nil {
  83  		t.Fatal(err)
  84  	}
  85  	// The requested window is capped at the host bucket.
  86  	if w.Expires > time.Now().Add(time.Minute).Unix()+1 {
  87  		t.Fatalf("window %d exceeds the bucket", w.Expires)
  88  	}
  89  	if !m.Valid(ctx, buyer.Pub(), asset) {
  90  		t.Fatal("paid window is not valid")
  91  	}
  92  	// Expire it by moving the clock forward.
  93  	m.now = func() time.Time { return time.Now().Add(2 * time.Minute) }
  94  	if m.Valid(ctx, buyer.Pub(), asset) {
  95  		t.Fatal("expired window is still valid")
  96  	}
  97  	if m.Remaining(ctx, buyer.Pub(), asset) != 0 {
  98  		t.Fatal("expired window reports remaining time")
  99  	}
 100  }
 101  
 102  func TestChallengeRejectsBadInput(t *testing.T) {
 103  	m := New(&memStore{}, time.Minute)
 104  	if _, err := m.Challenge("short", 50, ""); err == nil {
 105  		t.Fatal("invalid asset id accepted")
 106  	}
 107  	if _, err := m.Challenge(fakeAsset(), 0, ""); err == nil {
 108  		t.Fatal("zero price accepted")
 109  	}
 110  }
 111