@@ -560,3 +560,135 @@ func TestACL_DeleteEntry(t *testing.T) {
560560 })
561561 }
562562}
563+
564+ func TestGetEntry (t * testing.T ) {
565+ tests := []struct {
566+ name string
567+ setupFunc func (* ACL )
568+ searchTag Tag
569+ searchID uint32
570+ expectNil bool
571+ expectPerm uint16
572+ }{
573+ {
574+ name : "get existing user entry" ,
575+ setupFunc : func (a * ACL ) {
576+ a .entries = append (a .entries , NewEntry (TAG_ACL_USER , 1000 , 6 ))
577+ },
578+ searchTag : TAG_ACL_USER ,
579+ searchID : 1000 ,
580+ expectNil : false ,
581+ expectPerm : 6 ,
582+ },
583+ {
584+ name : "get existing group entry" ,
585+ setupFunc : func (a * ACL ) {
586+ a .entries = append (a .entries , NewEntry (TAG_ACL_GROUP , 2000 , 7 ))
587+ },
588+ searchTag : TAG_ACL_GROUP ,
589+ searchID : 2000 ,
590+ expectNil : false ,
591+ expectPerm : 7 ,
592+ },
593+ {
594+ name : "get non-existing entry returns nil" ,
595+ setupFunc : func (a * ACL ) {
596+ a .entries = append (a .entries , NewEntry (TAG_ACL_USER , 1000 , 6 ))
597+ },
598+ searchTag : TAG_ACL_USER ,
599+ searchID : 5000 ,
600+ expectNil : true ,
601+ },
602+ {
603+ name : "get from empty ACL returns nil" ,
604+ setupFunc : func (a * ACL ) {
605+ // no entries added
606+ },
607+ searchTag : TAG_ACL_USER ,
608+ searchID : 1000 ,
609+ expectNil : true ,
610+ },
611+ {
612+ name : "get user entry among multiple different tag types" ,
613+ setupFunc : func (a * ACL ) {
614+ a .entries = append (a .entries ,
615+ NewEntry (TAG_ACL_USER_OBJ , math .MaxUint32 , 7 ),
616+ NewEntry (TAG_ACL_USER , 1000 , 6 ),
617+ NewEntry (TAG_ACL_GROUP , 2000 , 5 ),
618+ NewEntry (TAG_ACL_MASK , math .MaxUint32 , 7 ),
619+ NewEntry (TAG_ACL_OTHER , math .MaxUint32 , 0 ),
620+ )
621+ },
622+ searchTag : TAG_ACL_USER ,
623+ searchID : 1000 ,
624+ expectNil : false ,
625+ expectPerm : 6 ,
626+ },
627+ {
628+ name : "get group entry among multiple different tag types" ,
629+ setupFunc : func (a * ACL ) {
630+ a .entries = append (a .entries ,
631+ NewEntry (TAG_ACL_USER_OBJ , math .MaxUint32 , 7 ),
632+ NewEntry (TAG_ACL_USER , 1000 , 6 ),
633+ NewEntry (TAG_ACL_GROUP , 2000 , 5 ),
634+ NewEntry (TAG_ACL_MASK , math .MaxUint32 , 7 ),
635+ NewEntry (TAG_ACL_OTHER , math .MaxUint32 , 0 ),
636+ )
637+ },
638+ searchTag : TAG_ACL_GROUP ,
639+ searchID : 2000 ,
640+ expectNil : false ,
641+ expectPerm : 5 ,
642+ },
643+ {
644+ name : "get user from multiple user entries with different IDs" ,
645+ setupFunc : func (a * ACL ) {
646+ a .entries = append (a .entries ,
647+ NewEntry (TAG_ACL_USER , 1000 , 6 ),
648+ NewEntry (TAG_ACL_USER , 1001 , 7 ),
649+ NewEntry (TAG_ACL_USER , 1002 , 5 ),
650+ NewEntry (TAG_ACL_GROUP , 2000 , 4 ),
651+ )
652+ },
653+ searchTag : TAG_ACL_USER ,
654+ searchID : 1001 ,
655+ expectNil : false ,
656+ expectPerm : 7 ,
657+ },
658+ {
659+ name : "get group from multiple group entries with different IDs" ,
660+ setupFunc : func (a * ACL ) {
661+ a .entries = append (a .entries ,
662+ NewEntry (TAG_ACL_GROUP , 2000 , 6 ),
663+ NewEntry (TAG_ACL_GROUP , 2001 , 7 ),
664+ NewEntry (TAG_ACL_GROUP , 2002 , 5 ),
665+ NewEntry (TAG_ACL_USER , 1000 , 4 ),
666+ )
667+ },
668+ searchTag : TAG_ACL_GROUP ,
669+ searchID : 2002 ,
670+ expectNil : false ,
671+ expectPerm : 5 ,
672+ },
673+ }
674+
675+ for _ , tt := range tests {
676+ t .Run (tt .name , func (t * testing.T ) {
677+ acl := & ACL {version : 2 }
678+ tt .setupFunc (acl )
679+
680+ searchEntry := NewEntry (tt .searchTag , tt .searchID , 0 )
681+ result := acl .GetEntry (searchEntry )
682+
683+ if tt .expectNil && result != nil {
684+ t .Errorf ("expected nil, got %v" , result )
685+ }
686+ if ! tt .expectNil && result == nil {
687+ t .Errorf ("expected entry, got nil" )
688+ }
689+ if ! tt .expectNil && result .perm != tt .expectPerm {
690+ t .Errorf ("expected perm %d, got %d" , tt .expectPerm , result .perm )
691+ }
692+ })
693+ }
694+ }
0 commit comments