Update V8 to version 4.7.42.
[chromium-blink-merge.git] / content / common / ssl_status_serialization_unittest.cc
blobf8b4852f757440f270f3b685a326b679ac5ba7ef
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"
10 namespace content {
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.
16 SSLStatus status;
17 status.security_style = SECURITY_STYLE_AUTHENTICATED;
18 status.cert_id = 1;
19 status.cert_status = net::CERT_STATUS_DATE_INVALID;
20 status.security_bits = 80;
21 status.connection_status = net::SSL_CONNECTION_VERSION_TLS1_2;
22 SignedCertificateTimestampIDAndStatus sct(1, net::ct::SCT_STATUS_OK);
23 status.signed_certificate_timestamp_ids.push_back(sct);
25 std::string serialized = SerializeSecurityInfo(status);
27 SSLStatus deserialized;
28 ASSERT_TRUE(DeserializeSecurityInfo(serialized, &deserialized));
29 EXPECT_EQ(status.security_style, deserialized.security_style);
30 EXPECT_EQ(status.cert_id, deserialized.cert_id);
31 EXPECT_EQ(status.cert_status, deserialized.cert_status);
32 EXPECT_EQ(status.security_bits, deserialized.security_bits);
33 EXPECT_EQ(status.connection_status, deserialized.connection_status);
34 EXPECT_EQ(status.signed_certificate_timestamp_ids.size(),
35 deserialized.signed_certificate_timestamp_ids.size());
36 EXPECT_EQ(sct, deserialized.signed_certificate_timestamp_ids[0]);
37 // Test that |content_status| has the default (initialized) value.
38 EXPECT_EQ(SSLStatus::NORMAL_CONTENT, deserialized.content_status);
41 // Test that an invalid serialized SSLStatus returns false on
42 // deserialization.
43 TEST(SSLStatusSerializationTest, DeserializeBogusStatus) {
44 // Test that a failure to deserialize returns false and returns
45 // initialized, default data.
46 SSLStatus invalid_deserialized;
47 ASSERT_FALSE(
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.
65 SSLStatus status;
66 status.security_style = SECURITY_STYLE_AUTHENTICATED;
67 status.cert_id = 1;
68 status.cert_status = net::CERT_STATUS_DATE_INVALID;
69 // |security_bits| must be <-1. (-1 means the strength is unknown, and
70 // |0 means the connection is not encrypted).
71 status.security_bits = -5;
72 status.connection_status = net::SSL_CONNECTION_VERSION_TLS1_2;
73 SignedCertificateTimestampIDAndStatus sct(1, net::ct::SCT_STATUS_OK);
74 status.signed_certificate_timestamp_ids.push_back(sct);
76 std::string serialized = SerializeSecurityInfo(status);
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());
91 // Now serialize a status with a bad |security_style| value and test
92 // that deserializing fails.
93 status.security_bits = 128;
94 status.security_style = static_cast<SecurityStyle>(100);
95 serialized = SerializeSecurityInfo(status);
96 ASSERT_FALSE(DeserializeSecurityInfo(serialized, &invalid_deserialized));
98 EXPECT_EQ(default_ssl_status.security_style,
99 invalid_deserialized.security_style);
100 EXPECT_EQ(default_ssl_status.cert_id, invalid_deserialized.cert_id);
101 EXPECT_EQ(default_ssl_status.cert_status, invalid_deserialized.cert_status);
102 EXPECT_EQ(default_ssl_status.security_bits,
103 invalid_deserialized.security_bits);
104 EXPECT_EQ(default_ssl_status.connection_status,
105 invalid_deserialized.connection_status);
106 EXPECT_EQ(default_ssl_status.content_status,
107 invalid_deserialized.content_status);
108 EXPECT_EQ(0u, invalid_deserialized.signed_certificate_timestamp_ids.size());
111 } // namespace