Home | History | Annotate | Download | only in ftp
      1 // Copyright (c) 2010 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 "net/ftp/ftp_auth_cache.h"
      6 
      7 #include "base/string_number_conversions.h"
      8 #include "base/string_util.h"
      9 #include "base/utf_string_conversions.h"
     10 #include "googleurl/src/gurl.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using net::FtpAuthCache;
     14 
     15 namespace {
     16 
     17 const string16 kBogus(ASCIIToUTF16("bogus"));
     18 const string16 kOthername(ASCIIToUTF16("othername"));
     19 const string16 kOtherword(ASCIIToUTF16("otherword"));
     20 const string16 kPassword(ASCIIToUTF16("password"));
     21 const string16 kPassword1(ASCIIToUTF16("password1"));
     22 const string16 kPassword2(ASCIIToUTF16("password2"));
     23 const string16 kPassword3(ASCIIToUTF16("password3"));
     24 const string16 kUsername(ASCIIToUTF16("username"));
     25 const string16 kUsername1(ASCIIToUTF16("username1"));
     26 const string16 kUsername2(ASCIIToUTF16("username2"));
     27 const string16 kUsername3(ASCIIToUTF16("username3"));
     28 
     29 }  // namespace
     30 
     31 TEST(FtpAuthCacheTest, LookupAddRemove) {
     32   FtpAuthCache cache;
     33 
     34   GURL origin1("ftp://foo1");
     35   GURL origin2("ftp://foo2");
     36 
     37   // Lookup non-existent entry.
     38   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
     39 
     40   // Add entry for origin1.
     41   cache.Add(origin1, kUsername1, kPassword1);
     42   FtpAuthCache::Entry* entry1 = cache.Lookup(origin1);
     43   ASSERT_TRUE(entry1);
     44   EXPECT_EQ(origin1, entry1->origin);
     45   EXPECT_EQ(kUsername1, entry1->username);
     46   EXPECT_EQ(kPassword1, entry1->password);
     47 
     48   // Add an entry for origin2.
     49   cache.Add(origin2, kUsername2, kPassword2);
     50   FtpAuthCache::Entry* entry2 = cache.Lookup(origin2);
     51   ASSERT_TRUE(entry2);
     52   EXPECT_EQ(origin2, entry2->origin);
     53   EXPECT_EQ(kUsername2, entry2->username);
     54   EXPECT_EQ(kPassword2, entry2->password);
     55 
     56   // The original entry1 should still be there.
     57   EXPECT_EQ(entry1, cache.Lookup(origin1));
     58 
     59   // Overwrite the entry for origin1.
     60   cache.Add(origin1, kUsername3, kPassword3);
     61   FtpAuthCache::Entry* entry3 = cache.Lookup(origin1);
     62   ASSERT_TRUE(entry3);
     63   EXPECT_EQ(origin1, entry3->origin);
     64   EXPECT_EQ(kUsername3, entry3->username);
     65   EXPECT_EQ(kPassword3, entry3->password);
     66 
     67   // Remove entry of origin1.
     68   cache.Remove(origin1, kUsername3, kPassword3);
     69   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
     70 
     71   // Remove non-existent entry.
     72   cache.Remove(origin1, kUsername3, kPassword3);
     73   EXPECT_TRUE(cache.Lookup(origin1) == NULL);
     74 }
     75 
     76 // Check that if the origin differs only by port number, it is considered
     77 // a separate origin.
     78 TEST(FtpAuthCacheTest, LookupWithPort) {
     79   FtpAuthCache cache;
     80 
     81   GURL origin1("ftp://foo:80");
     82   GURL origin2("ftp://foo:21");
     83 
     84   cache.Add(origin1, kUsername, kPassword);
     85   cache.Add(origin2, kUsername, kPassword);
     86 
     87   EXPECT_NE(cache.Lookup(origin1), cache.Lookup(origin2));
     88 }
     89 
     90 TEST(FtpAuthCacheTest, NormalizedKey) {
     91   // GURL is automatically canonicalized. Hence the following variations in
     92   // url format should all map to the same entry (case insensitive host,
     93   // default port of 21).
     94 
     95   FtpAuthCache cache;
     96 
     97   // Add.
     98   cache.Add(GURL("ftp://HoSt:21"), kUsername, kPassword);
     99 
    100   // Lookup.
    101   FtpAuthCache::Entry* entry1 = cache.Lookup(GURL("ftp://HoSt:21"));
    102   ASSERT_TRUE(entry1);
    103   EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host:21")));
    104   EXPECT_EQ(entry1, cache.Lookup(GURL("ftp://host")));
    105 
    106   // Overwrite.
    107   cache.Add(GURL("ftp://host"), kOthername, kOtherword);
    108   FtpAuthCache::Entry* entry2 = cache.Lookup(GURL("ftp://HoSt:21"));
    109   ASSERT_TRUE(entry2);
    110   EXPECT_EQ(GURL("ftp://host"), entry2->origin);
    111   EXPECT_EQ(kOthername, entry2->username);
    112   EXPECT_EQ(kOtherword, entry2->password);
    113 
    114   // Remove
    115   cache.Remove(GURL("ftp://HOsT"), kOthername, kOtherword);
    116   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
    117 }
    118 
    119 TEST(FtpAuthCacheTest, OnlyRemoveMatching) {
    120   FtpAuthCache cache;
    121 
    122   cache.Add(GURL("ftp://host"), kUsername, kPassword);
    123   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")));
    124 
    125   // Auth data doesn't match, shouldn't remove.
    126   cache.Remove(GURL("ftp://host"), kBogus, kBogus);
    127   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")));
    128 
    129   // Auth data matches, should remove.
    130   cache.Remove(GURL("ftp://host"), kUsername, kPassword);
    131   EXPECT_TRUE(cache.Lookup(GURL("ftp://host")) == NULL);
    132 }
    133 
    134 TEST(FtpAuthCacheTest, EvictOldEntries) {
    135   FtpAuthCache cache;
    136 
    137   for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) {
    138     cache.Add(GURL("ftp://host" + base::IntToString(i)),
    139               kUsername, kPassword);
    140   }
    141 
    142   // No entries should be evicted before reaching the limit.
    143   for (size_t i = 0; i < FtpAuthCache::kMaxEntries; i++) {
    144     EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i))));
    145   }
    146 
    147   // Adding one entry should cause eviction of the first entry.
    148   cache.Add(GURL("ftp://last_host"), kUsername, kPassword);
    149   EXPECT_TRUE(cache.Lookup(GURL("ftp://host0")) == NULL);
    150 
    151   // Remaining entries should not get evicted.
    152   for (size_t i = 1; i < FtpAuthCache::kMaxEntries; i++) {
    153     EXPECT_TRUE(cache.Lookup(GURL("ftp://host" + base::IntToString(i))));
    154   }
    155   EXPECT_TRUE(cache.Lookup(GURL("ftp://last_host")));
    156 }
    157