File: token_table_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (113 lines) | stat: -rw-r--r-- 3,064 bytes parent folder | download | duplicates (6)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/file_manager/indexing/token_table.h"

#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "sql/database.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::file_manager {
namespace {

const base::FilePath::CharType kDatabaseName[] =
    FILE_PATH_LITERAL("TokenTableTest.db");

class TokenTableTest : public testing::Test {
 public:
  void SetUp() override {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    db_ = std::make_unique<sql::Database>(sql::test::kTestTag);
    ASSERT_TRUE(InitDb(*db_));
  }

  void TearDown() override {
    db_->Close();
    EXPECT_TRUE(temp_dir_.Delete());
  }

  base::FilePath db_file_path() {
    return temp_dir_.GetPath().Append(kDatabaseName);
  }

  bool InitDb(sql::Database& db) {
    if (db.is_open()) {
      return true;
    }
    if (!db.Open(db_file_path())) {
      return false;
    }
    return true;
  }

 protected:
  base::ScopedTempDir temp_dir_;
  std::unique_ptr<sql::Database> db_;
};

TEST_F(TokenTableTest, Init) {
  TokenTable table(db_.get());
  EXPECT_TRUE(table.Init());
}

TEST_F(TokenTableTest, GetTokenId) {
  TokenTable table(db_.get());
  EXPECT_TRUE(table.Init());

  EXPECT_EQ(table.GetTokenId("hello"), -1);
  EXPECT_EQ(table.GetOrCreateTokenId("hello"), 1);
  EXPECT_EQ(table.GetTokenId("hello"), 1);
  EXPECT_EQ(table.GetOrCreateTokenId("there"), 2);
  EXPECT_EQ(table.GetTokenId("there"), 2);
  EXPECT_EQ(table.GetTokenId("O'Neill"), -1);
  EXPECT_EQ(table.GetOrCreateTokenId("O'Neill"), 3);
}

TEST_F(TokenTableTest, DeleteToken) {
  TokenTable table(db_.get());
  EXPECT_TRUE(table.Init());

  EXPECT_EQ(table.DeleteToken("hello"), -1);
  EXPECT_EQ(table.GetOrCreateTokenId("hello"), 1);
  EXPECT_EQ(table.DeleteToken("hello"), 1);
}

TEST_F(TokenTableTest, GetToken) {
  TokenTable table(db_.get());
  EXPECT_TRUE(table.Init());

  EXPECT_FALSE(table.GetToken(1).has_value());
  EXPECT_EQ(table.GetOrCreateTokenId("hello"), 1);

  auto token_or = table.GetToken(1);
  EXPECT_TRUE(token_or.has_value());
  EXPECT_EQ(token_or.value(), "hello");
  EXPECT_EQ(table.DeleteToken("hello"), 1);
  EXPECT_FALSE(table.GetToken(1).has_value());
}

TEST_F(TokenTableTest, ChangeToken) {
  TokenTable table(db_.get());
  EXPECT_TRUE(table.Init());

  // Test 1: Cannot change a non-existing token.
  std::string token;
  EXPECT_EQ(table.ChangeToken("foo", "bar"), -1);

  // Test 2: Change an existing token to a unique token.
  EXPECT_EQ(table.GetOrCreateTokenId("foo"), 1);
  EXPECT_EQ(table.ChangeToken("foo", "bar"), 1);

  // Test 3: Change token to itself
  EXPECT_EQ(table.ChangeToken("bar", "bar"), 1);

  // Test 4: It is invalid to change token to be the same as another token
  EXPECT_EQ(table.GetOrCreateTokenId("baz"), 2);
  EXPECT_EQ(table.ChangeToken("bar", "baz"), -1);
}

}  // namespace
}  // namespace ash::file_manager