tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestNullPrincipalPrecursor.cpp (1900B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 #include "gtest/gtest.h"
      5 #include "mozilla/gtest/MozAssertions.h"
      6 #include "mozilla/NullPrincipal.h"
      7 #include "nsIURIMutator.h"
      8 #include "nsPrintfCString.h"
      9 
     10 namespace mozilla {
     11 
     12 TEST(NullPrincipalPrecursor, EscapingRoundTrips)
     13 {
     14  nsTArray<nsCString> inputs;
     15 
     16  inputs.AppendElements(mozilla::Span(std::array{
     17      "mailbox:///dev/shm/tmp5wkt9ff_.mozrunner/Mail/Local%20Folders/secure-mail?number=5"_ns,
     18  }));
     19 
     20  // Add a string for every ASCII byte both escaped and unescaped.
     21  for (uint8_t c = 0; c < 128; ++c) {
     22    inputs.AppendElement(nsPrintfCString("%02X: %c", c, (char)c));
     23    inputs.AppendElement(nsPrintfCString("%02X: %%%02X", c, c));
     24  }
     25 
     26  nsID dummyID{0xddf15eaf,
     27               0x3837,
     28               0x4678,
     29               {0x80, 0x3b, 0x86, 0x86, 0xe8, 0x17, 0x66, 0x71}};
     30  nsCOMPtr<nsIURI> baseURI = NullPrincipal::CreateURI(nullptr, &dummyID);
     31  ASSERT_TRUE(baseURI);
     32 
     33  for (auto& input : inputs) {
     34    // First build an escaped version of the input string using
     35    // `EscapePrecursorQuery`.
     36    nsCString escaped(input);
     37    NullPrincipal::EscapePrecursorQuery(escaped);
     38 
     39    // Make sure that this escaped URI round-trips through a `moz-nullprincipal`
     40    // URI's query without any additional escapes.
     41    nsCOMPtr<nsIURI> clone;
     42    EXPECT_NS_SUCCEEDED(
     43        NS_MutateURI(baseURI).SetQuery(escaped).Finalize(clone));
     44    nsCString query;
     45    EXPECT_NS_SUCCEEDED(clone->GetQuery(query));
     46    EXPECT_EQ(escaped, query);
     47 
     48    // Try to unescape our escaped URI and make sure we recover the input
     49    // string.
     50    nsCString unescaped(escaped);
     51    NullPrincipal::UnescapePrecursorQuery(unescaped);
     52    EXPECT_EQ(input, unescaped);
     53  }
     54 }
     55 
     56 }  // namespace mozilla