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"
12 // Test that a valid serialized SSLStatus returns true on
13 // deserialization and deserializes correctly.
14 TEST(SSLStatusSerializationTest
, DeserializeSerializedStatus
) {
15 // Serialize dummy data and test that it deserializes properly.
17 net::CertStatus cert_status
= net::CERT_STATUS_DATE_INVALID
;
18 int security_bits
= 80;
19 int connection_status
= net::SSL_CONNECTION_VERSION_TLS1_2
;
20 SignedCertificateTimestampIDStatusList sct_list
;
21 SignedCertificateTimestampIDAndStatus
sct(1, net::ct::SCT_STATUS_OK
);
22 sct_list
.push_back(sct
);
24 std::string serialized
= SerializeSecurityInfo(
25 cert_id
, cert_status
, security_bits
, connection_status
, sct_list
);
27 SSLStatus deserialized
;
28 ASSERT_TRUE(DeserializeSecurityInfo(serialized
, &deserialized
));
29 EXPECT_EQ(cert_id
, deserialized
.cert_id
);
30 EXPECT_EQ(cert_status
, deserialized
.cert_status
);
31 EXPECT_EQ(security_bits
, deserialized
.security_bits
);
32 EXPECT_EQ(connection_status
, deserialized
.connection_status
);
33 EXPECT_EQ(sct_list
.size(),
34 deserialized
.signed_certificate_timestamp_ids
.size());
35 EXPECT_EQ(sct
, deserialized
.signed_certificate_timestamp_ids
[0]);
36 // Test that the other fields have default (initialized) values.
37 EXPECT_EQ(SECURITY_STYLE_UNKNOWN
, deserialized
.security_style
);
38 EXPECT_EQ(SSLStatus::NORMAL_CONTENT
, deserialized
.content_status
);
41 // Test that an invalid serialized SSLStatus returns false on
43 TEST(SSLStatusSerializationTest
, DeserializeBogusStatus
) {
44 // Test that a failure to deserialize returns false and returns
45 // initialized, default data.
46 SSLStatus invalid_deserialized
;
48 DeserializeSecurityInfo("not an SSLStatus", &invalid_deserialized
));
50 SSLStatus default_ssl_status
;
51 EXPECT_EQ(default_ssl_status
.security_style
,
52 invalid_deserialized
.security_style
);
53 EXPECT_EQ(default_ssl_status
.cert_id
, invalid_deserialized
.cert_id
);
54 EXPECT_EQ(default_ssl_status
.cert_status
, invalid_deserialized
.cert_status
);
55 EXPECT_EQ(default_ssl_status
.security_bits
,
56 invalid_deserialized
.security_bits
);
57 EXPECT_EQ(default_ssl_status
.connection_status
,
58 invalid_deserialized
.connection_status
);
59 EXPECT_EQ(default_ssl_status
.content_status
,
60 invalid_deserialized
.content_status
);
61 EXPECT_EQ(0u, invalid_deserialized
.signed_certificate_timestamp_ids
.size());
63 // Serialize a status with a bad |security_bits| value and test that
64 // deserializing it fails.
66 net::CertStatus cert_status
= net::CERT_STATUS_DATE_INVALID
;
67 // |security_bits| must be <-1. (-1 means the strength is unknown, and
68 // |0 means the connection is not encrypted).
69 int security_bits
= -5;
70 int connection_status
= net::SSL_CONNECTION_VERSION_TLS1_2
;
71 SignedCertificateTimestampIDStatusList sct_list
;
72 SignedCertificateTimestampIDAndStatus
sct(1, net::ct::SCT_STATUS_OK
);
73 sct_list
.push_back(sct
);
75 std::string serialized
= SerializeSecurityInfo(
76 cert_id
, cert_status
, security_bits
, connection_status
, sct_list
);
77 ASSERT_FALSE(DeserializeSecurityInfo(serialized
, &invalid_deserialized
));
79 EXPECT_EQ(default_ssl_status
.security_style
,
80 invalid_deserialized
.security_style
);
81 EXPECT_EQ(default_ssl_status
.cert_id
, invalid_deserialized
.cert_id
);
82 EXPECT_EQ(default_ssl_status
.cert_status
, invalid_deserialized
.cert_status
);
83 EXPECT_EQ(default_ssl_status
.security_bits
,
84 invalid_deserialized
.security_bits
);
85 EXPECT_EQ(default_ssl_status
.connection_status
,
86 invalid_deserialized
.connection_status
);
87 EXPECT_EQ(default_ssl_status
.content_status
,
88 invalid_deserialized
.content_status
);
89 EXPECT_EQ(0u, invalid_deserialized
.signed_certificate_timestamp_ids
.size());