14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// found in the LICENSE file. 44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/crash_keys.h" 64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 74e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <map> 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <set> 94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <string> 104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/command_line.h" 124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/compiler_specific.h" 134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/debug/crash_logging.h" 144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/strings/string_piece.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/strings/stringprintf.h" 164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class CrashKeysTest : public testing::Test { 194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) public: 204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) virtual void SetUp() OVERRIDE { 214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) self_ = this; 224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) base::debug::SetCrashKeyReportingFunctions( 234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) &SetCrashKeyValue, &ClearCrashKey); 244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) crash_keys::RegisterChromeCrashKeys(); 254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) virtual void TearDown() OVERRIDE { 284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) base::debug::ResetCrashLoggingForTesting(); 294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) self_ = NULL; 304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) bool HasCrashKey(const std::string& key) { 334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) return keys_.find(key) != keys_.end(); 344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::string GetKeyValue(const std::string& key) { 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::map<std::string, std::string>::const_iterator it = keys_.find(key); 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (it == keys_.end()) 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return std::string(); 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return it->second; 414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) private: 444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static void SetCrashKeyValue(const base::StringPiece& key, 454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const base::StringPiece& value) { 464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) self_->keys_[key.as_string()] = value.as_string(); 474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static void ClearCrashKey(const base::StringPiece& key) { 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) self_->keys_.erase(key.as_string()); 514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static CrashKeysTest* self_; 544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::map<std::string, std::string> keys_; 564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)CrashKeysTest* CrashKeysTest::self_ = NULL; 594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(CrashKeysTest, Switches) { 614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Set three switches. 624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) { 634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) CommandLine command_line(CommandLine::NO_PROGRAM); 644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (int i = 1; i <= 3; ++i) 654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) command_line.AppendSwitch(base::StringPrintf("--flag-%d", i)); 664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) crash_keys::SetSwitchesFromCommandLine(&command_line); 674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--flag-1", GetKeyValue("switch-1")); 684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--flag-2", GetKeyValue("switch-2")); 694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--flag-3", GetKeyValue("switch-3")); 704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_FALSE(HasCrashKey("switch-4")); 714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Set more than the max switches. 744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) { 754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) CommandLine command_line(CommandLine::NO_PROGRAM); 764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const int kMax = crash_keys::kSwitchesMaxCount + 2; 774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_GT(kMax, 15); 784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (int i = 1; i <= kMax; ++i) 794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) command_line.AppendSwitch(base::StringPrintf("--many-%d", i)); 804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) crash_keys::SetSwitchesFromCommandLine(&command_line); 814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--many-1", GetKeyValue("switch-1")); 824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--many-9", GetKeyValue("switch-9")); 834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--many-15", GetKeyValue("switch-15")); 844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_FALSE(HasCrashKey("switch-16")); 854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_FALSE(HasCrashKey("switch-17")); 864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Set fewer to ensure that old ones are erased. 894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) { 904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) CommandLine command_line(CommandLine::NO_PROGRAM); 914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (int i = 1; i <= 5; ++i) 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) command_line.AppendSwitch(base::StringPrintf("--fewer-%d", i)); 934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) crash_keys::SetSwitchesFromCommandLine(&command_line); 944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--fewer-1", GetKeyValue("switch-1")); 954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--fewer-2", GetKeyValue("switch-2")); 964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--fewer-3", GetKeyValue("switch-3")); 974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--fewer-4", GetKeyValue("switch-4")); 984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ("--fewer-5", GetKeyValue("switch-5")); 994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (int i = 6; i < 20; ++i) 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_FALSE(HasCrashKey(base::StringPrintf(crash_keys::kSwitch, i))); 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST_F(CrashKeysTest, Extensions) { 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Set three extensions. 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) { 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::set<std::string> extensions; 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.insert("ext.1"); 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.insert("ext.2"); 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.insert("ext.3"); 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) crash_keys::SetActiveExtensions(extensions); 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-1")); 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-2")); 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-3")); 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(0u, extensions.size()); 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ("3", GetKeyValue("num-extensions")); 1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_FALSE(HasCrashKey("extension-4")); 1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Set more than the max switches. 1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) { 1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::set<std::string> extensions; 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const int kMax = crash_keys::kExtensionIDMaxCount + 2; 1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_GT(kMax, 10); 1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (int i = 1; i <= kMax; ++i) 1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.insert(base::StringPrintf("ext.%d", i)); 1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) crash_keys::SetActiveExtensions(extensions); 1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (int i = 1; i <= kMax; ++i) { 1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase( 1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GetKeyValue(base::StringPrintf(crash_keys::kExtensionID, i))); 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(2u, extensions.size()); 1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ("12", GetKeyValue("num-extensions")); 1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_FALSE(HasCrashKey("extension-13")); 1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_FALSE(HasCrashKey("extension-14")); 1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Set fewer to ensure that old ones are erased. 1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) { 1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::set<std::string> extensions; 1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (int i = 1; i <= 5; ++i) 1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.insert(base::StringPrintf("ext.%d", i)); 1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) crash_keys::SetActiveExtensions(extensions); 1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-1")); 1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-2")); 1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-3")); 1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-4")); 1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) extensions.erase(GetKeyValue("extension-5")); 1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ(0u, extensions.size()); 1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_EQ("5", GetKeyValue("num-extensions")); 1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (int i = 6; i < 20; ++i) { 1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string key = base::StringPrintf(crash_keys::kExtensionID, i); 1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) EXPECT_FALSE(HasCrashKey(key)) << key; 1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 1624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 164a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 165a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#if defined(OS_CHROMEOS) 166a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochTEST_F(CrashKeysTest, IgnoreBoringFlags) { 167a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch CommandLine command_line(CommandLine::NO_PROGRAM); 168a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--enable-logging"); 169a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--user-data-dir=/tmp"); 170a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--v=1"); 171a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--ash-default-wallpaper-small=test.png"); 172a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 173a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--vv=1"); 174a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--vvv"); 175a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--enable-multi-profiles"); 176a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch command_line.AppendSwitch("--device-management-url=https://foo/bar"); 177a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 178a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch crash_keys::SetSwitchesFromCommandLine(&command_line); 179a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 180a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch EXPECT_EQ("--vv=1", GetKeyValue("switch-1")); 181a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch EXPECT_EQ("--vvv", GetKeyValue("switch-2")); 182a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch EXPECT_EQ("--enable-multi-profiles", GetKeyValue("switch-3")); 183a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch EXPECT_EQ("--device-management-url=https://foo/bar", GetKeyValue("switch-4")); 184a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch EXPECT_FALSE(HasCrashKey("switch-5")); 185a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch} 186a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#endif 187