summaryrefslogtreecommitdiffstats
path: root/net/ftp
diff options
context:
space:
mode:
authorcbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-28 18:44:58 +0000
committercbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-28 18:44:58 +0000
commitf3cf980ca36d5b557b626d1bba4db6ded3ab2b77 (patch)
tree74028618ccef405480ff6da6a9d0d8c80a8fda7f /net/ftp
parent7f5969dda833a858bc946ca59ba0a9afbee2bc89 (diff)
downloadchromium_src-f3cf980ca36d5b557b626d1bba4db6ded3ab2b77.zip
chromium_src-f3cf980ca36d5b557b626d1bba4db6ded3ab2b77.tar.gz
chromium_src-f3cf980ca36d5b557b626d1bba4db6ded3ab2b77.tar.bz2
Use AuthCredentials throughout the network stack instead of username/password.
This is a refactor only - no behavior change should happen. Review URL: http://codereview.chromium.org/8340026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@107766 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/ftp')
-rw-r--r--net/ftp/ftp_auth_cache.cc23
-rw-r--r--net/ftp/ftp_auth_cache.h21
-rw-r--r--net/ftp/ftp_auth_cache_unittest.cc51
-rw-r--r--net/ftp/ftp_network_transaction.cc23
-rw-r--r--net/ftp/ftp_network_transaction.h8
-rw-r--r--net/ftp/ftp_network_transaction_unittest.cc15
-rw-r--r--net/ftp/ftp_transaction.h5
7 files changed, 71 insertions, 75 deletions
diff --git a/net/ftp/ftp_auth_cache.cc b/net/ftp/ftp_auth_cache.cc
index a67c2e0..fc28922 100644
--- a/net/ftp/ftp_auth_cache.cc
+++ b/net/ftp/ftp_auth_cache.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 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.
@@ -13,11 +13,9 @@ namespace net {
const size_t FtpAuthCache::kMaxEntries = 10;
FtpAuthCache::Entry::Entry(const GURL& origin,
- const string16& username,
- const string16& password)
+ const AuthCredentials& credentials)
: origin(origin),
- username(username),
- password(password) {
+ credentials(credentials) {
}
FtpAuthCache::Entry::~Entry() {}
@@ -34,17 +32,15 @@ FtpAuthCache::Entry* FtpAuthCache::Lookup(const GURL& origin) {
return NULL;
}
-void FtpAuthCache::Add(const GURL& origin, const string16& username,
- const string16& password) {
+void FtpAuthCache::Add(const GURL& origin, const AuthCredentials& credentials) {
DCHECK(origin.SchemeIs("ftp"));
DCHECK_EQ(origin.GetOrigin(), origin);
Entry* entry = Lookup(origin);
if (entry) {
- entry->username = username;
- entry->password = password;
+ entry->credentials = credentials;
} else {
- entries_.push_front(Entry(origin, username, password));
+ entries_.push_front(Entry(origin, credentials));
// Prevent unbound memory growth of the cache.
if (entries_.size() > kMaxEntries)
@@ -52,11 +48,10 @@ void FtpAuthCache::Add(const GURL& origin, const string16& username,
}
}
-void FtpAuthCache::Remove(const GURL& origin, const string16& username,
- const string16& password) {
+void FtpAuthCache::Remove(const GURL& origin,
+ const AuthCredentials& credentials) {
for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
- if (it->origin == origin && it->username == username &&
- it->password == password) {
+ if (it->origin == origin && it->credentials.Equals(credentials)) {
entries_.erase(it);
DCHECK(!Lookup(origin));
return;
diff --git a/net/ftp/ftp_auth_cache.h b/net/ftp/ftp_auth_cache.h
index 6281f9d..393b23d 100644
--- a/net/ftp/ftp_auth_cache.h
+++ b/net/ftp/ftp_auth_cache.h
@@ -8,8 +8,8 @@
#include <list>
-#include "base/string16.h"
#include "googleurl/src/gurl.h"
+#include "net/base/auth.h"
#include "net/base/net_export.h"
namespace net {
@@ -28,13 +28,11 @@ class NET_EXPORT_PRIVATE FtpAuthCache {
static const size_t kMaxEntries;
struct Entry {
- Entry(const GURL& origin, const string16& username,
- const string16& password);
+ Entry(const GURL& origin, const AuthCredentials& credentials);
~Entry();
const GURL origin;
- string16 username;
- string16 password;
+ AuthCredentials credentials;
};
FtpAuthCache();
@@ -43,16 +41,13 @@ class NET_EXPORT_PRIVATE FtpAuthCache {
// Return Entry corresponding to given |origin| or NULL if not found.
Entry* Lookup(const GURL& origin);
- // Add an entry for |origin| to the cache (consisting of |username| and
- // |password|). If there is already an entry for |origin|, it will be
- // overwritten.
- void Add(const GURL& origin, const string16& username,
- const string16& password);
+ // Add an entry for |origin| to the cache using |credentials|. If there is
+ // already an entry for |origin|, it will be overwritten.
+ void Add(const GURL& origin, const AuthCredentials& credentials);
// Remove the entry for |origin| from the cache, if one exists and matches
- // |username| and |password|.
- void Remove(const GURL& origin, const string16& username,
- const string16& password);
+ // |credentials|.
+ void Remove(const GURL& origin, const AuthCredentials& credentials);
private:
typedef std::list<Entry> EntryList;
diff --git a/net/ftp/ftp_auth_cache_unittest.cc b/net/ftp/ftp_auth_cache_unittest.cc
index a8c5732..33ce553 100644
--- a/net/ftp/ftp_auth_cache_unittest.cc
+++ b/net/ftp/ftp_auth_cache_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 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.
@@ -8,6 +8,7 @@
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
+#include "net/base/auth.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::FtpAuthCache;
@@ -38,38 +39,38 @@ TEST(FtpAuthCacheTest, LookupAddRemove) {
EXPECT_TRUE(cache.Lookup(origin1) == NULL);
// Add entry for origin1.
- cache.Add(origin1, kUsername1, kPassword1);
+ cache.Add(origin1, net::AuthCredentials(kUsername1, kPassword1));
FtpAuthCache::Entry* entry1 = cache.Lookup(origin1);
ASSERT_TRUE(entry1);
EXPECT_EQ(origin1, entry1->origin);
- EXPECT_EQ(kUsername1, entry1->username);
- EXPECT_EQ(kPassword1, entry1->password);
+ EXPECT_EQ(kUsername1, entry1->credentials.username());
+ EXPECT_EQ(kPassword1, entry1->credentials.password());
// Add an entry for origin2.
- cache.Add(origin2, kUsername2, kPassword2);
+ cache.Add(origin2, net::AuthCredentials(kUsername2, kPassword2));
FtpAuthCache::Entry* entry2 = cache.Lookup(origin2);
ASSERT_TRUE(entry2);
EXPECT_EQ(origin2, entry2->origin);
- EXPECT_EQ(kUsername2, entry2->username);
- EXPECT_EQ(kPassword2, entry2->password);
+ EXPECT_EQ(kUsername2, entry2->credentials.username());
+ EXPECT_EQ(kPassword2, entry2->credentials.password());
// The original entry1 should still be there.
EXPECT_EQ(entry1, cache.Lookup(origin1));
// Overwrite the entry for origin1.
- cache.Add(origin1, kUsername3, kPassword3);
+ cache.Add(origin1, net::AuthCredentials(kUsername3, kPassword3));
FtpAuthCache::Entry* entry3 = cache.Lookup(origin1);
ASSERT_TRUE(entry3);
EXPECT_EQ(origin1, entry3->origin);
- EXPECT_EQ(kUsername3, entry3->username);
- EXPECT_EQ(kPassword3, entry3->password);
+ EXPECT_EQ(kUsername3, entry3->credentials.username());
+ EXPECT_EQ(kPassword3, entry3->credentials.password());
// Remove entry of origin1.
- cache.Remove(origin1, kUsername3, kPassword3);
+ cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3));
EXPECT_TRUE(cache.Lookup(origin1) == NULL);
// Remove non-existent entry.
- cache.Remove(origin1, kUsername3, kPassword3);
+ cache.Remove(origin1, net::AuthCredentials(kUsername3, kPassword3));
EXPECT_TRUE(cache.Lookup(origin1) == NULL);
}
@@ -81,8 +82,8 @@ TEST(FtpAuthCacheTest, LookupWithPort) {
GURL origin1("ftp://foo:80");
GURL origin2("ftp://foo:21");
- cache.Add(origin1, kUsername, kPassword);
- cache.Add(origin2, kUsername, kPassword);
+ cache.Add(origin1, net::AuthCredentials(kUsername, kPassword));
+ cache.Add(origin2, net::AuthCredentials(kUsername, kPassword));
EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2));
}
@@ -95,7 +96,7 @@ TEST(FtpAuthCacheTest, NormalizedKey) {
FtpAuthCache cache;
// Add.
- cache.Add(GURL("ftp://HoSt:21"), kUsername, kPassword);
+ cache.Add(GURL("ftp://HoSt:21"), net::AuthCredentials(kUsername, kPassword));
// Lookup.
FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21"));
@@ -104,30 +105,31 @@ TEST(FtpAuthCacheTest, NormalizedKey) {
EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host")));
// Overwrite.
- cache.Add(GURL("ftp://host"), kOthername, kOtherword);
+ cache.Add(GURL("ftp://host"), net::AuthCredentials(kOthername, kOtherword));
FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21"));
ASSERT_TRUE(entry2);
EXPECT_EQ(GURL("ftp://host"), entry2->origin);
- EXPECT_EQ(kOthername, entry2->username);
- EXPECT_EQ(kOtherword, entry2->password);
+ EXPECT_EQ(kOthername, entry2->credentials.username());
+ EXPECT_EQ(kOtherword, entry2->credentials.password());
// Remove
- cache.Remove(GURL("ftp://HOsT"), kOthername, kOtherword);
+ cache.Remove(GURL("ftp://HOsT"),
+ net::AuthCredentials(kOthername, kOtherword));
EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
}
TEST(FtpAuthCacheTest, OnlyRemoveMatching) {
FtpAuthCache cache;
- cache.Add(GURL("ftp://host"), kUsername, kPassword);
+ cache.Add(GURL("ftp://host"), net::AuthCredentials(kUsername, kPassword));
EXPECT_TRUE(cache.Lookup(GURL("ftp://host")));
// Auth data doesn't match, shouldn't remove.
- cache.Remove(GURL("ftp://host"), kBogus, kBogus);
+ cache.Remove(GURL("ftp://host"), net::AuthCredentials(kBogus, kBogus));
EXPECT_TRUE(cache.Lookup(GURL("ftp://host")));
// Auth data matches, should remove.
- cache.Remove(GURL("ftp://host"), kUsername, kPassword);
+ cache.Remove(GURL("ftp://host"), net::AuthCredentials(kUsername, kPassword));
EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
}
@@ -136,7 +138,7 @@ TEST(FtpAuthCacheTest, EvictOldEntries) {
for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) {
cache.Add(GURL("ftp://host" + base::IntToString(i)),
- kUsername, kPassword);
+ net::AuthCredentials(kUsername, kPassword));
}
// No entries should be evicted before reaching the limit.
@@ -145,7 +147,8 @@ TEST(FtpAuthCacheTest, EvictOldEntries) {
}
// Adding one entry should cause eviction of the first entry.
- cache.Add(GURL("ftp://last_host"), kUsername, kPassword);
+ cache.Add(GURL("ftp://last_host"),
+ net::AuthCredentials(kUsername, kPassword));
EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL);
// Remaining entries should not get evicted.
diff --git a/net/ftp/ftp_network_transaction.cc b/net/ftp/ftp_network_transaction.cc
index d448ad3..24ab0f9 100644
--- a/net/ftp/ftp_network_transaction.cc
+++ b/net/ftp/ftp_network_transaction.cc
@@ -248,10 +248,13 @@ int FtpNetworkTransaction::Start(const FtpRequestInfo* request_info,
request_ = request_info;
if (request_->url.has_username()) {
- GetIdentityFromURL(request_->url, &username_, &password_);
+ string16 username;
+ string16 password;
+ GetIdentityFromURL(request_->url, &username, &password);
+ credentials_.Set(username, password);
} else {
- username_ = ASCIIToUTF16("anonymous");
- password_ = ASCIIToUTF16("chrome@example.com");
+ credentials_.Set(ASCIIToUTF16("anonymous"),
+ ASCIIToUTF16("chrome@example.com"));
}
DetectTypecode();
@@ -263,13 +266,11 @@ int FtpNetworkTransaction::Start(const FtpRequestInfo* request_info,
return rv;
}
-int FtpNetworkTransaction::RestartWithAuth(const string16& username,
- const string16& password,
+int FtpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
OldCompletionCallback* callback) {
ResetStateForRestart();
- username_ = username;
- password_ = password;
+ credentials_ = credentials;
next_state_ = STATE_CTRL_RESOLVE_HOST;
int rv = DoLoop(OK);
@@ -668,8 +669,10 @@ int FtpNetworkTransaction::DoCtrlReadComplete(int result) {
// Some servers (for example Pure-FTPd) apparently close the control
// connection when anonymous login is not permitted. For more details
// see http://crbug.com/25023.
- if (command_sent_ == COMMAND_USER && username_ == ASCIIToUTF16("anonymous"))
+ if (command_sent_ == COMMAND_USER &&
+ credentials_.username() == ASCIIToUTF16("anonymous")) {
response_.needs_auth = true;
+ }
return Stop(ERR_EMPTY_RESPONSE);
}
if (result < 0)
@@ -715,7 +718,7 @@ int FtpNetworkTransaction::DoCtrlWriteComplete(int result) {
// USER Command.
int FtpNetworkTransaction::DoCtrlWriteUSER() {
- std::string command = "USER " + UTF16ToUTF8(username_);
+ std::string command = "USER " + UTF16ToUTF8(credentials_.username());
if (!IsValidFTPCommandString(command))
return Stop(ERR_MALFORMED_IDENTITY);
@@ -746,7 +749,7 @@ int FtpNetworkTransaction::ProcessResponseUSER(
// PASS command.
int FtpNetworkTransaction::DoCtrlWritePASS() {
- std::string command = "PASS " + UTF16ToUTF8(password_);
+ std::string command = "PASS " + UTF16ToUTF8(credentials_.password());
if (!IsValidFTPCommandString(command))
return Stop(ERR_MALFORMED_IDENTITY);
diff --git a/net/ftp/ftp_network_transaction.h b/net/ftp/ftp_network_transaction.h
index cce5adb..7d32d55 100644
--- a/net/ftp/ftp_network_transaction.h
+++ b/net/ftp/ftp_network_transaction.h
@@ -12,8 +12,8 @@
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
-#include "base/string16.h"
#include "net/base/address_list.h"
+#include "net/base/auth.h"
#include "net/base/host_resolver.h"
#include "net/base/net_log.h"
#include "net/base/single_request_host_resolver.h"
@@ -40,8 +40,7 @@ class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction {
virtual int Start(const FtpRequestInfo* request_info,
OldCompletionCallback* callback,
const BoundNetLog& net_log) OVERRIDE;
- virtual int RestartWithAuth(const string16& username,
- const string16& password,
+ virtual int RestartWithAuth(const AuthCredentials& credentials,
OldCompletionCallback* callback) OVERRIDE;
virtual int Read(IOBuffer* buf, int buf_len, OldCompletionCallback* callback)
OVERRIDE;
@@ -232,8 +231,7 @@ class NET_EXPORT_PRIVATE FtpNetworkTransaction : public FtpTransaction {
// EPSV fail, we fall back to PASV for the duration of connection.
bool use_epsv_;
- string16 username_;
- string16 password_;
+ AuthCredentials credentials_;
// Current directory on the remote server, as returned by last PWD command,
// with any trailing slash removed.
diff --git a/net/ftp/ftp_network_transaction_unittest.cc b/net/ftp/ftp_network_transaction_unittest.cc
index 6739847..5d686e0 100644
--- a/net/ftp/ftp_network_transaction_unittest.cc
+++ b/net/ftp/ftp_network_transaction_unittest.cc
@@ -1190,9 +1190,11 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartUser) {
ctrl_writes, arraysize(ctrl_writes));
mock_socket_factory_.AddSocketDataProvider(&ctrl_socket2);
ASSERT_EQ(ERR_IO_PENDING,
- transaction_.RestartWithAuth(ASCIIToUTF16("foo\nownz0red"),
- ASCIIToUTF16("innocent"),
- &callback_));
+ transaction_.RestartWithAuth(
+ AuthCredentials(
+ ASCIIToUTF16("foo\nownz0red"),
+ ASCIIToUTF16("innocent")),
+ &callback_));
EXPECT_EQ(ERR_MALFORMED_IDENTITY, callback_.WaitForResult());
}
@@ -1223,9 +1225,10 @@ TEST_F(FtpNetworkTransactionTest, EvilRestartPassword) {
ctrl_writes, arraysize(ctrl_writes));
mock_socket_factory_.AddSocketDataProvider(&ctrl_socket2);
ASSERT_EQ(ERR_IO_PENDING,
- transaction_.RestartWithAuth(ASCIIToUTF16("innocent"),
- ASCIIToUTF16("foo\nownz0red"),
- &callback_));
+ transaction_.RestartWithAuth(
+ AuthCredentials(ASCIIToUTF16("innocent"),
+ ASCIIToUTF16("foo\nownz0red")),
+ &callback_));
EXPECT_EQ(ERR_MALFORMED_IDENTITY, callback_.WaitForResult());
}
diff --git a/net/ftp/ftp_transaction.h b/net/ftp/ftp_transaction.h
index 300468b..269c8bb 100644
--- a/net/ftp/ftp_transaction.h
+++ b/net/ftp/ftp_transaction.h
@@ -6,7 +6,6 @@
#define NET_FTP_FTP_TRANSACTION_H_
#pragma once
-#include "base/string16.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/load_states.h"
@@ -14,6 +13,7 @@
namespace net {
+class AuthCredentials;
class FtpResponseInfo;
class FtpRequestInfo;
class BoundNetLog;
@@ -44,8 +44,7 @@ class NET_EXPORT_PRIVATE FtpTransaction {
const BoundNetLog& net_log) = 0;
// Restarts the FTP transaction with authentication credentials.
- virtual int RestartWithAuth(const string16& username,
- const string16& password,
+ virtual int RestartWithAuth(const AuthCredentials& credentials,
OldCompletionCallback* callback) = 0;
// Once response info is available for the transaction, response data may be