tor-browser

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

TestStorageOriginAttributes.cpp (7157B)


      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 
      7 #include "gtest/gtest.h"
      8 #include "mozilla/StorageOriginAttributes.h"
      9 
     10 namespace mozilla::dom::quota::test {
     11 
     12 TEST(DOM_Quota_StorageOriginAttributes, Constructor_Default)
     13 {
     14  {
     15    StorageOriginAttributes originAttributes;
     16 
     17    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
     18    ASSERT_EQ(originAttributes.UserContextId(), 0u);
     19  }
     20 }
     21 
     22 TEST(DOM_Quota_StorageOriginAttributes, Constructor_InIsolatedMozbrowser)
     23 {
     24  {
     25    StorageOriginAttributes originAttributes(/* aInIsolatedMozBrowser */ false);
     26 
     27    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
     28    ASSERT_EQ(originAttributes.UserContextId(), 0u);
     29  }
     30 
     31  {
     32    StorageOriginAttributes originAttributes(/* aInIsolatedMozBrowser */ true);
     33 
     34    ASSERT_TRUE(originAttributes.InIsolatedMozBrowser());
     35    ASSERT_EQ(originAttributes.UserContextId(), 0u);
     36  }
     37 }
     38 
     39 TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_NoOriginAttributes)
     40 {
     41  {
     42    StorageOriginAttributes originAttributes;
     43    nsCString originNoSuffix;
     44    bool ok = originAttributes.PopulateFromOrigin("https://www.example.com"_ns,
     45                                                  originNoSuffix);
     46 
     47    ASSERT_TRUE(ok);
     48    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
     49    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
     50  }
     51 }
     52 
     53 TEST(DOM_Quota_StorageOriginAttributes,
     54     PopulateFromOrigin_InvalidOriginAttribute)
     55 {
     56  {
     57    StorageOriginAttributes originAttributes;
     58    nsCString originNoSuffix;
     59    bool ok = originAttributes.PopulateFromOrigin(
     60        "https://www.example.com^foo=bar"_ns, originNoSuffix);
     61 
     62    ASSERT_FALSE(ok);
     63    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
     64    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
     65  }
     66 }
     67 
     68 TEST(DOM_Quota_StorageOriginAttributes,
     69     PopulateFromOrigin_InIsolatedMozBrowser_Valid)
     70 {
     71  {
     72    StorageOriginAttributes originAttributes;
     73    nsCString originNoSuffix;
     74    bool ok = originAttributes.PopulateFromOrigin(
     75        "https://www.example.com^inBrowser=1"_ns, originNoSuffix);
     76 
     77    ASSERT_TRUE(ok);
     78    ASSERT_TRUE(originAttributes.InIsolatedMozBrowser());
     79    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
     80  }
     81 }
     82 
     83 TEST(DOM_Quota_StorageOriginAttributes,
     84     PopulateFromOrigin_InIsolatedMozBrowser_Invalid)
     85 {
     86  {
     87    StorageOriginAttributes originAttributes;
     88    nsCString originNoSuffix;
     89    bool ok = originAttributes.PopulateFromOrigin(
     90        "https://www.example.com^inBrowser=0"_ns, originNoSuffix);
     91 
     92    ASSERT_FALSE(ok);
     93    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
     94    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
     95  }
     96 
     97  {
     98    StorageOriginAttributes originAttributes;
     99    nsCString originNoSuffix;
    100    bool ok = originAttributes.PopulateFromOrigin(
    101        "https://www.example.com^inBrowser=true"_ns, originNoSuffix);
    102 
    103    ASSERT_FALSE(ok);
    104    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
    105    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    106  }
    107 
    108  {
    109    StorageOriginAttributes originAttributes;
    110    nsCString originNoSuffix;
    111    bool ok = originAttributes.PopulateFromOrigin(
    112        "https://www.example.com^inBrowser=false"_ns, originNoSuffix);
    113 
    114    ASSERT_FALSE(ok);
    115    ASSERT_FALSE(originAttributes.InIsolatedMozBrowser());
    116    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    117  }
    118 }
    119 
    120 TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_UserContextId_Valid)
    121 {
    122  {
    123    StorageOriginAttributes originAttributes;
    124    nsCString originNoSuffix;
    125    bool ok = originAttributes.PopulateFromOrigin(
    126        "https://www.example.com^userContextId=1"_ns, originNoSuffix);
    127 
    128    ASSERT_TRUE(ok);
    129    ASSERT_EQ(originAttributes.UserContextId(), 1u);
    130    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    131  }
    132 
    133  {
    134    StorageOriginAttributes originAttributes;
    135    nsCString originNoSuffix;
    136    bool ok = originAttributes.PopulateFromOrigin(
    137        "https://www.example.com^userContextId=42"_ns, originNoSuffix);
    138 
    139    ASSERT_TRUE(ok);
    140    ASSERT_EQ(originAttributes.UserContextId(), 42u);
    141    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    142  }
    143 }
    144 
    145 TEST(DOM_Quota_StorageOriginAttributes,
    146     PopulateFromOrigin_UserContextId_Invalid)
    147 {
    148  {
    149    StorageOriginAttributes originAttributes;
    150    nsCString originNoSuffix;
    151    bool ok = originAttributes.PopulateFromOrigin(
    152        "https://www.example.com^userContextId=foo"_ns, originNoSuffix);
    153 
    154    ASSERT_FALSE(ok);
    155    ASSERT_EQ(originAttributes.UserContextId(), 0u);
    156    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    157  }
    158 }
    159 
    160 TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_Mixed_Valid)
    161 {
    162  {
    163    StorageOriginAttributes originAttributes;
    164    nsCString originNoSuffix;
    165    bool ok = originAttributes.PopulateFromOrigin(
    166        "https://www.example.com^inBrowser=1&userContextId=1"_ns,
    167        originNoSuffix);
    168 
    169    ASSERT_TRUE(ok);
    170    ASSERT_TRUE(originAttributes.InIsolatedMozBrowser());
    171    ASSERT_EQ(originAttributes.UserContextId(), 1u);
    172    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    173  }
    174 }
    175 
    176 TEST(DOM_Quota_StorageOriginAttributes, PopulateFromOrigin_Mixed_Invalid)
    177 {
    178  {
    179    StorageOriginAttributes originAttributes;
    180    nsCString originNoSuffix;
    181    bool ok = originAttributes.PopulateFromOrigin(
    182        "https://www.example.com^inBrowser=1&userContextId=1&foo=bar"_ns,
    183        originNoSuffix);
    184 
    185    ASSERT_FALSE(ok);
    186    ASSERT_TRUE(originAttributes.InIsolatedMozBrowser());
    187    ASSERT_EQ(originAttributes.UserContextId(), 1u);
    188    ASSERT_TRUE(originNoSuffix.Equals("https://www.example.com"_ns));
    189  }
    190 }
    191 
    192 TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_NoOriginAttributes)
    193 {
    194  {
    195    StorageOriginAttributes originAttributes;
    196    nsCString suffix;
    197    originAttributes.CreateSuffix(suffix);
    198 
    199    ASSERT_TRUE(suffix.IsEmpty());
    200  }
    201 }
    202 
    203 TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_InIsolatedMozbrowser)
    204 {
    205  {
    206    StorageOriginAttributes originAttributes;
    207    originAttributes.SetInIsolatedMozBrowser(true);
    208    nsCString suffix;
    209    originAttributes.CreateSuffix(suffix);
    210 
    211    ASSERT_TRUE(suffix.Equals("^inBrowser=1"_ns));
    212  }
    213 }
    214 
    215 TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_UserContextId)
    216 {
    217  {
    218    StorageOriginAttributes originAttributes;
    219    originAttributes.SetUserContextId(42);
    220    nsCString suffix;
    221    originAttributes.CreateSuffix(suffix);
    222 
    223    ASSERT_TRUE(suffix.Equals("^userContextId=42"_ns));
    224  }
    225 }
    226 
    227 TEST(DOM_Quota_StorageOriginAttributes, CreateSuffix_Mixed)
    228 {
    229  {
    230    StorageOriginAttributes originAttributes;
    231    originAttributes.SetInIsolatedMozBrowser(true);
    232    originAttributes.SetUserContextId(42);
    233    nsCString suffix;
    234    originAttributes.CreateSuffix(suffix);
    235 
    236    ASSERT_TRUE(suffix.Equals("^inBrowser=1&userContextId=42"_ns));
    237  }
    238 }
    239 
    240 }  // namespace mozilla::dom::quota::test