-
Notifications
You must be signed in to change notification settings - Fork 246
/
subjectset_test.go
2627 lines (2394 loc) · 64.2 KB
/
subjectset_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package datasets
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/require"
"github.com/authzed/spicedb/internal/caveats"
"github.com/authzed/spicedb/internal/testutil"
v1 "github.com/authzed/spicedb/pkg/proto/dispatch/v1"
)
var (
caveatexpr = caveats.CaveatExprForTesting
sub = testutil.FoundSubject
wc = testutil.Wildcard
csub = testutil.CaveatedFoundSubject
cwc = testutil.CaveatedWildcard
wrap = testutil.WrapFoundSubject
)
func TestSubjectSetAdd(t *testing.T) {
tcs := []struct {
name string
existing []*v1.FoundSubject
toAdd *v1.FoundSubject
expectedSet []*v1.FoundSubject
}{
{
"basic add",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
sub("baz"),
[]*v1.FoundSubject{sub("foo"), sub("bar"), sub("baz")},
},
{
"basic repeated add",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
sub("bar"),
[]*v1.FoundSubject{sub("foo"), sub("bar")},
},
{
"add of an empty wildcard",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
wc(),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
},
{
"add of a wildcard with exclusions",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
wc("1", "2"),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc("1", "2")},
},
{
"add of a wildcard to a wildcard",
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
wc(),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
},
{
"add of a wildcard with exclusions to a bare wildcard",
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
wc("1", "2"),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
},
{
"add of a bare wildcard to a wildcard with exclusions",
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc("1", "2")},
wc(),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc()},
},
{
"add of a wildcard with exclusions to a wildcard with exclusions",
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc("1", "2")},
wc("2", "3"),
[]*v1.FoundSubject{sub("foo"), sub("bar"), wc("2")},
},
{
"add of a subject to a wildcard with exclusions that does not have that subject",
[]*v1.FoundSubject{wc("1", "2")},
sub("3"),
[]*v1.FoundSubject{wc("1", "2"), sub("3")},
},
{
"add of a subject to a wildcard with exclusions that has that subject",
[]*v1.FoundSubject{wc("1", "2")},
sub("2"),
[]*v1.FoundSubject{wc("1"), sub("2")},
},
{
"add of a subject to a wildcard with exclusions that has that subject",
[]*v1.FoundSubject{sub("2")},
wc("1", "2"),
[]*v1.FoundSubject{wc("1"), sub("2")},
},
{
"add of a subject to a bare wildcard",
[]*v1.FoundSubject{wc()},
sub("1"),
[]*v1.FoundSubject{wc(), sub("1")},
},
{
"add of two wildcards",
[]*v1.FoundSubject{wc("1")},
wc("2"),
[]*v1.FoundSubject{wc()},
},
{
"add of two wildcards with same restrictions",
[]*v1.FoundSubject{wc("1")},
wc("1", "2"),
[]*v1.FoundSubject{wc("1")},
},
// Tests with caveats.
{
"basic add of caveated to non-caveated",
[]*v1.FoundSubject{
csub("foo", caveatexpr("testcaveat")),
sub("bar"),
},
sub("foo"),
[]*v1.FoundSubject{sub("bar"), sub("foo")},
},
{
"basic add of non-caveated to caveated",
[]*v1.FoundSubject{
sub("foo"),
sub("bar"),
},
csub("foo", caveatexpr("testcaveat")),
[]*v1.FoundSubject{sub("bar"), sub("foo")},
},
{
"basic add of caveated to caveated",
[]*v1.FoundSubject{
csub("foo", caveatexpr("testcaveat")),
sub("bar"),
},
csub("foo", caveatexpr("anothercaveat")),
[]*v1.FoundSubject{
sub("bar"),
csub("foo", caveatOr(caveatexpr("testcaveat"), caveatexpr("anothercaveat"))),
},
},
{
"add of caveated wildcard to non-caveated",
[]*v1.FoundSubject{
cwc(caveatexpr("testcaveat")),
sub("bar"),
},
wc(),
[]*v1.FoundSubject{sub("bar"), wc()},
},
{
"add of caveated wildcard to caveated",
[]*v1.FoundSubject{
cwc(caveatexpr("testcaveat")),
sub("bar"),
},
cwc(caveatexpr("anothercaveat")),
[]*v1.FoundSubject{
sub("bar"),
cwc(caveatOr(caveatexpr("testcaveat"), caveatexpr("anothercaveat"))),
},
},
{
"add of wildcard to a caveated sub",
[]*v1.FoundSubject{
csub("bar", caveatexpr("testcaveat")),
},
wc(),
[]*v1.FoundSubject{
csub("bar", caveatexpr("testcaveat")),
wc(),
},
},
{
"add caveated sub to wildcard with non-matching caveated exclusion",
[]*v1.FoundSubject{
cwc(nil, csub("bar", caveatexpr("testcaveat"))),
},
csub("foo", caveatexpr("anothercaveat")),
[]*v1.FoundSubject{
cwc(nil, csub("bar", caveatexpr("testcaveat"))),
csub("foo", caveatexpr("anothercaveat")),
},
},
{
"add caveated sub to wildcard with matching caveated exclusion",
[]*v1.FoundSubject{
cwc(nil, csub("foo", caveatexpr("testcaveat"))),
},
csub("foo", caveatexpr("anothercaveat")),
[]*v1.FoundSubject{
// The caveat of the exclusion is now the combination of the caveats from the original
// wildcard and the concrete which was added, as the exclusion applies when the exclusion
// caveat is true and the concrete's caveat is false.
cwc(nil, csub("foo",
caveatAnd(
caveatexpr("testcaveat"),
caveatInvert(caveatexpr("anothercaveat")),
),
)),
csub("foo", caveatexpr("anothercaveat")),
},
},
{
"add caveated sub to caveated wildcard with matching caveated exclusion",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat"), csub("foo", caveatexpr("testcaveat"))),
},
csub("foo", caveatexpr("anothercaveat")),
[]*v1.FoundSubject{
// The caveat of the exclusion is now the combination of the caveats from the original
// wildcard and the concrete which was added, as the exclusion applies when the exclusion
// caveat is true and the concrete's caveat is false.
cwc(caveatexpr("wildcardcaveat"), csub("foo",
caveatAnd(
caveatexpr("testcaveat"),
caveatInvert(caveatexpr("anothercaveat")),
),
)),
csub("foo", caveatexpr("anothercaveat")),
},
},
}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
existingSet := NewSubjectSet()
for _, existing := range tc.existing {
err := existingSet.Add(existing)
require.NoError(t, err)
}
err := existingSet.Add(tc.toAdd)
require.NoError(t, err)
expectedSet := tc.expectedSet
computedSet := existingSet.AsSlice()
testutil.RequireEquivalentSets(t, expectedSet, computedSet)
require.Equal(t, len(expectedSet), existingSet.SubjectCount())
if existingSet.HasWildcard() {
require.Equal(t, len(expectedSet), existingSet.ConcreteSubjectCount()+1)
} else {
require.Equal(t, len(expectedSet), existingSet.ConcreteSubjectCount())
}
})
}
}
func TestSubjectSetSubtract(t *testing.T) {
tcs := []struct {
name string
existing []*v1.FoundSubject
toSubtract *v1.FoundSubject
expectedSet []*v1.FoundSubject
}{
{
"basic subtract, no overlap",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
sub("baz"),
[]*v1.FoundSubject{sub("foo"), sub("bar")},
},
{
"basic subtract, with overlap",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
sub("bar"),
[]*v1.FoundSubject{sub("foo")},
},
{
"basic subtract from bare wildcard",
[]*v1.FoundSubject{sub("foo"), wc()},
sub("bar"),
[]*v1.FoundSubject{sub("foo"), wc("bar")},
},
{
"subtract from bare wildcard and set",
[]*v1.FoundSubject{sub("bar"), wc()},
sub("bar"),
[]*v1.FoundSubject{wc("bar")},
},
{
"subtract from wildcard",
[]*v1.FoundSubject{sub("bar"), wc("bar")},
sub("bar"),
[]*v1.FoundSubject{wc("bar")},
},
{
"subtract from wildcard with existing exclusions",
[]*v1.FoundSubject{sub("bar"), wc("hiya")},
sub("bar"),
[]*v1.FoundSubject{wc("hiya", "bar")},
},
{
"subtract bare wildcard from set",
[]*v1.FoundSubject{sub("bar"), sub("foo")},
wc(),
[]*v1.FoundSubject{},
},
{
"subtract wildcard with no matching exclusions from set",
[]*v1.FoundSubject{sub("bar"), sub("foo")},
wc("baz"),
[]*v1.FoundSubject{},
},
{
"subtract wildcard with matching exclusions from set",
[]*v1.FoundSubject{sub("bar"), sub("foo")},
wc("bar"),
[]*v1.FoundSubject{sub("bar")},
},
{
"subtract wildcard from another wildcard, both with the same exclusions",
[]*v1.FoundSubject{wc("sarah")},
wc("sarah"),
[]*v1.FoundSubject{},
},
{
"subtract wildcard from another wildcard, with different exclusions",
[]*v1.FoundSubject{wc("tom"), sub("foo"), sub("bar")},
wc("sarah"),
[]*v1.FoundSubject{sub("sarah")},
},
{
"subtract wildcard from another wildcard, with more exclusions",
[]*v1.FoundSubject{wc("sarah")},
wc("sarah", "tom"),
[]*v1.FoundSubject{sub("tom")},
},
// Tests with caveats.
{
"subtract non-caveated from caveated",
[]*v1.FoundSubject{csub("foo", caveatexpr("somecaveat"))},
sub("foo"),
[]*v1.FoundSubject{},
},
{
"subtract caveated from non-caveated",
[]*v1.FoundSubject{sub("foo")},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should only appear now when the caveat is false.
csub("foo", caveatInvert(caveatexpr("somecaveat"))),
},
},
{
"subtract caveated from caveated",
[]*v1.FoundSubject{
csub("foo", caveatexpr("startingcaveat")),
},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should only appear when its caveat is true, and the subtracted caveat
// is false.
csub("foo", caveatAnd(caveatexpr("startingcaveat"), caveatInvert(caveatexpr("somecaveat")))),
},
},
{
"subtract caveated bare wildcard from non-caveated",
[]*v1.FoundSubject{sub("foo")},
cwc(caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should only appear now when the caveat is false.
csub("foo", caveatInvert(caveatexpr("somecaveat"))),
},
},
{
"subtract bare wildcard from caveated",
[]*v1.FoundSubject{csub("foo", caveatexpr("something"))},
wc(),
[]*v1.FoundSubject{},
},
{
"subtract caveated bare wildcard from caveated",
[]*v1.FoundSubject{csub("foo", caveatexpr("something"))},
cwc(caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should only appear now when its caveat is true and the wildcard's caveat
// is false.
csub("foo", caveatAnd(caveatexpr("something"), caveatInvert(caveatexpr("somecaveat")))),
},
},
{
"subtract wildcard with caveated exception from non-caveated",
[]*v1.FoundSubject{sub("foo")},
cwc(nil, csub("foo", caveatexpr("somecaveat"))),
[]*v1.FoundSubject{
// The subject should only appear now when somecaveat is true, making the exclusion present
// on the wildcard, and thus preventing the wildcard from removing the concrete subject.
csub("foo", caveatexpr("somecaveat")),
},
},
{
"subtract wildcard with caveated exception from caveated",
[]*v1.FoundSubject{csub("foo", caveatexpr("somecaveat"))},
cwc(nil, csub("foo", caveatexpr("anothercaveat"))),
[]*v1.FoundSubject{
// The subject should only appear now when both caveats are true.
csub("foo", caveatAnd(caveatexpr("somecaveat"), caveatexpr("anothercaveat"))),
},
},
{
"subtract caveated wildcard with caveated exception from caveated",
[]*v1.FoundSubject{csub("foo", caveatexpr("somecaveat"))},
cwc(caveatexpr("wildcardcaveat"), csub("foo", caveatexpr("anothercaveat"))),
[]*v1.FoundSubject{
// The subject should appear when:
// somecaveat && (!wildcardcaveat || anothercaveat)
csub("foo", caveatAnd(caveatexpr("somecaveat"), caveatOr(caveatInvert(caveatexpr("wildcardcaveat")), caveatexpr("anothercaveat")))),
},
},
{
"subtract caveated concrete from non-caveated wildcard",
[]*v1.FoundSubject{wc()},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should be a caveated exception on the wildcard.
cwc(nil, csub("foo", caveatexpr("somecaveat"))),
},
},
{
"subtract caveated concrete from caveated wildcard",
[]*v1.FoundSubject{cwc(caveatexpr("wildcardcaveat"))},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should be a caveated exception on the wildcard.
cwc(caveatexpr("wildcardcaveat"), csub("foo", caveatexpr("somecaveat"))),
},
},
{
"subtract caveated concrete from wildcard with exclusion",
[]*v1.FoundSubject{
wc("foo"),
},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// No change, as `foo` is always removed.
wc("foo"),
},
},
{
"subtract caveated concrete from caveated wildcard with exclusion",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat"), sub("foo")),
},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// No change, as `foo` is always removed.
cwc(caveatexpr("wildcardcaveat"), sub("foo")),
},
},
{
"subtract caveated concrete from caveated wildcard with caveated exclusion",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat"), csub("foo", caveatexpr("exclusioncaveat"))),
},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// The subject should be excluded now if *either* caveat is true.
cwc(caveatexpr("wildcardcaveat"), csub("foo", caveatOr(caveatexpr("exclusioncaveat"), caveatexpr("somecaveat")))),
},
},
{
"subtract non-caveated bare wildcard from caveated wildcard",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat")),
},
wc(),
[]*v1.FoundSubject{},
},
{
"subtract caveated bare wildcard from non-caveated wildcard",
[]*v1.FoundSubject{
wc(),
},
cwc(caveatexpr("wildcardcaveat")),
[]*v1.FoundSubject{
// The new wildcard is caveated on the inversion of the caveat.
cwc(caveatInvert(caveatexpr("wildcardcaveat"))),
},
},
{
"subtract non-caveated bare wildcard from caveated wildcard with exclusions",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat"), sub("foo")),
},
wc(),
[]*v1.FoundSubject{},
},
{
"subtract caveated bare wildcard from non-caveated wildcard with exclusions",
[]*v1.FoundSubject{
wc("foo"),
},
cwc(caveatexpr("wildcardcaveat")),
[]*v1.FoundSubject{
// The new wildcard is caveated on the inversion of the caveat.
cwc(caveatInvert(caveatexpr("wildcardcaveat")), sub("foo")),
},
},
{
"subtract caveated wildcard with caveated exclusion from caveated wildcard with caveated exclusion",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat1"), csub("foo", caveatexpr("exclusion1"))),
},
cwc(caveatexpr("wildcardcaveat2"), csub("foo", caveatexpr("exclusion2"))),
[]*v1.FoundSubject{
cwc(
// The wildcard itself only appears if caveat1 is true and caveat2 is *false*.
caveatAnd(caveatexpr("wildcardcaveat1"), caveatInvert(caveatexpr("wildcardcaveat2"))),
// The exclusion still only applies if exclusion1 is true, because if it is false,
// it doesn't matter that the subject was excluded from the subtraction.
csub("foo", caveatexpr("exclusion1")),
),
// A concrete of foo is produced when all of these are true:
// 1) the first wildcard is present
// 2) the second wildcard is present
// 3) the exclusion on the first wildcard is false, meaning the concrete is present within
// the wildcard
// 4) the exclusion within the second is true, meaning that the concrete was not present
//
// thus causing the expression to become `{*} - {* - {foo}}`, and therefore producing
// `foo` as a concrete.
csub("foo",
caveatAnd(
caveatAnd(
caveatAnd(
caveatexpr("wildcardcaveat1"),
caveatexpr("wildcardcaveat2"),
),
caveatInvert(caveatexpr("exclusion1")),
),
caveatexpr("exclusion2"),
),
),
},
},
{
"subtract caveated wildcard with caveated exclusion from caveated wildcard with different caveated exclusion",
[]*v1.FoundSubject{
cwc(caveatexpr("wildcardcaveat1"), csub("foo", caveatexpr("exclusion1"))),
},
cwc(caveatexpr("wildcardcaveat2"), csub("bar", caveatexpr("exclusion2"))),
[]*v1.FoundSubject{
cwc(
// The wildcard itself only appears if caveat1 is true and caveat2 is *false*.
caveatAnd(caveatexpr("wildcardcaveat1"), caveatInvert(caveatexpr("wildcardcaveat2"))),
// The foo exclusion remains the same.
csub("foo", caveatexpr("exclusion1")),
),
// Because `bar` is excluded in the *subtraction*, it can appear as a *concrete* subject
// in the scenario where the first wildcard is applied, the second wildcard is applied
// and its own exclusion is true.
// Therefore, bar must be *concretely* in the set if:
// wildcard1 && wildcard2 && exclusion2
csub("bar",
caveatAnd(
caveatAnd(
caveatexpr("wildcardcaveat1"),
caveatexpr("wildcardcaveat2"),
),
caveatexpr("exclusion2"),
),
),
},
},
{
"concretes with wildcard with partially matching exclusions subtracted",
[]*v1.FoundSubject{
sub("foo"),
sub("bar"),
},
cwc(caveatexpr("caveat"), sub("foo")),
[]*v1.FoundSubject{
sub("foo"),
csub("bar", caveatInvert(caveatexpr("caveat"))),
},
},
{
"subtract caveated from caveated (same caveat)",
[]*v1.FoundSubject{
csub("foo", caveatexpr("somecaveat")),
},
csub("foo", caveatexpr("somecaveat")),
[]*v1.FoundSubject{
// Since no expression simplification is occurring, subtracting a caveated concrete
// subject from another with the *same* caveat, results in an expression that &&'s
// together the expression and its inversion. In practice, this will simplify down to
// nothing, but that happens at a different layer.
csub("foo", caveatAnd(caveatexpr("somecaveat"), caveatInvert(caveatexpr("somecaveat")))),
},
},
}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
existingSet := NewSubjectSet()
for _, existing := range tc.existing {
existingSet.MustAdd(existing)
}
existingSet.Subtract(tc.toSubtract)
expectedSet := tc.expectedSet
computedSet := existingSet.AsSlice()
testutil.RequireEquivalentSets(t, expectedSet, computedSet)
})
}
}
func TestSubjectSetIntersection(t *testing.T) {
tcs := []struct {
name string
existing []*v1.FoundSubject
toIntersect []*v1.FoundSubject
expectedSet []*v1.FoundSubject
expectedInvertedSet []*v1.FoundSubject
}{
{
"basic intersection, full overlap",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
[]*v1.FoundSubject{sub("foo"), sub("bar")},
[]*v1.FoundSubject{sub("foo"), sub("bar")},
nil,
},
{
"basic intersection, partial overlap",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
[]*v1.FoundSubject{sub("foo")},
[]*v1.FoundSubject{sub("foo")},
nil,
},
{
"basic intersection, no overlap",
[]*v1.FoundSubject{sub("foo"), sub("bar")},
[]*v1.FoundSubject{sub("baz")},
[]*v1.FoundSubject{},
nil,
},
{
"intersection between bare wildcard and concrete",
[]*v1.FoundSubject{sub("foo")},
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{sub("foo")},
nil,
},
{
"intersection between wildcard with exclusions and concrete",
[]*v1.FoundSubject{sub("foo")},
[]*v1.FoundSubject{wc("tom")},
[]*v1.FoundSubject{sub("foo")},
nil,
},
{
"intersection between wildcard with matching exclusions and concrete",
[]*v1.FoundSubject{sub("foo")},
[]*v1.FoundSubject{wc("foo")},
[]*v1.FoundSubject{},
nil,
},
{
"intersection between bare wildcards",
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{wc()},
nil,
},
{
"intersection between bare wildcard and one with exclusions",
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{wc("1", "2")},
[]*v1.FoundSubject{wc("1", "2")},
nil,
},
{
"intersection between wildcards",
[]*v1.FoundSubject{wc("2", "3")},
[]*v1.FoundSubject{wc("1", "2")},
[]*v1.FoundSubject{wc("1", "2", "3")},
nil,
},
{
"intersection wildcard with exclusions and concrete",
[]*v1.FoundSubject{wc("2", "3")},
[]*v1.FoundSubject{sub("4")},
[]*v1.FoundSubject{sub("4")},
nil,
},
{
"intersection wildcard with matching exclusions and concrete",
[]*v1.FoundSubject{wc("2", "3", "4")},
[]*v1.FoundSubject{sub("4")},
[]*v1.FoundSubject{},
nil,
},
{
"intersection of wildcards and two concrete types",
[]*v1.FoundSubject{wc(), sub("1")},
[]*v1.FoundSubject{wc(), sub("2")},
[]*v1.FoundSubject{wc(), sub("1"), sub("2")},
nil,
},
// Tests with caveats.
{
"intersection of non-caveated concrete and caveated concrete",
[]*v1.FoundSubject{sub("1")},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat"))},
// Resulting subject is caveated on the caveat.
[]*v1.FoundSubject{csub("1", caveatexpr("caveat"))},
nil,
},
{
"intersection of caveated concrete and non-caveated concrete",
[]*v1.FoundSubject{csub("1", caveatexpr("caveat"))},
[]*v1.FoundSubject{sub("1")},
// Resulting subject is caveated on the caveat.
[]*v1.FoundSubject{csub("1", caveatexpr("caveat"))},
nil,
},
{
"intersection of caveated concrete and caveated concrete",
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat2"))},
// Resulting subject is caveated both caveats.
[]*v1.FoundSubject{csub("1", caveatAnd(caveatexpr("caveat1"), caveatexpr("caveat2")))},
// Inverted result has the caveats reversed in the `&&` expression.
[]*v1.FoundSubject{csub("1", caveatAnd(caveatexpr("caveat2"), caveatexpr("caveat1")))},
},
{
"intersection of caveated concrete and non-caveated bare wildcard",
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
[]*v1.FoundSubject{wc()},
// Resulting subject is caveated and concrete.
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
nil,
},
{
"intersection of non-caveated bare wildcard and caveated concrete",
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Resulting subject is caveated and concrete.
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
nil,
},
{
"intersection of caveated concrete and caveated bare wildcard",
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
[]*v1.FoundSubject{cwc(caveatexpr("caveat2"))},
// Resulting subject is caveated from both and concrete.
[]*v1.FoundSubject{csub("1", caveatAnd(caveatexpr("caveat1"), caveatexpr("caveat2")))},
nil,
},
{
"intersection of caveated bare wildcard and caveated concrete",
[]*v1.FoundSubject{cwc(caveatexpr("caveat2"))},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Resulting subject is caveated from both and concrete.
[]*v1.FoundSubject{csub("1", caveatAnd(caveatexpr("caveat1"), caveatexpr("caveat2")))},
nil,
},
{
"intersection of caveated concrete and non-caveated wildcard with non-matching exclusion",
[]*v1.FoundSubject{wc("2")},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Resulting subject is caveated and concrete.
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
nil,
},
{
"intersection of caveated concrete and non-caveated wildcard with matching exclusion",
[]*v1.FoundSubject{wc("1")},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Empty since the subject was in the exclusion set.
[]*v1.FoundSubject{},
nil,
},
{
"intersection of caveated concrete and caveated wildcard with non-matching exclusion",
[]*v1.FoundSubject{cwc(caveatexpr("wcaveat"), sub("2"))},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Since the wildcard is caveated and has a non-matching exclusion, the caveat is added to
// the subject.
[]*v1.FoundSubject{csub("1", caveatAnd(caveatexpr("caveat1"), caveatexpr("wcaveat")))},
nil,
},
{
"intersection of caveated concrete and caveated wildcard with matching exclusion",
[]*v1.FoundSubject{cwc(caveatexpr("wcaveat"), sub("1"))},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
// Since the wildcard is caveated but has a matching exclusion, the result is empty.
[]*v1.FoundSubject{},
nil,
},
{
"intersection of caveated concrete and caveated wildcard with matching caveated exclusion",
[]*v1.FoundSubject{cwc(caveatexpr("wcaveat"), csub("1", caveatexpr("ecaveat")))},
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1"))},
[]*v1.FoundSubject{
// The concrete is included if its own caveat is true, the wildcard's caveat is true
// and the exclusion's caveat is false.
csub("1",
caveatAnd(
caveatexpr("caveat1"),
caveatAnd(
caveatexpr("wcaveat"),
caveatInvert(caveatexpr("ecaveat")),
),
),
),
},
nil,
},
{
"intersection of caveated concrete and wildcard",
[]*v1.FoundSubject{csub("1", caveatexpr("first"))},
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{
csub("1",
caveatexpr("first"),
),
},
nil,
},
{
"intersection of caveated concrete and wildcards",
[]*v1.FoundSubject{wc(), csub("1", caveatexpr("first"))},
[]*v1.FoundSubject{wc(), csub("1", caveatexpr("second"))},
[]*v1.FoundSubject{
// Wildcard is included because it is in both sets.
wc(),
// The subject is caveated to appear if either first or second is true, or both are true.
// This is because of the interaction with the wildcards in each set:
//
// - If first is true and second is false, we get {*, 1} ∩ {*} => {*, 1}
// - If first is false and second is true, we get {*} ∩ {*, 1} => {*, 1}
// - If both are true, then the subject appears, but that is just a remnant of the join
// on the concrete subject itself (since the set does not simplify expressions)
csub("1",
caveatOr(
caveatOr(
caveatAnd(
caveatexpr("first"),
caveatexpr("second"),
),
caveatexpr("first"),
),
caveatexpr("second"),
),
),
},
[]*v1.FoundSubject{
wc(),
csub("1",
caveatOr(
caveatOr(
caveatAnd(
caveatexpr("second"),
caveatexpr("first"),
),
caveatexpr("second"),
),
caveatexpr("first"),
),
),
},
},
{
"intersection of caveated concrete and caveated wildcard",
[]*v1.FoundSubject{csub("1", caveatexpr("first"))},
[]*v1.FoundSubject{
cwc(caveatexpr("wcaveat")),
csub("1", caveatexpr("second")),
},
[]*v1.FoundSubject{
csub("1",
caveatOr(
caveatAnd(
caveatexpr("first"),
caveatexpr("second"),
),
caveatAnd(
caveatexpr("first"),
caveatexpr("wcaveat"),
),
),
),
},
[]*v1.FoundSubject{
csub("1",
caveatOr(
caveatAnd(
caveatexpr("second"),
caveatexpr("first"),
),
caveatAnd(
caveatexpr("first"),
caveatexpr("wcaveat"),
),
),
),
},
},
{
"intersection of caveated concrete and wildcard with wildcard",
[]*v1.FoundSubject{csub("1", caveatexpr("caveat1")), wc()},
[]*v1.FoundSubject{wc()},
[]*v1.FoundSubject{wc(), csub("1", caveatexpr("caveat1"))},
nil,
},
}
for _, tc := range tcs {
tc := tc
t.Run(tc.name, func(t *testing.T) {
existingSet := NewSubjectSet()
for _, existing := range tc.existing {
existingSet.MustAdd(existing)
}
toIntersect := NewSubjectSet()
for _, toAdd := range tc.toIntersect {
toIntersect.MustAdd(toAdd)
}
existingSet.MustIntersectionDifference(toIntersect)
expectedSet := tc.expectedSet
computedSet := existingSet.AsSlice()
testutil.RequireEquivalentSets(t, expectedSet, computedSet)
// Run the intersection inverted, which should always result in the same results.
t.Run("inverted", func(t *testing.T) {
existingSet := NewSubjectSet()
for _, existing := range tc.existing {
existingSet.MustAdd(existing)
}
toIntersect := NewSubjectSet()
for _, toAdd := range tc.toIntersect {
toIntersect.MustAdd(toAdd)
}
toIntersect.MustIntersectionDifference(existingSet)
// The inverted set is necessary for some caveated sets, because their expressions may
// have references in reversed locations.
expectedSet := tc.expectedSet
if tc.expectedInvertedSet != nil {
expectedSet = tc.expectedInvertedSet
}
computedSet := toIntersect.AsSlice()
testutil.RequireEquivalentSets(t, expectedSet, computedSet)
})
})
}
}
func TestMultipleOperations(t *testing.T) {
tcs := []struct {
name string
runOps func(set SubjectSet)
expectedSet []*v1.FoundSubject
}{
{
"basic adds",
func(set SubjectSet) {
set.MustAdd(sub("1"))
set.MustAdd(sub("2"))