| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/ssl_status_serialization.h" | 5 #include "content/common/ssl_status_serialization.h" |
| 6 | 6 |
| 7 #include "net/ssl/ssl_connection_status_flags.h" | 7 #include "net/ssl/ssl_connection_status_flags.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 void SetTestStatus(SSLStatus* status) { | 14 void SetTestStatus(SSLStatus* status) { |
| 15 status->security_style = SECURITY_STYLE_AUTHENTICATED; | 15 status->security_style = SECURITY_STYLE_AUTHENTICATED; |
| 16 status->cert_id = 1; | 16 status->cert_id = 1; |
| 17 status->cert_status = net::CERT_STATUS_DATE_INVALID; | 17 status->cert_status = net::CERT_STATUS_DATE_INVALID; |
| 18 status->security_bits = 80; | 18 status->security_bits = 80; |
| 19 status->key_exchange_info = 23; | 19 status->key_exchange_info = 23; |
| 20 status->connection_status = net::SSL_CONNECTION_VERSION_TLS1_2; | 20 status->connection_status = net::SSL_CONNECTION_VERSION_TLS1_2; |
| 21 status->signed_certificate_timestamp_ids.push_back( | 21 status->num_unknown_scts = 0; |
| 22 SignedCertificateTimestampIDAndStatus(1, net::ct::SCT_STATUS_OK)); | 22 status->num_invalid_scts = 0; |
| 23 status->num_valid_scts = 1; |
| 23 } | 24 } |
| 24 | 25 |
| 25 bool SSLStatusAreEqual(const SSLStatus& a, const SSLStatus &b) { | 26 bool SSLStatusAreEqual(const SSLStatus& a, const SSLStatus &b) { |
| 26 return a.Equals(b); | 27 return a.Equals(b); |
| 27 } | 28 } |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 std::ostream& operator<<(std::ostream& os, const SSLStatus& status) { | 32 std::ostream& operator<<(std::ostream& os, const SSLStatus& status) { |
| 32 return os << "Security Style: " << status.security_style | 33 return os << "Security Style: " << status.security_style |
| 33 << "\nCert ID: " << status.cert_id | 34 << "\nCert ID: " << status.cert_id |
| 34 << "\nCert Status: " << status.cert_status | 35 << "\nCert Status: " << status.cert_status |
| 35 << "\nSecurity bits: " << status.security_bits | 36 << "\nSecurity bits: " << status.security_bits |
| 36 << "\nKey exchange info: " << status.key_exchange_info | 37 << "\nKey exchange info: " << status.key_exchange_info |
| 37 << "\nConnection status: " << status.connection_status | 38 << "\nConnection status: " << status.connection_status |
| 38 << "\nContent Status: " << status.content_status | 39 << "\nContent Status: " << status.content_status |
| 39 << "\nNumber of SCTs: " << status.signed_certificate_timestamp_ids.size(); | 40 << "\nNumber of SCTs: " << status.signed_certificate_timestamp_ids.size() |
| 41 << "\nNumber of unknown SCTs: " << status.num_unknown_scts |
| 42 << "\nNumber of invalid SCTs: " << status.num_invalid_scts |
| 43 << "\nNumber of valid SCTs: " << status.num_valid_scts; |
| 40 } | 44 } |
| 41 | 45 |
| 42 // Test that a valid serialized SSLStatus returns true on | 46 // Test that a valid serialized SSLStatus returns true on |
| 43 // deserialization and deserializes correctly. | 47 // deserialization and deserializes correctly. |
| 44 TEST(SSLStatusSerializationTest, DeserializeSerializedStatus) { | 48 TEST(SSLStatusSerializationTest, DeserializeSerializedStatus) { |
| 45 // Serialize dummy data and test that it deserializes properly. | 49 // Serialize dummy data and test that it deserializes properly. |
| 46 SSLStatus status; | 50 SSLStatus status; |
| 47 SetTestStatus(&status); | 51 SetTestStatus(&status); |
| 48 std::string serialized = SerializeSecurityInfo(status); | 52 std::string serialized = SerializeSecurityInfo(status); |
| 49 | 53 |
| 50 SSLStatus deserialized; | 54 SSLStatus deserialized; |
| 51 ASSERT_TRUE(DeserializeSecurityInfo(serialized, &deserialized)); | 55 ASSERT_TRUE(DeserializeSecurityInfo(serialized, &deserialized)); |
| 52 EXPECT_PRED2(SSLStatusAreEqual, status, deserialized); | 56 EXPECT_PRED2(SSLStatusAreEqual, status, deserialized); |
| 53 EXPECT_EQ(SignedCertificateTimestampIDAndStatus(1, net::ct::SCT_STATUS_OK), | |
| 54 deserialized.signed_certificate_timestamp_ids[0]); | |
| 55 // Test that |content_status| has the default (initialized) value. | 57 // Test that |content_status| has the default (initialized) value. |
| 56 EXPECT_EQ(SSLStatus::NORMAL_CONTENT, deserialized.content_status); | 58 EXPECT_EQ(SSLStatus::NORMAL_CONTENT, deserialized.content_status); |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Test that an invalid serialized SSLStatus returns false on | 61 // Test that an invalid serialized SSLStatus returns false on |
| 60 // deserialization. | 62 // deserialization. |
| 61 TEST(SSLStatusSerializationTest, DeserializeBogusStatus) { | 63 TEST(SSLStatusSerializationTest, DeserializeBogusStatus) { |
| 62 // Test that a failure to deserialize returns false and returns | 64 // Test that a failure to deserialize returns false and returns |
| 63 // initialized, default data. | 65 // initialized, default data. |
| 64 SSLStatus invalid_deserialized; | 66 SSLStatus invalid_deserialized; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 SetTestStatus(&status); | 104 SetTestStatus(&status); |
| 103 status.security_style = static_cast<SecurityStyle>(100); | 105 status.security_style = static_cast<SecurityStyle>(100); |
| 104 std::string serialized = SerializeSecurityInfo(status); | 106 std::string serialized = SerializeSecurityInfo(status); |
| 105 | 107 |
| 106 SSLStatus invalid_deserialized; | 108 SSLStatus invalid_deserialized; |
| 107 ASSERT_FALSE(DeserializeSecurityInfo(serialized, &invalid_deserialized)); | 109 ASSERT_FALSE(DeserializeSecurityInfo(serialized, &invalid_deserialized)); |
| 108 EXPECT_PRED2(SSLStatusAreEqual, SSLStatus(), invalid_deserialized); | 110 EXPECT_PRED2(SSLStatusAreEqual, SSLStatus(), invalid_deserialized); |
| 109 } | 111 } |
| 110 | 112 |
| 111 } // namespace | 113 } // namespace |
| OLD | NEW |