github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/tlf/handle_test.go (about) 1 package tlf 2 3 import ( 4 "testing" 5 6 "github.com/keybase/client/go/protocol/keybase1" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestMakeHandle(t *testing.T) { 12 w := []keybase1.UserOrTeamID{ 13 keybase1.MakeTestUID(4).AsUserOrTeam(), 14 keybase1.MakeTestUID(3).AsUserOrTeam(), 15 } 16 17 r := []keybase1.UserOrTeamID{ 18 keybase1.MakeTestUID(5).AsUserOrTeam(), 19 keybase1.MakeTestUID(1).AsUserOrTeam(), 20 } 21 22 uw := []keybase1.SocialAssertion{ 23 { 24 User: "user2", 25 Service: "service3", 26 }, 27 { 28 User: "user1", 29 Service: "service1", 30 }, 31 } 32 33 ur := []keybase1.SocialAssertion{ 34 { 35 User: "user5", 36 Service: "service3", 37 }, 38 { 39 User: "user1", 40 Service: "service2", 41 }, 42 } 43 44 h, err := MakeHandle(w, r, uw, ur, nil) 45 require.NoError(t, err) 46 require.Equal(t, []keybase1.UserOrTeamID{ 47 keybase1.MakeTestUID(3).AsUserOrTeam(), 48 keybase1.MakeTestUID(4).AsUserOrTeam(), 49 }, h.Writers) 50 require.Equal(t, []keybase1.UserOrTeamID{ 51 keybase1.MakeTestUID(1).AsUserOrTeam(), 52 keybase1.MakeTestUID(5).AsUserOrTeam(), 53 }, h.Readers) 54 require.Equal(t, []keybase1.SocialAssertion{ 55 { 56 User: "user1", 57 Service: "service1", 58 }, 59 { 60 User: "user2", 61 Service: "service3", 62 }, 63 }, h.UnresolvedWriters) 64 require.Equal(t, []keybase1.SocialAssertion{ 65 { 66 User: "user1", 67 Service: "service2", 68 }, 69 { 70 User: "user5", 71 Service: "service3", 72 }, 73 }, h.UnresolvedReaders) 74 } 75 76 func TestMakeHandleFailures(t *testing.T) { 77 _, err := MakeHandle(nil, nil, nil, nil, nil) 78 assert.Equal(t, errNoWriters, err) 79 80 w := []keybase1.UserOrTeamID{ 81 keybase1.MakeTestUID(4).AsUserOrTeam(), 82 keybase1.MakeTestUID(3).AsUserOrTeam(), 83 } 84 85 r := []keybase1.UserOrTeamID{ 86 keybase1.UserOrTeamID(keybase1.PUBLIC_UID), 87 keybase1.MakeTestUID(2).AsUserOrTeam(), 88 } 89 90 _, err = MakeHandle(r, nil, nil, nil, nil) 91 assert.Equal(t, errInvalidWriter, err) 92 93 _, err = MakeHandle(w, r, nil, nil, nil) 94 assert.Equal(t, errInvalidReader, err) 95 96 ur := []keybase1.SocialAssertion{ 97 { 98 User: "user5", 99 Service: "service3", 100 }, 101 } 102 103 _, err = MakeHandle(w, r[:1], nil, ur, nil) 104 assert.Equal(t, errInvalidReader, err) 105 106 // Any handle with a team must be a single-team handle. 107 w = []keybase1.UserOrTeamID{ 108 keybase1.MakeTestTeamID(3, false).AsUserOrTeam(), 109 keybase1.MakeTestTeamID(4, false).AsUserOrTeam(), 110 } 111 r = []keybase1.UserOrTeamID{} 112 _, err = MakeHandle(w, r, nil, nil, nil) 113 assert.Equal(t, errInvalidWriter, err) 114 115 w = []keybase1.UserOrTeamID{ 116 keybase1.MakeTestUID(4).AsUserOrTeam(), 117 keybase1.MakeTestTeamID(3, false).AsUserOrTeam(), 118 } 119 r = []keybase1.UserOrTeamID{} 120 _, err = MakeHandle(w, r, nil, nil, nil) 121 assert.Equal(t, errInvalidWriter, err) 122 123 w = []keybase1.UserOrTeamID{ 124 keybase1.MakeTestTeamID(3, false).AsUserOrTeam(), 125 keybase1.MakeTestUID(4).AsUserOrTeam(), 126 } 127 r = []keybase1.UserOrTeamID{} 128 _, err = MakeHandle(w, r, nil, nil, nil) 129 assert.Equal(t, errInvalidWriter, err) 130 131 w = []keybase1.UserOrTeamID{ 132 keybase1.MakeTestTeamID(3, false).AsUserOrTeam(), 133 } 134 r = []keybase1.UserOrTeamID{ 135 keybase1.MakeTestUID(4).AsUserOrTeam(), 136 } 137 _, err = MakeHandle(w, r, nil, nil, nil) 138 assert.Equal(t, errInvalidReader, err) 139 140 w = []keybase1.UserOrTeamID{ 141 keybase1.MakeTestUID(4).AsUserOrTeam(), 142 keybase1.MakeTestUID(5).AsUserOrTeam(), 143 } 144 r = []keybase1.UserOrTeamID{ 145 keybase1.MakeTestTeamID(3, false).AsUserOrTeam(), 146 } 147 _, err = MakeHandle(w, r, nil, nil, nil) 148 assert.Equal(t, errInvalidReader, err) 149 } 150 151 func TestHandleAccessorsPrivate(t *testing.T) { 152 w := []keybase1.UserOrTeamID{ 153 keybase1.MakeTestUID(4).AsUserOrTeam(), 154 keybase1.MakeTestUID(3).AsUserOrTeam(), 155 } 156 157 r := []keybase1.UserOrTeamID{ 158 keybase1.MakeTestUID(5).AsUserOrTeam(), 159 keybase1.MakeTestUID(1).AsUserOrTeam(), 160 } 161 162 uw := []keybase1.SocialAssertion{ 163 { 164 User: "user2", 165 Service: "service3", 166 }, 167 { 168 User: "user1", 169 Service: "service1", 170 }, 171 } 172 173 ur := []keybase1.SocialAssertion{ 174 { 175 User: "user5", 176 Service: "service3", 177 }, 178 { 179 User: "user1", 180 Service: "service2", 181 }, 182 } 183 184 h, err := MakeHandle(w, r, uw, ur, nil) 185 require.NoError(t, err) 186 187 require.Equal(t, Private, h.Type()) 188 189 for _, u := range w { 190 require.True(t, h.IsWriter(u)) 191 require.True(t, h.IsReader(u)) 192 } 193 194 for _, u := range r { 195 require.False(t, h.IsWriter(u)) 196 require.True(t, h.IsReader(u)) 197 } 198 199 for i := 6; i < 10; i++ { 200 u := keybase1.MakeTestUID(uint32(i)).AsUserOrTeam() 201 require.False(t, h.IsWriter(u)) 202 require.False(t, h.IsReader(u)) 203 } 204 205 require.Equal(t, h.ResolvedUsers(), 206 []keybase1.UserOrTeamID{ 207 keybase1.MakeTestUID(3).AsUserOrTeam(), 208 keybase1.MakeTestUID(4).AsUserOrTeam(), 209 keybase1.MakeTestUID(1).AsUserOrTeam(), 210 keybase1.MakeTestUID(5).AsUserOrTeam(), 211 }) 212 require.True(t, h.HasUnresolvedUsers()) 213 require.Equal(t, h.UnresolvedUsers(), 214 []keybase1.SocialAssertion{ 215 { 216 User: "user1", 217 Service: "service1", 218 }, 219 { 220 User: "user2", 221 Service: "service3", 222 }, 223 { 224 User: "user1", 225 Service: "service2", 226 }, 227 { 228 User: "user5", 229 Service: "service3", 230 }, 231 }) 232 } 233 234 func TestHandleAccessorsPublic(t *testing.T) { 235 w := []keybase1.UserOrTeamID{ 236 keybase1.MakeTestUID(4).AsUserOrTeam(), 237 keybase1.MakeTestUID(3).AsUserOrTeam(), 238 } 239 240 uw := []keybase1.SocialAssertion{ 241 { 242 User: "user2", 243 Service: "service3", 244 }, 245 { 246 User: "user1", 247 Service: "service1", 248 }, 249 } 250 251 h, err := MakeHandle( 252 w, []keybase1.UserOrTeamID{keybase1.UserOrTeamID(keybase1.PUBLIC_UID)}, 253 uw, nil, nil) 254 require.NoError(t, err) 255 256 require.Equal(t, Public, h.Type()) 257 258 for _, u := range w { 259 require.True(t, h.IsWriter(u)) 260 require.True(t, h.IsReader(u)) 261 } 262 263 for i := 6; i < 10; i++ { 264 u := keybase1.MakeTestUID(uint32(i)).AsUserOrTeam() 265 require.False(t, h.IsWriter(u)) 266 require.True(t, h.IsReader(u)) 267 } 268 269 require.Equal(t, h.ResolvedUsers(), 270 []keybase1.UserOrTeamID{ 271 keybase1.MakeTestUID(3).AsUserOrTeam(), 272 keybase1.MakeTestUID(4).AsUserOrTeam(), 273 }) 274 require.True(t, h.HasUnresolvedUsers()) 275 require.Equal(t, h.UnresolvedUsers(), 276 []keybase1.SocialAssertion{ 277 { 278 User: "user1", 279 Service: "service1", 280 }, 281 { 282 User: "user2", 283 Service: "service3", 284 }, 285 }) 286 } 287 288 func TestHandleAccessorsSingleTeam(t *testing.T) { 289 w := []keybase1.UserOrTeamID{ 290 keybase1.MakeTestTeamID(4, false).AsUserOrTeam(), 291 } 292 293 h, err := MakeHandle( 294 w, nil, nil, nil, nil) 295 require.NoError(t, err) 296 297 require.Equal(t, SingleTeam, h.Type()) 298 299 // All types of IsWriter/IsReader calls should panic for team TLFs. 300 for _, u := range w { 301 require.Panics(t, func() { h.IsWriter(u) }) 302 require.Panics(t, func() { h.IsReader(u) }) 303 } 304 305 u := keybase1.MakeTestUID(uint32(6)).AsUserOrTeam() 306 require.Panics(t, func() { h.IsWriter(u) }) 307 require.Panics(t, func() { h.IsReader(u) }) 308 309 require.False(t, h.HasUnresolvedUsers()) 310 } 311 312 func TestHandleHasUnresolvedUsers(t *testing.T) { 313 w := []keybase1.UserOrTeamID{ 314 keybase1.MakeTestUID(4).AsUserOrTeam(), 315 keybase1.MakeTestUID(3).AsUserOrTeam(), 316 } 317 318 uw := []keybase1.SocialAssertion{ 319 { 320 User: "user2", 321 Service: "service3", 322 }, 323 { 324 User: "user1", 325 Service: "service1", 326 }, 327 } 328 329 ur := []keybase1.SocialAssertion{ 330 { 331 User: "user5", 332 Service: "service3", 333 }, 334 { 335 User: "user1", 336 Service: "service2", 337 }, 338 } 339 340 h, err := MakeHandle(w, nil, uw, ur, nil) 341 require.NoError(t, err) 342 require.True(t, h.HasUnresolvedUsers()) 343 344 uw = h.UnresolvedWriters 345 h.UnresolvedWriters = nil 346 require.True(t, h.HasUnresolvedUsers()) 347 348 h.UnresolvedReaders = nil 349 require.False(t, h.HasUnresolvedUsers()) 350 351 h.UnresolvedWriters = uw 352 require.True(t, h.HasUnresolvedUsers()) 353 } 354 355 func TestHandleResolveAssertions(t *testing.T) { 356 w := []keybase1.UserOrTeamID{ 357 keybase1.MakeTestUID(4).AsUserOrTeam(), 358 keybase1.MakeTestUID(3).AsUserOrTeam(), 359 } 360 361 r := []keybase1.UserOrTeamID{ 362 keybase1.MakeTestUID(5).AsUserOrTeam(), 363 keybase1.MakeTestUID(1).AsUserOrTeam(), 364 } 365 366 uw := []keybase1.SocialAssertion{ 367 { 368 User: "user2", 369 Service: "service3", 370 }, 371 { 372 User: "user7", 373 Service: "service2", 374 }, 375 { 376 User: "user1", 377 Service: "service1", 378 }, 379 } 380 381 ur := []keybase1.SocialAssertion{ 382 { 383 User: "user6", 384 Service: "service3", 385 }, 386 { 387 User: "user8", 388 Service: "service1", 389 }, 390 { 391 User: "user5", 392 Service: "service1", 393 }, 394 { 395 User: "user1", 396 Service: "service2", 397 }, 398 { 399 User: "user9", 400 Service: "service1", 401 }, 402 { 403 User: "user9", 404 Service: "service3", 405 }, 406 } 407 408 h, err := MakeHandle(w, r, uw, ur, nil) 409 require.NoError(t, err) 410 411 assertions := make(map[keybase1.SocialAssertion]keybase1.UID) 412 assertions[uw[0]] = keybase1.MakeTestUID(2) // new writer 413 assertions[uw[2]] = keybase1.MakeTestUID(1) // reader promoted to writer 414 assertions[ur[0]] = keybase1.MakeTestUID(6) // new reader 415 assertions[ur[2]] = keybase1.MakeTestUID(5) // already a reader 416 assertions[ur[3]] = keybase1.MakeTestUID(1) // already a writer 417 assertions[ur[4]] = keybase1.MakeTestUID(9) // new reader 418 assertions[ur[5]] = keybase1.MakeTestUID(9) // already a reader 419 420 h = h.ResolveAssertions(assertions) 421 422 require.Equal(t, []keybase1.UserOrTeamID{ 423 keybase1.MakeTestUID(1).AsUserOrTeam(), 424 keybase1.MakeTestUID(2).AsUserOrTeam(), 425 keybase1.MakeTestUID(3).AsUserOrTeam(), 426 keybase1.MakeTestUID(4).AsUserOrTeam(), 427 }, h.Writers) 428 require.Equal(t, []keybase1.UserOrTeamID{ 429 keybase1.MakeTestUID(5).AsUserOrTeam(), 430 keybase1.MakeTestUID(6).AsUserOrTeam(), 431 keybase1.MakeTestUID(9).AsUserOrTeam(), 432 }, h.Readers) 433 require.Equal(t, []keybase1.SocialAssertion{ 434 { 435 User: "user7", 436 Service: "service2", 437 }, 438 }, h.UnresolvedWriters) 439 require.Equal(t, []keybase1.SocialAssertion{ 440 { 441 User: "user8", 442 Service: "service1", 443 }, 444 }, h.UnresolvedReaders) 445 }