public void BlockFactorHandling() { const int MinimumBlockFactor = 1; const int MaximumBlockFactor = 64; const int FillFactor = 2; for ( int factor = MinimumBlockFactor; factor < MaximumBlockFactor; ++factor) { MemoryStream ms = new MemoryStream(); using ( TarOutputStream tarOut = new TarOutputStream(ms, factor) ) { TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); entry.Size = (TarBuffer.BlockSize * factor * FillFactor); tarOut.PutNextEntry(entry); byte[] buffer = new byte[TarBuffer.BlockSize]; Random r = new Random(); r.NextBytes(buffer); // Last block is a partial one for ( int i = 0; i < factor * FillFactor; ++i) { tarOut.Write(buffer, 0, buffer.Length); } } byte[] tarData = ms.ToArray(); Assert.IsNotNull(tarData, "Data written is null"); // Blocks = Header + Data Blocks + Zero block + Record trailer int usedBlocks = 1 + (factor * FillFactor) + 1; int totalBlocks = usedBlocks + (factor - 1); totalBlocks /= factor; totalBlocks *= factor; Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, "Tar file should contain {0} blocks in length", totalBlocks); if ( usedBlocks < totalBlocks ) { // Start at first byte after header. int byteIndex = TarBuffer.BlockSize * ((factor * FillFactor)+ 1); while ( byteIndex < tarData.Length ) { int blockNumber = byteIndex / TarBuffer.BlockSize; int offset = blockNumber % TarBuffer.BlockSize; Assert.AreEqual(0, tarData[byteIndex], string.Format("Trailing block data should be null iteration {0} block {1} offset {2} index {3}", factor, blockNumber, offset, byteIndex)); byteIndex += 1; } } } }
public void TrailerContainsNulls() { const int TestBlockFactor = 3; for ( int iteration = 0; iteration < TestBlockFactor * 2; ++iteration) { MemoryStream ms = new MemoryStream(); using ( TarOutputStream tarOut = new TarOutputStream(ms, TestBlockFactor) ) { TarEntry entry = TarEntry.CreateTarEntry("TestEntry"); if ( iteration > 0 ) { entry.Size = (TarBuffer.BlockSize * (iteration - 1)) + 9; } tarOut.PutNextEntry(entry); byte[] buffer = new byte[TarBuffer.BlockSize]; Random r = new Random(); r.NextBytes(buffer); if ( iteration > 0 ) { for ( int i = 0; i < iteration - 1; ++i ) { tarOut.Write(buffer, 0, buffer.Length); } // Last block is a partial one for ( int i = 1; i < 10; ++i) { tarOut.WriteByte((byte)i); } } } byte[] tarData = ms.ToArray(); Assert.IsNotNull(tarData, "Data written is null"); // Blocks = Header + Data Blocks + Zero block + Record trailer int usedBlocks = 1 + iteration + 1; int totalBlocks = usedBlocks + (TestBlockFactor - 1); totalBlocks /= TestBlockFactor; totalBlocks *= TestBlockFactor; Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length, string.Format("Tar file should be {0} blocks in length", totalBlocks)); if ( usedBlocks < totalBlocks ) { // Start at first byte after header. int byteIndex = TarBuffer.BlockSize * (iteration + 1); while ( byteIndex < tarData.Length ) { int blockNumber = byteIndex / TarBuffer.BlockSize; int offset = blockNumber % TarBuffer.BlockSize; Assert.AreEqual(0, tarData[byteIndex], string.Format("Trailing block data should be null iteration {0} block {1} offset {2} index {3}", iteration, blockNumber, offset, byteIndex)); byteIndex += 1; } } } }