Skip to content

Commit f5d0726

Browse files
committed
Limit argument count on router cases
1 parent 44aa0ae commit f5d0726

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

flows/definition/flow_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestBrokenFlows(t *testing.T) {
100100
},
101101
{
102102
"invalid_case_category.json",
103-
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: case category 37d8813f-1402-4ad2-9cc2-e9054a96525b is not a valid category",
103+
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: invalid case[uuid=5d6abc80-39e7-4620-9988-a2447bffe526]: category 37d8813f-1402-4ad2-9cc2-e9054a96525b is not a valid category",
104104
},
105105
{
106106
"invalid_exit_dest.json",
@@ -120,11 +120,11 @@ func TestBrokenFlows(t *testing.T) {
120120
},
121121
{
122122
"too_many_categories.json",
123-
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: router can't have more than 100 categories (has 101)",
123+
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: can't have more than 100 categories (has 101)",
124124
},
125125
{
126126
"too_many_cases.json",
127-
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: switch router can't have more than 100 cases (has 101)",
127+
"invalid node[uuid=a58be63b-907d-4a1a-856b-0bb5579d7507]: invalid router: can't have more than 100 cases (has 101)",
128128
},
129129
}
130130

flows/interfaces.go

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
MaxActionsPerNode = 100 // max number of actions in a node
2222
MaxExitsPerNode = 100 // max number of exits in a node
2323
MaxCategoriesPerRouter = 100 // max number of categories a router can have
24+
MaxCasesPerRouter = 100 // max number of categories a switch router can have
25+
MaxArgumentsPerCase = 100 // max number of test arguments a switch router case can have
2426
)
2527

2628
// NodeUUID is a UUID of a flow node

flows/routers/base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (r *baseRouter) EnumerateLocalizables(include func(uuids.UUID, string, []st
9292

9393
func (r *baseRouter) validate(flow flows.Flow, exits []flows.Exit) error {
9494
if len(r.categories) > flows.MaxCategoriesPerRouter {
95-
return fmt.Errorf("router can't have more than %d categories (has %d)", flows.MaxCategoriesPerRouter, len(r.categories))
95+
return fmt.Errorf("can't have more than %d categories (has %d)", flows.MaxCategoriesPerRouter, len(r.categories))
9696
}
9797

9898
// check wait timeout category is valid

flows/routers/switch.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ func NewCase(uuid uuids.UUID, type_ string, arguments []string, categoryUUID flo
4141
}
4242
}
4343

44+
func (c *Case) validate(r *SwitchRouter) error {
45+
if !r.isValidCategory(c.CategoryUUID) {
46+
return fmt.Errorf("category %s is not a valid category", c.CategoryUUID)
47+
}
48+
49+
if _, exists := cases.XTESTS[c.Type]; !exists {
50+
return fmt.Errorf("%s is not a registered test function", c.Type)
51+
}
52+
53+
if len(c.Arguments) > flows.MaxArgumentsPerCase {
54+
return fmt.Errorf("can't have more than %d arguments (has %d)", flows.MaxArgumentsPerCase, len(c.Arguments))
55+
}
56+
57+
return nil
58+
}
59+
4460
// LocalizationUUID gets the UUID which identifies this object for localization
4561
func (c *Case) LocalizationUUID() uuids.UUID { return uuids.UUID(c.UUID) }
4662

@@ -94,8 +110,8 @@ func (r *SwitchRouter) Cases() []*Case { return r.cases }
94110

95111
// Validate validates the arguments for this router
96112
func (r *SwitchRouter) Validate(flow flows.Flow, exits []flows.Exit) error {
97-
if len(r.cases) > flows.MaxCategoriesPerRouter {
98-
return fmt.Errorf("switch router can't have more than %d cases (has %d)", flows.MaxCategoriesPerRouter, len(r.cases))
113+
if len(r.cases) > flows.MaxCasesPerRouter {
114+
return fmt.Errorf("can't have more than %d cases (has %d)", flows.MaxCasesPerRouter, len(r.cases))
99115
}
100116

101117
// check the default category is valid
@@ -104,14 +120,8 @@ func (r *SwitchRouter) Validate(flow flows.Flow, exits []flows.Exit) error {
104120
}
105121

106122
for _, c := range r.cases {
107-
// check each case points to a valid category
108-
if !r.isValidCategory(c.CategoryUUID) {
109-
return fmt.Errorf("case category %s is not a valid category", c.CategoryUUID)
110-
}
111-
112-
// and each case test is valid
113-
if _, exists := cases.XTESTS[c.Type]; !exists {
114-
return fmt.Errorf("case test %s is not a registered test function", c.Type)
123+
if err := c.validate(r); err != nil {
124+
return fmt.Errorf("invalid case[uuid=%s]: %s", c.UUID, err)
115125
}
116126
}
117127

flows/routers/testdata/switch.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
}
8888
]
8989
},
90-
"read_error": "case category 33c829d5-9092-484e-9683-c03614b6a446 is not a valid category"
90+
"read_error": "invalid case[uuid=98503572-25bf-40ce-ad72-8836b6549a38]: category 33c829d5-9092-484e-9683-c03614b6a446 is not a valid category"
9191
},
9292
{
9393
"description": "Read fails for invalid case test",
@@ -132,7 +132,7 @@
132132
}
133133
]
134134
},
135-
"read_error": "case test has_any_icecream is not a registered test function"
135+
"read_error": "invalid case[uuid=98503572-25bf-40ce-ad72-8836b6549a38]: has_any_icecream is not a registered test function"
136136
},
137137
{
138138
"description": "Result created with matching test result",

0 commit comments

Comments
 (0)