Tests: Added tests to NegotiateRequestParsingTests and NegotiateResponseParsingTests

This commit is contained in:
Tal Aloni 2024-08-03 01:42:00 +03:00
parent 1485c365d5
commit 3f4381b5f9
2 changed files with 75 additions and 14 deletions

View file

@ -6,6 +6,7 @@
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SMBLibrary.SMB2;
using System;
namespace SMBLibrary.Tests.SMB2
{
@ -13,9 +14,47 @@ namespace SMBLibrary.Tests.SMB2
public class NegotiateRequestParsingTests
{
[TestMethod]
public void ParseNegotiateRequestWithNegotiateContextList()
public void ParseNegotiateRequestWithNegotiateContextList_WhenOffsetIsZero()
{
byte[] negotiateRequestCommandBytes = new byte[]
byte[] negotiateRequestCommandBytes = GetNegotiateRequestWithNegotiateContextListBytes();
NegotiateRequest negotiateRequest = new NegotiateRequest(negotiateRequestCommandBytes, 0);
Assert.AreEqual(5, negotiateRequest.Dialects.Count);
Assert.AreEqual(4, negotiateRequest.NegotiateContextList.Count);
Assert.AreEqual(NegotiateContextType.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, negotiateRequest.NegotiateContextList[0].ContextType);
Assert.AreEqual(NegotiateContextType.SMB2_ENCRYPTION_CAPABILITIES, negotiateRequest.NegotiateContextList[1].ContextType);
}
[TestMethod]
public void ParseNegotiateRequestWithNegotiateContextList_WhenOffsetIsNonZero()
{
// Test non-zero offset
byte[] negotiateRequestCommandBytes = GetNegotiateRequestWithNegotiateContextListBytes();
byte[] buffer = new byte[negotiateRequestCommandBytes.Length + 2];
Array.Copy(negotiateRequestCommandBytes, 0, buffer, 2, negotiateRequestCommandBytes.Length);
NegotiateRequest negotiateRequest = new NegotiateRequest(buffer, 2);
Assert.AreEqual(5, negotiateRequest.Dialects.Count);
Assert.AreEqual(4, negotiateRequest.NegotiateContextList.Count);
Assert.AreEqual(NegotiateContextType.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, negotiateRequest.NegotiateContextList[0].ContextType);
Assert.AreEqual(NegotiateContextType.SMB2_ENCRYPTION_CAPABILITIES, negotiateRequest.NegotiateContextList[1].ContextType);
}
[TestMethod]
public void ParseRewrittenNegotiateRequestWithNegotiateContextList()
{
byte[] negotiateRequestCommandBytes = GetNegotiateRequestWithNegotiateContextListBytes();
NegotiateRequest negotiateRequest = new NegotiateRequest(negotiateRequestCommandBytes, 0);
negotiateRequestCommandBytes = negotiateRequest.GetBytes();
negotiateRequest = new NegotiateRequest(negotiateRequestCommandBytes, 0);
Assert.AreEqual(5, negotiateRequest.Dialects.Count);
Assert.AreEqual(4, negotiateRequest.NegotiateContextList.Count);
Assert.AreEqual(NegotiateContextType.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, negotiateRequest.NegotiateContextList[0].ContextType);
Assert.AreEqual(NegotiateContextType.SMB2_ENCRYPTION_CAPABILITIES, negotiateRequest.NegotiateContextList[1].ContextType);
}
private static byte[] GetNegotiateRequestWithNegotiateContextListBytes()
{
return new byte[]
{
0xfe,0x53,0x4d,0x42,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
@ -35,12 +74,6 @@ namespace SMBLibrary.Tests.SMB2
0x73,0x00,0x65,0x00,0x72,0x00,0x73,0x00,0x2e,0x00,0x6c,0x00,0x6f,0x00,0x63,0x00,
0x61,0x00,0x6c,0x00
};
NegotiateRequest negotiateRequest = new NegotiateRequest(negotiateRequestCommandBytes, 0);
Assert.AreEqual(5, negotiateRequest.Dialects.Count);
Assert.AreEqual(4, negotiateRequest.NegotiateContextList.Count);
Assert.AreEqual(NegotiateContextType.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, negotiateRequest.NegotiateContextList[0].ContextType);
Assert.AreEqual(NegotiateContextType.SMB2_ENCRYPTION_CAPABILITIES, negotiateRequest.NegotiateContextList[1].ContextType);
}
}
}

View file

@ -6,6 +6,7 @@
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SMBLibrary.SMB2;
using System;
namespace SMBLibrary.Tests.SMB2
{
@ -13,9 +14,40 @@ namespace SMBLibrary.Tests.SMB2
public class NegotiateResponseParsingTests
{
[TestMethod]
public void ParseNegotiateResponseWithNegotiateContextList()
public void ParseNegotiateResponseWithNegotiateContextList_WhenOffsetIsZero()
{
byte[] negotiateResponseCommandBytes = new byte[]
byte[] negotiateResponseCommandBytes = GetNegotiateResponseWithNegotiateContextListBytes();
NegotiateResponse negotiateResponse = new NegotiateResponse(negotiateResponseCommandBytes, 0);
Assert.AreEqual(SMB2Dialect.SMB311, negotiateResponse.DialectRevision);
Assert.AreEqual(2, negotiateResponse.NegotiateContextList.Count);
}
[TestMethod]
public void ParseNegotiateResponseWithNegotiateContextList_WhenOffsetIsNonZero()
{
byte[] negotiateResponseCommandBytes = GetNegotiateResponseWithNegotiateContextListBytes();
byte[] buffer = new byte[negotiateResponseCommandBytes.Length + 2];
Array.Copy(negotiateResponseCommandBytes, 0, buffer, 2, negotiateResponseCommandBytes.Length);
NegotiateResponse negotiateResponse = new NegotiateResponse(buffer, 2);
Assert.AreEqual(SMB2Dialect.SMB311, negotiateResponse.DialectRevision);
Assert.AreEqual(2, negotiateResponse.NegotiateContextList.Count);
}
[TestMethod]
public void ParseRewrittenNegotiateResponseWithNegotiateContextList()
{
byte[] negotiateResponseCommandBytes = GetNegotiateResponseWithNegotiateContextListBytes();
NegotiateResponse negotiateResponse = new NegotiateResponse(negotiateResponseCommandBytes, 0);
negotiateResponseCommandBytes = negotiateResponse.GetBytes();
negotiateResponse = new NegotiateResponse(negotiateResponseCommandBytes, 0);
Assert.AreEqual(SMB2Dialect.SMB311, negotiateResponse.DialectRevision);
Assert.AreEqual(2, negotiateResponse.NegotiateContextList.Count);
}
private static byte[] GetNegotiateResponseWithNegotiateContextListBytes()
{
return new byte[]
{
0xfe, 0x53, 0x4d, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -50,10 +82,6 @@ namespace SMBLibrary.Tests.SMB2
0xe5, 0x74, 0x26, 0x7b, 0x55, 0xe9, 0x14, 0x7e, 0x96, 0xea, 0x2e, 0x56, 0x41, 0xfe, 0x00, 0x00,
0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00
};
NegotiateResponse negotiateResponse = new NegotiateResponse(negotiateResponseCommandBytes, 0);
Assert.AreEqual(SMB2Dialect.SMB311, negotiateResponse.DialectRevision);
Assert.AreEqual(2, negotiateResponse.NegotiateContextList.Count);
}
}
}