|
| 1 | +// Copyright (c) ktsu.dev |
| 2 | +// All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +namespace ktsu.CredentialCache.Test; |
| 6 | + |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using ktsu.CredentialCache.Storage; |
| 9 | +using ktsu.Semantics.Strings; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Exercises the platform-native credential store returned by |
| 13 | +/// <see cref="CredentialStoreFactory.CreateDefault(string)"/> on whatever OS |
| 14 | +/// the test happens to be running on. The store is scoped to a per-run service |
| 15 | +/// name so the tests don't collide with other applications' real credentials. |
| 16 | +/// |
| 17 | +/// On Linux these require a running Secret Service implementation (e.g. |
| 18 | +/// <c>gnome-keyring-daemon</c> launched under <c>dbus-run-session</c>). The |
| 19 | +/// cross-platform CI workflow provides one; locally they will fail-fast with |
| 20 | +/// a clear <see cref="CredentialStoreException"/> if no daemon is available. |
| 21 | +/// </summary> |
| 22 | +[TestClass] |
| 23 | +public class NativeCredentialStoreTests |
| 24 | +{ |
| 25 | + private static string UniqueServiceName() => |
| 26 | + $"ktsu.CredentialCache.IntegrationTest.{Guid.NewGuid():N}"; |
| 27 | + |
| 28 | + private static ICredentialStore CreateNativeStore() => |
| 29 | + CredentialStoreFactory.CreateDefault(UniqueServiceName()); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Performs a tiny no-op call against the native store to verify the platform |
| 33 | + /// dependencies are actually present (e.g. libsecret loaded and a Secret |
| 34 | + /// Service daemon is reachable on Linux). If not, the test is reported as |
| 35 | + /// <see cref="Assert.Inconclusive(string)"/> rather than failed, so a |
| 36 | + /// developer without a keyring set up doesn't see scary red. |
| 37 | + /// </summary> |
| 38 | + private static void AssertNativeStoreAvailableOrInconclusive(ICredentialStore store) |
| 39 | + { |
| 40 | + PersonaGUID probe = CredentialCache.CreatePersonaGUID(); |
| 41 | + try |
| 42 | + { |
| 43 | + // Removing a key that doesn't exist must not throw on a working backend. |
| 44 | + _ = store.Remove(probe); |
| 45 | + } |
| 46 | + catch (Exception ex) when (IsMissingPlatformDependency(ex)) |
| 47 | + { |
| 48 | + Assert.Inconclusive($"Native credential store is not available in this environment: {ex.GetType().Name}: {ex.Message}"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static bool IsMissingPlatformDependency(Exception ex) |
| 53 | + { |
| 54 | + // Walk the inner-exception chain - libsecret/Security.framework load |
| 55 | + // failures surface inside a TypeInitializationException when triggered |
| 56 | + // during a static cctor (e.g. the libsecret schema handle). |
| 57 | + for (Exception? current = ex; current is not null; current = current.InnerException) |
| 58 | + { |
| 59 | + if (current is DllNotFoundException or CredentialStoreException) |
| 60 | + { |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void NativeStoreRoundTripsCredentialWithToken() |
| 69 | + { |
| 70 | + ICredentialStore store = CreateNativeStore(); |
| 71 | + AssertNativeStoreAvailableOrInconclusive(store); |
| 72 | + PersonaGUID persona = CredentialCache.CreatePersonaGUID(); |
| 73 | + Credential original = new CredentialWithToken |
| 74 | + { |
| 75 | + Token = SemanticString<CredentialToken>.Create("native-test-token"), |
| 76 | + }; |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + store.Save(persona, original); |
| 81 | + |
| 82 | + Assert.IsTrue(store.TryLoad(persona, out Credential? loaded)); |
| 83 | + CredentialWithToken? typed = loaded as CredentialWithToken; |
| 84 | + Assert.IsNotNull(typed); |
| 85 | + Assert.AreEqual("native-test-token", typed!.Token.ToString()); |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + store.Remove(persona); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void NativeStoreSaveOverwritesExistingEntry() |
| 95 | + { |
| 96 | + ICredentialStore store = CreateNativeStore(); |
| 97 | + AssertNativeStoreAvailableOrInconclusive(store); |
| 98 | + PersonaGUID persona = CredentialCache.CreatePersonaGUID(); |
| 99 | + |
| 100 | + try |
| 101 | + { |
| 102 | + store.Save(persona, new CredentialWithToken |
| 103 | + { |
| 104 | + Token = SemanticString<CredentialToken>.Create("first"), |
| 105 | + }); |
| 106 | + store.Save(persona, new CredentialWithToken |
| 107 | + { |
| 108 | + Token = SemanticString<CredentialToken>.Create("second"), |
| 109 | + }); |
| 110 | + |
| 111 | + Assert.IsTrue(store.TryLoad(persona, out Credential? loaded)); |
| 112 | + Assert.AreEqual("second", ((CredentialWithToken)loaded!).Token.ToString()); |
| 113 | + } |
| 114 | + finally |
| 115 | + { |
| 116 | + store.Remove(persona); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + public void NativeStoreRemoveReturnsFalseForUnknownPersona() |
| 122 | + { |
| 123 | + ICredentialStore store = CreateNativeStore(); |
| 124 | + AssertNativeStoreAvailableOrInconclusive(store); |
| 125 | + PersonaGUID persona = CredentialCache.CreatePersonaGUID(); |
| 126 | + |
| 127 | + Assert.IsFalse(store.Remove(persona)); |
| 128 | + Assert.IsFalse(store.TryLoad(persona, out _)); |
| 129 | + } |
| 130 | + |
| 131 | + [TestMethod] |
| 132 | + public void NativeStoreSurvivesAcrossStoreInstances() |
| 133 | + { |
| 134 | + string service = UniqueServiceName(); |
| 135 | + PersonaGUID persona = CredentialCache.CreatePersonaGUID(); |
| 136 | + Credential original = new CredentialWithUsernamePassword |
| 137 | + { |
| 138 | + Username = SemanticString<CredentialUsername>.Create("bob"), |
| 139 | + Password = SemanticString<CredentialPassword>.Create("sekrit"), |
| 140 | + }; |
| 141 | + |
| 142 | + ICredentialStore writer = CredentialStoreFactory.CreateDefault(service); |
| 143 | + AssertNativeStoreAvailableOrInconclusive(writer); |
| 144 | + try |
| 145 | + { |
| 146 | + writer.Save(persona, original); |
| 147 | + |
| 148 | + ICredentialStore reader = CredentialStoreFactory.CreateDefault(service); |
| 149 | + Assert.IsTrue(reader.TryGet(persona, out Credential? loaded)); |
| 150 | + CredentialWithUsernamePassword? typed = loaded as CredentialWithUsernamePassword; |
| 151 | + Assert.IsNotNull(typed); |
| 152 | + Assert.AreEqual("bob", typed!.Username.ToString()); |
| 153 | + Assert.AreEqual("sekrit", typed.Password.ToString()); |
| 154 | + } |
| 155 | + finally |
| 156 | + { |
| 157 | + writer.Remove(persona); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + public void WindowsStoreEnumerateKeysReturnsWrittenPersonas() |
| 163 | + { |
| 164 | + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 165 | + { |
| 166 | + Assert.Inconclusive("EnumerateKeys is only implemented on Windows Credential Manager."); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + ICredentialStore store = CreateNativeStore(); |
| 171 | + AssertNativeStoreAvailableOrInconclusive(store); |
| 172 | + ISearchableCredentialStore? searchable = store as ISearchableCredentialStore; |
| 173 | + Assert.IsNotNull(searchable, "Windows store should implement ISearchableCredentialStore."); |
| 174 | + |
| 175 | + PersonaGUID persona = CredentialCache.CreatePersonaGUID(); |
| 176 | + try |
| 177 | + { |
| 178 | + searchable!.Save(persona, new CredentialWithNothing()); |
| 179 | + IEnumerable<PersonaGUID> keys = searchable.EnumerateKeys(); |
| 180 | + Assert.IsTrue(keys.Any(k => string.Equals(k.ToString(), persona.ToString(), StringComparison.Ordinal))); |
| 181 | + } |
| 182 | + finally |
| 183 | + { |
| 184 | + searchable!.Remove(persona); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +/// <summary> |
| 190 | +/// Small helper that wires TryLoad through as TryGet for symmetry with |
| 191 | +/// CredentialCache's API in test assertions. Keeps the assertion sites readable. |
| 192 | +/// </summary> |
| 193 | +internal static class CredentialStoreTestExtensions |
| 194 | +{ |
| 195 | + public static bool TryGet(this ICredentialStore store, PersonaGUID persona, out Credential? credential) |
| 196 | + => store.TryLoad(persona, out credential); |
| 197 | +} |
0 commit comments