github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/block_util_test.go (about)

     1  package libkbfs
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/keybase/client/go/kbfs/data"
     9  	"github.com/keybase/client/go/kbfs/kbfsblock"
    10  	"github.com/keybase/client/go/kbfs/tlf"
    11  	"github.com/stretchr/testify/require"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  func blockUtilInit(t *testing.T) (mockCtrl *gomock.Controller,
    16  	ctr *SafeTestReporter, bserver *MockBlockServer, ctx context.Context) {
    17  	ctr = NewSafeTestReporter(t)
    18  	mockCtrl = gomock.NewController(ctr)
    19  	bserver = NewMockBlockServer(mockCtrl)
    20  	ctx = context.Background()
    21  	return mockCtrl, ctr, bserver, ctx
    22  }
    23  
    24  func blockUtilShutdown(mockCtrl *gomock.Controller, ctr *SafeTestReporter) {
    25  	ctr.CheckForFailures()
    26  	mockCtrl.Finish()
    27  }
    28  
    29  func TestBlockUtilPutNewBlockSuccess(t *testing.T) {
    30  	mockCtrl, ctr, bserver, ctx := blockUtilInit(t)
    31  	defer blockUtilShutdown(mockCtrl, ctr)
    32  
    33  	// expect one call to put a block
    34  	id := kbfsblock.FakeID(1)
    35  	encData := []byte{1, 2, 3, 4}
    36  	blockPtr := data.BlockPointer{ID: id}
    37  
    38  	tlfID := tlf.FakeID(1, tlf.Private)
    39  
    40  	readyBlockData := data.ReadyBlockData{
    41  		Buf: encData,
    42  	}
    43  
    44  	bserver.EXPECT().Put(ctx, tlfID, id, blockPtr.Context,
    45  		readyBlockData.Buf, readyBlockData.ServerHalf, gomock.Any()).Return(nil)
    46  
    47  	err := putBlockToServer(
    48  		ctx, bserver, tlfID, blockPtr, readyBlockData, DiskBlockAnyCache)
    49  	require.NoError(t, err)
    50  }
    51  
    52  func TestBlockUtilPutIncRefSuccess(t *testing.T) {
    53  	mockCtrl, ctr, bserver, ctx := blockUtilInit(t)
    54  	defer blockUtilShutdown(mockCtrl, ctr)
    55  
    56  	// expect one call to put a block
    57  	id := kbfsblock.FakeID(1)
    58  	encData := []byte{1, 2, 3, 4}
    59  	nonce := kbfsblock.RefNonce([8]byte{1, 2, 3, 4, 5, 6, 7, 8})
    60  	blockPtr := data.BlockPointer{
    61  		ID: id,
    62  		Context: kbfsblock.Context{
    63  			RefNonce: nonce,
    64  		},
    65  	}
    66  
    67  	tlfID := tlf.FakeID(0, tlf.Private)
    68  
    69  	readyBlockData := data.ReadyBlockData{
    70  		Buf: encData,
    71  	}
    72  
    73  	bserver.EXPECT().AddBlockReference(ctx, tlfID, id,
    74  		blockPtr.Context).Return(nil)
    75  
    76  	err := putBlockToServer(
    77  		ctx, bserver, tlfID, blockPtr, readyBlockData, DiskBlockAnyCache)
    78  	require.NoError(t, err)
    79  }
    80  
    81  func TestBlockUtilPutFail(t *testing.T) {
    82  	mockCtrl, ctr, bserver, ctx := blockUtilInit(t)
    83  	defer blockUtilShutdown(mockCtrl, ctr)
    84  
    85  	// fail the put call
    86  	id := kbfsblock.FakeID(1)
    87  	encData := []byte{1, 2, 3, 4}
    88  	blockPtr := data.BlockPointer{ID: id}
    89  
    90  	expectedErr := errors.New("Fake fail")
    91  
    92  	tlfID := tlf.FakeID(1, tlf.Private)
    93  
    94  	readyBlockData := data.ReadyBlockData{
    95  		Buf: encData,
    96  	}
    97  
    98  	bserver.EXPECT().Put(
    99  		ctx, tlfID, id, blockPtr.Context, readyBlockData.Buf,
   100  		readyBlockData.ServerHalf, gomock.Any()).Return(expectedErr)
   101  
   102  	err := putBlockToServer(
   103  		ctx, bserver, tlfID, blockPtr, readyBlockData, DiskBlockAnyCache)
   104  	require.Equal(t, expectedErr, err)
   105  }