|
| 1 | +using System.Text; |
| 2 | +using CosmoSQLClient.MsSql.Auth; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace CosmoSQLClient.MsSql.Tests; |
| 6 | + |
| 7 | +public class NtlmAuthTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void TestMD4_EmptyString() |
| 11 | + { |
| 12 | + var result = MD4.Hash(Array.Empty<byte>()); |
| 13 | + Assert.Equal("31d6cfe0d16ae931b73c59d7e0c089c0", ToHex(result)); |
| 14 | + } |
| 15 | + |
| 16 | + [Fact] |
| 17 | + public void TestMD4_Message() |
| 18 | + { |
| 19 | + var result = MD4.Hash(Encoding.ASCII.GetBytes("a")); |
| 20 | + Assert.Equal("bde52cb31de33e46245e05fbdbd6fb24", ToHex(result)); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public void TestMD4_LongString() |
| 25 | + { |
| 26 | + var input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 27 | + var result = MD4.Hash(Encoding.ASCII.GetBytes(input)); |
| 28 | + Assert.Equal("043f8582f241db351ce627e153e7f0e4", ToHex(result)); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void TestNtlmExchange_Structure() |
| 33 | + { |
| 34 | + // 1. Test Negotiate |
| 35 | + var negotiate = NtlmAuth.BuildNegotiate(); |
| 36 | + Assert.StartsWith("NTLMSSP", Encoding.ASCII.GetString(negotiate)); |
| 37 | + Assert.Equal(1u, BitConverter.ToUInt32(negotiate, 8)); // Type 1 |
| 38 | + |
| 39 | + // 2. Test Challenge Parsing (using a synthetic challenge) |
| 40 | + var challengeBytes = new byte[64]; |
| 41 | + Encoding.ASCII.GetBytes("NTLMSSP\0").CopyTo(challengeBytes, 0); |
| 42 | + BitConverter.GetBytes(2u).CopyTo(challengeBytes, 8); // Type 2 |
| 43 | + var serverChallenge = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; |
| 44 | + serverChallenge.CopyTo(challengeBytes, 24); |
| 45 | + BitConverter.GetBytes(56).CopyTo(challengeBytes, 44); // TargetInfo Offset |
| 46 | + |
| 47 | + var parsed = NtlmAuth.ParseChallenge(challengeBytes); |
| 48 | + Assert.Equal(serverChallenge, parsed.ServerChallenge); |
| 49 | + |
| 50 | + // 3. Test Authenticate (Type 3) |
| 51 | + var auth = NtlmAuth.BuildAuthenticate(challengeBytes, "User", "Password", "Domain", "WORKSTATION"); |
| 52 | + Assert.StartsWith("NTLMSSP", Encoding.ASCII.GetString(auth)); |
| 53 | + Assert.Equal(3u, BitConverter.ToUInt32(auth, 8)); // Type 3 |
| 54 | + } |
| 55 | + |
| 56 | + private static string ToHex(byte[] bytes) |
| 57 | + { |
| 58 | + var sb = new StringBuilder(); |
| 59 | + foreach (var b in bytes) sb.Append(b.ToString("x2")); |
| 60 | + return sb.ToString(); |
| 61 | + } |
| 62 | +} |
0 commit comments