private void SMB2ChainedCompression_Variant(CompressionTestVariant variant, bool needPatternV1 = false, bool needCompressionAlgorithm = false, bool needEncryption = false) { CompressionAlgorithm?compressionAlgorithmToCheck = null; if (needPatternV1) { compressionAlgorithmToCheck = CompressionAlgorithm.Pattern_V1; } CheckCompressionAndEncryptionApplicability(compressionAlgorithmToCheck, needEncryption, true, needCompressionAlgorithm); var compressionAlgorithms = new List <CompressionAlgorithm>(); if (needPatternV1) { compressionAlgorithms.Add(CompressionAlgorithm.Pattern_V1); } if (needCompressionAlgorithm) { compressionAlgorithms.AddRange(Smb2Utility.GetSupportedCompressionAlgorithms(TestConfig.SupportedCompressionAlgorithmList.ToArray())); } SMB2CompressionTest(compressionAlgorithms.ToArray(), variant, needEncryption, enableChainedCompression: true); }
private void SMB2Compression_Variant(CompressionTestVariant variant) { CheckCompressionAndEncryptionApplicability(); var compressionAlgorithms = TestConfig.SupportedCompressionAlgorithmList.ToArray(); SMB2CompressionTest(compressionAlgorithms, variant); }
private void SMB2Compression_Variant(CompressionTestVariant variant, bool isLargeFile = false) { CheckCompressionAndEncryptionApplicability(); var compressionAlgorithms = TestConfig.SupportedCompressionAlgorithmList.ToArray(); SMB2CompressionTest(compressionAlgorithms, variant, isLargeFile: isLargeFile); }
/// <summary> /// Generate a series of compression test runner with given test variant. /// </summary> /// <param name="variant">Compression test variant to use.</param> /// <returns></returns> public static CompressionTestRunner[] Generate(CompressionAlgorithm[] compressionAlgorithms, CompressionTestVariant variant) { var callDict = new Dictionary <CompressionTestVariant, Func <CompressionAlgorithm[], CompressionTestRunner[]> > { [CompressionTestVariant.BasicReadWrite] = GenerateBasicReadWrite, [CompressionTestVariant.CompressibleWrite] = GenerateCompressibleWrite, [CompressionTestVariant.CompressibleRead] = GenerateCompressibleRead, [CompressionTestVariant.IncompressibleRead] = GenerateIncompressibleRead, [CompressionTestVariant.BasicChainedReadWrite] = GenerateBasicChainedReadWrite, [CompressionTestVariant.ChainedCompressibleWritePatternV1AtFront] = GenerateChainedCompressibleWritePatternV1AtFront, [CompressionTestVariant.ChainedCompressibleWritePatternV1AtEnd] = GenerateChainedCompressibleWritePatternV1AtEnd, [CompressionTestVariant.ChainedCompressibleWritePatternV1AtFrontAndEnd] = GenerateChainedCompressibleWritePatternV1AtFrontAndEnd, [CompressionTestVariant.ChainedCompressibleRead] = GenerateChainedCompressibleRead, [CompressionTestVariant.ChainedIncompressibleRead] = GenerateChainedIncompressibleRead, }; var result = callDict[variant](compressionAlgorithms); return(result); }
private void SMB2CompressionTest(CompressionAlgorithm[] compressionAlgorithms, CompressionTestVariant variant, bool enableEncryption = false, bool enableChainedCompression = false) { uint treeId; FILEID fileId; if (!enableChainedCompression) { compressionAlgorithms = Smb2Utility.GetSupportedCompressionAlgorithms(compressionAlgorithms); } CreateTestFile(compressionAlgorithms, enableEncryption, out treeId, out fileId, enableChainedCompression); var instances = CompressionTestRunner.Generate(compressionAlgorithms, variant); foreach (var instance in instances) { instance.Run(client, treeId, fileId); } client.Close(treeId, fileId); client.TreeDisconnect(treeId); client.LogOff(); }
/// <summary> /// Run compression test against given input parameters. /// 1. Write testData to test file given by treeId and fileId, and compress message based on compressWriteRequest using compressionAlgorithmForTest. /// 2. Read out the data just written, and request compressing READ response message based on compressReadRequest. /// 3. Check whether the READ response based on readResponseShouldBeCompressed. /// 4. Check whether data read out is equal to test data. /// </summary> /// <param name="client">SMB2 functional client to use.</param> /// <param name="variant">Compression test variant.</param> /// <param name="treeId">TreeId to use.</param> /// <param name="fileId">FileId to use.</param> /// <param name="isLargeFile">Whether is large file.</param> public void Run(Smb2FunctionalClient client, CompressionTestVariant variant, uint treeId, FILEID fileId, bool isLargeFile = false) { if (compressionAlgorithmForTest != CompressionAlgorithm.NONE) { if (!client.Smb2Client.CompressionInfo.CompressionIds.Any(compressionAlgorithmSupported => compressionAlgorithmSupported == compressionAlgorithmForTest)) { // The specified compression algorithm is not supported by SUT. return; } } BaseTestSite.Log.Add( LogEntryKind.TestStep, "Test will trigger WRITE request with CompressWrite: {0} and preferred compression algorithm: {1}.", compressWriteRequest, compressionAlgorithmForTest ); // Specify the compression algorithm for write request. client.Smb2Client.CompressionInfo.PreferredCompressionAlgorithm = compressionAlgorithmForTest; if (compressWriteRequestBufferOnly) { client.Smb2Client.CompressionInfo.CompressBufferOnly = true; } if (isLargeFile) { // Write several times for existing testData for large data write and read tests // Base testData: // Length of LZ77 and LZ77 Huffman testData is 300 bytes. // Length of LZNT1 testData is 142 bytes. // Length of Pattern_V1 testData is 256 bytes. // Length of Compressible data testData is 2048 bytes. // We will change testData to 1 MB, and write 100 times to generate a 100 MB file int writeRequestCount = 100; ulong offset = 0; // According to real experience with copying large file to shared folder, the client will request 1 MB packet per request. // Change testData to 512 bytes * 2048 = 1 megabytes, so we can test large file if (compressionAlgorithmForTest == CompressionAlgorithm.LZ77Huffman) { // LZ77Huffman match length needs to be less than 65538, so we use 512*128=65536. var test512Bytes = Enumerable.Repeat(testData, 2).SelectMany(a => a).Take(512).ToArray(); testData = Enumerable.Repeat(test512Bytes, 128).SelectMany(b => b).ToArray(); } else if (variant == CompressionTestVariant.ChainedCompressibleWritePatternV1AtFront) { // For ChainedCompressibleWritePatternV1AtFront, we need to prepend some data which can be compressed with PatternV1. var test256Bytes = commonCompressibleData.Take(256).ToArray(); var newCommonCompressibleData = Enumerable.Repeat(test256Bytes, 4 * 1024 - 1).SelectMany(b => b).ToArray(); testData = GenerateByteArray(exampleTestData[CompressionAlgorithm.Pattern_V1], newCommonCompressibleData); } else if (variant == CompressionTestVariant.ChainedCompressibleRead || variant == CompressionTestVariant.ChainedCompressibleWritePatternV1AtEnd) { // For ChainedCompressibleRead or ChainedCompressibleWritePatternV1AtEnd, we need to append some data which can be compressed with PatternV1. var test256Bytes = commonCompressibleData.Take(256).ToArray(); var newCommonCompressibleData = Enumerable.Repeat(test256Bytes, 4 * 1024 - 1).SelectMany(b => b).ToArray(); testData = GenerateByteArray(newCommonCompressibleData, exampleTestData[CompressionAlgorithm.Pattern_V1]); } else if (variant == CompressionTestVariant.ChainedCompressibleWritePatternV1AtFrontAndEnd) { // For ChainedCompressibleWritePatternV1AtFrontAndEnd, we need to add start and end with PatternV1. var test256Bytes = commonCompressibleData.Take(256).ToArray(); var newCommonCompressibleData = Enumerable.Repeat(test256Bytes, 4 * 1024 - 2).SelectMany(b => b).ToArray(); testData = GenerateByteArray(exampleTestData[CompressionAlgorithm.Pattern_V1], newCommonCompressibleData, exampleTestData[CompressionAlgorithm.Pattern_V1]); } else { // For other cases, testData with a length of 1 MB will be used. var test512Bytes = Enumerable.Repeat(testData, 4).SelectMany(a => a).Take(512).ToArray(); testData = Enumerable.Repeat(test512Bytes, 2048).SelectMany(b => b).ToArray(); } int requestBytes = testData.Length; for (int time = 0; time < writeRequestCount; time++) { client.Write(treeId, fileId, testData, offset, compressWrite: compressWriteRequest); offset += (uint)requestBytes; } } else { client.Write(treeId, fileId, testData, compressWrite: compressWriteRequest); } if (compressWriteRequestBufferOnly) { client.Smb2Client.CompressionInfo.CompressBufferOnly = false; } byte[] readOutData = null; bool readResponseIsCompressed = false; bool readResponseIsChained = false; Smb2ReadResponsePacket readResponsePacket = null; Smb2CompressedPacket compressedPacket = null; Action <Smb2Packet> Smb2Client_PacketReceived = (Smb2Packet obj) => { if (obj is Smb2ReadResponsePacket) { readResponsePacket = obj as Smb2ReadResponsePacket; readResponseIsCompressed = readResponsePacket.Compressed; compressedPacket = readResponsePacket.CompressedPacket; if (compressedPacket is Smb2ChainedCompressedPacket) { readResponseIsChained = true; } } }; BaseTestSite.Log.Add( LogEntryKind.TestStep, "Test will trigger READ request with CompressRead: {0} and check whether READ response is compressed: {1}.", compressReadRequest, readResponseShouldBeCompressed ); client.Smb2Client.PacketReceived += Smb2Client_PacketReceived; if (isLargeFile) { // Read several times for exist testData for large data write and read tests int requestBytes = testData.Length; int readRequestCount = 100; ulong offset = 0; for (int time = 0; time < readRequestCount; time++) { client.Read(treeId, fileId, (uint)offset, (uint)requestBytes, out readOutData, compressRead: compressReadRequest); BaseTestSite.Assert.IsTrue(Enumerable.SequenceEqual(testData, readOutData), $"Request times:{time + 1}, packet offset: {offset}, byteSize:{requestBytes}, the read out content MUST be the same with that is written."); offset += (uint)requestBytes; } } else { client.Read(treeId, fileId, 0, (uint)testData.Length, out readOutData, compressRead: compressReadRequest); BaseTestSite.Assert.IsTrue(Enumerable.SequenceEqual(testData, readOutData), "The read out content MUST be the same with that is written."); } client.Smb2Client.PacketReceived -= Smb2Client_PacketReceived; if (compressReadRequest) { if (readResponseShouldBeCompressed) { BaseTestSite.Assert.IsTrue(readResponseIsCompressed && compressedPacket != null, "[MS-SMB2] section 3.3.5.12: When SMB2_READFLAG_REQUEST_COMPRESSED is specified in read request, the server MUST compress the message if compression will shrink the message size."); BaseTestSite.Log.Add(LogEntryKind.Debug, "Read response is compressed using {0}.", compressedPacket.Header.CompressionAlgorithm); if (readResponseShouldBeChained) { BaseTestSite.Assert.IsTrue(readResponseIsChained, "The read response should be chained."); } else { BaseTestSite.Assert.IsFalse(readResponseIsChained, "The read response should not be chained."); } } else { BaseTestSite.Assert.IsTrue(!readResponseIsCompressed && compressedPacket == null, "[MS-SMB2] section 3.3.5.12: When SMB2_READFLAG_REQUEST_COMPRESSED is specified in read request, the server MUST NOT compress the message if compression will not shrink the message size."); } } else { BaseTestSite.Log.Add(LogEntryKind.Debug, "SMB2_READFLAG_REQUEST_COMPRESSED is not specified in read request, and read response is compressed: {0}.", readResponseIsCompressed); if (readResponseIsCompressed) { BaseTestSite.Assert.IsTrue(compressedPacket != null, "Compressed packet is received."); BaseTestSite.Log.Add(LogEntryKind.Debug, "Read response is compressed using {0}.", compressedPacket.Header.CompressionAlgorithm); } } }
/// <summary> /// Generate a series of compression test runner with given test variant. /// </summary> /// <param name="variant">Compression test variant to use.</param> /// <returns></returns> public static CompressionTestRunner[] Generate(CompressionTestVariant variant) { CompressionTestRunner[] result = null; switch (variant) { case CompressionTestVariant.BasicReadWrite: { // Use specific test data for all supported compression algorithms result = compressionAlgorithms.Select(compressionAlgorithm => { var instance = new CompressionTestRunner(); instance.compressionAlgorithmForTest = compressionAlgorithm; instance.compressWriteRequest = true; instance.compressReadRequest = true; instance.readResponseShouldBeCompressed = true; // Use example test data. instance.testData = exampleTestData[compressionAlgorithm]; return(instance); }).ToArray(); } break; case CompressionTestVariant.CompressibleWrite: { // Use common test data for all supported compression algorithms result = compressionAlgorithms.Select(compressionAlgorithm => { var instance = new CompressionTestRunner(); instance.compressionAlgorithmForTest = compressionAlgorithm; instance.compressWriteRequest = true; instance.compressReadRequest = false; instance.readResponseShouldBeCompressed = false; // Use example test data. instance.testData = commonCompressibleData; return(instance); }).ToArray(); } break; case CompressionTestVariant.CompressibleRead: { // Use common test data var instance = new CompressionTestRunner(); instance.compressionAlgorithmForTest = CompressionAlgorithm.NONE; instance.compressWriteRequest = false; instance.compressReadRequest = true; instance.readResponseShouldBeCompressed = true; instance.testData = commonCompressibleData; result = new CompressionTestRunner[] { instance }; } break; case CompressionTestVariant.IncompressibleRead: { // Use common test data var instance = new CompressionTestRunner(); instance.compressionAlgorithmForTest = CompressionAlgorithm.NONE; instance.compressWriteRequest = false; instance.compressReadRequest = true; instance.readResponseShouldBeCompressed = false; instance.testData = commonIncompressibleData; result = new CompressionTestRunner[] { instance }; } break; default: throw new InvalidOperationException("Unknown test variant!"); } return(result); }
private void SMB2CompressionTest(CompressionAlgorithm[] compressionAlgorithms, bool enableEncryption, CompressionTestVariant variant) { uint treeId; FILEID fileId; CreateTestFile(compressionAlgorithms, enableEncryption, out treeId, out fileId); var instances = CompressionTestRunner.Generate(variant); foreach (var instance in instances) { instance.Run(client, treeId, fileId); } client.Close(treeId, fileId); client.TreeDisconnect(treeId); client.LogOff(); }