-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from bucko13/poc-creation-time-caveat
Timeout Caveat Support
- Loading branch information
Showing
11 changed files
with
325 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
/aperture | ||
cmd/aperture/aperture | ||
|
||
# misc | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package lsat | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestTimeoutSatisfier tests that the Timeout Satisfier implementation behaves | ||
// as expected and correctly accepts or rejects calls based on if the | ||
// timeout has been reached or not. | ||
func TestTimeoutSatisfier(t *testing.T) { | ||
t.Parallel() | ||
|
||
now := int64(0) | ||
|
||
var tests = []struct { | ||
name string | ||
timeouts []int64 | ||
expectFinalErr bool | ||
expectPrevErr bool | ||
}{ | ||
{ | ||
name: "current time is before expiration", | ||
timeouts: []int64{now + 1000}, | ||
}, | ||
{ | ||
name: "time passed is greater than " + | ||
"expiration", | ||
timeouts: []int64{now - 1000}, | ||
expectFinalErr: true, | ||
}, | ||
{ | ||
name: "successive caveats are increasingly " + | ||
"restrictive and not yet expired", | ||
timeouts: []int64{now + 1000, now + 500}, | ||
}, | ||
{ | ||
name: "latter caveat is less restrictive " + | ||
"then previous", | ||
timeouts: []int64{now + 500, now + 1000}, | ||
expectPrevErr: true, | ||
}, | ||
} | ||
|
||
var ( | ||
service = "restricted" | ||
condition = service + CondTimeoutSuffix | ||
satisfier = NewTimeoutSatisfier(service, func() time.Time { | ||
return time.Unix(now, 0) | ||
}) | ||
) | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
var prev *Caveat | ||
for _, timeout := range test.timeouts { | ||
caveat := NewCaveat( | ||
condition, fmt.Sprintf("%d", timeout), | ||
) | ||
|
||
if prev != nil { | ||
err := satisfier.SatisfyPrevious( | ||
*prev, caveat, | ||
) | ||
if test.expectPrevErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
err := satisfier.SatisfyFinal(caveat) | ||
if test.expectFinalErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
|
||
prev = &caveat | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.