github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libfs/util_test.go (about)

     1  // Copyright 2019 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 libfs
     6  
     7  import (
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/keybase/client/go/kbfs/libkbfs"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestDeobfuscate(t *testing.T) {
    17  	os.Setenv("KEYBASE_TEST_OBFUSCATE_LOGS", "true")
    18  	ctx, _, fs := makeFS(t, "")
    19  	defer libkbfs.CheckConfigAndShutdown(ctx, t, fs.config)
    20  
    21  	t.Log("Check basic obfuscate->deobfuscate path")
    22  	p := "a/b"
    23  	err := fs.MkdirAll(p, os.FileMode(0600))
    24  	require.NoError(t, err)
    25  	obsPath := fs.PathForLogging(p)
    26  	require.NotEqual(t, p, obsPath)
    27  	res, err := Deobfuscate(ctx, fs, obsPath)
    28  	require.NoError(t, err)
    29  	require.Len(t, res, 1)
    30  	require.Equal(t, p, res[0])
    31  
    32  	t.Log("Check conflict suffix with no extension")
    33  	res, err = Deobfuscate(ctx, fs, obsPath+"-2")
    34  	require.NoError(t, err)
    35  	require.Len(t, res, 1)
    36  	require.Equal(t, p, res[0])
    37  
    38  	t.Log("Check conflict suffix with extension")
    39  	file := "a/foo.txt"
    40  	f, err := fs.Create(file)
    41  	require.NoError(t, err)
    42  	f.Close()
    43  	obsPathFile := fs.PathForLogging(file)
    44  	require.NotEqual(t, p, obsPathFile)
    45  	res, err = Deobfuscate(ctx, fs, obsPathFile)
    46  	require.NoError(t, err)
    47  	require.Len(t, res, 1)
    48  	require.Equal(t, file, res[0])
    49  	obsPathFileSuffix := strings.Replace(obsPathFile, ".txt", "-2.txt", 1)
    50  	res, err = Deobfuscate(ctx, fs, obsPathFileSuffix)
    51  	require.NoError(t, err)
    52  	require.Len(t, res, 1)
    53  	require.Equal(t, file, res[0])
    54  
    55  	t.Log("Check symlink")
    56  	p2 := "a/c"
    57  	err = fs.Symlink("b", p2)
    58  	require.NoError(t, err)
    59  	res, err = Deobfuscate(ctx, fs, obsPath)
    60  	require.NoError(t, err)
    61  	require.Len(t, res, 2)
    62  	require.Contains(t, res, p)
    63  	require.Contains(t, res, p2+" (b)")
    64  }