Initial commit: Client Doc docs Server Tools
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using Xunit;
|
||||
using XWorld.Server.Auth;
|
||||
|
||||
namespace XWorld.Server.Auth.Tests
|
||||
{
|
||||
public class TokenServiceTests
|
||||
{
|
||||
static byte[] Key() => new byte[32]; // 全零密钥,测试用固定值
|
||||
|
||||
[Fact]
|
||||
public void Issue_then_verify_roundtrip_returns_pid()
|
||||
{
|
||||
var t = new TokenService(Key());
|
||||
string tok = t.Issue(4242, TimeSpan.FromHours(1));
|
||||
Assert.True(t.Verify(tok, out int pid));
|
||||
Assert.Equal(4242, pid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tampered_token_rejected()
|
||||
{
|
||||
var t = new TokenService(Key());
|
||||
string tok = t.Issue(7, TimeSpan.FromHours(1));
|
||||
string bad = tok.Substring(0, tok.Length - 1) + (tok[tok.Length - 1] == 'A' ? 'B' : 'A');
|
||||
Assert.False(t.Verify(bad, out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Expired_token_rejected()
|
||||
{
|
||||
var t = new TokenService(Key());
|
||||
string tok = t.Issue(7, TimeSpan.FromSeconds(-1)); // 已过期
|
||||
Assert.False(t.Verify(tok, out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Wrong_key_rejected()
|
||||
{
|
||||
var signer = new TokenService(Key());
|
||||
string tok = signer.Issue(7, TimeSpan.FromHours(1));
|
||||
var other = new TokenService(new byte[32] { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 });
|
||||
Assert.False(other.Verify(tok, out _));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Garbage_token_rejected()
|
||||
{
|
||||
var t = new TokenService(Key());
|
||||
Assert.False(t.Verify("not-a-token", out _));
|
||||
Assert.False(t.Verify("", out _));
|
||||
Assert.False(t.Verify(null, out _));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user