| 1 | package qq |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestQQIdentifyIntentProfiles(t *testing.T) { |
| 11 | const shared = 1<<0 | 1<<1 | 1<<12 | 1<<25 |
| 12 | if qqPrivateIdentifyIntents != shared|1<<9 { |
| 13 | t.Fatalf("private intents = %b, want %b", qqPrivateIdentifyIntents, shared|1<<9) |
| 14 | } |
| 15 | if qqPublicIdentifyIntents != shared|1<<30 { |
| 16 | t.Fatalf("public intents = %b, want %b", qqPublicIdentifyIntents, shared|1<<30) |
| 17 | } |
| 18 | if qqPrivateIdentifyIntents&(1<<26) != 0 || qqPublicIdentifyIntents&(1<<26) != 0 { |
| 19 | t.Fatal("identify profiles contain privileged INTERACTION intent") |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestQQIntentFallbackPreservesPrivateProfileWhenAuthorized(t *testing.T) { |
| 24 | selected := qqPrivateIdentifyIntents |
| 25 | var attempts []int |
| 26 | downgraded, err := connectQQGatewayWithIntentFallback(context.Background(), "token", &selected, func(_ context.Context, _ string, intents int) error { |
| 27 | attempts = append(attempts, intents) |
| 28 | return nil |
| 29 | }, nil) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if downgraded { |
| 34 | t.Fatal("authorized private profile was downgraded") |
| 35 | } |
| 36 | if selected != qqPrivateIdentifyIntents || len(attempts) != 1 || attempts[0] != qqPrivateIdentifyIntents { |
| 37 | t.Fatalf("selected = %b, attempts = %v", selected, attempts) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestQQIntentFallbackDowngradesOnceAndRemembersPublicProfile(t *testing.T) { |
| 42 | selected := qqPrivateIdentifyIntents |
| 43 | var attempts []int |
| 44 | connect := func(_ context.Context, _ string, intents int) error { |
| 45 | attempts = append(attempts, intents) |
| 46 | if intents == qqPrivateIdentifyIntents { |
| 47 | return errQQIdentifyRejected |
| 48 | } |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | downgraded, err := connectQQGatewayWithIntentFallback(context.Background(), "token", &selected, connect, nil) |
| 53 | if err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if !downgraded || selected != qqPublicIdentifyIntents { |
| 57 | t.Fatalf("downgraded = %v, selected = %b", downgraded, selected) |
| 58 | } |
| 59 | wantAttempts := []int{qqPrivateIdentifyIntents, qqPublicIdentifyIntents} |
| 60 | if fmt.Sprint(attempts) != fmt.Sprint(wantAttempts) { |
| 61 | t.Fatalf("attempts = %v, want %v", attempts, wantAttempts) |
| 62 | } |
| 63 | |
| 64 | attempts = nil |
| 65 | downgraded, err = connectQQGatewayWithIntentFallback(context.Background(), "token", &selected, connect, nil) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if downgraded || len(attempts) != 1 || attempts[0] != qqPublicIdentifyIntents { |
| 70 | t.Fatalf("second attempt downgraded = %v, attempts = %v", downgraded, attempts) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestQQIntentFallbackDoesNotReconnectAfterCancellation(t *testing.T) { |
| 75 | ctx, cancel := context.WithCancel(context.Background()) |
| 76 | selected := qqPrivateIdentifyIntents |
| 77 | attempts := 0 |
| 78 | _, err := connectQQGatewayWithIntentFallback(ctx, "token", &selected, func(_ context.Context, _ string, _ int) error { |
| 79 | attempts++ |
| 80 | cancel() |
| 81 | return errQQIdentifyRejected |
| 82 | }, nil) |
| 83 | if !errors.Is(err, context.Canceled) { |
| 84 | t.Fatalf("error = %v, want context cancellation", err) |
| 85 | } |
| 86 | if attempts != 1 || selected != qqPrivateIdentifyIntents { |
| 87 | t.Fatalf("attempts = %d, selected = %b", attempts, selected) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestQQReadyPayloadClassifiesInvalidSession(t *testing.T) { |
| 92 | err := validateQQReadyPayload(gatewayPayload{Op: opInvalid}) |
| 93 | if !errors.Is(err, errQQIdentifyRejected) { |
| 94 | t.Fatalf("validateQQReadyPayload() error = %v, want identify rejection", err) |
| 95 | } |
| 96 | } |
| 97 |