Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: services/authentication/accounts_db_manager_unittest.cc

Issue 1466733002: Google OAuth Device Flow support for FNL (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Addressed review comments Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "accounts_db_manager.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_tokenizer.h"
9 #include "mojo/public/cpp/application/application_impl.h"
10 #include "mojo/public/cpp/application/application_test_base.h"
11 #include "mojo/services/files/interfaces/types.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace authentication {
15 namespace {
16
17 class AccountsDBTest : public mojo::test::ApplicationTestBase {
18 public:
19 AccountsDBTest(){};
20 ~AccountsDBTest() override{};
21
22 protected:
23 void SetUp() override {
24 mojo::test::ApplicationTestBase::SetUp();
25 mojo::files::FilesPtr files;
26 application_impl()->ConnectToService("mojo:files", &files);
27
28 mojo::files::Error error = mojo::files::Error::INTERNAL;
29 mojo::files::DirectoryPtr directory;
30 files->OpenFileSystem(nullptr, GetProxy(&directory), Capture(&error));
31 files.WaitForIncomingResponse();
32 if (mojo::files::Error::OK != error) {
33 LOG(FATAL) << "Unable to initialize accounts DB";
34 }
35 accounts_db_manager_ = new AccountsDbManager(directory);
36 }
37
38 AccountsDbManager* accountsDBPtr() { return accounts_db_manager_; }
39
40 private:
41 AccountsDbManager* accounts_db_manager_;
42
43 DISALLOW_COPY_AND_ASSIGN(AccountsDBTest);
44 };
45
46 TEST_F(AccountsDBTest, CanAddNewAccount) {
47 std::string test_str("test:abcdefgh");
48 bool result = accountsDBPtr()->UpdateAccount("test", test_str);
49 ASSERT_TRUE(result);
50
51 const mojo::Array<uint8_t>& accounts = accountsDBPtr()->FetchAllAccounts();
52 const std::vector<uint8_t> vec = accounts.storage();
53 std::string db_contents(vec.begin(), vec.end());
54
55 // db_contents adds extra '\0' to terminate the file contents'
56 EXPECT_EQ(test_str.size(), db_contents.size() - 1);
57 }
58
59 TEST_F(AccountsDBTest, CanUpdateAnExistingAccount) {
60 bool result = accountsDBPtr()->UpdateAccount("user1", "user1:hello");
61 ASSERT_TRUE(result);
62
63 result = accountsDBPtr()->UpdateAccount("user1", "user1:world");
64 ASSERT_TRUE(result);
65
66 result = accountsDBPtr()->UpdateAccount("user3", "user3:google");
67 ASSERT_TRUE(result);
68
69 const mojo::Array<uint8_t>& accounts = accountsDBPtr()->FetchAllAccounts();
70 const std::vector<uint8_t> vec = accounts.storage();
71 std::string db_contents(vec.begin(), vec.end());
72
73 LOG(INFO) << "Db contents are: " << db_contents;
74 base::StringTokenizer lines(db_contents, "\n");
75 int n = 0;
76 while (lines.GetNext()) {
77 n++;
78 }
79 ASSERT_FALSE(db_contents.find("hello") != std::string::npos);
80 ASSERT_TRUE(db_contents.find("world") != std::string::npos);
81 EXPECT_EQ(2, n);
82 }
83
84 TEST_F(AccountsDBTest, CanGetUserAccountDataByName) {
85 bool result =
86 accountsDBPtr()->UpdateAccount("test_user", "test_user:turquoise_creds");
87 ASSERT_TRUE(result);
88
89 mojo::String user_data;
90 accountsDBPtr()->GetAccountDataForUser("test_user", user_data);
91
92 ASSERT_TRUE(!user_data.is_null());
93 EXPECT_EQ("test_user:turquoise_creds", user_data);
94 }
95
96 TEST_F(AccountsDBTest, CanGetAllAccountsData) {
97 bool result = accountsDBPtr()->UpdateAccount("user1", "user1:hello");
98 ASSERT_TRUE(result);
99
100 result = accountsDBPtr()->UpdateAccount("user2", "user2:world");
101 ASSERT_TRUE(result);
102
103 result = accountsDBPtr()->UpdateAccount("user3", "user3:google");
104 ASSERT_TRUE(result);
105
106 const mojo::Array<uint8_t>& accounts = accountsDBPtr()->FetchAllAccounts();
107 const std::vector<uint8_t> vec = accounts.storage();
108 std::string db_contents(vec.begin(), vec.end());
109
110 LOG(INFO) << "Db contents are: " << db_contents;
111 base::StringTokenizer lines(db_contents, "\n");
112 int n = 0;
113 while (lines.GetNext()) {
114 n++;
115 }
116 EXPECT_EQ(3, n);
117 }
118
119 } // namespace
120 } // namespace authentication
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698