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

     1  // Copyright 2018 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  	"context"
     9  
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/keybase/client/go/kbfs/kbfsblock"
    14  	"github.com/keybase/client/go/kbfs/kbfscodec"
    15  	"github.com/keybase/client/go/logger"
    16  	"github.com/pkg/errors"
    17  	"github.com/stretchr/testify/require"
    18  	ldberrors "github.com/syndtr/goleveldb/leveldb/errors"
    19  )
    20  
    21  type testBlockMetadataStoreConfig struct {
    22  	codec       kbfscodec.Codec
    23  	log         logger.Logger
    24  	storageRoot string
    25  }
    26  
    27  func (t *testBlockMetadataStoreConfig) Codec() kbfscodec.Codec {
    28  	return t.codec
    29  }
    30  func (t *testBlockMetadataStoreConfig) MakeLogger(
    31  	module string) logger.Logger {
    32  	return t.log
    33  }
    34  func (t *testBlockMetadataStoreConfig) StorageRoot() string {
    35  	return t.storageRoot
    36  }
    37  func (t *testBlockMetadataStoreConfig) Mode() InitMode {
    38  	return modeTest{modeDefault{}}
    39  }
    40  
    41  func makeBlockMetadataStoreForTest(t *testing.T) (
    42  	blockMetadataStore BlockMetadataStore, tempdir string) {
    43  	tempdir, err := os.MkdirTemp(os.TempDir(), "xattr_test")
    44  	require.NoError(t, err)
    45  	config := testBlockMetadataStoreConfig{
    46  		codec:       kbfscodec.NewMsgpack(),
    47  		log:         logger.NewTestLogger(t),
    48  		storageRoot: tempdir,
    49  	}
    50  	s, err := newDiskBlockMetadataStore(
    51  		&config, modeTest{modeDefault{}}, config.StorageRoot())
    52  	require.NoError(t, err)
    53  	return s, tempdir
    54  }
    55  
    56  func shutdownBlockMetadataStoreTest(
    57  	blockMetadataStore BlockMetadataStore, tempdir string) {
    58  	blockMetadataStore.Shutdown()
    59  	os.RemoveAll(tempdir)
    60  }
    61  
    62  func TestDiskXattr(t *testing.T) {
    63  	t.Parallel()
    64  	t.Log("Test creating disk Xattr storage")
    65  	blockMetadataStore, tempdir := makeBlockMetadataStoreForTest(t)
    66  	defer shutdownBlockMetadataStoreTest(blockMetadataStore, tempdir)
    67  
    68  	xattrStore := NewXattrStoreFromBlockMetadataStore(blockMetadataStore)
    69  
    70  	ctx := context.Background()
    71  	blockID := kbfsblock.FakeID(23)
    72  
    73  	t.Log("Test getting non-existent xattr")
    74  	_, err := xattrStore.GetXattr(
    75  		ctx, blockID, XattrAppleQuarantine)
    76  	require.Equal(t, ldberrors.ErrNotFound, errors.Cause(err))
    77  
    78  	value := []byte("rational irrationality")
    79  
    80  	t.Log("Test setting xattr")
    81  	err = xattrStore.SetXattr(
    82  		ctx, blockID, XattrAppleQuarantine, value)
    83  	require.NoError(t, err)
    84  
    85  	t.Log("Test getting xattr")
    86  	v, err := xattrStore.GetXattr(ctx, blockID, XattrAppleQuarantine)
    87  	require.NoError(t, err)
    88  	require.Equal(t, value, v)
    89  }