github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/journal_block_server_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/ioutil" 12 "github.com/keybase/client/go/kbfs/kbfsblock" 13 "github.com/keybase/client/go/kbfs/kbfscrypto" 14 "github.com/keybase/client/go/kbfs/tlf" 15 "github.com/keybase/client/go/protocol/keybase1" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 "golang.org/x/net/context" 19 ) 20 21 func setupJournalBlockServerTest(t *testing.T) ( 22 tempdir string, ctx context.Context, cancel context.CancelFunc, 23 config *ConfigLocal, jManager *JournalManager) { 24 tempdir, err := ioutil.TempDir(os.TempDir(), "journal_block_server") 25 require.NoError(t, err) 26 27 // Clean up the tempdir if the rest of the setup fails. 28 setupSucceeded := false 29 defer func() { 30 if !setupSucceeded { 31 err := ioutil.RemoveAll(tempdir) 32 assert.NoError(t, err) 33 } 34 }() 35 36 ctx, cancel = context.WithTimeout( 37 context.Background(), individualTestTimeout) 38 39 // Clean up the context if the rest of the setup fails. 40 defer func() { 41 if !setupSucceeded { 42 cancel() 43 } 44 }() 45 46 config = MakeTestConfigOrBust(t, "test_user") 47 48 // Clean up the config if the rest of the setup fails. 49 defer func() { 50 if !setupSucceeded { 51 CheckConfigAndShutdown(ctx, t, config) 52 } 53 }() 54 55 err = config.EnableDiskLimiter(tempdir) 56 require.NoError(t, err) 57 err = config.EnableJournaling( 58 ctx, tempdir, TLFJournalBackgroundWorkEnabled) 59 require.NoError(t, err) 60 jManager, err = GetJournalManager(config) 61 require.NoError(t, err) 62 blockServer := jManager.blockServer() 63 // Turn this on for testing. 64 blockServer.enableAddBlockReference = true 65 config.SetBlockServer(blockServer) 66 67 setupSucceeded = true 68 return tempdir, ctx, cancel, config, jManager 69 } 70 71 func teardownJournalBlockServerTest( 72 ctx context.Context, t *testing.T, tempdir string, 73 cancel context.CancelFunc, config Config) { 74 CheckConfigAndShutdown(ctx, t, config) 75 cancel() 76 err := ioutil.RemoveAll(tempdir) 77 assert.NoError(t, err) 78 } 79 80 type shutdownOnlyBlockServer struct{ BlockServer } 81 82 func (shutdownOnlyBlockServer) Shutdown(context.Context) {} 83 84 func TestJournalBlockServerPutGetAddReference(t *testing.T) { 85 tempdir, ctx, cancel, config, jManager := setupJournalBlockServerTest(t) 86 defer teardownJournalBlockServerTest(ctx, t, tempdir, cancel, config) 87 88 // Use a shutdown-only BlockServer so that it errors if the 89 // journal tries to access it. 90 jManager.delegateBlockServer = shutdownOnlyBlockServer{} 91 92 tlfID := tlf.FakeID(2, tlf.Private) 93 err := jManager.Enable(ctx, tlfID, nil, TLFJournalBackgroundWorkPaused) 94 require.NoError(t, err) 95 96 blockServer := config.BlockServer() 97 98 uid1 := keybase1.MakeTestUID(1) 99 bCtx := kbfsblock.MakeFirstContext( 100 uid1.AsUserOrTeam(), keybase1.BlockType_DATA) 101 data := []byte{1, 2, 3, 4} 102 bID, err := kbfsblock.MakePermanentID( 103 data, kbfscrypto.EncryptionSecretboxWithKeyNonce) 104 require.NoError(t, err) 105 106 // Put a block. 107 serverHalf, err := kbfscrypto.MakeRandomBlockCryptKeyServerHalf() 108 require.NoError(t, err) 109 err = blockServer.Put( 110 ctx, tlfID, bID, bCtx, data, serverHalf, DiskBlockAnyCache) 111 require.NoError(t, err) 112 113 // Now get the same block back. 114 buf, key, err := blockServer.Get(ctx, tlfID, bID, bCtx, DiskBlockAnyCache) 115 require.NoError(t, err) 116 require.Equal(t, data, buf) 117 require.Equal(t, serverHalf, key) 118 119 // Add a reference. 120 uid2 := keybase1.MakeTestUID(2) 121 nonce, err := kbfsblock.MakeRefNonce() 122 require.NoError(t, err) 123 bCtx2 := kbfsblock.MakeContext( 124 uid1.AsUserOrTeam(), uid2.AsUserOrTeam(), nonce, 125 keybase1.BlockType_DATA) 126 err = blockServer.AddBlockReference(ctx, tlfID, bID, bCtx2) 127 require.NoError(t, err) 128 129 // Now get the same block back. 130 buf, key, err = blockServer.Get(ctx, tlfID, bID, bCtx2, DiskBlockAnyCache) 131 require.NoError(t, err) 132 require.Equal(t, data, buf) 133 require.Equal(t, serverHalf, key) 134 }