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
3 // found in the LICENSE file.
5 #include "content/common/ssl_status_serialization.h"
7 #include "net/ssl/ssl_connection_status_flags.h"
8 #include "testing/gtest/include/gtest/gtest.h"
14 void SetTestStatus(SSLStatus
* status
) {
15 status
->security_style
= SECURITY_STYLE_AUTHENTICATED
;
17 status
->cert_status
= net::CERT_STATUS_DATE_INVALID
;
18 status
->security_bits
= 80;
19 status
->key_exchange_info
= 23;
20 status
->connection_status
= net::SSL_CONNECTION_VERSION_TLS1_2
;
21 status
->signed_certificate_timestamp_ids
.push_back(
22 SignedCertificateTimestampIDAndStatus(1, net::ct::SCT_STATUS_OK
));
25 bool SSLStatusAreEqual(const SSLStatus
& a
, const SSLStatus
&b
) {
31 std::ostream
& operator<<(std::ostream
& os
, const SSLStatus
& status
) {
32 return os
<< "Security Style: " << status
.security_style
33 << "\nCert ID: " << status
.cert_id
34 << "\nCert Status: " << status
.cert_status
35 << "\nSecurity bits: " << status
.security_bits
36 << "\nKey exchange info: " << status
.key_exchange_info
37 << "\nConnection status: " << status
.connection_status
38 << "\nContent Status: " << status
.content_status
39 << "\nNumber of SCTs: " << status
.signed_certificate_timestamp_ids
.size();
42 // Test that a valid serialized SSLStatus returns true on
43 // deserialization and deserializes correctly.
44 TEST(SSLStatusSerializationTest
, DeserializeSerializedStatus
) {
45 // Serialize dummy data and test that it deserializes properly.
47 SetTestStatus(&status
);
48 std::string serialized
= SerializeSecurityInfo(status
);
50 SSLStatus deserialized
;
51 ASSERT_TRUE(DeserializeSecurityInfo(serialized
, &deserialized
));
52 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.
56 EXPECT_EQ(SSLStatus::NORMAL_CONTENT
, deserialized
.content_status
);
59 // Test that an invalid serialized SSLStatus returns false on
61 TEST(SSLStatusSerializationTest
, DeserializeBogusStatus
) {
62 // Test that a failure to deserialize returns false and returns
63 // initialized, default data.
64 SSLStatus invalid_deserialized
;
66 DeserializeSecurityInfo("not an SSLStatus", &invalid_deserialized
));
67 EXPECT_PRED2(SSLStatusAreEqual
, SSLStatus(), invalid_deserialized
);
70 // Serialize a status with a bad |security_bits| value and test that
71 // deserializing it fails.
72 TEST(SSLStatusSerializationTest
, DeserializeBogusSecurityBits
) {
74 SetTestStatus(&status
);
75 // |security_bits| must be <-1. (-1 means the strength is unknown, and
76 // |0 means the connection is not encrypted).
77 status
.security_bits
= -5;
78 std::string serialized
= SerializeSecurityInfo(status
);
80 SSLStatus invalid_deserialized
;
81 ASSERT_FALSE(DeserializeSecurityInfo(serialized
, &invalid_deserialized
));
82 EXPECT_PRED2(SSLStatusAreEqual
, SSLStatus(), invalid_deserialized
);
85 // Serialize a status with a bad |key_exchange_info| value and test that
86 // deserializing it fails.
87 TEST(SSLStatusSerializationTest
, DeserializeBogusKeyExchangeInfo
) {
89 SetTestStatus(&status
);
90 status
.key_exchange_info
= -1;
92 SSLStatus invalid_deserialized
;
93 std::string serialized
= SerializeSecurityInfo(status
);
94 ASSERT_FALSE(DeserializeSecurityInfo(serialized
, &invalid_deserialized
));
95 EXPECT_PRED2(SSLStatusAreEqual
, SSLStatus(), invalid_deserialized
);
98 // Serialize a status with a bad |security_style| value and test that
99 // deserializing it fails.
100 TEST(SSLStatusSerializationTest
, DeserializeBogusSecurityStyle
) {
102 SetTestStatus(&status
);
103 status
.security_style
= static_cast<SecurityStyle
>(100);
104 std::string serialized
= SerializeSecurityInfo(status
);
106 SSLStatus invalid_deserialized
;
107 ASSERT_FALSE(DeserializeSecurityInfo(serialized
, &invalid_deserialized
));
108 EXPECT_PRED2(SSLStatusAreEqual
, SSLStatus(), invalid_deserialized
);