Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / components / sessions / session_backend_unittest.cc
blob793663a0d08182969f5475c573d369cd943db634
1 // Copyright 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.
5 #include "base/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/stl_util.h"
8 #include "base/strings/string_util.h"
9 #include "components/sessions/session_backend.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace sessions {
13 namespace {
15 typedef ScopedVector<sessions::SessionCommand> SessionCommands;
17 struct TestData {
18 sessions::SessionCommand::id_type command_id;
19 std::string data;
22 sessions::SessionCommand* CreateCommandFromData(const TestData& data) {
23 sessions::SessionCommand* command =
24 new sessions::SessionCommand(
25 data.command_id,
26 static_cast<sessions::SessionCommand::size_type>(data.data.size()));
27 if (!data.data.empty())
28 memcpy(command->contents(), data.data.c_str(), data.data.size());
29 return command;
32 } // namespace
34 class SessionBackendTest : public testing::Test {
35 protected:
36 void SetUp() override {
37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
38 path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("SessionTestDirs"));
39 base::CreateDirectory(path_);
42 void AssertCommandEqualsData(const TestData& data,
43 sessions::SessionCommand* command) {
44 EXPECT_EQ(data.command_id, command->id());
45 EXPECT_EQ(data.data.size(), command->size());
46 EXPECT_TRUE(
47 memcmp(command->contents(), data.data.c_str(), command->size()) == 0);
50 // Path used in testing.
51 base::FilePath path_;
52 base::ScopedTempDir temp_dir_;
55 TEST_F(SessionBackendTest, SimpleReadWrite) {
56 scoped_refptr<SessionBackend> backend(
57 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
58 struct TestData data = { 1, "a" };
59 SessionCommands commands;
60 commands.push_back(CreateCommandFromData(data));
61 backend->AppendCommands(commands.Pass(), false);
62 ASSERT_TRUE(commands.empty());
64 // Read it back in.
65 backend = NULL;
66 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
67 path_);
68 backend->ReadLastSessionCommandsImpl(&commands);
70 ASSERT_EQ(1U, commands.size());
71 AssertCommandEqualsData(data, commands[0]);
73 commands.clear();
75 backend = NULL;
76 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
77 path_);
78 backend->ReadLastSessionCommandsImpl(&commands);
80 ASSERT_EQ(0U, commands.size());
82 // Make sure we can delete.
83 backend->DeleteLastSession();
84 backend->ReadLastSessionCommandsImpl(&commands);
85 ASSERT_EQ(0U, commands.size());
88 TEST_F(SessionBackendTest, RandomData) {
89 struct TestData data[] = {
90 { 1, "a" },
91 { 2, "ab" },
92 { 3, "abc" },
93 { 4, "abcd" },
94 { 5, "abcde" },
95 { 6, "abcdef" },
96 { 7, "abcdefg" },
97 { 8, "abcdefgh" },
98 { 9, "abcdefghi" },
99 { 10, "abcdefghij" },
100 { 11, "abcdefghijk" },
101 { 12, "abcdefghijkl" },
102 { 13, "abcdefghijklm" },
105 for (size_t i = 0; i < arraysize(data); ++i) {
106 scoped_refptr<SessionBackend> backend(
107 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
108 path_));
109 SessionCommands commands;
110 if (i != 0) {
111 // Read previous data.
112 backend->ReadLastSessionCommandsImpl(&commands);
113 ASSERT_EQ(i, commands.size());
114 for (std::vector<sessions::SessionCommand*>::iterator j =
115 commands.begin(); j != commands.end(); ++j) {
116 AssertCommandEqualsData(data[j - commands.begin()], *j);
118 backend->AppendCommands(commands.Pass(), false);
120 commands.push_back(CreateCommandFromData(data[i]));
121 backend->AppendCommands(commands.Pass(), false);
125 TEST_F(SessionBackendTest, BigData) {
126 struct TestData data[] = {
127 { 1, "a" },
128 { 2, "ab" },
131 scoped_refptr<SessionBackend> backend(
132 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
133 ScopedVector<sessions::SessionCommand> commands;
135 commands.push_back(CreateCommandFromData(data[0]));
136 const sessions::SessionCommand::size_type big_size =
137 SessionBackend::kFileReadBufferSize + 100;
138 const sessions::SessionCommand::id_type big_id = 50;
139 sessions::SessionCommand* big_command =
140 new sessions::SessionCommand(big_id, big_size);
141 reinterpret_cast<char*>(big_command->contents())[0] = 'a';
142 reinterpret_cast<char*>(big_command->contents())[big_size - 1] = 'z';
143 commands.push_back(big_command);
144 commands.push_back(CreateCommandFromData(data[1]));
145 backend->AppendCommands(commands.Pass(), false);
147 backend = NULL;
148 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
149 path_);
151 backend->ReadLastSessionCommandsImpl(&commands);
152 ASSERT_EQ(3U, commands.size());
153 AssertCommandEqualsData(data[0], commands[0]);
154 AssertCommandEqualsData(data[1], commands[2]);
156 EXPECT_EQ(big_id, commands[1]->id());
157 ASSERT_EQ(big_size, commands[1]->size());
158 EXPECT_EQ('a', reinterpret_cast<char*>(commands[1]->contents())[0]);
159 EXPECT_EQ('z',
160 reinterpret_cast<char*>(commands[1]->contents())[big_size - 1]);
161 commands.clear();
164 TEST_F(SessionBackendTest, EmptyCommand) {
165 TestData empty_command;
166 empty_command.command_id = 1;
167 scoped_refptr<SessionBackend> backend(
168 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
169 SessionCommands empty_commands;
170 empty_commands.push_back(CreateCommandFromData(empty_command));
171 backend->AppendCommands(empty_commands.Pass(), true);
172 backend->MoveCurrentSessionToLastSession();
174 SessionCommands commands;
175 backend->ReadLastSessionCommandsImpl(&commands);
176 ASSERT_EQ(1U, commands.size());
177 AssertCommandEqualsData(empty_command, commands[0]);
178 commands.clear();
181 // Writes a command, appends another command with reset to true, then reads
182 // making sure we only get back the second command.
183 TEST_F(SessionBackendTest, Truncate) {
184 scoped_refptr<SessionBackend> backend(
185 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
186 struct TestData first_data = { 1, "a" };
187 SessionCommands commands;
188 commands.push_back(CreateCommandFromData(first_data));
189 backend->AppendCommands(commands.Pass(), false);
191 // Write another command, this time resetting the file when appending.
192 struct TestData second_data = { 2, "b" };
193 commands.push_back(CreateCommandFromData(second_data));
194 backend->AppendCommands(commands.Pass(), true);
196 // Read it back in.
197 backend = NULL;
198 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
199 path_);
200 backend->ReadLastSessionCommandsImpl(&commands);
202 // And make sure we get back the expected data.
203 ASSERT_EQ(1U, commands.size());
204 AssertCommandEqualsData(second_data, commands[0]);
206 commands.clear();
209 } // namespace sessions