TestReportingEndpointsParser.cpp (3751B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #include "gtest/gtest.h" 7 #include "mozilla/dom/ReportingHeader.h" 8 #include "nsIURI.h" 9 #include "nsNetUtil.h" 10 11 using namespace mozilla; 12 using namespace mozilla::dom; 13 14 TEST(ReportingEndpointsParser, Basic) 15 { 16 nsCOMPtr<nsIURI> uri1; 17 nsCOMPtr<nsIURI> uri2; 18 19 nsresult rv = 20 NS_NewURI(getter_AddRefs(uri1), "https://example.com/csp-reports"); 21 ASSERT_EQ(NS_OK, rv); 22 rv = NS_NewURI(getter_AddRefs(uri2), "https://example.com/hpkp-reports"); 23 ASSERT_EQ(NS_OK, rv); 24 25 bool urlEqual = false; 26 27 // Empty header 28 UniquePtr<ReportingHeader::Client> client = 29 ReportingHeader::ParseReportingEndpointsHeader(""_ns, uri1); 30 ASSERT_TRUE(!client); 31 32 // Empty header 33 client = ReportingHeader::ParseReportingEndpointsHeader(" "_ns, uri1); 34 ASSERT_TRUE(!client); 35 36 // Single client 37 client = ReportingHeader::ParseReportingEndpointsHeader( 38 "csp-endpoint=\"https://example.com/csp-reports\""_ns, uri1); 39 ASSERT_TRUE(client); 40 ASSERT_EQ((uint32_t)1, client->mGroups.Length()); 41 ASSERT_TRUE(client->mGroups.ElementAt(0).mName.EqualsLiteral("csp-endpoint")); 42 ASSERT_EQ((uint32_t)1, client->mGroups.ElementAt(0).mEndpoints.Length()); 43 ASSERT_TRUE( 44 NS_SUCCEEDED( 45 client->mGroups.ElementAt(0).mEndpoints.ElementAt(0).mUrl->Equals( 46 uri1, &urlEqual)) && 47 urlEqual); 48 49 // 2 clients, different group names 50 client = ReportingHeader::ParseReportingEndpointsHeader( 51 "csp-endpoint=\"https://example.com/csp-reports\",\thpkp-endpoint=\"https://example.com/hpkp-reports\""_ns, 52 uri1); 53 ASSERT_TRUE(client); 54 ASSERT_EQ((uint32_t)2, client->mGroups.Length()); 55 ASSERT_TRUE(client->mGroups.ElementAt(0).mName.EqualsLiteral("csp-endpoint")); 56 ASSERT_EQ((uint32_t)1, client->mGroups.ElementAt(0).mEndpoints.Length()); 57 ASSERT_TRUE( 58 NS_SUCCEEDED( 59 client->mGroups.ElementAt(0).mEndpoints.ElementAt(0).mUrl->Equals( 60 uri1, &urlEqual)) && 61 urlEqual); 62 ASSERT_TRUE( 63 client->mGroups.ElementAt(1).mName.EqualsLiteral("hpkp-endpoint")); 64 ASSERT_EQ((uint32_t)1, client->mGroups.ElementAt(0).mEndpoints.Length()); 65 ASSERT_TRUE( 66 NS_SUCCEEDED( 67 client->mGroups.ElementAt(1).mEndpoints.ElementAt(0).mUrl->Equals( 68 uri2, &urlEqual)) && 69 urlEqual); 70 71 // Single client, passed in as an inner list with parameters to ignore 72 client = ReportingHeader::ParseReportingEndpointsHeader( 73 "csp-endpoint=(\"https://example.com/csp-reports\" 5);valid"_ns, uri1); 74 ASSERT_TRUE(client); 75 ASSERT_EQ((uint32_t)1, client->mGroups.Length()); 76 ASSERT_TRUE(client->mGroups.ElementAt(0).mName.EqualsLiteral("csp-endpoint")); 77 ASSERT_EQ((uint32_t)1, client->mGroups.ElementAt(0).mEndpoints.Length()); 78 ASSERT_TRUE( 79 NS_SUCCEEDED( 80 client->mGroups.ElementAt(0).mEndpoints.ElementAt(0).mUrl->Equals( 81 uri1, &urlEqual)) && 82 urlEqual); 83 84 // Single client, key's value is an empty string 85 client = ReportingHeader::ParseReportingEndpointsHeader( 86 "csp-endpoint=\" \""_ns, uri1); 87 ASSERT_TRUE(client); 88 89 // Single client, key's value is a non-URL string 90 client = ReportingHeader::ParseReportingEndpointsHeader( 91 "csp-endpoint=\"Not URL syntax\""_ns, uri1); 92 ASSERT_TRUE(client); 93 94 // Single client, key's value cannot be translated to a String SFVItem 95 client = 96 ReportingHeader::ParseReportingEndpointsHeader("csp-endpoint=1"_ns, uri1); 97 ASSERT_TRUE(!client); 98 }