-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkerberos_artifact_synchronizer_test.cc
217 lines (168 loc) · 7.69 KB
/
kerberos_artifact_synchronizer_test.cc
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <utility>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <gtest/gtest.h>
#include <kerberos/proto_bindings/kerberos_service.pb.h>
#include "smbfs/fake_kerberos_artifact_client.h"
#include "smbfs/kerberos_artifact_synchronizer.h"
namespace smbfs {
namespace {
constexpr char kKrb5FileName[] = "krb5.conf";
constexpr char kCCacheFileName[] = "ccache";
constexpr char kTestUserGuid[] = "test user guid";
void ExpectSetupSuccess(bool success) {
EXPECT_TRUE(success);
}
void ExpectSetupFailure(bool success) {
EXPECT_FALSE(success);
}
void ExpectFileEqual(const base::FilePath& path,
const std::string expected_contents) {
std::string actual_contents;
EXPECT_TRUE(ReadFileToString(path, &actual_contents));
EXPECT_EQ(expected_contents, actual_contents);
}
void ExpectFileNotEqual(const base::FilePath& path,
const std::string expected_contents) {
std::string actual_contents;
EXPECT_TRUE(ReadFileToString(path, &actual_contents));
EXPECT_NE(expected_contents, actual_contents);
}
kerberos::KerberosFiles CreateKerberosFilesProto(const std::string& krb5cc,
const std::string& krb5conf) {
kerberos::KerberosFiles kerberos_files;
kerberos_files.set_krb5cc(krb5cc);
kerberos_files.set_krb5conf(krb5conf);
return kerberos_files;
}
} // namespace
class KerberosArtifactSynchronizerTest : public testing::Test {
public:
KerberosArtifactSynchronizerTest() {
auto fake_ptr = std::make_unique<FakeKerberosArtifactClient>();
fake_artifact_client_ = fake_ptr.get();
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
krb5_conf_path_ = temp_dir_.GetPath().Append(kKrb5FileName);
krb5_ccache_path_ = temp_dir_.GetPath().Append(kCCacheFileName);
synchronizer_ = std::make_unique<KerberosArtifactSynchronizer>(
krb5_conf_path_, krb5_ccache_path_, kTestUserGuid, std::move(fake_ptr));
}
KerberosArtifactSynchronizerTest(const KerberosArtifactSynchronizerTest&) =
delete;
KerberosArtifactSynchronizerTest& operator=(
const KerberosArtifactSynchronizerTest&) = delete;
~KerberosArtifactSynchronizerTest() override = default;
protected:
base::ScopedTempDir temp_dir_;
base::FilePath krb5_conf_path_;
base::FilePath krb5_ccache_path_;
FakeKerberosArtifactClient* fake_artifact_client_;
std::unique_ptr<KerberosArtifactSynchronizer> synchronizer_;
};
// SetupKerberos makes a call to GetKerberosFiles.
TEST_F(KerberosArtifactSynchronizerTest, SetupKerberosCallsGetFiles) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
EXPECT_EQ(1, fake_artifact_client_->GetFilesMethodCallCount());
}
// SetupKerberos writes the files to the correct location.
TEST_F(KerberosArtifactSynchronizerTest, KerberosFilesWriteToCorrectLocation) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
ExpectFileEqual(krb5_conf_path_, krb5conf);
ExpectFileEqual(krb5_ccache_path_, krb5cc);
}
// SetupKerberos connects to a signal.
TEST_F(KerberosArtifactSynchronizerTest, SetupKerberosConnectsToSignal) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
EXPECT_TRUE(fake_artifact_client_->IsConnected());
}
// Synchronizer calls GetFiles an additional time when the signal fires.
TEST_F(KerberosArtifactSynchronizerTest, GetFilesRunsOnSignalFire) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
EXPECT_EQ(1, fake_artifact_client_->GetFilesMethodCallCount());
fake_artifact_client_->FireSignal();
EXPECT_EQ(2, fake_artifact_client_->GetFilesMethodCallCount());
}
// Synchronizer calls GetFiles an additional time when the signal fires, but
// GetKerberosFiles() fails.
TEST_F(KerberosArtifactSynchronizerTest,
GetFilesRunsOnSignalFireWithGetFilesFailure) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
EXPECT_EQ(1, fake_artifact_client_->GetFilesMethodCallCount());
fake_artifact_client_->ResetKerberosFiles();
fake_artifact_client_->FireSignal();
EXPECT_EQ(2, fake_artifact_client_->GetFilesMethodCallCount());
}
// Synchronizer overwrites the Kerberos files when the signal fires.
TEST_F(KerberosArtifactSynchronizerTest, GetFilesOverwritesOldFiles) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
ExpectFileEqual(krb5_conf_path_, krb5conf);
ExpectFileEqual(krb5_ccache_path_, krb5cc);
const std::string new_krb5cc = "new test creds";
const std::string new_krb5conf = "new test conf";
kerberos::KerberosFiles new_kerberos_files =
CreateKerberosFilesProto(new_krb5cc, new_krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, new_kerberos_files);
fake_artifact_client_->FireSignal();
ExpectFileNotEqual(krb5_conf_path_, krb5conf);
ExpectFileNotEqual(krb5_ccache_path_, krb5cc);
ExpectFileEqual(krb5_conf_path_, new_krb5conf);
ExpectFileEqual(krb5_ccache_path_, new_krb5cc);
}
// SetupKerberos fails when the getting the user's kerberos files fails.
TEST_F(KerberosArtifactSynchronizerTest, SetupKerberosFailsKerberosFilesEmpty) {
kerberos::KerberosFiles kerberos_files;
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupFailure));
}
// Synchronizer calls GetFiles an additional time when the signal fires, but
// files are empty.
TEST_F(KerberosArtifactSynchronizerTest,
GetFilesRunsOnSignalFireWithFilesEmpty) {
const std::string krb5cc = "test creds";
const std::string krb5conf = "test conf";
kerberos::KerberosFiles kerberos_files =
CreateKerberosFilesProto(krb5cc, krb5conf);
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, kerberos_files);
synchronizer_->SetupKerberos(base::BindOnce(&ExpectSetupSuccess));
EXPECT_EQ(1, fake_artifact_client_->GetFilesMethodCallCount());
fake_artifact_client_->ResetKerberosFiles();
fake_artifact_client_->AddKerberosFiles(kTestUserGuid, {});
fake_artifact_client_->FireSignal();
EXPECT_EQ(2, fake_artifact_client_->GetFilesMethodCallCount());
}
} // namespace smbfs