1// Copyright (c) 2012 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 "ppapi/tests/test_tcp_socket_private_trusted.h"
6
7#include "ppapi/cpp/private/tcp_socket_private.h"
8#include "ppapi/cpp/private/x509_certificate_private.h"
9#include "ppapi/tests/testing_instance.h"
10#include "ppapi/tests/test_utils.h"
11
12REGISTER_TEST_CASE(TCPSocketPrivateTrusted);
13
14TestTCPSocketPrivateTrusted::TestTCPSocketPrivateTrusted(
15    TestingInstance* instance)
16    : TestCase(instance) {
17}
18
19bool TestTCPSocketPrivateTrusted::Init() {
20  if (!pp::TCPSocketPrivate::IsAvailable())
21    return false;
22
23  // We need something to connect to, so we connect to the HTTP server whence we
24  // came. Grab the host and port.
25  if (!EnsureRunningOverHTTP())
26    return false;
27
28  if (!GetLocalHostPort(instance_->pp_instance(), &host_, &port_))
29    return false;
30
31  // Get the port for the SSL server.
32  ssl_port_ = instance_->ssl_server_port();
33
34  return true;
35}
36
37void TestTCPSocketPrivateTrusted::RunTests(const std::string& filter) {
38  RUN_CALLBACK_TEST(TestTCPSocketPrivateTrusted, GetServerCertificate, filter);
39}
40
41std::string TestTCPSocketPrivateTrusted::TestGetServerCertificate() {
42  pp::TCPSocketPrivate socket(instance_);
43  TestCompletionCallback cb(instance_->pp_instance(), callback_type());
44
45  cb.WaitForResult(
46      socket.Connect(host_.c_str(), ssl_port_, cb.GetCallback()));
47  CHECK_CALLBACK_BEHAVIOR(cb);
48  ASSERT_EQ(PP_OK, cb.result());
49
50  cb.WaitForResult(
51      socket.SSLHandshake(host_.c_str(), ssl_port_, cb.GetCallback()));
52  CHECK_CALLBACK_BEHAVIOR(cb);
53  ASSERT_EQ(PP_OK, cb.result());
54
55  const pp::X509CertificatePrivate& cert = socket.GetServerCertificate();
56  ASSERT_EQ(
57      cert.GetField(PP_X509CERTIFICATE_PRIVATE_ISSUER_COMMON_NAME).AsString(),
58      "Test Root CA");
59  ASSERT_EQ(
60      cert.GetField(PP_X509CERTIFICATE_PRIVATE_SUBJECT_COMMON_NAME).AsString(),
61      "127.0.0.1");
62  PASS();
63}
64