Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | // Copyright (c) 2011, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // memory_range_unittest.cc: Unit tests for google_breakpad::MemoryRange. |
michael@0 | 31 | |
michael@0 | 32 | #include "breakpad_googletest_includes.h" |
michael@0 | 33 | #include "common/memory_range.h" |
michael@0 | 34 | |
michael@0 | 35 | using google_breakpad::MemoryRange; |
michael@0 | 36 | using testing::Message; |
michael@0 | 37 | |
michael@0 | 38 | namespace { |
michael@0 | 39 | |
michael@0 | 40 | const uint32_t kBuffer[10] = { 0 }; |
michael@0 | 41 | const size_t kBufferSize = sizeof(kBuffer); |
michael@0 | 42 | const uint8_t* kBufferPointer = reinterpret_cast<const uint8_t*>(kBuffer); |
michael@0 | 43 | |
michael@0 | 44 | // Test vectors for verifying Covers, GetData, and Subrange. |
michael@0 | 45 | const struct { |
michael@0 | 46 | bool valid; |
michael@0 | 47 | size_t offset; |
michael@0 | 48 | size_t length; |
michael@0 | 49 | } kSubranges[] = { |
michael@0 | 50 | { true, 0, 0 }, |
michael@0 | 51 | { true, 0, 2 }, |
michael@0 | 52 | { true, 0, kBufferSize }, |
michael@0 | 53 | { true, 2, 0 }, |
michael@0 | 54 | { true, 2, 4 }, |
michael@0 | 55 | { true, 2, kBufferSize - 2 }, |
michael@0 | 56 | { true, kBufferSize - 1, 1 }, |
michael@0 | 57 | { false, kBufferSize, 0 }, |
michael@0 | 58 | { false, kBufferSize, static_cast<size_t>(-1) }, |
michael@0 | 59 | { false, kBufferSize + 1, 0 }, |
michael@0 | 60 | { false, static_cast<size_t>(-1), 2 }, |
michael@0 | 61 | { false, 1, kBufferSize }, |
michael@0 | 62 | { false, kBufferSize - 1, 2 }, |
michael@0 | 63 | { false, 0, static_cast<size_t>(-1) }, |
michael@0 | 64 | { false, 1, static_cast<size_t>(-1) }, |
michael@0 | 65 | }; |
michael@0 | 66 | const size_t kNumSubranges = sizeof(kSubranges) / sizeof(kSubranges[0]); |
michael@0 | 67 | |
michael@0 | 68 | // Test vectors for verifying GetArrayElement. |
michael@0 | 69 | const struct { |
michael@0 | 70 | size_t offset; |
michael@0 | 71 | size_t size; |
michael@0 | 72 | size_t index; |
michael@0 | 73 | const void* const pointer; |
michael@0 | 74 | } kElements[] = { |
michael@0 | 75 | // Valid array elemenets |
michael@0 | 76 | { 0, 1, 0, kBufferPointer }, |
michael@0 | 77 | { 0, 1, 1, kBufferPointer + 1 }, |
michael@0 | 78 | { 0, 1, kBufferSize - 1, kBufferPointer + kBufferSize - 1 }, |
michael@0 | 79 | { 0, 2, 1, kBufferPointer + 2 }, |
michael@0 | 80 | { 0, 4, 2, kBufferPointer + 8 }, |
michael@0 | 81 | { 0, 4, 9, kBufferPointer + 36 }, |
michael@0 | 82 | { kBufferSize - 1, 1, 0, kBufferPointer + kBufferSize - 1 }, |
michael@0 | 83 | // Invalid array elemenets |
michael@0 | 84 | { 0, 1, kBufferSize, NULL }, |
michael@0 | 85 | { 0, 4, 10, NULL }, |
michael@0 | 86 | { kBufferSize - 1, 1, 1, NULL }, |
michael@0 | 87 | { kBufferSize - 1, 2, 0, NULL }, |
michael@0 | 88 | { kBufferSize, 1, 0, NULL }, |
michael@0 | 89 | }; |
michael@0 | 90 | const size_t kNumElements = sizeof(kElements) / sizeof(kElements[0]); |
michael@0 | 91 | |
michael@0 | 92 | } // namespace |
michael@0 | 93 | |
michael@0 | 94 | TEST(MemoryRangeTest, DefaultConstructor) { |
michael@0 | 95 | MemoryRange range; |
michael@0 | 96 | EXPECT_EQ(NULL, range.data()); |
michael@0 | 97 | EXPECT_EQ(0U, range.length()); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | TEST(MemoryRangeTest, ConstructorWithDataAndLength) { |
michael@0 | 101 | MemoryRange range(kBuffer, kBufferSize); |
michael@0 | 102 | EXPECT_EQ(kBufferPointer, range.data()); |
michael@0 | 103 | EXPECT_EQ(kBufferSize, range.length()); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | TEST(MemoryRangeTest, Reset) { |
michael@0 | 107 | MemoryRange range; |
michael@0 | 108 | range.Reset(); |
michael@0 | 109 | EXPECT_EQ(NULL, range.data()); |
michael@0 | 110 | EXPECT_EQ(0U, range.length()); |
michael@0 | 111 | |
michael@0 | 112 | range.Set(kBuffer, kBufferSize); |
michael@0 | 113 | EXPECT_EQ(kBufferPointer, range.data()); |
michael@0 | 114 | EXPECT_EQ(kBufferSize, range.length()); |
michael@0 | 115 | |
michael@0 | 116 | range.Reset(); |
michael@0 | 117 | EXPECT_EQ(NULL, range.data()); |
michael@0 | 118 | EXPECT_EQ(0U, range.length()); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | TEST(MemoryRangeTest, Set) { |
michael@0 | 122 | MemoryRange range; |
michael@0 | 123 | range.Set(kBuffer, kBufferSize); |
michael@0 | 124 | EXPECT_EQ(kBufferPointer, range.data()); |
michael@0 | 125 | EXPECT_EQ(kBufferSize, range.length()); |
michael@0 | 126 | |
michael@0 | 127 | range.Set(NULL, 0); |
michael@0 | 128 | EXPECT_EQ(NULL, range.data()); |
michael@0 | 129 | EXPECT_EQ(0U, range.length()); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | TEST(MemoryRangeTest, SubrangeOfEmptyMemoryRange) { |
michael@0 | 133 | MemoryRange range; |
michael@0 | 134 | MemoryRange subrange = range.Subrange(0, 10); |
michael@0 | 135 | EXPECT_EQ(NULL, subrange.data()); |
michael@0 | 136 | EXPECT_EQ(0U, subrange.length()); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | TEST(MemoryRangeTest, SubrangeAndGetData) { |
michael@0 | 140 | MemoryRange range(kBuffer, kBufferSize); |
michael@0 | 141 | for (size_t i = 0; i < kNumSubranges; ++i) { |
michael@0 | 142 | bool valid = kSubranges[i].valid; |
michael@0 | 143 | size_t sub_offset = kSubranges[i].offset; |
michael@0 | 144 | size_t sub_length = kSubranges[i].length; |
michael@0 | 145 | SCOPED_TRACE(Message() << "offset=" << sub_offset |
michael@0 | 146 | << ", length=" << sub_length); |
michael@0 | 147 | |
michael@0 | 148 | MemoryRange subrange = range.Subrange(sub_offset, sub_length); |
michael@0 | 149 | if (valid) { |
michael@0 | 150 | EXPECT_TRUE(range.Covers(sub_offset, sub_length)); |
michael@0 | 151 | EXPECT_EQ(kBufferPointer + sub_offset, |
michael@0 | 152 | range.GetData(sub_offset, sub_length)); |
michael@0 | 153 | EXPECT_EQ(kBufferPointer + sub_offset, subrange.data()); |
michael@0 | 154 | EXPECT_EQ(sub_length, subrange.length()); |
michael@0 | 155 | } else { |
michael@0 | 156 | EXPECT_FALSE(range.Covers(sub_offset, sub_length)); |
michael@0 | 157 | EXPECT_EQ(NULL, range.GetData(sub_offset, sub_length)); |
michael@0 | 158 | EXPECT_EQ(NULL, subrange.data()); |
michael@0 | 159 | EXPECT_EQ(0U, subrange.length()); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | TEST(MemoryRangeTest, GetDataWithTemplateType) { |
michael@0 | 165 | MemoryRange range(kBuffer, kBufferSize); |
michael@0 | 166 | const char* char_pointer = range.GetData<char>(0); |
michael@0 | 167 | EXPECT_EQ(reinterpret_cast<const char*>(kBufferPointer), char_pointer); |
michael@0 | 168 | const int* int_pointer = range.GetData<int>(0); |
michael@0 | 169 | EXPECT_EQ(reinterpret_cast<const int*>(kBufferPointer), int_pointer); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | TEST(MemoryRangeTest, GetArrayElement) { |
michael@0 | 173 | MemoryRange range(kBuffer, kBufferSize); |
michael@0 | 174 | for (size_t i = 0; i < kNumElements; ++i) { |
michael@0 | 175 | size_t element_offset = kElements[i].offset; |
michael@0 | 176 | size_t element_size = kElements[i].size; |
michael@0 | 177 | unsigned element_index = kElements[i].index; |
michael@0 | 178 | const void* const element_pointer = kElements[i].pointer; |
michael@0 | 179 | SCOPED_TRACE(Message() << "offset=" << element_offset |
michael@0 | 180 | << ", size=" << element_size |
michael@0 | 181 | << ", index=" << element_index); |
michael@0 | 182 | EXPECT_EQ(element_pointer, range.GetArrayElement( |
michael@0 | 183 | element_offset, element_size, element_index)); |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | TEST(MemoryRangeTest, GetArrayElmentWithTemplateType) { |
michael@0 | 188 | MemoryRange range(kBuffer, kBufferSize); |
michael@0 | 189 | const char* char_pointer = range.GetArrayElement<char>(0, 0); |
michael@0 | 190 | EXPECT_EQ(reinterpret_cast<const char*>(kBufferPointer), char_pointer); |
michael@0 | 191 | const int* int_pointer = range.GetArrayElement<int>(0, 0); |
michael@0 | 192 | EXPECT_EQ(reinterpret_cast<const int*>(kBufferPointer), int_pointer); |
michael@0 | 193 | } |