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

     1  package teams
     2  
     3  import (
     4  	"context"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"github.com/keybase/client/go/protocol/keybase1"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func createProofTerm(idInt int, seqno keybase1.Seqno) proofTerm {
    13  	var id [16]byte
    14  	id[15] = 0x25
    15  	id[0] = byte(idInt)
    16  	return proofTerm{
    17  		leafID: keybase1.UserOrTeamID(hex.EncodeToString(id[:])),
    18  		sigMeta: keybase1.SignatureMetadata{
    19  			SigChainLocation: keybase1.SigChainLocation{
    20  				Seqno: seqno,
    21  			},
    22  		},
    23  	}
    24  }
    25  
    26  func proofEq(x, y proof) bool {
    27  	return termEq(x.a, y.a) && termEq(x.b, y.b)
    28  }
    29  
    30  func termEq(a, b proofTerm) bool {
    31  	return a.leafID == b.leafID && a.sigMeta.SigChainLocation.Seqno == b.sigMeta.SigChainLocation.Seqno
    32  }
    33  
    34  func TestProofSetAdd(t *testing.T) {
    35  	tc := SetupTest(t, "ps", 1)
    36  	defer tc.Cleanup()
    37  
    38  	ps := newProofSet(tc.G)
    39  
    40  	a := createProofTerm(1, keybase1.Seqno(10))
    41  	b := createProofTerm(2, keybase1.Seqno(100))
    42  	c := createProofTerm(1, keybase1.Seqno(40))
    43  	d := createProofTerm(2, keybase1.Seqno(400))
    44  	e := createProofTerm(1, keybase1.Seqno(80))
    45  	f := createProofTerm(2, keybase1.Seqno(800))
    46  
    47  	// Test moving in only the right side
    48  	g := createProofTerm(1, keybase1.Seqno(10))
    49  	h := createProofTerm(2, keybase1.Seqno(98))
    50  
    51  	// Test moving in only the left side
    52  	i := createProofTerm(1, keybase1.Seqno(42))
    53  	j := createProofTerm(2, keybase1.Seqno(400))
    54  
    55  	// Test moving in both sides
    56  	k := createProofTerm(1, keybase1.Seqno(84))
    57  	l := createProofTerm(2, keybase1.Seqno(798))
    58  
    59  	// Should be a new proof
    60  	m := createProofTerm(2, keybase1.Seqno(402))
    61  	n := createProofTerm(1, keybase1.Seqno(52))
    62  
    63  	// Makes a new proof, also, since didn't overlap
    64  	o := createProofTerm(2, keybase1.Seqno(404))
    65  	p := createProofTerm(1, keybase1.Seqno(50))
    66  
    67  	// Clamps the {m,n} proof
    68  	q := createProofTerm(2, keybase1.Seqno(403))
    69  	r := createProofTerm(1, keybase1.Seqno(49))
    70  
    71  	ps.AddNeededHappensBeforeProof(context.TODO(), a, b, "")
    72  	ps.AddNeededHappensBeforeProof(context.TODO(), c, d, "")
    73  	ps.AddNeededHappensBeforeProof(context.TODO(), e, f, "")
    74  	ps.AddNeededHappensBeforeProof(context.TODO(), g, h, "")
    75  	ps.AddNeededHappensBeforeProof(context.TODO(), i, j, "")
    76  	ps.AddNeededHappensBeforeProof(context.TODO(), k, l, "")
    77  	ps.AddNeededHappensBeforeProof(context.TODO(), m, n, "")
    78  	ps.AddNeededHappensBeforeProof(context.TODO(), o, p, "")
    79  	ps.AddNeededHappensBeforeProof(context.TODO(), q, r, "")
    80  
    81  	ret := ps.AllProofs()
    82  
    83  	require.Len(t, ret, 5)
    84  	require.True(t, proofEq(ret[0], proof{a, h, ""}))
    85  	require.True(t, proofEq(ret[1], proof{i, d, ""}))
    86  	require.True(t, proofEq(ret[2], proof{k, l, ""}))
    87  	require.True(t, proofEq(ret[3], proof{q, r, ""}))
    88  	require.True(t, proofEq(ret[4], proof{o, p, ""}))
    89  }
    90  
    91  func TestProofSetImply(t *testing.T) {
    92  	tc := SetupTest(t, "ps", 1)
    93  	defer tc.Cleanup()
    94  
    95  	ps := newProofSet(tc.G)
    96  
    97  	a := createProofTerm(1, keybase1.Seqno(1)) // user provisions device
    98  	b := createProofTerm(2, keybase1.Seqno(1)) // signed link
    99  	c := createProofTerm(2, keybase1.Seqno(2)) // signed link
   100  	d := createProofTerm(2, keybase1.Seqno(3)) // signed link
   101  
   102  	ps.AddNeededHappensBeforeProof(context.TODO(), a, b, "")
   103  	ps.AddNeededHappensBeforeProof(context.TODO(), a, c, "")
   104  	ps.AddNeededHappensBeforeProof(context.TODO(), a, d, "")
   105  
   106  	ret := ps.AllProofs()
   107  	for _, p := range ret {
   108  		t.Logf("%v\n", p.shortForm())
   109  	}
   110  	require.Len(t, ret, 1)
   111  	require.True(t, proofEq(ret[0], proof{a, b, ""}))
   112  }
   113  
   114  // proof orderings on the same chain should get dropped because they are self evident
   115  func TestProofSetSameChain(t *testing.T) {
   116  	tc := SetupTest(t, "ps", 1)
   117  	defer tc.Cleanup()
   118  
   119  	ps := newProofSet(tc.G)
   120  
   121  	a := createProofTerm(1, keybase1.Seqno(10))
   122  	b := createProofTerm(1, keybase1.Seqno(11))
   123  	c := createProofTerm(1, keybase1.Seqno(12))
   124  
   125  	ps.AddNeededHappensBeforeProof(context.TODO(), a, b, "")
   126  	ps.AddNeededHappensBeforeProof(context.TODO(), b, c, "")
   127  
   128  	ret := ps.AllProofs()
   129  	for _, p := range ret {
   130  		t.Logf("%v\n", p.shortForm())
   131  	}
   132  	require.Len(t, ret, 0)
   133  }