35
35
constexpr char kTempFileTemplate [] = " /tmp/sasl_xoauth2_test_token.XXXXXX" ;
36
36
constexpr char kTokenTemplate [] =
37
37
R"( {"access_token": "%s", "refresh_token": "%s", "expiry": "%s"})" ;
38
+ constexpr char kTokenTemplateWithUser [] =
39
+ R"( {"access_token": "%s", "refresh_token": "%s", "expiry": "%s",
40
+ "user": "%s"})" ;
38
41
39
42
constexpr char kServerPermanentError [] =
40
43
R"( {"status":"500","schemes":"Bearer","scope":"https://mail.google.com/"})" ;
@@ -60,6 +63,14 @@ void SetPasswordToValidToken() {
60
63
fclose (f);
61
64
}
62
65
66
+ void SetPasswordToValidTokenWithUserOverride (const std::string& user) {
67
+ FILE *f = OpenTempTokenFile ();
68
+ std::string expiry_str = std::to_string (time (nullptr ) + 3600 );
69
+ fprintf (f, kTokenTemplateWithUser , " access" , " refresh" , expiry_str.c_str (),
70
+ user.c_str ());
71
+ fclose (f);
72
+ }
73
+
63
74
void SetPasswordToInvalidToken () {
64
75
FILE *f = OpenTempTokenFile ();
65
76
std::string expiry_str = std::to_string (0 );
@@ -330,6 +341,44 @@ bool TestWithCallbacks(sasl_client_plug_t plug) {
330
341
return true ;
331
342
}
332
343
344
+ bool TestWithCallbacksAndUserOverride (sasl_client_plug_t plug) {
345
+ const std::string
kUserNameOverride =
" [email protected] " ;
346
+
347
+ PrintTestName (__func__);
348
+ SetPasswordToValidTokenWithUserOverride (kUserNameOverride );
349
+ sasl_xoauth2::SetHttpInterceptForTesting (&DefaultHttpIntercept);
350
+
351
+ sasl_utils_t utils = {};
352
+ utils.free = &FakeFree;
353
+ utils.getcallback = &FakeGetCallbackAll;
354
+ utils.malloc = &FakeMalloc;
355
+
356
+ void *context = nullptr ;
357
+ TEST_ASSERT_OK (plug.mech_new (nullptr , nullptr , &context));
358
+ PlugCleanup _ (&utils, plug, context);
359
+
360
+ sasl_client_params_t params = {};
361
+ params.utils = &utils;
362
+ params.canon_user = &FakeCanonUser;
363
+
364
+ const char *to_server = nullptr ;
365
+ unsigned int to_server_len = 0 ;
366
+ sasl_out_params_t out_params = {};
367
+
368
+ TEST_ASSERT_OK (plug.mech_step (context, ¶ms, nullptr , 0 , nullptr ,
369
+ &to_server, &to_server_len, &out_params));
370
+ fprintf (stderr, " to_server=[%s], len=%d\n " , to_server, to_server_len);
371
+ TEST_ASSERT (strstr (to_server, " Bearer" ) != nullptr );
372
+ TEST_ASSERT (strstr (to_server, kUserName .c_str ()) == nullptr );
373
+ TEST_ASSERT (strstr (to_server, kUserNameOverride .c_str ()) != nullptr );
374
+
375
+ TEST_ASSERT_OK (plug.mech_step (context, ¶ms, " " , 0 , nullptr , &to_server,
376
+ &to_server_len, &out_params));
377
+ TEST_ASSERT (to_server_len == 0 );
378
+
379
+ return true ;
380
+ }
381
+
333
382
bool TestWithPermanentError (sasl_client_plug_t plug) {
334
383
PrintTestName (__func__);
335
384
SetPasswordToValidToken ();
@@ -537,6 +586,7 @@ int main(int argc, char **argv) {
537
586
TEST_ABORT (TestWithPrompts (plug));
538
587
TEST_ABORT (TestWithoutPrompts (plug));
539
588
TEST_ABORT (TestWithCallbacks (plug));
589
+ TEST_ABORT (TestWithCallbacksAndUserOverride (plug));
540
590
TEST_ABORT (TestWithPermanentError (plug));
541
591
TEST_ABORT (TestWithTokenExpiredError (plug));
542
592
TEST_ABORT (TestPreemptiveTokenRefresh (plug));
0 commit comments