github.com/boxboat/in-toto-golang@v0.0.3-0.20210303203820-2fa16ecbe6f6/in_toto/util_test.go (about) 1 package in_toto 2 3 import ( 4 "reflect" 5 "sort" 6 "testing" 7 ) 8 9 func TestSet(t *testing.T) { 10 testSet := NewSet() 11 if testSet.Has("a") { 12 t.Errorf("not added element 'a' in set %s", testSet.Slice()) 13 } 14 testSet.Add("a") 15 if !testSet.Has("a") { 16 t.Errorf("added element 'a' not in set %s", testSet.Slice()) 17 } 18 testSet.Add("a") 19 setLen := len(testSet) 20 if setLen != 1 { 21 t.Errorf("expected len 1, got %v for set %s", setLen, testSet.Slice()) 22 } 23 testSet.Remove("a") 24 if testSet.Has("a") { 25 t.Errorf("removed element 'a' in set %s", testSet.Slice()) 26 } 27 // Nothing should happen 28 testSet.Remove("a") 29 } 30 31 func TestSetIntersection(t *testing.T) { 32 testSet1 := NewSet("a", "b", "c") 33 testSet2 := NewSet("b", "c", "d") 34 expected := NewSet("b", "c") 35 res := testSet1.Intersection(testSet2) 36 37 if !reflect.DeepEqual(res, expected) { 38 t.Errorf("expected %s, got %s", expected.Slice(), res.Slice()) 39 } 40 } 41 42 func TestSetDifference(t *testing.T) { 43 testSet1 := NewSet("a", "b", "c") 44 testSet2 := NewSet("b", "c", "d") 45 expected := NewSet("a") 46 res := testSet1.Difference(testSet2) 47 48 if !reflect.DeepEqual(res, expected) { 49 t.Errorf("expected %s, got %s", expected.Slice(), res.Slice()) 50 } 51 } 52 53 func TestSetSlice(t *testing.T) { 54 testSet := NewSet("a", "b", "c") 55 expected := []string{"a", "b", "c"} 56 57 res := testSet.Slice() 58 sort.Strings(res) 59 if !reflect.DeepEqual(res, expected) { 60 t.Errorf("expected: %s, got: %s", expected, res) 61 } 62 } 63 64 func TestSetFilter(t *testing.T) { 65 cases := []struct { 66 name string 67 pattern string 68 base, expected Set 69 }{ 70 {"match foo", "foo", NewSet("foo", "foobar", "bar"), NewSet("foo")}, 71 {"match with wildcard", "foo*", NewSet("foo", "foobar", "bar"), NewSet("foo", "foobar")}, 72 {"no match", "foo", NewSet("bar"), NewSet()}, 73 {"no match (due to invalid pattern)", "[", NewSet("["), NewSet()}, 74 } 75 76 for _, tc := range cases { 77 t.Run(tc.name, func(t *testing.T) { 78 res := tc.base.Filter(tc.pattern) 79 if !reflect.DeepEqual(res, tc.expected) { 80 t.Errorf("expected: %s, got: %s", tc.expected.Slice(), res.Slice()) 81 } 82 }) 83 } 84 } 85 86 func TestInterfaceKeyStrings(t *testing.T) { 87 expected := []string{"a", "b", "c"} 88 testMap := map[string]interface{}{"a": nil, "b": nil, "c": nil} 89 res := InterfaceKeyStrings(testMap) 90 sort.Strings(res) 91 if !reflect.DeepEqual(res, expected) { 92 t.Errorf("expected: %s, got: %s", expected, res) 93 } 94 } 95 96 func TestSubsetCheck(t *testing.T) { 97 tables := []struct { 98 subset []string 99 superset Set 100 result bool 101 }{ 102 {[]string{"sha256"}, NewSet("sha256", "sha512"), true}, 103 {[]string{"sha512"}, NewSet("sha256"), false}, 104 {[]string{"sha256", "sha512"}, NewSet("sha128", "sha256", "sha512"), true}, 105 {[]string{"sha256", "sha512", "sha384"}, NewSet("sha128"), false}, 106 } 107 for _, table := range tables { 108 result := table.superset.IsSubSet(NewSet(table.subset...)) 109 if table.result != result { 110 t.Errorf("result mismatch for: %#v, %#v, got: %t, should have got: %t", table.subset, table.superset, result, table.result) 111 } 112 } 113 }