1 // Copyright 2014 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/browser/safe_browsing/incident_reporting/binary_integrity_incident_handlers.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "chrome/common/safe_browsing/csd.pb.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace safe_browsing { 12 13 namespace { 14 15 scoped_ptr<ClientIncidentReport_IncidentData> MakeIncident() { 16 scoped_ptr<ClientIncidentReport_IncidentData> incident( 17 new ClientIncidentReport_IncidentData); 18 19 incident->mutable_binary_integrity()->set_file_basename("foo"); 20 21 // Set the signature. 22 incident->mutable_binary_integrity()->mutable_signature()->set_trusted(true); 23 ClientDownloadRequest_CertificateChain* certificate_chain = 24 incident->mutable_binary_integrity()->mutable_signature() 25 ->add_certificate_chain(); 26 27 // Fill the certificate chain with 2 elements. 28 const unsigned char certificates[][5] = { 29 {42, 255, 100, 53, 2}, 30 {64, 33, 51, 91, 210}, 31 }; 32 for (size_t i = 0; i < arraysize(certificates); ++i) { 33 ClientDownloadRequest_CertificateChain_Element* element = 34 certificate_chain->add_element(); 35 element->set_certificate(certificates[i], arraysize(certificates[i])); 36 } 37 38 return incident.Pass(); 39 } 40 41 } // namespace 42 43 TEST(BinaryIntegrityIncidentHandlersTest, GetKeyIsFile) { 44 ClientIncidentReport_IncidentData incident; 45 46 incident.mutable_binary_integrity()->set_file_basename("foo"); 47 ASSERT_EQ(std::string("foo"), GetBinaryIntegrityIncidentKey(incident)); 48 } 49 50 TEST(BinaryIntegrityIncidentHandlersTest, SameIncidentSameDigest) { 51 scoped_ptr<ClientIncidentReport_IncidentData> incident(MakeIncident()); 52 53 uint32_t digest = GetBinaryIntegrityIncidentDigest(*incident); 54 ASSERT_EQ(digest, GetBinaryIntegrityIncidentDigest(*MakeIncident())); 55 } 56 57 TEST(BinaryIntegrityIncidentHandlersTest, DifferentIncidentDifferentDigest) { 58 scoped_ptr<ClientIncidentReport_IncidentData> incident(MakeIncident()); 59 60 uint32_t digest = GetBinaryIntegrityIncidentDigest(*incident); 61 62 scoped_ptr<ClientIncidentReport_IncidentData> incident2(MakeIncident()); 63 incident2->mutable_binary_integrity()->set_file_basename("bar"); 64 65 ASSERT_NE(digest, GetBinaryIntegrityIncidentDigest(*incident2)); 66 } 67 68 } // namespace safe_browsing 69