github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/tlf/handle_extension_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 tlf 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/keybase/client/go/kbfs/kbfscodec" 12 "github.com/keybase/go-codec/codec" 13 ) 14 15 func TestHandleExtension(t *testing.T) { 16 codec := kbfscodec.NewMsgpack() 17 for _, et := range []HandleExtensionType{ 18 HandleExtensionConflict, 19 HandleExtensionFinalized, 20 } { 21 e, err := NewHandleExtension(et, 1, "alice", time.Now()) 22 if err != nil { 23 t.Fatal(err) 24 } 25 exts, err := ParseHandleExtensionSuffix(e.String()) 26 if err != nil { 27 t.Fatal(err) 28 } 29 if len(exts) != 1 { 30 t.Fatalf("Expected 1 extension, got: %d", len(exts)) 31 } 32 // check that extensions can be encoded/decoded 33 buf, err := codec.Encode(exts[0]) 34 if err != nil { 35 t.Fatal(err) 36 } 37 var e2 HandleExtension 38 err = codec.Decode(buf, &e2) 39 if err != nil { 40 t.Fatal(err) 41 } 42 if e2.Number != e.Number { 43 t.Fatalf("Expected %d, got: %d", e.Number, e2.Number) 44 } 45 if e2.Date != e.Date { 46 t.Fatalf("Expected %d, got: %d", e.Date, e2.Date) 47 } 48 if e2.String() != e.String() { 49 t.Fatalf("Expected %s, got: %s", e, e2) 50 } 51 if e.Type == HandleExtensionConflict { 52 if e2.Username != "" { 53 t.Fatalf("Expected empty username got: %s", e2.Username) 54 } 55 continue 56 } 57 if e2.Username != e.Username { 58 t.Fatalf("Expected %s, got: %s", e.Username, e2.Username) 59 } 60 } 61 } 62 63 func TestHandleExtensionNumber(t *testing.T) { 64 for _, et := range []HandleExtensionType{ 65 HandleExtensionConflict, 66 HandleExtensionFinalized, 67 } { 68 e, err := NewHandleExtension(et, 2, "bob", time.Now()) 69 if err != nil { 70 t.Fatal(err) 71 } 72 exts, err := ParseHandleExtensionSuffix(e.String()) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if len(exts) != 1 { 77 t.Fatalf("Expected 1 extension, got: %d", len(exts)) 78 } 79 e2 := exts[0] 80 if e2.Number != e.Number { 81 t.Fatalf("Expected %d, got: %d", e.Number, e2.Number) 82 } 83 if e2.Date != e.Date { 84 t.Fatalf("Expected %d, got: %d", e.Date, e2.Date) 85 } 86 if e2.String() != e.String() { 87 t.Fatalf("Expected %s, got: %s", e, e2) 88 } 89 if e.Type == HandleExtensionConflict { 90 continue 91 } 92 if e2.Username != e.Username { 93 t.Fatalf("Expected %s, got: %s", e.Username, e2.Username) 94 } 95 } 96 } 97 98 func TestHandleExtensionKnownTime(t *testing.T) { 99 e := &HandleExtension{ 100 Date: 1462838400, 101 Number: 1, 102 Type: HandleExtensionFinalized, 103 Username: "alice", 104 } 105 expect := "(files before alice account reset 2016-05-10)" 106 if e.String() != expect { 107 t.Fatalf("Expected %s, got: %s", expect, e) 108 } 109 e2 := &HandleExtension{ 110 Date: 1462838400, 111 Number: 12345, 112 Type: HandleExtensionConflict, 113 } 114 expect = "(conflicted copy 2016-05-10 #12345)" 115 if e2.String() != expect { 116 t.Fatalf("Expected %s, got: %s", expect, e2) 117 } 118 e3 := &HandleExtension{ 119 Date: 1462838400, 120 Number: 2, 121 Type: HandleExtensionFinalized, 122 } 123 expect = "(files before account reset 2016-05-10 #2)" 124 if e3.String() != expect { 125 t.Fatalf("Expected %s, got: %s", expect, e3) 126 } 127 } 128 129 func TestHandleExtensionErrors(t *testing.T) { 130 _, err := NewHandleExtension(HandleExtensionConflict, 0, "", time.Now()) 131 if err != errHandleExtensionInvalidNumber { 132 t.Fatalf("Expected errHandleExtensionInvalidNumber, got: %v", err) 133 } 134 _, err = ParseHandleExtensionSuffix("(conflicted copy 2016-05-10 #0)") 135 if err != errHandleExtensionInvalidNumber { 136 t.Fatalf("Expected errHandleExtensionInvalidNumber, got: %v", err) 137 } 138 _, err = ParseHandleExtensionSuffix("nope") 139 if err != errHandleExtensionInvalidString { 140 t.Fatalf("Expected errHandleExtensionInvalidString, got: %v", err) 141 } 142 _, err = ParseHandleExtensionSuffix("(conflicted copy #2)") 143 if err != errHandleExtensionInvalidString { 144 t.Fatalf("Expected errHandleExtensionInvalidString, got: %v", err) 145 } 146 _, err = ParseHandleExtensionSuffix("(conflicted copy 2016-05-10 #)") 147 if err != errHandleExtensionInvalidString { 148 t.Fatalf("Expected errHandleExtensionInvalidString, got: %v", err) 149 } 150 } 151 152 type tlfHandleExtensionFuture struct { 153 HandleExtension 154 kbfscodec.Extra 155 } 156 157 func (ci tlfHandleExtensionFuture) ToCurrentStruct() kbfscodec.CurrentStruct { 158 return ci.HandleExtension 159 } 160 161 func TestHandleExtensionUnknownFields(t *testing.T) { 162 cFuture := kbfscodec.NewMsgpack() 163 cCurrent := kbfscodec.NewMsgpack() 164 cCurrentKnownOnly := kbfscodec.NewMsgpackNoUnknownFields() 165 kbfscodec.TestStructUnknownFields(t, 166 cFuture, cCurrent, cCurrentKnownOnly, 167 tlfHandleExtensionFuture{ 168 HandleExtension{ 169 time.Now().UTC().Unix(), 170 2, 171 HandleExtensionFinalized, 172 "", 173 codec.UnknownFieldSetHandler{}, 174 }, 175 kbfscodec.MakeExtraOrBust("HandleExtension", t), 176 }) 177 } 178 179 func TestHandleExtensionMultiple(t *testing.T) { 180 e, err := NewTestHandleExtensionStaticTime(HandleExtensionConflict, 1, "") 181 if err != nil { 182 t.Fatal(err) 183 } 184 e2, err := NewTestHandleExtensionStaticTime(HandleExtensionFinalized, 2, "charlie") 185 if err != nil { 186 t.Fatal(err) 187 } 188 exts := []HandleExtension{*e, *e2} 189 suffix := newHandleExtensionSuffix(exts, false) 190 expectSuffix := " (conflicted copy 2016-03-14) (files before charlie account reset 2016-03-14 #2)" 191 if suffix != expectSuffix { 192 t.Fatalf("Expected suffix '%s', got: '%s'", expectSuffix, suffix) 193 } 194 exts2, err := ParseHandleExtensionSuffix(suffix) 195 if err != nil { 196 t.Fatal(err) 197 } 198 if len(exts2) != 2 { 199 t.Fatalf("Expected 2 extensions, got: %d", len(exts2)) 200 } 201 for i, ext := range exts2 { 202 if ext.Number != exts[i].Number { 203 t.Fatalf("Expected %d, got: %d", exts[i].Number, ext.Number) 204 } 205 if ext.Date != exts[i].Date { 206 t.Fatalf("Expected %d, got: %d", exts[i].Date, ext.Date) 207 } 208 if ext.Username != exts[i].Username { 209 t.Fatalf("Expected %s, got: %s", exts[i].Username, ext.Username) 210 } 211 if ext.String() != exts[i].String() { 212 t.Fatalf("Expected %s, got: %s", ext, exts[i]) 213 } 214 } 215 } 216 217 func TestHandleExtensionMultipleSingleUser(t *testing.T) { 218 e, err := NewTestHandleExtensionStaticTime(HandleExtensionConflict, 2, "") 219 if err != nil { 220 t.Fatal(err) 221 } 222 e2, err := NewTestHandleExtensionStaticTime(HandleExtensionFinalized, 1, "") 223 if err != nil { 224 t.Fatal(err) 225 } 226 exts := []HandleExtension{*e, *e2} 227 suffix := newHandleExtensionSuffix(exts, false) 228 expectSuffix := " (conflicted copy 2016-03-14 #2) (files before account reset 2016-03-14)" 229 if suffix != expectSuffix { 230 t.Fatalf("Expected suffix '%s', got: '%s'", expectSuffix, suffix) 231 } 232 exts2, err := ParseHandleExtensionSuffix(suffix) 233 if err != nil { 234 t.Fatal(err) 235 } 236 if len(exts2) != 2 { 237 t.Fatalf("Expected 2 extensions, got: %d", len(exts2)) 238 } 239 for i, ext := range exts2 { 240 if ext.Number != exts[i].Number { 241 t.Fatalf("Expected %d, got: %d", exts[i].Number, ext.Number) 242 } 243 if ext.Date != exts[i].Date { 244 t.Fatalf("Expected %d, got: %d", exts[i].Date, ext.Date) 245 } 246 if ext.Username != exts[i].Username { 247 t.Fatalf("Expected %s, got: %s", exts[i].Username, ext.Username) 248 } 249 if ext.String() != exts[i].String() { 250 t.Fatalf("Expected %s, got: %s", ext, exts[i]) 251 } 252 } 253 }