|
3 | 3 |
|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.IO;
|
| 6 | +using System.Linq; |
6 | 7 | using Xunit;
|
7 | 8 |
|
8 | 9 | namespace System.Formats.Tar.Tests
|
@@ -299,5 +300,57 @@ public void WriteTimestampsBeyondOctalLimit(TarEntryFormat format)
|
299 | 300 | }
|
300 | 301 | }
|
301 | 302 | }
|
| 303 | + |
| 304 | + [Theory] |
| 305 | + [InlineData(TarEntryFormat.V7)] |
| 306 | + // [InlineData(TarEntryFormat.Ustar)] https://github.com/dotnet/runtime/issues/75360 |
| 307 | + [InlineData(TarEntryFormat.Pax)] |
| 308 | + [InlineData(TarEntryFormat.Gnu)] |
| 309 | + public void WriteLongName(TarEntryFormat format) |
| 310 | + { |
| 311 | + var r = new Random(); |
| 312 | + foreach (int length in new[] { 99, 100, 101, 199, 200, 201, 254, 255, 256 }) |
| 313 | + { |
| 314 | + string name = string.Concat(Enumerable.Range(0, length).Select(_ => (char)('a' + r.Next(26)))); |
| 315 | + WriteLongNameCore(format, name); |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + private void WriteLongNameCore(TarEntryFormat format, string maxPathComponent) |
| 320 | + { |
| 321 | + TarEntry entry; |
| 322 | + MemoryStream ms = new(); |
| 323 | + using (TarWriter writer = new(ms, true)) |
| 324 | + { |
| 325 | + TarEntryType entryType = format == TarEntryFormat.V7 ? TarEntryType.V7RegularFile : TarEntryType.RegularFile; |
| 326 | + entry = InvokeTarEntryCreationConstructor(format, entryType, maxPathComponent); |
| 327 | + writer.WriteEntry(entry); |
| 328 | + |
| 329 | + entry = InvokeTarEntryCreationConstructor(format, entryType, Path.Join(maxPathComponent, maxPathComponent)); |
| 330 | + writer.WriteEntry(entry); |
| 331 | + } |
| 332 | + |
| 333 | + ms.Position = 0; |
| 334 | + using TarReader reader = new(ms); |
| 335 | + |
| 336 | + entry = reader.GetNextEntry(); |
| 337 | + string expectedName = GetExpectedNameForFormat(format, maxPathComponent); |
| 338 | + Assert.Equal(expectedName, entry.Name); |
| 339 | + |
| 340 | + entry = reader.GetNextEntry(); |
| 341 | + expectedName = GetExpectedNameForFormat(format, Path.Join(maxPathComponent, maxPathComponent)); |
| 342 | + Assert.Equal(expectedName, entry.Name); |
| 343 | + |
| 344 | + Assert.Null(reader.GetNextEntry()); |
| 345 | + |
| 346 | + string GetExpectedNameForFormat(TarEntryFormat format, string expectedName) |
| 347 | + { |
| 348 | + if (format is TarEntryFormat.V7 && expectedName.Length > 100) // V7 truncates names at 100 characters. |
| 349 | + { |
| 350 | + return expectedName.Substring(0, 100); |
| 351 | + } |
| 352 | + return expectedName; |
| 353 | + } |
| 354 | + } |
302 | 355 | }
|
303 | 356 | }
|
0 commit comments