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