github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/mdserver_tlf_storage_test.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package libkbfs 6 7 import ( 8 "os" 9 "testing" 10 11 "github.com/keybase/client/go/kbfs/data" 12 "github.com/keybase/client/go/kbfs/ioutil" 13 "github.com/keybase/client/go/kbfs/kbfscodec" 14 "github.com/keybase/client/go/kbfs/kbfscrypto" 15 "github.com/keybase/client/go/kbfs/kbfsmd" 16 "github.com/keybase/client/go/kbfs/tlf" 17 "github.com/keybase/client/go/protocol/keybase1" 18 "github.com/stretchr/testify/require" 19 "golang.org/x/net/context" 20 ) 21 22 func getMDStorageLength(t *testing.T, s *mdServerTlfStorage, bid kbfsmd.BranchID) int { 23 len, err := s.journalLength(bid) 24 require.NoError(t, err) 25 return int(len) 26 } 27 28 // TestMDServerTlfStorageBasic copies TestMDServerBasics, but for a 29 // single mdServerTlfStorage. 30 func TestMDServerTlfStorageBasic(t *testing.T) { 31 codec := kbfscodec.NewMsgpack() 32 signingKey := kbfscrypto.MakeFakeSigningKeyOrBust("test key") 33 verifyingKey := kbfscrypto.MakeFakeVerifyingKeyOrBust("test key") 34 signer := kbfscrypto.SigningKeySigner{Key: signingKey} 35 36 tempdir, err := ioutil.TempDir(os.TempDir(), "mdserver_tlf_storage") 37 require.NoError(t, err) 38 defer func() { 39 err := ioutil.RemoveAll(tempdir) 40 require.NoError(t, err) 41 }() 42 43 tlfID := tlf.FakeID(1, tlf.Private) 44 s := makeMDServerTlfStorage(tlfID, codec, data.WallClock{}, nil, 45 defaultClientMetadataVer, tempdir) 46 defer s.shutdown() 47 48 require.Equal(t, 0, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 49 50 uid := keybase1.MakeTestUID(1) 51 h, err := tlf.MakeHandle( 52 []keybase1.UserOrTeamID{uid.AsUserOrTeam()}, nil, nil, nil, nil) 53 require.NoError(t, err) 54 55 // (1) Validate merged branch is empty. 56 57 ctx := context.Background() 58 head, err := s.getForTLF(ctx, uid, kbfsmd.NullBranchID) 59 require.NoError(t, err) 60 require.Nil(t, head) 61 62 require.Equal(t, 0, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 63 64 // (2) Push some new metadata blocks. 65 66 prevRoot := kbfsmd.ID{} 67 middleRoot := kbfsmd.ID{} 68 for i := kbfsmd.Revision(1); i <= 10; i++ { 69 brmd := makeBRMDForTest(t, codec, tlfID, h, i, uid, prevRoot) 70 rmds := signRMDSForTest(t, codec, signer, brmd) 71 // MDv3 TODO: pass extra metadata 72 recordBranchID, err := s.put(ctx, uid, verifyingKey, rmds, nil) 73 require.NoError(t, err) 74 require.False(t, recordBranchID) 75 prevRoot, err = kbfsmd.MakeID(codec, rmds.MD) 76 require.NoError(t, err) 77 if i == 5 { 78 middleRoot = prevRoot 79 } 80 } 81 82 require.Equal(t, 10, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 83 84 // (3) Trigger a conflict. 85 86 brmd := makeBRMDForTest(t, codec, tlfID, h, 10, uid, prevRoot) 87 rmds := signRMDSForTest(t, codec, signer, brmd) 88 // MDv3 TODO: pass extra metadata 89 _, err = s.put(ctx, uid, verifyingKey, rmds, nil) 90 require.IsType(t, kbfsmd.ServerErrorConflictRevision{}, err) 91 92 require.Equal(t, 10, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 93 94 // (4) Push some new unmerged metadata blocks linking to the 95 // middle merged block. 96 97 prevRoot = middleRoot 98 bid := kbfsmd.FakeBranchID(1) 99 for i := kbfsmd.Revision(6); i < 41; i++ { 100 brmd := makeBRMDForTest(t, codec, tlfID, h, i, uid, prevRoot) 101 brmd.SetUnmerged() 102 brmd.SetBranchID(bid) 103 rmds := signRMDSForTest(t, codec, signer, brmd) 104 // MDv3 TODO: pass extra metadata 105 recordBranchID, err := s.put(ctx, uid, verifyingKey, rmds, nil) 106 require.NoError(t, err) 107 require.Equal(t, i == kbfsmd.Revision(6), recordBranchID) 108 prevRoot, err = kbfsmd.MakeID(codec, rmds.MD) 109 require.NoError(t, err) 110 } 111 112 require.Equal(t, 10, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 113 require.Equal(t, 35, getMDStorageLength(t, s, bid)) 114 115 // (5) Check for proper unmerged head. 116 117 head, err = s.getForTLF(ctx, uid, bid) 118 require.NoError(t, err) 119 require.NotNil(t, head) 120 require.Equal(t, kbfsmd.Revision(40), head.MD.RevisionNumber()) 121 122 require.Equal(t, 10, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 123 require.Equal(t, 35, getMDStorageLength(t, s, bid)) 124 125 // (6) Try to get unmerged range. 126 127 rmdses, err := s.getRange(ctx, uid, bid, 1, 100) 128 require.NoError(t, err) 129 require.Equal(t, 35, len(rmdses)) 130 for i := kbfsmd.Revision(6); i < 16; i++ { 131 require.Equal(t, i, rmdses[i-6].MD.RevisionNumber()) 132 } 133 134 // Nothing corresponds to (7) - (9) from MDServerTestBasics. 135 136 // (10) Check for proper merged head. 137 138 head, err = s.getForTLF(ctx, uid, kbfsmd.NullBranchID) 139 require.NoError(t, err) 140 require.NotNil(t, head) 141 require.Equal(t, kbfsmd.Revision(10), head.MD.RevisionNumber()) 142 143 // (11) Try to get merged range. 144 145 rmdses, err = s.getRange(ctx, uid, kbfsmd.NullBranchID, 1, 100) 146 require.NoError(t, err) 147 require.Equal(t, 10, len(rmdses)) 148 for i := kbfsmd.Revision(1); i <= 10; i++ { 149 require.Equal(t, i, rmdses[i-1].MD.RevisionNumber()) 150 } 151 152 require.Equal(t, 10, getMDStorageLength(t, s, kbfsmd.NullBranchID)) 153 require.Equal(t, 35, getMDStorageLength(t, s, bid)) 154 }