Fixed flow checking algorithm for hopefully the last time.

Signed-off-by: Andrew Morgan (https://amorgan.xyz) <andrew@amorgan.xyz>
This commit is contained in:
Andrew Morgan (https://amorgan.xyz) 2017-11-28 09:33:32 -08:00
parent c431255134
commit 1537188123
No known key found for this signature in database
GPG key ID: 174BEAB009FD176D
2 changed files with 35 additions and 9 deletions

View file

@ -425,18 +425,21 @@ func checkFlows(a []authtypes.LoginType, b []authtypes.LoginType) bool {
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
sort.Slice(b, func(i, j int) bool { return b[i] < b[j] }) sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
// Account for any extra stages a user may unnecessarily do. // Iterate through each slice, going to the next allowed slice only once
extraStages := len(a) - len(b) // we've found a match.
for i := range b { i, j := 0, 0
if extraStages < 0 { for j < len(b) {
// The provided flow has run out of possible extraneous stages. // Exit if we've reached the end of our input without being able to
// match all of the allowed stages.
if i >= len(a) {
return false return false
} }
if a[i] != b[i] {
// Wasn't a match, drop an extraneous stage. // If we've found a stage we want, move on to the next allowed stage.
extraStages-- if a[i] == b[j] {
continue j++
} }
i++
} }
return true return true
} }

View file

@ -109,3 +109,26 @@ func TestFlowCheckingUnorderedAndExtraneous(t *testing.T) {
t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be true.") t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be true.")
} }
} }
// Should return false as we're providing less stages than are required.
func TestFlowCheckingShortIncorrectInput(t *testing.T) {
testFlow := []authtypes.LoginType{
authtypes.LoginType("stage8"),
}
if checkFlowCompleted(testFlow, allowedFlows) {
t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be false.")
}
}
// Should return false as we're providing less stages than are required.
func TestFlowCheckingExtraneousIncorrectInput(t *testing.T) {
testFlow := []authtypes.LoginType{
authtypes.LoginType("stage8"),
authtypes.LoginType("stage9"),
authtypes.LoginType("stage10"),
authtypes.LoginType("stage11"),
}
if checkFlowCompleted(testFlow, allowedFlows) {
t.Error("Incorrect registration flow verification: ", testFlow, ", from allowed flows: ", allowedFlows, ". Should be false.")
}
}