Skip to content

Commit

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

* Remove invalidation of whitespace in TryGetNextExtendedAttribute

* Add requested test
  • Loading branch information
stephentoub authored Nov 19, 2022
1 parent 0dc4466 commit 0d7f758
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 @@ -723,12 +723,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 @@ -333,6 +333,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 0d7f758

Please sign in to comment.