Home | History | Annotate | Download | only in webdata
      1 // Copyright (c) 2011 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/file_util.h"
      6 #include "base/path_service.h"
      7 #include "base/string_number_conversions.h"
      8 #include "base/time.h"
      9 #include "chrome/browser/webdata/web_database.h"
     10 #include "chrome/common/chrome_paths.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     PathService::Get(chrome::DIR_TEST_DATA, &file_);
     23     const std::string test_db = "TestWebDatabase" +
     24         base::Int64ToString(Time::Now().ToTimeT()) +
     25         ".db";
     26     file_ = file_.AppendASCII(test_db);
     27     file_util::Delete(file_, false);
     28   }
     29 
     30   virtual void TearDown() {
     31     file_util::Delete(file_, false);
     32   }
     33 
     34   FilePath file_;
     35 
     36  private:
     37   DISALLOW_COPY_AND_ASSIGN(TokenServiceTableTest);
     38 };
     39 
     40 TEST_F(TokenServiceTableTest, TokenServiceGetAllRemoveAll) {
     41   WebDatabase db;
     42   ASSERT_EQ(sql::INIT_OK, db.Init(file_));
     43 
     44   std::map<std::string, std::string> out_map;
     45   std::string service;
     46   std::string service2;
     47   service = "testservice";
     48   service2 = "othertestservice";
     49 
     50   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     51   EXPECT_TRUE(out_map.empty());
     52 
     53   // Check that get all tokens works
     54   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service,
     55                                                             "pepperoni"));
     56   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service2, "steak"));
     57   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     58   EXPECT_EQ(out_map.find(service)->second, "pepperoni");
     59   EXPECT_EQ(out_map.find(service2)->second, "steak");
     60   out_map.clear();
     61 
     62   // Purge
     63   EXPECT_TRUE(db.GetTokenServiceTable()->RemoveAllTokens());
     64   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     65   EXPECT_TRUE(out_map.empty());
     66 
     67   // Check that you can still add it back in
     68   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, "cheese"));
     69   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     70   EXPECT_EQ(out_map.find(service)->second, "cheese");
     71 }
     72 
     73 TEST_F(TokenServiceTableTest, TokenServiceGetSet) {
     74   WebDatabase db;
     75   ASSERT_EQ(sql::INIT_OK, db.Init(file_));
     76 
     77   std::map<std::string, std::string> out_map;
     78   std::string service;
     79   service = "testservice";
     80 
     81   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     82   EXPECT_TRUE(out_map.empty());
     83 
     84   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service,
     85                                                             "pepperoni"));
     86   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     87   EXPECT_EQ(out_map.find(service)->second, "pepperoni");
     88   out_map.clear();
     89 
     90   // try blanking it - won't remove it from the db though!
     91   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, ""));
     92   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     93   EXPECT_EQ(out_map.find(service)->second, "");
     94   out_map.clear();
     95 
     96   // try mutating it
     97   EXPECT_TRUE(db.GetTokenServiceTable()->SetTokenForService(service, "ham"));
     98   EXPECT_TRUE(db.GetTokenServiceTable()->GetAllTokens(&out_map));
     99   EXPECT_EQ(out_map.find(service)->second, "ham");
    100 }
    101