-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathGnuPassCredentialStoreTests.cs
More file actions
202 lines (161 loc) · 8.06 KB
/
GnuPassCredentialStoreTests.cs
File metadata and controls
202 lines (161 loc) · 8.06 KB
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
using System;
using System.IO;
using System.Text;
using Xunit;
using GitCredentialManager.Interop.Posix;
using GitCredentialManager.Tests.Objects;
namespace GitCredentialManager.Tests.Interop.Posix
{
public class GnuPassCredentialStoreTests
{
private const string TestNamespace = "git-test";
[PosixFact]
public void GnuPassCredentialStore_ReadWriteDelete()
{
var fs = new TestFileSystem();
var gpg = new TestGpg(fs);
string storeRoot = InitializePasswordStore(fs, gpg);
var collection = new GpgPassCredentialStore(fs, gpg, storeRoot, TestNamespace);
// Create a service that is guaranteed to be unique
string uniqueGuid = Guid.NewGuid().ToString("N");
string service = $"https://example.com/{uniqueGuid}";
const string userName = "john.doe";
const string password = "letmein123"; // [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="Fake credential")]
string expectedSlug = $"{TestNamespace}/https/example.com/{uniqueGuid}/{userName}.gpg";
string expectedFilePath = Path.Combine(storeRoot, expectedSlug);
string expectedFileContents = password + Environment.NewLine +
$"service={service}" + Environment.NewLine +
$"account={userName}" + Environment.NewLine;
byte[] expectedFileBytes = Encoding.UTF8.GetBytes(expectedFileContents);
try
{
// Write
collection.AddOrUpdate(service, userName, password);
// Read
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(userName, userName);
Assert.Equal(password, outCredential.Password);
Assert.True(fs.Files.ContainsKey(expectedFilePath));
Assert.Equal(expectedFileBytes, fs.Files[expectedFilePath]);
}
finally
{
// Ensure we clean up after ourselves even in case of 'get' failures
collection.Remove(service, userName);
}
}
[PosixFact]
public void GnuPassCredentialStore_Get_NotFound_ReturnsNull()
{
var fs = new TestFileSystem();
var gpg = new TestGpg(fs);
string storeRoot = InitializePasswordStore(fs, gpg);
var collection = new GpgPassCredentialStore(fs, gpg, storeRoot, TestNamespace);
// Unique service; guaranteed not to exist!
string service = $"https://example.com/{Guid.NewGuid():N}";
ICredential credential = collection.Get(service, null);
Assert.Null(credential);
}
[PosixFact]
public void GnuPassCredentialStore_Remove_NotFound_ReturnsFalse()
{
var fs = new TestFileSystem();
var gpg = new TestGpg(fs);
string storeRoot = InitializePasswordStore(fs, gpg);
var collection = new GpgPassCredentialStore(fs, gpg, storeRoot, TestNamespace);
// Unique service; guaranteed not to exist!
string service = $"https://example.com/{Guid.NewGuid():N}";
bool result = collection.Remove(service, account: null);
Assert.False(result);
}
[PosixFact]
public void GnuPassCredentialStore_ReadWriteDelete_GpgIdInSubdirectory()
{
var fs = new TestFileSystem();
var gpg = new TestGpg(fs);
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string storePath = Path.Combine(homePath, ".password-store");
const string userId = "gcm-test@example.com";
// Place .gpg-id only in the namespace subdirectory (not the store root),
// simulating a pass store where the root has no .gpg-id but submodules do.
string subDirPath = Path.Combine(storePath, TestNamespace);
string gpgIdPath = Path.Combine(subDirPath, ".gpg-id");
gpg.GenerateKeys(userId);
fs.Directories.Add(storePath);
fs.Directories.Add(subDirPath);
fs.Files[gpgIdPath] = Encoding.UTF8.GetBytes(userId);
var collection = new GpgPassCredentialStore(fs, gpg, storePath, TestNamespace);
string service = $"https://example.com/{Guid.NewGuid():N}";
const string userName = "john.doe";
string password = Guid.NewGuid().ToString("N");
try
{
// Write
collection.AddOrUpdate(service, userName, password);
// Read
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(userName, outCredential.Account);
Assert.Equal(password, outCredential.Password);
}
finally
{
// Ensure we clean up after ourselves even in case of 'get' failures
collection.Remove(service, userName);
}
}
[PosixFact]
public void GnuPassCredentialStore_WriteCredential_MultipleGpgIds_UsesNearestGpgId()
{
// Verify that when two subdirectories each have their own .gpg-id, encrypting a credential
// under one subdirectory uses that subdirectory's GPG identity, not the other one.
var fs = new TestFileSystem();
var gpg = new TestGpg(fs);
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string storePath = Path.Combine(homePath, ".password-store");
const string personalUserId = "personal@example.com";
const string workUserId = "work@example.com";
// Only register the personal key; if the wrong (work) key is picked, EncryptFile will throw.
gpg.GenerateKeys(personalUserId);
string personalSubDir = Path.Combine(storePath, "personal");
string workSubDir = Path.Combine(storePath, "work");
fs.Directories.Add(storePath);
fs.Directories.Add(personalSubDir);
fs.Directories.Add(workSubDir);
fs.Files[Path.Combine(personalSubDir, ".gpg-id")] = Encoding.UTF8.GetBytes(personalUserId);
fs.Files[Path.Combine(workSubDir, ".gpg-id")] = Encoding.UTF8.GetBytes(workUserId);
// Use "personal" namespace so credentials are stored under storePath/personal/...
var collection = new GpgPassCredentialStore(fs, gpg, storePath, "personal");
string service = $"https://example.com/{Guid.NewGuid():N}";
const string userName = "john.doe";
string password = Guid.NewGuid().ToString("N");
try
{
// Write - should pick personal/.gpg-id (personalUserId), not work/.gpg-id (workUserId)
collection.AddOrUpdate(service, userName, password);
ICredential outCredential = collection.Get(service, userName);
Assert.NotNull(outCredential);
Assert.Equal(userName, outCredential.Account);
Assert.Equal(password, outCredential.Password);
}
finally
{
collection.Remove(service, userName);
}
}
private static string InitializePasswordStore(TestFileSystem fs, TestGpg gpg)
{
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string storePath = Path.Combine(homePath, ".password-store");
string userId = "gcm-test@example.com";
string gpgIdPath = Path.Combine(storePath, ".gpg-id");
// Ensure we have a GPG key for use with testing
gpg.GenerateKeys(userId);
// Init the password store
fs.Directories.Add(storePath);
fs.Files[gpgIdPath] = Encoding.UTF8.GetBytes(userId);
return storePath;
}
}
}