Skip to content

Commit

Permalink
Remove invalidation of whitespace in TryGetNextExtendedAttribute (dot…
Browse files Browse the repository at this point in the history
…net#78465)

* Remove invalidation of whitespace in TryGetNextExtendedAttribute

* Add requested test
  • Loading branch information
stephentoub authored and carlossanlop committed Nov 23, 2022
1 parent de84cf9 commit 622f6a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,6 @@ private static bool TryGetNextExtendedAttribute(
}
line = line.Slice(spacePos + 1).TrimStart((byte)' ');

// If there are any more spaces, it's malformed.
if (line.IndexOf((byte)' ') >= 0)
{
return false;
}

// Find the equal separator.
int equalPos = line.IndexOf((byte)'=');
if (equalPos < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,27 @@ public void ExtractGlobalExtendedAttributesEntry_Throws()
Assert.Throws<InvalidOperationException>(() => entry.ExtractToFile(Path.Join(root.Path, "file"), overwrite: true));
}
}

[Theory]
[InlineData("key", "value")]
[InlineData("key ", " value ")]
[InlineData(" key ", " value ")]
[InlineData("many sla/s\\hes", "/////////////\\\\\\///////////")]
public void GlobalExtendedAttribute_Roundtrips(string key, string value)
{
var stream = new MemoryStream();
using (var writer = new TarWriter(stream, leaveOpen: true))
{
writer.WriteEntry(new PaxGlobalExtendedAttributesTarEntry(new Dictionary<string, string>() { { key, value } }));
}

stream.Position = 0;
using (var reader = new TarReader(stream))
{
PaxGlobalExtendedAttributesTarEntry entry = Assert.IsType<PaxGlobalExtendedAttributesTarEntry>(reader.GetNextEntry());
Assert.Equal(1, entry.GlobalExtendedAttributes.Count);
Assert.Equal(KeyValuePair.Create(key.TrimStart(), value), entry.GlobalExtendedAttributes.First());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,28 @@ public void PaxSizeLargerThanMaxAllowedByStream()
Assert.Throws<ArgumentOutOfRangeException>(() => reader.GetNextEntry());
}

[Theory]
[InlineData("key", "value")]
[InlineData("key ", " value ")]
[InlineData(" key ", " value ")]
[InlineData("many sla/s\\hes", "/////////////\\\\\\///////////")]
public void PaxExtendedAttribute_Roundtrips(string key, string value)
{
var stream = new MemoryStream();
using (var writer = new TarWriter(stream, leaveOpen: true))
{
writer.WriteEntry(new PaxTarEntry(TarEntryType.Directory, "entryName", new Dictionary<string, string>() { { key, value } }));
}

stream.Position = 0;
using (var reader = new TarReader(stream))
{
PaxTarEntry entry = Assert.IsType<PaxTarEntry>(reader.GetNextEntry());
Assert.Equal(5, entry.ExtendedAttributes.Count);
Assert.Contains(KeyValuePair.Create(key.TrimStart(), value), entry.ExtendedAttributes);
}
}

private static void VerifyDataStreamOfTarUncompressedInternal(string testFolderName, string testCaseName, bool copyData)
{
using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, testFolderName, testCaseName);
Expand Down

0 comments on commit 622f6a0

Please sign in to comment.