|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// TestTurnUsernameLifetime checks the "expiryUnix:userId" TURN REST username is |
| 11 | +// turned into a remaining lifetime with a safety margin, a 1-minute floor for |
| 12 | +// already-expired creds, and a default for unparsable input. |
| 13 | +func TestTurnUsernameLifetime(t *testing.T) { |
| 14 | + future := fmt.Sprintf("%d:601279409979", time.Now().Add(2*time.Hour).Unix()) |
| 15 | + if d := turnUsernameLifetime(future); d <= time.Hour { |
| 16 | + t.Fatalf("future lifetime = %s, want > 1h", d) |
| 17 | + } |
| 18 | + past := fmt.Sprintf("%d:1", time.Now().Add(-time.Hour).Unix()) |
| 19 | + if d := turnUsernameLifetime(past); d != time.Minute { |
| 20 | + t.Fatalf("past lifetime = %s, want 1m", d) |
| 21 | + } |
| 22 | + if d := turnUsernameLifetime("not-a-number:1"); d != 8*time.Minute { |
| 23 | + t.Fatalf("garbage lifetime = %s, want 8m", d) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// TestVkHashFromLink checks the call join hash is extracted from full URLs (with |
| 28 | +// query/fragment/trailing slash stripped) and passed through unchanged when it is |
| 29 | +// already a bare hash. |
| 30 | +func TestVkHashFromLink(t *testing.T) { |
| 31 | + cases := map[string]string{ |
| 32 | + "https://vk.com/call/join/ABC123": "ABC123", |
| 33 | + "https://vk.com/call/join/ABC123?p=1": "ABC123", |
| 34 | + "https://vk.com/call/join/ABC123#frag": "ABC123", |
| 35 | + " https://vk.com/call/join/ABC123/ ": "ABC123", |
| 36 | + "ABC123": "ABC123", |
| 37 | + } |
| 38 | + for in, want := range cases { |
| 39 | + if got := vkHashFromLink(in); got != want { |
| 40 | + t.Errorf("vkHashFromLink(%q) = %q, want %q", in, got, want) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestRandomDeviceIDFormat checks the OK SDK device id is a v4-shaped UUID. |
| 46 | +func TestRandomDeviceIDFormat(t *testing.T) { |
| 47 | + id := randomDeviceID() |
| 48 | + if len(id) != 36 { |
| 49 | + t.Fatalf("device id len = %d, want 36 (%q)", len(id), id) |
| 50 | + } |
| 51 | + parts := strings.Split(id, "-") |
| 52 | + if len(parts) != 5 || parts[2][0] != '4' { |
| 53 | + t.Fatalf("device id not uuid-v4 shaped: %q", id) |
| 54 | + } |
| 55 | +} |
0 commit comments