github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/data/path_part_string_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 data
     6  
     7  import (
     8  	"encoding/json"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/keybase/client/go/libkb"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPathPartStringBasic(t *testing.T) {
    17  	secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4})
    18  	no := NewNodeObfuscator(secret)
    19  
    20  	t.Log("Check String() without extensions")
    21  	pps := NewPathPartString("test", no)
    22  	ob := pps.String()
    23  	words := strings.Split(ob, separator)
    24  	require.Len(t, words, 2)
    25  	require.True(t, libkb.ValidSecWord(words[0]), words[0])
    26  	require.True(t, libkb.ValidSecWord(words[1]), words[1])
    27  
    28  	t.Log("Check String() with short extension")
    29  	pps2 := NewPathPartString("test.txt", no)
    30  	ob2 := pps2.String()
    31  	require.True(t, strings.HasSuffix(ob2, ".txt"))
    32  	words2 := strings.Split(strings.Replace(ob2, ".txt", "", 1), separator)
    33  	require.Len(t, words2, 2)
    34  	require.True(t, libkb.ValidSecWord(words2[0]), words2[0])
    35  	require.True(t, libkb.ValidSecWord(words2[1]), words2[1])
    36  	t.Log("Make sure that the words aren't the same")
    37  	require.False(t, words[0] == words2[0] && words[1] == words2[1])
    38  
    39  	t.Log("Check String() with longer, but still valid, extension")
    40  	pps3 := NewPathPartString("test.tar.gz", no)
    41  	ob3 := pps3.String()
    42  	require.True(t, strings.HasSuffix(ob3, ".tar.gz"))
    43  	words3 := strings.Split(strings.Replace(ob3, ".tar.gz", "", 1), separator)
    44  	require.Len(t, words3, 2)
    45  	require.True(t, libkb.ValidSecWord(words3[0]), words3[0])
    46  	require.True(t, libkb.ValidSecWord(words3[1]), words3[1])
    47  
    48  	t.Log("Check String() with a too-long extension")
    49  	pps4 := NewPathPartString("test.longextension", no)
    50  	ob4 := pps4.String()
    51  	require.False(t, strings.HasSuffix(ob4, ".longextension"))
    52  	words4 := strings.Split(ob4, separator)
    53  	require.Len(t, words4, 2)
    54  	require.True(t, libkb.ValidSecWord(words4[0]), words4[0])
    55  	require.True(t, libkb.ValidSecWord(words4[1]), words4[1])
    56  
    57  	t.Log("Check String() with a conflicted suffix")
    58  	cSuffix := ".conflicted (alice's device copy 2019-06-10).txt"
    59  	pps5 := NewPathPartString("test"+cSuffix, no)
    60  	ob5 := pps5.String()
    61  	require.True(t, strings.HasSuffix(ob5, cSuffix))
    62  	words5 := strings.Split(strings.Replace(ob5, cSuffix, "", 1), separator)
    63  	require.Len(t, words5, 2)
    64  	require.Equal(t, words2[0], words5[0])
    65  	require.Equal(t, words2[1], words5[1])
    66  }
    67  
    68  func TestPathPartStringEquality(t *testing.T) {
    69  	secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4})
    70  	no := NewNodeObfuscator(secret)
    71  
    72  	t.Log("Check equal PathPartStrings")
    73  	pps1 := NewPathPartString("test", no)
    74  	pps2 := NewPathPartString("test", no)
    75  	require.True(t, pps1 == pps2)
    76  
    77  	t.Log("Check as a map key")
    78  	pps3 := NewPathPartString("test2", no)
    79  	val1 := 50
    80  	val3 := 100
    81  	m := map[PathPartString]int{
    82  		pps1: val1,
    83  		pps3: val3,
    84  	}
    85  	require.Equal(t, val1, m[pps1])
    86  	require.Equal(t, val1, m[pps2])
    87  	require.Equal(t, val3, m[pps3])
    88  
    89  	t.Log("Check unequal PathPartStrings with different strings")
    90  	pps1 = NewPathPartString("test", no)
    91  	pps2 = NewPathPartString("test2", no)
    92  	require.False(t, pps1 == pps2)
    93  
    94  	t.Log("Check unequal PathPartStrings with different obfuscators")
    95  	no2 := NewNodeObfuscator(secret)
    96  	pps1 = NewPathPartString("test", no)
    97  	pps2 = NewPathPartString("test", no2)
    98  	require.False(t, pps1 == pps2)
    99  }
   100  
   101  func TestPathPartStringPrefix(t *testing.T) {
   102  	secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4})
   103  	no := NewNodeObfuscator(secret)
   104  
   105  	t.Log("Check that special .kbfs_ files are ignored")
   106  	status := ".kbfs_status"
   107  	pps1 := NewPathPartString(status, no)
   108  	ob1 := pps1.String()
   109  	require.Equal(t, status, ob1)
   110  
   111  	t.Log("Check that .kbfs_fileinfo files are still partially obfuscated")
   112  	fileinfo := ".kbfs_fileinfo_test.txt"
   113  	pps2 := NewPathPartString(fileinfo, no)
   114  	ob2 := pps2.String()
   115  	require.True(t, strings.HasPrefix(ob2, ".kbfs_fileinfo_"))
   116  	require.True(t, strings.HasSuffix(ob2, ".txt"))
   117  	words2 := strings.Split(
   118  		strings.TrimSuffix(strings.TrimPrefix(ob2, ".kbfs_fileinfo_"), ".txt"),
   119  		separator)
   120  	require.Len(t, words2, 2)
   121  	require.True(t, libkb.ValidSecWord(words2[0]), words2[0])
   122  	require.True(t, libkb.ValidSecWord(words2[1]), words2[1])
   123  
   124  	t.Log("Check file info with a conflicted suffix")
   125  	cSuffix := ".conflicted (alice's device copy 2019-06-10).txt"
   126  	pps3 := NewPathPartString(".kbfs_fileinfo_test"+cSuffix, no)
   127  	ob3 := pps3.String()
   128  	require.True(t, strings.HasPrefix(ob3, ".kbfs_fileinfo_"))
   129  	require.True(t, strings.HasSuffix(ob3, cSuffix))
   130  	words3 := strings.Split(
   131  		strings.TrimSuffix(strings.TrimPrefix(ob3, ".kbfs_fileinfo_"), cSuffix),
   132  		separator)
   133  	require.Len(t, words3, 2)
   134  	require.Equal(t, words2[0], words3[0])
   135  	require.Equal(t, words2[1], words3[1])
   136  }
   137  
   138  func TestPathPartStringMarshal(t *testing.T) {
   139  	secret := NodeObfuscatorSecret([]byte{1, 2, 3, 4})
   140  	no := NewNodeObfuscator(secret)
   141  	pps := NewPathPartString("test", no)
   142  	require.Panics(t, func() {
   143  		_, _ = json.Marshal(pps)
   144  	})
   145  
   146  	type m struct {
   147  		PPS   PathPartString
   148  		Other string
   149  	}
   150  	m1 := m{pps, "test"}
   151  	require.Panics(t, func() {
   152  		_, _ = json.Marshal(m1)
   153  	})
   154  }