Path: redb-identity/tests/redb.Identity.Tests/Architecture/TokenStatusWriteTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Architecture/TokenStatusWriteTests.cs ---- using System.Text.RegularExpressions; using AwesomeAssertions; using OpenIddict.Abstractions; using redb.Core.Models.Entities; using redb.Identity.Core.Models; using redb.Identity.Core.Stores; using Xunit; namespace redb.Identity.Tests.Architecture; /// /// A token's status is written only through , which keeps the object row's /// value_bool in step with it; the resource side's entry check reads only that column. A status written past /// the helper would leave the column stale and a revoked token accepted. /// /// The scan is by text, so it fails closed: every .Props.Status = in the Identity sources outside the helper /// has to be one of the known writes of another kind of object (sessions, authorizations), named by file and /// variable below. A new write of a session or authorization status is added to that list; a write of a token's /// status goes through the helper. /// /// public class TokenStatusWriteTests { private static readonly string SourceRoot = LocateSourceRoot(); // file name -> variables whose Props.Status is written there and which are not tokens. private static readonly Dictionary OtherObjectStatusWrites = new(StringComparer.Ordinal) { ["SessionCleanupProcessor.cs"] = ["s"], ["ConsentService.cs"] = ["target"], ["SessionService.cs"] = ["s", "session", "auth"], ["RedbAuthorizationStore.cs"] = ["target", "authorization"], }; private static readonly Regex StatusWrite = new(@"(?[\w\.\]\)]+?)\.Props\.Status\s*=(?![=>])", RegexOptions.Compiled); private static readonly Regex TokenPropsInitializerStatus = new(@"new\s+TokenProps\s*(\(\s*\))?\s*\{[^}]*\bStatus\s*=", RegexOptions.Compiled | RegexOptions.Singleline); private static string LocateSourceRoot() { var dir = new DirectoryInfo(AppContext.BaseDirectory); while (dir is not null) { var candidate = Path.Combine(dir.FullName, "redb.Identity", "src"); if (Directory.Exists(Path.Combine(candidate, "redb.Identity.Core"))) return candidate; candidate = Path.Combine(dir.FullName, "src"); if (Directory.Exists(Path.Combine(candidate, "redb.Identity.Core"))) return candidate; dir = dir.Parent; } throw new InvalidOperationException("Could not locate redb.Identity/src from the test base directory."); } private static IEnumerable<(string Path, string Source)> Sources() { var sep = Path.DirectorySeparatorChar; foreach (var file in Directory.EnumerateFiles(SourceRoot, "*.cs", SearchOption.AllDirectories)) { if (file.Contains($"{sep}obj{sep}", StringComparison.Ordinal) || file.Contains($"{sep}bin{sep}", StringComparison.Ordinal)) continue; var source = File.ReadAllText(file); source = Regex.Replace(source, @"/\*.*?\*/", string.Empty, RegexOptions.Singleline); source = Regex.Replace(source, @"//[^\n]*", string.Empty); yield return (file, source); } } [Fact] public void A_token_status_is_written_only_through_the_helper() { var offenders = new List(); foreach (var (path, source) in Sources()) { var name = Path.GetFileName(path); if (name == "TokenStatus.cs") continue; OtherObjectStatusWrites.TryGetValue(name, out var allowed); foreach (Match m in StatusWrite.Matches(source)) { var target = m.Groups["target"].Value; if (allowed is null || !allowed.Contains(target, StringComparer.Ordinal)) offenders.Add($"{Path.GetRelativePath(SourceRoot, path)}: {target}.Props.Status ="); } if (TokenPropsInitializerStatus.IsMatch(source)) offenders.Add($"{Path.GetRelativePath(SourceRoot, path)}: new TokenProps {{ Status = ... }}"); } offenders.Should().BeEmpty( "a token's status is set with TokenStatus.Set, which keeps value_bool in step; a session or " + "authorization status write is added to OtherObjectStatusWrites"); } [Fact] public void The_known_other_writes_still_exist() { // The list stays exact: an entry whose write is gone is removed, so the list cannot hide a later token write // under an old name. var seen = new HashSet(StringComparer.Ordinal); foreach (var (path, source) in Sources()) { var name = Path.GetFileName(path); foreach (Match m in StatusWrite.Matches(source)) seen.Add(name + ":" + m.Groups["target"].Value); } foreach (var (file, variables) in OtherObjectStatusWrites) foreach (var variable in variables) seen.Should().Contain(file + ":" + variable); } [Theory] [InlineData(OpenIddictConstants.Statuses.Valid, true)] [InlineData(OpenIddictConstants.Statuses.Revoked, false)] [InlineData(OpenIddictConstants.Statuses.Redeemed, false)] [InlineData(OpenIddictConstants.Statuses.Inactive, false)] [InlineData(OpenIddictConstants.Statuses.Rejected, false)] [InlineData(null, false)] public void The_helper_mirrors_valid_only(string? status, bool expected) { var token = new RedbObject(new TokenProps()); TokenStatus.Set(token, status); token.Props.Status.Should().Be(status); token.value_bool.Should().Be(expected); } }