|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System.IO; |
| 5 | +using System.Linq; |
5 | 6 | using Xunit; |
6 | 7 |
|
7 | 8 | namespace System.Formats.Tar.Tests |
@@ -144,6 +145,74 @@ public void Verify_Checksum_SymbolicLink_LongLink(TarEntryFormat format) => |
144 | 145 | public void Verify_Checksum_SymbolicLink_LongLink_LongPath(TarEntryFormat format) => |
145 | 146 | Verify_Checksum_Internal(format, TarEntryType.SymbolicLink, longPath: true, longLink: true); |
146 | 147 |
|
| 148 | + [Fact] |
| 149 | + public void Verify_Size_RegularFile_Empty() |
| 150 | + { |
| 151 | + using MemoryStream archiveStream = new(); |
| 152 | + string entryName = "entry.txt"; |
| 153 | + using (TarWriter archive = new(archiveStream, TarEntryFormat.V7, leaveOpen: true)) |
| 154 | + { |
| 155 | + V7TarEntry e = new(TarEntryType.V7RegularFile, entryName) |
| 156 | + { |
| 157 | + DataStream = new MemoryStream(0) |
| 158 | + }; |
| 159 | + archive.WriteEntry(e); |
| 160 | + } |
| 161 | + |
| 162 | + int sizeLocation = 100 + // Name |
| 163 | + 8 + // Mode |
| 164 | + 8 + // Uid |
| 165 | + 8; // Gid |
| 166 | + int sizeLength = 12; |
| 167 | + |
| 168 | + archiveStream.Position = 0; |
| 169 | + byte[] actual = archiveStream.GetBuffer()[sizeLocation..(sizeLocation + sizeLength)]; |
| 170 | + |
| 171 | + byte[] expected = [0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0]; |
| 172 | + AssertExtensions.SequenceEqual(expected, actual); |
| 173 | + |
| 174 | + archiveStream.Position = 0; |
| 175 | + using TarReader reader = new(archiveStream); |
| 176 | + |
| 177 | + TarEntry? actualEntry = reader.GetNextEntry(); |
| 178 | + Assert.NotNull(actualEntry); |
| 179 | + Assert.Equal(0, actualEntry.Length); |
| 180 | + Assert.Null(actualEntry.DataStream); // No stream created when size field's value is 0 |
| 181 | + } |
| 182 | + |
| 183 | + [Fact] |
| 184 | + public void Verify_Compatibility_RegularFile_EmptyFile_NoSizeStored() |
| 185 | + { |
| 186 | + using MemoryStream archiveStream = new(); |
| 187 | + string entryName = "entry.txt"; |
| 188 | + using (TarWriter archive = new(archiveStream, TarEntryFormat.V7, leaveOpen: true)) |
| 189 | + { |
| 190 | + V7TarEntry e = new(TarEntryType.V7RegularFile, entryName) |
| 191 | + { |
| 192 | + DataStream = new MemoryStream(0) |
| 193 | + }; |
| 194 | + archive.WriteEntry(e); |
| 195 | + } |
| 196 | + |
| 197 | + int sizeLocation = 100 + // Name |
| 198 | + 8 + // Mode |
| 199 | + 8 + // Uid |
| 200 | + 8; // Gid |
| 201 | + |
| 202 | + // Fill the size field with 12 zeros as we used to before the bug fix |
| 203 | + byte[] replacement = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 204 | + archiveStream.Seek(sizeLocation, SeekOrigin.Begin); |
| 205 | + archiveStream.Write(replacement); |
| 206 | + |
| 207 | + archiveStream.Position = 0; |
| 208 | + using TarReader reader = new(archiveStream); |
| 209 | + |
| 210 | + TarEntry? actualEntry = reader.GetNextEntry(); // Should succeed to read the entry with a malformed size field value |
| 211 | + Assert.NotNull(actualEntry); |
| 212 | + Assert.Equal(0, actualEntry.Length); // Should succeed to detect the size field's value as zero |
| 213 | + Assert.Null(actualEntry.DataStream); // No stream created when size field's value is 0 |
| 214 | + } |
| 215 | + |
147 | 216 | private void Verify_Checksum_Internal(TarEntryFormat format, TarEntryType entryType, bool longPath, bool longLink) |
148 | 217 | { |
149 | 218 | using MemoryStream archive = new MemoryStream(); |
|
0 commit comments