1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/child_process_logging.h"
#include <map>
#include <string>
#import <Foundation/Foundation.h>
#include "base/debug/crash_logging.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
typedef PlatformTest ChildProcessLoggingTest;
namespace {
// Class to mock breakpad's setkeyvalue/clearkeyvalue functions needed for
// SetActiveRendererURLImpl.
// The Keys are stored in a static dictionary and methods are provided to
// verify correctness.
class MockBreakpadKeyValueStore {
public:
MockBreakpadKeyValueStore() {
// Only one of these objects can be active at once.
DCHECK(dict == NULL);
dict = new std::map<std::string, std::string>;
}
~MockBreakpadKeyValueStore() {
// Only one of these objects can be active at once.
DCHECK(dict != NULL);
delete dict;
dict = NULL;
}
static void SetKeyValue(const base::StringPiece& key,
const base::StringPiece& value) {
DCHECK(dict != NULL);
(*dict)[key.as_string()] = value.as_string();
}
static void ClearKeyValue(const base::StringPiece& key) {
DCHECK(dict != NULL);
dict->erase(key.as_string());
}
size_t CountDictionaryEntries() {
return dict->size();
}
bool VerifyDictionaryContents(const std::string& url) {
using child_process_logging::kMaxNumCrashURLChunks;
using child_process_logging::kMaxNumURLChunkValueLength;
using child_process_logging::kUrlChunkFormatStr;
size_t num_url_chunks = CountDictionaryEntries();
EXPECT_LE(num_url_chunks, kMaxNumCrashURLChunks);
std::string accumulated_url;
for (size_t i = 0; i < num_url_chunks; ++i) {
// URL chunk names are 1-based.
std::string key = base::StringPrintf(kUrlChunkFormatStr, i + 1);
std::string value = (*dict)[key];
EXPECT_GT(value.length(), 0u);
EXPECT_LE(value.length(),
static_cast<size_t>(kMaxNumURLChunkValueLength));
accumulated_url += value;
}
return url == accumulated_url;
}
private:
static std::map<std::string, std::string>* dict;
DISALLOW_COPY_AND_ASSIGN(MockBreakpadKeyValueStore);
};
// static
std::map<std::string, std::string>* MockBreakpadKeyValueStore::dict;
} // namespace
// Call through to SetActiveURLImpl using the functions from
// MockBreakpadKeyValueStore.
void SetActiveURLWithMock(const GURL& url) {
using child_process_logging::SetActiveURLImpl;
base::debug::SetCrashKeyValueFuncT setFunc =
MockBreakpadKeyValueStore::SetKeyValue;
base::debug::ClearCrashKeyValueFuncT clearFunc =
MockBreakpadKeyValueStore::ClearKeyValue;
SetActiveURLImpl(url, setFunc, clearFunc);
}
TEST_F(ChildProcessLoggingTest, TestUrlSplitting) {
using child_process_logging::kMaxNumCrashURLChunks;
using child_process_logging::kMaxNumURLChunkValueLength;
const std::string short_url("http://abc/");
std::string long_url("http://");
std::string overflow_url("http://");
long_url += std::string(kMaxNumURLChunkValueLength * 2, 'a');
long_url += "/";
int max_num_chars_stored_in_dump = kMaxNumURLChunkValueLength *
kMaxNumCrashURLChunks;
overflow_url += std::string(max_num_chars_stored_in_dump + 1, 'a');
overflow_url += "/";
// Check that Clearing NULL URL works.
MockBreakpadKeyValueStore mock;
SetActiveURLWithMock(GURL());
EXPECT_EQ(0u, mock.CountDictionaryEntries());
// Check that we can set a URL.
SetActiveURLWithMock(GURL(short_url.c_str()));
EXPECT_TRUE(mock.VerifyDictionaryContents(short_url));
EXPECT_EQ(1u, mock.CountDictionaryEntries());
SetActiveURLWithMock(GURL());
EXPECT_EQ(0u, mock.CountDictionaryEntries());
// Check that we can replace a long url with a short url.
SetActiveURLWithMock(GURL(long_url.c_str()));
EXPECT_TRUE(mock.VerifyDictionaryContents(long_url));
SetActiveURLWithMock(GURL(short_url.c_str()));
EXPECT_TRUE(mock.VerifyDictionaryContents(short_url));
SetActiveURLWithMock(GURL());
EXPECT_EQ(0u, mock.CountDictionaryEntries());
// Check that overflow works correctly.
SetActiveURLWithMock(GURL(overflow_url.c_str()));
EXPECT_TRUE(mock.VerifyDictionaryContents(
overflow_url.substr(0, max_num_chars_stored_in_dump)));
SetActiveURLWithMock(GURL());
EXPECT_EQ(0u, mock.CountDictionaryEntries());
}
|