michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: #include michael@0: #include "nsNetUtil.h" michael@0: #include "nsISiteSecurityService.h" michael@0: michael@0: #define EXPECT_SUCCESS(rv, ...) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (NS_FAILED(rv)) { \ michael@0: fail(__VA_ARGS__); \ michael@0: return false; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: michael@0: #define EXPECT_FAILURE(rv, ...) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (NS_SUCCEEDED(rv)) { \ michael@0: fail(__VA_ARGS__); \ michael@0: return false; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: #define REQUIRE_EQUAL(a, b, ...) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (a != b) { \ michael@0: fail(__VA_ARGS__); \ michael@0: return false; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: bool michael@0: TestSuccess(const char* hdr, bool extraTokens, michael@0: uint64_t expectedMaxAge, bool expectedIncludeSubdomains, michael@0: nsISiteSecurityService* sss) michael@0: { michael@0: nsCOMPtr dummyUri; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(dummyUri), "https://foo.com/bar.html"); michael@0: EXPECT_SUCCESS(rv, "Failed to create URI"); michael@0: michael@0: uint64_t maxAge = 0; michael@0: bool includeSubdomains = false; michael@0: rv = sss->ProcessHeader(nsISiteSecurityService::HEADER_HSTS, dummyUri, hdr, michael@0: 0, &maxAge, &includeSubdomains); michael@0: EXPECT_SUCCESS(rv, "Failed to process valid header: %s", hdr); michael@0: michael@0: REQUIRE_EQUAL(maxAge, expectedMaxAge, "Did not correctly parse maxAge"); michael@0: REQUIRE_EQUAL(includeSubdomains, expectedIncludeSubdomains, "Did not correctly parse presence/absence of includeSubdomains"); michael@0: michael@0: if (extraTokens) { michael@0: REQUIRE_EQUAL(rv, NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA, michael@0: "Extra tokens were expected when parsing, but were not encountered."); michael@0: } else { michael@0: REQUIRE_EQUAL(rv, NS_OK, "Unexpected tokens found during parsing."); michael@0: } michael@0: michael@0: passed(hdr); michael@0: return true; michael@0: } michael@0: michael@0: bool TestFailure(const char* hdr, michael@0: nsISiteSecurityService* sss) michael@0: { michael@0: nsCOMPtr dummyUri; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(dummyUri), "https://foo.com/bar.html"); michael@0: EXPECT_SUCCESS(rv, "Failed to create URI"); michael@0: michael@0: rv = sss->ProcessHeader(nsISiteSecurityService::HEADER_HSTS, dummyUri, hdr, michael@0: 0, nullptr, nullptr); michael@0: EXPECT_FAILURE(rv, "Parsed invalid header: %s", hdr); michael@0: passed(hdr); michael@0: return true; michael@0: } michael@0: michael@0: michael@0: int michael@0: main(int32_t argc, char *argv[]) michael@0: { michael@0: nsresult rv; michael@0: ScopedXPCOM xpcom("STS Parser Tests"); michael@0: if (xpcom.failed()) michael@0: return -1; michael@0: // Initialize a profile folder to ensure a clean shutdown. michael@0: nsCOMPtr profile = xpcom.GetProfileDirectory(); michael@0: if (!profile) { michael@0: fail("Couldn't get the profile directory."); michael@0: return -1; michael@0: } michael@0: michael@0: // grab handle to the service michael@0: nsCOMPtr sss; michael@0: sss = do_GetService("@mozilla.org/ssservice;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, -1); michael@0: michael@0: int rv0, rv1; michael@0: michael@0: nsTArray rvs(24); michael@0: michael@0: // *** parsing tests michael@0: printf("*** Attempting to parse valid STS headers ...\n"); michael@0: michael@0: // SHOULD SUCCEED: michael@0: rvs.AppendElement(TestSuccess("max-age=100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age =100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess(" max-age=100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age = 100 ", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age = \"100\" ", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age=\"100\"", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess(" max-age =\"100\" ", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("\tmax-age\t=\t\"100\"\t", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age = 100 ", false, 100, false, sss)); michael@0: michael@0: rvs.AppendElement(TestSuccess("maX-aGe=100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("MAX-age =100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("max-AGE=100", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("Max-Age = 100 ", false, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("MAX-AGE = 100 ", false, 100, false, sss)); michael@0: michael@0: rvs.AppendElement(TestSuccess("max-age=100;includeSubdomains", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age=100\t; includeSubdomains", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess(" max-age=100; includeSubdomains", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age = 100 ; includeSubdomains", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age = 100 ; includeSubdomains", false, 100, true, sss)); michael@0: michael@0: rvs.AppendElement(TestSuccess("maX-aGe=100; includeSUBDOMAINS", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("MAX-age =100; includeSubDomains", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("max-AGE=100; iNcLuDeSuBdoMaInS", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("Max-Age = 100; includesubdomains ", false, 100, true, sss)); michael@0: rvs.AppendElement(TestSuccess("INCLUDESUBDOMAINS;MaX-AgE = 100 ", false, 100, true, sss)); michael@0: // Turns out, the actual directive is entirely optional (hence the michael@0: // trailing semicolon) michael@0: rvs.AppendElement(TestSuccess("max-age=100;includeSubdomains;", true, 100, true, sss)); michael@0: michael@0: // these are weird tests, but are testing that some extended syntax is michael@0: // still allowed (but it is ignored) michael@0: rvs.AppendElement(TestSuccess("max-age=100 ; includesubdomainsSomeStuff", true, 100, false, sss)); michael@0: rvs.AppendElement(TestSuccess("\r\n\t\t \tcompletelyUnrelated = foobar; max-age= 34520103 \t \t; alsoUnrelated;asIsThis;\tincludeSubdomains\t\t \t", true, 34520103, true, sss)); michael@0: rvs.AppendElement(TestSuccess("max-age=100; unrelated=\"quoted \\\"thingy\\\"\"", true, 100, false, sss)); michael@0: michael@0: rv0 = rvs.Contains(false) ? 1 : 0; michael@0: if (rv0 == 0) michael@0: passed("Successfully Parsed STS headers with mixed case and LWS"); michael@0: michael@0: rvs.Clear(); michael@0: michael@0: // SHOULD FAIL: michael@0: printf("*** Attempting to parse invalid STS headers (should not parse)...\n"); michael@0: // invalid max-ages michael@0: rvs.AppendElement(TestFailure("max-age", sss)); michael@0: rvs.AppendElement(TestFailure("max-age ", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=p", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=*1p2", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=.20032", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=!20032", sss)); michael@0: rvs.AppendElement(TestFailure("max-age==20032", sss)); michael@0: michael@0: // invalid headers michael@0: rvs.AppendElement(TestFailure("foobar", sss)); michael@0: rvs.AppendElement(TestFailure("maxage=100", sss)); michael@0: rvs.AppendElement(TestFailure("maxa-ge=100", sss)); michael@0: rvs.AppendElement(TestFailure("max-ag=100", sss)); michael@0: rvs.AppendElement(TestFailure("includesubdomains", sss)); michael@0: rvs.AppendElement(TestFailure(";", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=\"100", sss)); michael@0: // The max-age directive here doesn't conform to the spec, so it MUST michael@0: // be ignored. Consequently, the REQUIRED max-age directive is not michael@0: // present in this header, and so it is invalid. michael@0: rvs.AppendElement(TestFailure("max-age=100, max-age=200; includeSubdomains", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=100 includesubdomains", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=100 bar foo", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=100randomstuffhere", sss)); michael@0: // All directives MUST appear only once in an STS header field. michael@0: rvs.AppendElement(TestFailure("max-age=100; max-age=200", sss)); michael@0: rvs.AppendElement(TestFailure("includeSubdomains; max-age=200; includeSubdomains", sss)); michael@0: rvs.AppendElement(TestFailure("max-age=200; includeSubdomains; includeSubdomains", sss)); michael@0: // The includeSubdomains directive is valueless. michael@0: rvs.AppendElement(TestFailure("max-age=100; includeSubdomains=unexpected", sss)); michael@0: // LWS must have at least one space or horizontal tab michael@0: rvs.AppendElement(TestFailure("\r\nmax-age=200", sss)); michael@0: michael@0: rv1 = rvs.Contains(false) ? 1 : 0; michael@0: if (rv1 == 0) michael@0: passed("Avoided parsing invalid STS headers"); michael@0: michael@0: return (rv0 + rv1); michael@0: }