From 3dfa50a776dc4061f26062655f2999ef8c13fb97 Mon Sep 17 00:00:00 2001 From: sunil mayya Date: Wed, 13 Sep 2023 22:38:58 +0000 Subject: [PATCH] Bug 1848763 - update ntlm connection reset tests. r=necko-reviewers,valentin Differential Revision: https://phabricator.services.mozilla.com/D187425 --- netwerk/test/unit/test_ntlm_proxy_auth.js | 110 +++++++++++++++------- 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/netwerk/test/unit/test_ntlm_proxy_auth.js b/netwerk/test/unit/test_ntlm_proxy_auth.js index 24c7d8cedfdd..b6cb27890b88 100644 --- a/netwerk/test/unit/test_ntlm_proxy_auth.js +++ b/netwerk/test/unit/test_ntlm_proxy_auth.js @@ -221,9 +221,12 @@ function connectionReset(metadata, response) { authPrefix = authorization.substring(0, NTLM_PREFIX_LEN); Assert.equal(NTLM_TYPE3_PREFIX, authPrefix, "Expecting a Type 3 message"); ntlmTypeTwoCount++; - response.finish(); - response.seizePower(); - response.bodyOutPutStream.close(); + try { + response.seizePower(); + response.finish(); + } catch (e) { + Assert.ok(false, "unexpected exception" + e); + } break; default: // Should not get any further requests on this channel @@ -234,31 +237,32 @@ function connectionReset(metadata, response) { } // -// Reset the connection after a nogotiate message has been received +// Reset the connection after a negotiate message has been received // function connectionReset02(metadata, response) { + var connectionNumber = httpserver.connectionNumber; switch (requestsMade) { case 0: - // Proxy - First request to the Proxy resppond with a 407 to start auth + // Proxy - First request to the Proxy respond with a 407 to start auth response.setStatusLine(metadata.httpVersion, 407, "Unauthorized"); response.setHeader("Proxy-Authenticate", "NTLM", false); + Assert.equal(connectionNumber, httpserver.connectionNumber); break; case 1: + // eslint-disable-next-line no-fallthrough + default: // Proxy - Expecting a type 1 negotiate message from the client + Assert.equal(connectionNumber, httpserver.connectionNumber); var authorization = metadata.getHeader("Proxy-Authorization"); var authPrefix = authorization.substring(0, NTLM_PREFIX_LEN); Assert.equal(NTLM_TYPE1_PREFIX, authPrefix, "Expecting a Type 1 message"); ntlmTypeOneCount++; - response.setStatusLine(metadata.httpVersion, 407, "Unauthorized"); - response.setHeader("Proxy-Authenticate", PROXY_CHALLENGE, false); - response.finish(); - response.seizePower(); - response.bodyOutPutStream.close(); - break; - default: - // Should not get any further requests on this channel - dump("ERROR: NTLM Proxy Authentication, connection should not be reused"); - Assert.ok(false); + try { + response.seizePower(); + response.finish(); + } catch (e) { + Assert.ok(false, "unexpected exception" + e); + } } requestsMade++; } @@ -323,40 +327,82 @@ function setupTest(path, handler, requests, response, clearCache) { }); } +let ntlmTypeOneCount = 0; // The number of NTLM type one messages received +let exptTypeOneCount = 0; // The number of NTLM type one messages that should be received +let ntlmTypeTwoCount = 0; // The number of NTLM type two messages received +let exptTypeTwoCount = 0; // The number of NTLM type two messages that should received + // Happy code path -// Succesful proxy auth. -add_task(async function test_happy_path() { +// Successful proxy auth. +async function test_happy_path() { dump("RUNNING TEST: test_happy_path"); await setupTest("/auth", successfulAuth, 3, 200, 1); -}); +} // Failed proxy authentication -add_task(async function test_failed_auth() { +async function test_failed_auth() { dump("RUNNING TEST:failed auth "); await setupTest("/auth", failedAuth, 4, 407, 1); -}); +} -var ntlmTypeOneCount = 0; // The number of NTLM type one messages received -var exptTypeOneCount = 0; // The number of NTLM type one messages that should be received - -var ntlmTypeTwoCount = 0; // The number of NTLM type two messages received -var exptTypeTwoCount = 0; // The number of NTLM type two messages that should received // Test connection reset, after successful auth -add_task(async function test_connection_reset() { +async function test_connection_reset() { dump("RUNNING TEST:connection reset "); ntlmTypeOneCount = 0; ntlmTypeTwoCount = 0; exptTypeOneCount = 1; exptTypeTwoCount = 1; - await setupTest("/auth", connectionReset, 2, 500, 1); -}); + await setupTest("/auth", connectionReset, 3, 500, 1); +} // Test connection reset after sending a negotiate. -add_task(async function test_connection_reset02() { +// Client should retry request using the same connection +async function test_connection_reset02() { dump("RUNNING TEST:connection reset "); ntlmTypeOneCount = 0; ntlmTypeTwoCount = 0; - exptTypeOneCount = 1; + let maxRetryAttempt = 5; + exptTypeOneCount = maxRetryAttempt; exptTypeTwoCount = 0; - await setupTest("/auth", connectionReset02, 1, 500, 1); -}); + + Services.prefs.setIntPref( + "network.http.request.max-attempts", + maxRetryAttempt + ); + + await setupTest("/auth", connectionReset02, maxRetryAttempt + 1, 500, 1); +} + +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", false]] }, + test_happy_path +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", false]] }, + test_failed_auth +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", false]] }, + test_connection_reset +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", false]] }, + test_connection_reset02 +); + +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", true]] }, + test_happy_path +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", true]] }, + test_failed_auth +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", true]] }, + test_connection_reset +); +add_task( + { pref_set: [["network.auth.use_redirect_for_retries", true]] }, + test_connection_reset02 +);