@@ -371,17 +371,45 @@ func TestAnyUserNotDefaultBehavior(t *testing.T) {
371371
372372 cache := NewCache ()
373373
374+ // Ensure we can't provide a nil list of Users *and* have a false AnyUser flag
374375 duration , _ := time .ParseDuration ("1h" )
375376 err = cache .AddKeyFromRecord (
376377 pr , "user" , "weakpassword" , "" ,
377378 & Usage {
378379 1 , []string {"red" , "blue" },
379- nil ,
380+ nil , // Set a nil list of users
380381 time .Now ().Add (duration ),
381382 false , // Set AnyUser flag to false
382383 },
383384 )
385+ if err == nil {
386+ t .Fatalf ("Should have seen error with Users=nil and AnyUser=false" )
387+ }
384388
389+ // Ensure we can't provide an empty list of Users either
390+ err = cache .AddKeyFromRecord (
391+ pr , "user" , "weakpassword" , "" ,
392+ & Usage {
393+ 1 , []string {"red" , "blue" },
394+ []string {}, // Set an empty list of users
395+ time .Now ().Add (duration ),
396+ false , // Set AnyUser flag to false
397+ },
398+ )
399+ if err == nil {
400+ t .Fatalf ("Should have seen error with Users=[]string{} and AnyUser=false" )
401+ }
402+
403+ // Ensure we only the specified user can decrypt when AnyUser is false
404+ err = cache .AddKeyFromRecord (
405+ pr , "user" , "weakpassword" , "" ,
406+ & Usage {
407+ 1 , []string {"red" , "blue" },
408+ []string {"alice" }, // Set a valid list of users
409+ time .Now ().Add (duration ),
410+ false , // Set AnyUser flag to false
411+ },
412+ )
385413 if err != nil {
386414 t .Fatalf ("%v" , err )
387415 }
@@ -407,4 +435,16 @@ func TestAnyUserNotDefaultBehavior(t *testing.T) {
407435 if len (cache .UserKeys ) != 1 {
408436 t .Fatalf ("Error in number of live keys %v" , cache .UserKeys )
409437 }
438+
439+ // Sanity check to make sure our user can still decrpyt
440+ _ , err = cache .DecryptKey (dummy , "user" , "alice" , []string {"red" }, pubEncryptedKey )
441+ if err != nil {
442+ t .Fatalf ("%v" , err )
443+ }
444+
445+ cache .Refresh ()
446+ if len (cache .UserKeys ) != 0 {
447+ t .Fatalf ("Error in number of live keys %v" , cache .UserKeys )
448+ }
449+
410450}
0 commit comments