yubikey_test.go

 1package pgp
 2
 3import (
 4	"errors"
 5	"strconv"
 6	"strings"
 7	"testing"
 8)
 9
10// The OpenPGP signature packet construction and its ASN.1 / MPI helpers now
11// live in github.com/floatpane/go-openpgp-card-hl and are tested there. What
12// remains here is matcha's own MIME multipart/signed framing.
13
14func TestGenerateMIMEBoundaryUsesCryptoRandomBytes(t *testing.T) {
15	oldRandRead := randRead
16	defer func() { randRead = oldRandRead }()
17
18	randRead = func(p []byte) (int, error) {
19		for i := range p {
20			p[i] = byte(i)
21		}
22		return len(p), nil
23	}
24
25	got := generateMIMEBoundary()
26	want := "----=_Part_000102030405060708090a0b0c0d0e0f"
27	if got != want {
28		t.Fatalf("boundary = %q, want %q", got, want)
29	}
30}
31
32func TestGenerateMIMEBoundaryFallsBackToUnixNano(t *testing.T) {
33	oldRandRead := randRead
34	defer func() { randRead = oldRandRead }()
35
36	randRead = func(_ []byte) (int, error) {
37		return 0, errors.New("random source unavailable")
38	}
39
40	const prefix = "----=_Part_"
41	got := generateMIMEBoundary()
42	if !strings.HasPrefix(got, prefix) {
43		t.Fatalf("boundary = %q, want prefix %q", got, prefix)
44	}
45	if _, err := strconv.ParseInt(strings.TrimPrefix(got, prefix), 10, 64); err != nil {
46		t.Fatalf("fallback boundary suffix is not a UnixNano timestamp: %v", err)
47	}
48}