toolkit/crashreporter/google-breakpad/src/client/minidump_file_writer_unittest.cc

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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) 2006, 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 // Author: waylonis@google.com (Dan Waylonis)
michael@0 31
michael@0 32 /*
michael@0 33 g++ -I../ ../common/convert_UTF.c \
michael@0 34 ../common/string_conversion.cc \
michael@0 35 minidump_file_writer.cc \
michael@0 36 minidump_file_writer_unittest.cc \
michael@0 37 -o minidump_file_writer_unittest
michael@0 38 */
michael@0 39
michael@0 40 #include <fcntl.h>
michael@0 41 #include <unistd.h>
michael@0 42
michael@0 43 #include "minidump_file_writer-inl.h"
michael@0 44
michael@0 45 using google_breakpad::MinidumpFileWriter;
michael@0 46
michael@0 47 #define ASSERT_TRUE(cond) \
michael@0 48 if (!(cond)) { \
michael@0 49 fprintf(stderr, "FAILED: %s at %s:%d\n", #cond, __FILE__, __LINE__); \
michael@0 50 return false; \
michael@0 51 }
michael@0 52
michael@0 53 #define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2))
michael@0 54 #define ASSERT_NE(e1, e2) ASSERT_TRUE((e1) != (e2))
michael@0 55
michael@0 56 struct StringStructure {
michael@0 57 unsigned long integer_value;
michael@0 58 MDLocationDescriptor first_string;
michael@0 59 MDLocationDescriptor second_string;
michael@0 60 };
michael@0 61
michael@0 62 struct ArrayStructure {
michael@0 63 unsigned char char_value;
michael@0 64 unsigned short short_value;
michael@0 65 unsigned long long_value;
michael@0 66 };
michael@0 67
michael@0 68 typedef struct {
michael@0 69 unsigned long count;
michael@0 70 ArrayStructure array[0];
michael@0 71 } ObjectAndArrayStructure;
michael@0 72
michael@0 73 static bool WriteFile(const char *path) {
michael@0 74 MinidumpFileWriter writer;
michael@0 75 if (writer.Open(path)) {
michael@0 76 // Test a single structure
michael@0 77 google_breakpad::TypedMDRVA<StringStructure> strings(&writer);
michael@0 78 ASSERT_TRUE(strings.Allocate());
michael@0 79 strings.get()->integer_value = 0xBEEF;
michael@0 80 const char *first = "First String";
michael@0 81 ASSERT_TRUE(writer.WriteString(first, 0, &strings.get()->first_string));
michael@0 82 const wchar_t *second = L"Second String";
michael@0 83 ASSERT_TRUE(writer.WriteString(second, 0, &strings.get()->second_string));
michael@0 84
michael@0 85 // Test an array structure
michael@0 86 google_breakpad::TypedMDRVA<ArrayStructure> array(&writer);
michael@0 87 unsigned int count = 10;
michael@0 88 ASSERT_TRUE(array.AllocateArray(count));
michael@0 89 for (unsigned char i = 0; i < count; ++i) {
michael@0 90 ArrayStructure local;
michael@0 91 local.char_value = i;
michael@0 92 local.short_value = i + 1;
michael@0 93 local.long_value = i + 2;
michael@0 94 ASSERT_TRUE(array.CopyIndex(i, &local));
michael@0 95 }
michael@0 96
michael@0 97 // Test an object followed by an array
michael@0 98 google_breakpad::TypedMDRVA<ObjectAndArrayStructure> obj_array(&writer);
michael@0 99 ASSERT_TRUE(obj_array.AllocateObjectAndArray(count,
michael@0 100 sizeof(ArrayStructure)));
michael@0 101 obj_array.get()->count = count;
michael@0 102 for (unsigned char i = 0; i < count; ++i) {
michael@0 103 ArrayStructure local;
michael@0 104 local.char_value = i;
michael@0 105 local.short_value = i + 1;
michael@0 106 local.long_value = i + 2;
michael@0 107 ASSERT_TRUE(obj_array.CopyIndexAfterObject(i, &local, sizeof(local)));
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111 return writer.Close();
michael@0 112 }
michael@0 113
michael@0 114 static bool CompareFile(const char *path) {
michael@0 115 unsigned long expected[] = {
michael@0 116 #if defined(__BIG_ENDIAN__)
michael@0 117 0x0000beef, 0x0000001e, 0x00000018, 0x00000020, 0x00000038, 0x00000000,
michael@0 118 0x00000018, 0x00460069, 0x00720073, 0x00740020, 0x00530074, 0x00720069,
michael@0 119 0x006e0067, 0x00000000, 0x0000001a, 0x00530065, 0x0063006f, 0x006e0064,
michael@0 120 0x00200053, 0x00740072, 0x0069006e, 0x00670000, 0x00000001, 0x00000002,
michael@0 121 0x01000002, 0x00000003, 0x02000003, 0x00000004, 0x03000004, 0x00000005,
michael@0 122 0x04000005, 0x00000006, 0x05000006, 0x00000007, 0x06000007, 0x00000008,
michael@0 123 0x07000008, 0x00000009, 0x08000009, 0x0000000a, 0x0900000a, 0x0000000b,
michael@0 124 0x0000000a, 0x00000001, 0x00000002, 0x01000002, 0x00000003, 0x02000003,
michael@0 125 0x00000004, 0x03000004, 0x00000005, 0x04000005, 0x00000006, 0x05000006,
michael@0 126 0x00000007, 0x06000007, 0x00000008, 0x07000008, 0x00000009, 0x08000009,
michael@0 127 0x0000000a, 0x0900000a, 0x0000000b, 0x00000000
michael@0 128 #else
michael@0 129 0x0000beef, 0x0000001e, 0x00000018, 0x00000020,
michael@0 130 0x00000038, 0x00000000, 0x00000018, 0x00690046,
michael@0 131 0x00730072, 0x00200074, 0x00740053, 0x00690072,
michael@0 132 0x0067006e, 0x00000000, 0x0000001a, 0x00650053,
michael@0 133 0x006f0063, 0x0064006e, 0x00530020, 0x00720074,
michael@0 134 0x006e0069, 0x00000067, 0x00011e00, 0x00000002,
michael@0 135 0x00021e01, 0x00000003, 0x00031e02, 0x00000004,
michael@0 136 0x00041e03, 0x00000005, 0x00051e04, 0x00000006,
michael@0 137 0x00061e05, 0x00000007, 0x00071e06, 0x00000008,
michael@0 138 0x00081e07, 0x00000009, 0x00091e08, 0x0000000a,
michael@0 139 0x000a1e09, 0x0000000b, 0x0000000a, 0x00011c00,
michael@0 140 0x00000002, 0x00021c01, 0x00000003, 0x00031c02,
michael@0 141 0x00000004, 0x00041c03, 0x00000005, 0x00051c04,
michael@0 142 0x00000006, 0x00061c05, 0x00000007, 0x00071c06,
michael@0 143 0x00000008, 0x00081c07, 0x00000009, 0x00091c08,
michael@0 144 0x0000000a, 0x000a1c09, 0x0000000b, 0x00000000,
michael@0 145 #endif
michael@0 146 };
michael@0 147 size_t expected_byte_count = sizeof(expected);
michael@0 148 int fd = open(path, O_RDONLY, 0600);
michael@0 149 void *buffer = malloc(expected_byte_count);
michael@0 150 ASSERT_NE(fd, -1);
michael@0 151 ASSERT_TRUE(buffer);
michael@0 152 ASSERT_EQ(read(fd, buffer, expected_byte_count),
michael@0 153 static_cast<ssize_t>(expected_byte_count));
michael@0 154
michael@0 155 char *b1, *b2;
michael@0 156 b1 = reinterpret_cast<char*>(buffer);
michael@0 157 b2 = reinterpret_cast<char*>(expected);
michael@0 158 while (*b1 == *b2) {
michael@0 159 b1++;
michael@0 160 b2++;
michael@0 161 }
michael@0 162
michael@0 163 printf("%p\n", reinterpret_cast<void*>(b1 - (char*)buffer));
michael@0 164
michael@0 165 ASSERT_EQ(memcmp(buffer, expected, expected_byte_count), 0);
michael@0 166 return true;
michael@0 167 }
michael@0 168
michael@0 169 static bool RunTests() {
michael@0 170 const char *path = "/tmp/minidump_file_writer_unittest.dmp";
michael@0 171 ASSERT_TRUE(WriteFile(path));
michael@0 172 ASSERT_TRUE(CompareFile(path));
michael@0 173 unlink(path);
michael@0 174 return true;
michael@0 175 }
michael@0 176
michael@0 177 extern "C" int main(int argc, const char *argv[]) {
michael@0 178 return RunTests() ? 0 : 1;
michael@0 179 }

mercurial