Skip to content

Commit 4ff74f4

Browse files
committed
Add unit test for user override.
1 parent 30beb43 commit 4ff74f4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/xoauth2_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const std::string kUserName = "[email protected]";
3535
constexpr char kTempFileTemplate[] = "/tmp/sasl_xoauth2_test_token.XXXXXX";
3636
constexpr char kTokenTemplate[] =
3737
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"})";
3841

3942
constexpr char kServerPermanentError[] =
4043
R"({"status":"500","schemes":"Bearer","scope":"https://mail.google.com/"})";
@@ -60,6 +63,14 @@ void SetPasswordToValidToken() {
6063
fclose(f);
6164
}
6265

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+
6374
void SetPasswordToInvalidToken() {
6475
FILE *f = OpenTempTokenFile();
6576
std::string expiry_str = std::to_string(0);
@@ -330,6 +341,44 @@ bool TestWithCallbacks(sasl_client_plug_t plug) {
330341
return true;
331342
}
332343

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, &params, 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, &params, "", 0, nullptr, &to_server,
376+
&to_server_len, &out_params));
377+
TEST_ASSERT(to_server_len == 0);
378+
379+
return true;
380+
}
381+
333382
bool TestWithPermanentError(sasl_client_plug_t plug) {
334383
PrintTestName(__func__);
335384
SetPasswordToValidToken();
@@ -537,6 +586,7 @@ int main(int argc, char **argv) {
537586
TEST_ABORT(TestWithPrompts(plug));
538587
TEST_ABORT(TestWithoutPrompts(plug));
539588
TEST_ABORT(TestWithCallbacks(plug));
589+
TEST_ABORT(TestWithCallbacksAndUserOverride(plug));
540590
TEST_ABORT(TestWithPermanentError(plug));
541591
TEST_ABORT(TestWithTokenExpiredError(plug));
542592
TEST_ABORT(TestPreemptiveTokenRefresh(plug));

0 commit comments

Comments
 (0)