Files
2026-11-13 11:08:23 +03:00

27 lines
1.8 KiB
C#

using Messenger.Storage;
using Messenger.Domain;
using Messenger.Api;
using System.Security.Cryptography;
using System.Text.Json;
static void Check(bool condition,string label){if(!condition)throw new Exception(label);Console.WriteLine("PASS: "+label);}
var passwords=new Passwords();
var unicode=string.Concat(Enumerable.Repeat("🔑",90))+":пароль";
var encoded=await passwords.Hash(unicode);
Check(await passwords.Verify(unicode,encoded),"Argon2id verifies Unicode password >72 bytes");
Check(!await passwords.Verify(unicode+"x",encoded),"Argon2id rejects changed suffix (no truncation)");
Check(!await passwords.Verify(unicode,null),"unknown account rejected after KDF");
Check(encoded!=await passwords.Hash(unicode),"random salt changes password hash");
Environment.SetEnvironmentVariable("CURSOR_KEY",Convert.ToBase64String(RandomNumberGenerator.GetBytes(32)));
var codec=new CursorCodec();
var message=new Message(Guid.NewGuid(),Guid.NewGuid(),Guid.NewGuid(),"test",DateTime.UtcNow);
var scope="messages:"+message.ChatId;
var cursor=codec.Encode(scope,message);
Check(new CursorCodec().Decode(cursor,scope)==(message.CreatedAt.Ticks,message.Id),"cursor survives codec recreation with same key");
try{codec.Decode(cursor,"messages:"+Guid.NewGuid());throw new Exception("accepted wrong scope");}
catch(ApiException ex){Check(ex.Status==400,"foreign chat cursor rejected");}
try{codec.Decode("!"+cursor[1..],scope);throw new Exception("accepted corrupted cursor");}
catch(ApiException ex){Check(ex.Status==400,"corrupt cursor rejected");}
using var body=JsonDocument.Parse(JsonSerializer.Serialize(new {password=unicode}));
Check(Input.Password(body.RootElement)==unicode,"password input counts Unicode code points");
Console.WriteLine("Unit checks only: PostgreSQL, Redis and HTTP not tested here.");