| 1 | package mobilelink |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestSnapshotPollingQuery(t *testing.T) { |
| 10 | const id = "12345678-1234-4321-aaaa-123456789abc" |
| 11 | for _, tc := range []struct { |
| 12 | query string |
| 13 | want bool |
| 14 | }{ |
| 15 | {"", true}, {"after=0", true}, {"after=18446744073709551615", true}, |
| 16 | {"after=2&epoch=" + id, true}, {"epoch=" + id + "&after=2", true}, |
| 17 | {"epoch=" + id, false}, {"after=2&epoch=", false}, {"after=2&epoch=" + strings.ToUpper(id), false}, |
| 18 | {"after=2&epoch=12345678-1234-5321-aaaa-123456789abc", false}, |
| 19 | {"after=2&epoch=12345678-1234-4321-0aaa-123456789abc", false}, |
| 20 | {"after=-1&epoch=" + id, false}, {"after=0&after=1", false}, {"after=1&other=x", false}, |
| 21 | {"after=1&epoch=" + id + "&epoch=" + id, false}, {"after=%31&epoch=" + id, false}, |
| 22 | {"after=18446744073709551616", false}, |
| 23 | } { |
| 24 | u := &url.URL{Path: "/api/mobile/cli/sessions/" + id, RawQuery: tc.query} |
| 25 | if got := allowed("GET", u); got != tc.want { |
| 26 | t.Errorf("poll query %q allowed=%v, want %v", tc.query, got, tc.want) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestBrowserFrameQuery(t *testing.T) { |
| 32 | const tag = "b542cd94286a2489bc2df1241b043c76" |
| 33 | const id = "12345678-1234-4321-aaaa-123456789abc" |
| 34 | for _, tc := range []struct { |
| 35 | method, surface, operation, query string |
| 36 | want bool |
| 37 | }{ |
| 38 | {"GET", "browser", "frame", "after=" + tag, true}, |
| 39 | {"GET", "browser", "frame", "after=" + tag + "&after=" + tag, false}, |
| 40 | {"GET", "browser", "frame", "after=" + strings.ToUpper(tag), false}, |
| 41 | {"POST", "browser", "action", "after=" + tag, false}, |
| 42 | {"GET", "desktop", "frame", "after=" + tag, false}, |
| 43 | } { |
| 44 | u := &url.URL{Path: "/api/mobile/" + tc.surface + "/" + id + "/" + tc.operation, RawQuery: tc.query} |
| 45 | if allowed(tc.method, u) != tc.want { |
| 46 | t.Errorf("unexpected result for %s %s", tc.method, u.RequestURI()) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestSessionListMutations(t *testing.T) { |
| 52 | for _, operation := range []string{"metadata", "delete", "archive", "restore", "purge"} { |
| 53 | u := &url.URL{Path: "/api/mobile/cli/sessions/12345678-1234-4321-aaaa-123456789abc/" + operation} |
| 54 | if !allowed("POST", u) || allowed("GET", u) || allowed("DELETE", u) { |
| 55 | t.Fatal("mutation method boundary", operation) |
| 56 | } |
| 57 | u.RawQuery = "after=1" |
| 58 | if allowed("POST", u) { |
| 59 | t.Fatal("mutation query accepted", operation) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestArchivedSessionQuery(t *testing.T) { |
| 65 | u := &url.URL{Path: "/api/mobile/cli/sessions", RawQuery: "archived=true"} |
| 66 | if !allowed("GET", u) || allowed("POST", u) { |
| 67 | t.Fatal("archive query method") |
| 68 | } |
| 69 | for _, query := range []string{"archived=false", "archived=true&other=1", "archived=true&archived=true"} { |
| 70 | u.RawQuery = query |
| 71 | if allowed("GET", u) { |
| 72 | t.Fatal("unexpected archive query", query) |
| 73 | } |
| 74 | } |
| 75 | } |