summaryrefslogtreecommitdiffstats
path: root/net/http/http_auth_handler_negotiate_unittest.cc
blob: 910b5ef9fee972f90a9e9f3c6459ca09850967fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http/http_auth_handler_negotiate.h"

#include "net/base/mock_host_resolver.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#if defined(OS_WIN)
#include "net/http/mock_sspi_library_win.h"
#endif
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

// TODO(cbentzel): Remove the OS_WIN condition once Negotiate is supported
// on all platforms.
#if defined(OS_WIN)
namespace {

void CreateHandler(bool disable_cname_lookup, bool include_port,
                   const std::string& url_string,
                   SSPILibrary* sspi_library,
                   scoped_ptr<HttpAuthHandlerNegotiate>* handler) {
  handler->reset(new HttpAuthHandlerNegotiate(sspi_library, 50, NULL,
                                              disable_cname_lookup,
                                              include_port));
  std::string challenge = "Negotiate";
  HttpAuth::ChallengeTokenizer props(challenge.begin(), challenge.end());
  GURL gurl(url_string);
  (*handler)->InitFromChallenge(&props, HttpAuth::AUTH_SERVER, gurl,
                                BoundNetLog());
}

}  // namespace

TEST(HttpAuthHandlerNegotiateTest, DisableCname) {
  MockSSPILibrary mock_library;
  scoped_ptr<HttpAuthHandlerNegotiate> auth_handler;
  CreateHandler(true, false, "http://alias:500", &mock_library, &auth_handler);
  EXPECT_FALSE(auth_handler->NeedsCanonicalName());
  EXPECT_EQ(L"HTTP/alias", auth_handler->spn());
}

TEST(HttpAuthHandlerNegotiateTest, DisableCnameStandardPort) {
  MockSSPILibrary mock_library;
  scoped_ptr<HttpAuthHandlerNegotiate> auth_handler;
  CreateHandler(true, true, "http://alias:80", &mock_library, &auth_handler);
  EXPECT_FALSE(auth_handler->NeedsCanonicalName());
  EXPECT_EQ(L"HTTP/alias", auth_handler->spn());
}

TEST(HttpAuthHandlerNegotiateTest, DisableCnameNonstandardPort) {
  MockSSPILibrary mock_library;
  scoped_ptr<HttpAuthHandlerNegotiate> auth_handler;
  CreateHandler(true, true, "http://alias:500", &mock_library, &auth_handler);
  EXPECT_FALSE(auth_handler->NeedsCanonicalName());
  EXPECT_EQ(L"HTTP/alias:500", auth_handler->spn());
}

TEST(HttpAuthHandlerNegotiateTest, CnameSync) {
  MockSSPILibrary mock_library;
  scoped_ptr<HttpAuthHandlerNegotiate> auth_handler;
  CreateHandler(false, false, "http://alias:500", &mock_library, &auth_handler);
  EXPECT_TRUE(auth_handler->NeedsCanonicalName());
  MockHostResolver* mock_resolver = new MockHostResolver();
  scoped_refptr<HostResolver> scoped_resolver(mock_resolver);
  mock_resolver->set_synchronous_mode(true);
  mock_resolver->rules()->AddIPv4Rule("alias", "10.0.0.2",
                                      "canonical.example.com");
  TestCompletionCallback callback;
  EXPECT_EQ(OK, auth_handler->ResolveCanonicalName(mock_resolver, &callback));
  EXPECT_EQ(L"HTTP/canonical.example.com", auth_handler->spn());
}

TEST(HttpAuthHandlerNegotiateTest, CnameAsync) {
  MockSSPILibrary mock_library;
  scoped_ptr<HttpAuthHandlerNegotiate> auth_handler;
  CreateHandler(false, false, "http://alias:500", &mock_library, &auth_handler);
  EXPECT_TRUE(auth_handler->NeedsCanonicalName());
  MockHostResolver* mock_resolver = new MockHostResolver();
  scoped_refptr<HostResolver> scoped_resolver(mock_resolver);
  mock_resolver->set_synchronous_mode(false);
  mock_resolver->rules()->AddIPv4Rule("alias", "10.0.0.2",
                                      "canonical.example.com");
  TestCompletionCallback callback;
  EXPECT_EQ(ERR_IO_PENDING, auth_handler->ResolveCanonicalName(mock_resolver,
                                                               &callback));
  EXPECT_EQ(OK, callback.WaitForResult());
  EXPECT_EQ(L"HTTP/canonical.example.com", auth_handler->spn());
}
#endif  // defined(OS_WIN)

}  // namespace net