github.com/in-toto/in-toto-golang@v0.9.1-0.20240517212500-990269f763cf/in_toto/rulelib_test.go (about) 1 package in_toto 2 3 import ( 4 "testing" 5 ) 6 7 func TestUnpackValidRules(t *testing.T) { 8 // A list of valid rules (lists) 9 // Each will be passed to rulelib.UnpackRule below 10 rules := [][]string{ 11 {"CREATE", "foo"}, 12 {"DELETE", "foo"}, 13 {"MODIFY", "foo"}, 14 {"ALLOW", "foo"}, 15 {"DISALLOW", "foo"}, 16 {"REQUIRE", "foo"}, 17 {"MATCH", "foo", "IN", "source-path", "WITH", "PRODUCTS", "IN", 18 "dest-path", "FROM", "step-name"}, 19 {"MATCH", "foo", "IN", "source-path", "WITH", "MATERIALS", 20 "FROM", "step-name"}, 21 {"MATCH", "foo", "WITH", "PRODUCTS", "IN", "dest-path", 22 "FROM", "step-name"}, 23 {"MATCH", "foo", "WITH", "MATERIALS", "FROM", "step-name"}, 24 } 25 26 // These are the expected results from rulelib.UnpackRule for above rules 27 // (associated by index) 28 expectedRuleMaps := []map[string]string{ 29 {"type": "create", "pattern": "foo"}, 30 {"type": "delete", "pattern": "foo"}, 31 {"type": "modify", "pattern": "foo"}, 32 {"type": "allow", "pattern": "foo"}, 33 {"type": "disallow", "pattern": "foo"}, 34 {"type": "require", "pattern": "foo"}, 35 {"type": "match", "pattern": "foo", 36 "srcPrefix": "source-path", "dstPrefix": "dest-path", 37 "dstType": "products", "dstName": "step-name"}, 38 {"type": "match", "pattern": "foo", 39 "srcPrefix": "source-path", "dstPrefix": "", 40 "dstType": "materials", "dstName": "step-name"}, 41 {"type": "match", "pattern": "foo", 42 "srcPrefix": "", "dstPrefix": "dest-path", 43 "dstType": "products", "dstName": "step-name"}, 44 {"type": "match", "pattern": "foo", 45 "srcPrefix": "", "dstPrefix": "", 46 "dstType": "materials", "dstName": "step-name"}, 47 } 48 49 for i, rule := range rules { 50 returnedRuleMap, err := UnpackRule(rule) 51 if err != nil { 52 t.Error(err) 53 } 54 55 for _, key := range []string{"type", "pattern", "srcPrefix", "dstPrefix", 56 "dstName", "dstType"} { 57 if returnedRuleMap[key] != expectedRuleMaps[i][key] { 58 t.Errorf("invalid '%s' in unpacked rule '%s', should be '%s', got"+ 59 " '%s'", key, rule, expectedRuleMaps[i][key], 60 returnedRuleMap[key]) 61 } 62 } 63 } 64 } 65 66 func TestUnpackInvalidRules(t *testing.T) { 67 rules := [][]string{ 68 {"CREATE", "foo", "too-long"}, 69 {"SUBVERT", "foo"}, 70 {"MODIFY"}, 71 {"MATCH", "foo", "too-many-patterns", "IN", "source-path", "WITH", 72 "PRODUCTS", "IN", "dest-path", "FROM", "step-name"}, 73 {"MATCH", "foo", "WITH", "GUMMY", "BEARS"}, 74 } 75 for _, rule := range rules { 76 if _, err := UnpackRule(rule); err == nil { 77 t.Errorf("invalid rule %s should return error from UnpackRule.", rule) 78 } 79 } 80 }