Download this file
| 1 | package kernel |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestCursorScopeIntegrityAndExpiry(t *testing.T) { |
| 10 | now := time.Now() |
| 11 | codec := CursorCodec{Key: "instance-one", Now: func() time.Time { return now }} |
| 12 | token, err := codec.Seal("person\x00route\x00filter", "last-id") |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | var pos string |
| 17 | if err = codec.Open("person\x00route\x00filter", token, &pos); err != nil || pos != "last-id" { |
| 18 | t.Fatal(pos, err) |
| 19 | } |
| 20 | for _, scope := range []string{"other\x00route\x00filter", "person\x00other\x00filter", "person\x00route\x00other"} { |
| 21 | if codec.Open(scope, token, &pos) == nil { |
| 22 | t.Fatal("cursor scope ignored", scope) |
| 23 | } |
| 24 | } |
| 25 | for _, bad := range []string{token + "x", token[:len(token)-3], strings.Repeat("x", 10000)} { |
| 26 | if codec.Open("person\x00route\x00filter", bad, &pos) == nil { |
| 27 | t.Fatal("tamper accepted") |
| 28 | } |
| 29 | } |
| 30 | other := CursorCodec{Key: "instance-two"} |
| 31 | if other.Open("person\x00route\x00filter", token, &pos) == nil { |
| 32 | t.Fatal("foreign instance cursor accepted") |
| 33 | } |
| 34 | now = now.Add(24 * time.Hour) |
| 35 | if codec.Open("person\x00route\x00filter", token, &pos) == nil { |
| 36 | t.Fatal("expired cursor accepted") |
| 37 | } |
| 38 | } |
| 39 | |