Improved validating of executable files

- improved unit tests with categories
- removed validating of batch files, we can not really validate them
This commit is contained in:
MaxXor 2015-07-31 10:01:41 +02:00
parent 4d0ae7bca0
commit 0306acdbe6
10 changed files with 56 additions and 73 deletions

View File

@ -54,7 +54,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Core\Commands\MiscHandler.Tests.cs" />
<Compile Include="Core\Helper\FileHelper.Tests.cs" />
<Compile Include="Core\Compression\JpgCompression.Tests.cs" />
<Compile Include="Core\Compression\SafeQuickLZ.Tests.cs" />
<Compile Include="Core\Encryption\AES.Tests.cs" />

View File

@ -1,48 +0,0 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using xClient.Core.Commands;
namespace xClient.Tests.Core.Compression
{
[TestClass]
public class MiscHandlerTests
{
[TestMethod]
public void UploadValidBatch()
{
var bytes = new byte[2];
bytes[0] = 101;
bytes[1] = 99;
var command = new xClient.Core.Packets.ServerPackets.DoUploadAndExecute(1, "bat.bat", bytes, 100, 0, false);
var result = CommandHandler.IsValidExecuteFile(command);
Assert.IsTrue(result, "Uploading a .bat file failed!");
}
[TestMethod]
public void UploadValidExe()
{
var bytes = new byte[2];
bytes[0] = 77;
bytes[1] = 90;
var command = new xClient.Core.Packets.ServerPackets.DoUploadAndExecute(1, "bat.bat", bytes, 100, 0, false);
var result = CommandHandler.IsValidExecuteFile(command);
Assert.IsTrue(result, "Uploading a .exe file failed!");
}
[TestMethod]
public void UploadInValidFile()
{
var bytes = new byte[2];
bytes[0] = 22;
bytes[1] = 93;
var command = new xClient.Core.Packets.ServerPackets.DoUploadAndExecute(1, "bat.bat", bytes, 100, 0, false);
var result = CommandHandler.IsValidExecuteFile(command);
Assert.IsFalse(result, "Uploading an invalid file worked!");
}
}
}

View File

@ -8,7 +8,7 @@ namespace xClient.Tests.Core.Compression
[TestClass]
public class JpgCompressionTests
{
[TestMethod]
[TestMethod, TestCategory("Compression")]
public void CompressionTest()
{
var quality = Int64.MaxValue;

View File

@ -11,8 +11,7 @@ public class SafeQuickLZTests
* Purpose: To validate a small amount of data after compression/decompression
* using SafeQuickLZ with level 1 compression.
*/
[TestMethod]
[TestCategory("Compression")]
[TestMethod, TestCategory("Compression")]
public void SmallDataCompressionTestLevel1()
{
SafeQuickLZ safeQuickLZtest = new SafeQuickLZ();
@ -40,8 +39,7 @@ public void SmallDataCompressionTestLevel1()
* Purpose: To validate a small amount of data after compression/decompression
* using SafeQuickLZ with level 3 compression.
*/
[TestMethod]
[TestCategory("Compression")]
[TestMethod, TestCategory("Compression")]
public void SmallDataCompressionTestLevel3()
{
SafeQuickLZ safeQuickLZtest = new SafeQuickLZ();
@ -69,8 +67,7 @@ public void SmallDataCompressionTestLevel3()
* Purpose: To validate a large amount of data after compression/decompression
* using SafeQuickLZ with level 1 compression.
*/
[TestMethod]
[TestCategory("Compression")]
[TestMethod, TestCategory("Compression")]
public void BigDataCompressionTestLevel1()
{
SafeQuickLZ safeQuickLZtest = new SafeQuickLZ();
@ -98,8 +95,7 @@ public void BigDataCompressionTestLevel1()
* Purpose: To validate a large amount of data after compression/decompression
* using SafeQuickLZ with level 3 compression.
*/
[TestMethod]
[TestCategory("Compression")]
[TestMethod, TestCategory("Compression")]
public void BigDataCompressionTestLevel3()
{
SafeQuickLZ safeQuickLZtest = new SafeQuickLZ();

View File

@ -8,7 +8,7 @@ namespace xClient.Tests.Core.Encryption
[TestClass]
public class AESTests
{
[TestMethod]
[TestMethod, TestCategory("Encryption")]
public void EncryptAndDecryptStringTest()
{
var input = FileHelper.GetRandomFilename(100);
@ -23,7 +23,7 @@ public void EncryptAndDecryptStringTest()
Assert.AreEqual(input, decrypted);
}
[TestMethod]
[TestMethod, TestCategory("Encryption")]
public void EncryptAndDecryptByteArrayTest()
{
var input = FileHelper.GetRandomFilename(100);

View File

@ -7,7 +7,7 @@ namespace xClient.Tests.Core.Encryption
[TestClass]
public class SHA256Tests
{
[TestMethod]
[TestMethod, TestCategory("Encryption")]
public void ComputeHashTest()
{
var input = FileHelper.GetRandomFilename(100);

View File

@ -0,0 +1,38 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using xClient.Core.Helper;
namespace xClient.Tests.Core.Helper
{
[TestClass]
public class FileHelperTests
{
[TestMethod, TestCategory("Helper")]
public void RandomFilenameTest()
{
int length = 100;
var name = FileHelper.GetRandomFilename(length);
Assert.IsNotNull(name);
Assert.IsTrue(name.Length == length, "Filename has wrong length!");
}
[TestMethod, TestCategory("Helper")]
public void ValidateExecutableTest()
{
var bytes = new byte[] {77, 90};
var result = FileHelper.IsValidExecuteableFile(bytes);
Assert.IsTrue(result, "Validating a .exe file failed!");
}
[TestMethod, TestCategory("Helper")]
public void ValidateInvalidFileTest()
{
var bytes = new byte[] {22, 93};
var result = FileHelper.IsValidExecuteableFile(bytes);
Assert.IsFalse(result, "Validating an invalid file worked!");
}
}
}

View File

@ -75,7 +75,8 @@ public static void HandleDoUploadAndExecute(Packets.ServerPackets.DoUploadAndExe
try
{
if (!IsValidExecuteFile(command)) throw new Exception("File type is not valid");
if (command.CurrentBlock == 0 && Path.GetExtension(command.FileName) == ".exe" && !FileHelper.IsValidExecuteableFile(command.Block))
throw new Exception("No executable file");
FileSplit destFile = new FileSplit(filePath);
@ -156,15 +157,5 @@ public static void HandleDoShowMessageBox(Packets.ServerPackets.DoShowMessageBox
new Packets.ClientPackets.SetStatus("Showed Messagebox").Execute(client);
}
public static bool IsValidExecuteFile(Packets.ServerPackets.DoUploadAndExecute command)
{
if (command.CurrentBlock == 0 && command.Block[0] != 'M' && command.Block[1] != 'Z' &&
command.CurrentBlock == 0 && command.Block[0] != 'e' && command.Block[1] != 'c')
return false;
return true;
}
}
}

View File

@ -16,5 +16,11 @@ public static string GetRandomFilename(int length, string extension = "")
return string.Concat(randomName.ToString(), extension);
}
public static bool IsValidExecuteableFile(byte[] block)
{
if (block.Length < 2) return false;
return (block[0] == 'M' && block[1] == 'Z') || (block[0] == 'Z' && block[1] == 'M');
}
}
}

View File

@ -647,7 +647,7 @@ private void ctxtLocalFile_Click(object sender, EventArgs e)
foreach (Client c in GetSelectedClients())
{
if (c == null) continue;
if(error) continue;
if (error) continue;
FileSplit srcFile = new FileSplit(UploadAndExecute.FilePath);
if (srcFile.MaxBlocks < 0)