Home | History | Annotate | Download | only in webdata
      1 // Copyright 2014 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 "base/files/scoped_temp_dir.h"
      6 #include "base/path_service.h"
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/time/time.h"
      9 #include "components/signin/core/browser/webdata/token_service_table.h"
     10 #include "components/webdata/common/web_database.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using base::Time;
     14 
     15 class TokenServiceTableTest : public testing::Test {
     16  public:
     17   TokenServiceTableTest() {}
     18   virtual ~TokenServiceTableTest() {}
     19 
     20  protected:
     21   virtual void SetUp() {
     22     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     23     file_ = temp_dir_.path().AppendASCII("TestWebDatabase");
     24 
     25     table_.reset(new TokenServiceTable);
     26     db_.reset(new WebDatabase);
     27     db_->AddTable(table_.get());
     28     ASSERT_EQ(sql::INIT_OK, db_->Init(file_));
     29   }
     30 
     31   base::FilePath file_;
     32   base::ScopedTempDir temp_dir_;
     33   scoped_ptr<TokenServiceTable> table_;
     34   scoped_ptr<WebDatabase> db_;
     35  private:
     36   DISALLOW_COPY_AND_ASSIGN(TokenServiceTableTest);
     37 };
     38 
     39 // Flaky on mac_rel. See http://crbug.com/228943
     40 #if defined(OS_MACOSX)
     41 #define MAYBE_TokenServiceGetAllRemoveAll DISABLED_TokenServiceGetAllRemoveAll
     42 #define MAYBE_TokenServiceGetSet DISABLED_TokenServiceGetSet
     43 #define MAYBE_TokenServiceRemove DISABLED_TokenServiceRemove
     44 #else
     45 #define MAYBE_TokenServiceGetAllRemoveAll TokenServiceGetAllRemoveAll
     46 #define MAYBE_TokenServiceGetSet TokenServiceGetSet
     47 #define MAYBE_TokenServiceRemove TokenServiceRemove
     48 #endif
     49 
     50 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceGetAllRemoveAll) {
     51   std::map<std::string, std::string> out_map;
     52   std::string service;
     53   std::string service2;
     54   service = "testservice";
     55   service2 = "othertestservice";
     56 
     57   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     58   EXPECT_TRUE(out_map.empty());
     59 
     60   // Check that get all tokens works
     61   EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
     62   EXPECT_TRUE(table_->SetTokenForService(service2, "steak"));
     63   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     64   EXPECT_EQ("pepperoni", out_map.find(service)->second);
     65   EXPECT_EQ("steak", out_map.find(service2)->second);
     66   out_map.clear();
     67 
     68   // Purge
     69   EXPECT_TRUE(table_->RemoveAllTokens());
     70   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     71   EXPECT_TRUE(out_map.empty());
     72 
     73   // Check that you can still add it back in
     74   EXPECT_TRUE(table_->SetTokenForService(service, "cheese"));
     75   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     76   EXPECT_EQ("cheese", out_map.find(service)->second);
     77 }
     78 
     79 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceGetSet) {
     80   std::map<std::string, std::string> out_map;
     81   std::string service;
     82   service = "testservice";
     83 
     84   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     85   EXPECT_TRUE(out_map.empty());
     86 
     87   EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
     88   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     89   EXPECT_EQ("pepperoni", out_map.find(service)->second);
     90   out_map.clear();
     91 
     92   // try blanking it - won't remove it from the db though!
     93   EXPECT_TRUE(table_->SetTokenForService(service, std::string()));
     94   EXPECT_TRUE(table_->GetAllTokens(&out_map));
     95   EXPECT_EQ("", out_map.find(service)->second);
     96   out_map.clear();
     97 
     98   // try mutating it
     99   EXPECT_TRUE(table_->SetTokenForService(service, "ham"));
    100   EXPECT_TRUE(table_->GetAllTokens(&out_map));
    101   EXPECT_EQ("ham", out_map.find(service)->second);
    102 }
    103 
    104 TEST_F(TokenServiceTableTest, MAYBE_TokenServiceRemove) {
    105   std::map<std::string, std::string> out_map;
    106   std::string service;
    107   std::string service2;
    108   service = "testservice";
    109   service2 = "othertestservice";
    110 
    111   EXPECT_TRUE(table_->SetTokenForService(service, "pepperoni"));
    112   EXPECT_TRUE(table_->SetTokenForService(service2, "steak"));
    113   EXPECT_TRUE(table_->RemoveTokenForService(service));
    114   EXPECT_TRUE(table_->GetAllTokens(&out_map));
    115   EXPECT_EQ(0u, out_map.count(service));
    116   EXPECT_EQ("steak", out_map.find(service2)->second);
    117 }
    118