2017-11-09 19:14:09 +00:00
|
|
|
|
// ReSharper disable ObjectCreationAsStatement
|
2018-01-10 19:49:32 +00:00
|
|
|
|
namespace UglyToad.PdfPig.Tests.Parser.Parts
|
2017-11-09 19:14:09 +00:00
|
|
|
|
{
|
|
|
|
|
using System;
|
2018-01-21 19:34:21 +00:00
|
|
|
|
using PdfPig.IO;
|
2018-01-10 19:49:32 +00:00
|
|
|
|
using PdfPig.Parser.Parts;
|
|
|
|
|
using PdfPig.Util;
|
2017-11-09 19:14:09 +00:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
public class BruteForceSearcherTests
|
|
|
|
|
{
|
|
|
|
|
private const string TestData = @"%PDF-1.5
|
|
|
|
|
%¿÷¢þ
|
2018-01-21 18:08:00 +00:00
|
|
|
|
2 17 obj
|
2017-11-09 19:14:09 +00:00
|
|
|
|
<< /Linearized 1 /L 26082 /H [ 722 130 ] /O 6 /E 25807 /N 1 /T 25806 >>
|
|
|
|
|
endobj
|
|
|
|
|
|
|
|
|
|
3 0 obj
|
|
|
|
|
<< /Type /XRef /Length 58 /Filter /FlateDecode /DecodeParms << /Columns 4 /Predictor 12 >> /W [ 1 2 1 ] /Index [ 2 20 ] /Info 13 0 R /Root 4 0 R /Size 22 /Prev 25807 /ID [<2ee88f3ee8a59b4041754ecb5960c518><2ee88f3ee8a59b4041754ecb5960c518>] >>
|
|
|
|
|
stream
|
|
|
|
|
xœcbdàg`b`8 $˜N€XF@‚± DÜÌå@ÂÞ$›$$¦ƒXêLŒó~30A
|
|
|
|
|
endstream
|
|
|
|
|
endobj
|
|
|
|
|
4 0 obj
|
|
|
|
|
<< /Pages 14 0 R /Type /Catalog >>
|
|
|
|
|
endobj
|
|
|
|
|
5 0 obj
|
|
|
|
|
<< /Filter /FlateDecode /S 36 /Length 53 >>
|
|
|
|
|
stream
|
|
|
|
|
xœc```g``ºÄ
|
|
|
|
|
endstream
|
|
|
|
|
endobj
|
|
|
|
|
|
|
|
|
|
startxref
|
|
|
|
|
216
|
|
|
|
|
%%EOF";
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2018-01-21 18:08:00 +00:00
|
|
|
|
public void ReaderNull_Throws()
|
2017-11-09 19:14:09 +00:00
|
|
|
|
{
|
2018-01-21 18:08:00 +00:00
|
|
|
|
Action action = () => new BruteForceSearcher(null);
|
2017-11-09 19:14:09 +00:00
|
|
|
|
|
2018-01-21 18:08:00 +00:00
|
|
|
|
Assert.Throws<ArgumentNullException>(action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SearcherFindsCorrectObjects()
|
|
|
|
|
{
|
|
|
|
|
var input = new ByteArrayInputBytes(OtherEncodings.StringAsLatin1Bytes(TestData));
|
2017-11-09 19:14:09 +00:00
|
|
|
|
|
2018-01-21 18:08:00 +00:00
|
|
|
|
var searcher = new BruteForceSearcher(input);
|
2017-11-09 19:14:09 +00:00
|
|
|
|
|
|
|
|
|
var locations = searcher.GetObjectLocations();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(4, locations.Count);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(locations.Values, new long[]
|
|
|
|
|
{
|
2018-01-21 18:08:00 +00:00
|
|
|
|
TestData.IndexOf("2 17 obj", StringComparison.OrdinalIgnoreCase) + 1,
|
|
|
|
|
TestData.IndexOf("3 0 obj", StringComparison.OrdinalIgnoreCase) + 1,
|
|
|
|
|
TestData.IndexOf("4 0 obj", StringComparison.OrdinalIgnoreCase) + 1,
|
|
|
|
|
TestData.IndexOf("5 0 obj", StringComparison.OrdinalIgnoreCase) + 1
|
2017-11-09 19:14:09 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ReaderOnlyCallsOnce()
|
|
|
|
|
{
|
2018-01-21 18:08:00 +00:00
|
|
|
|
var reader = StringBytesTestConverter.Convert(TestData, false);
|
2017-11-09 19:14:09 +00:00
|
|
|
|
|
2018-01-21 18:08:00 +00:00
|
|
|
|
var searcher = new BruteForceSearcher(reader.Bytes);
|
2017-11-09 19:14:09 +00:00
|
|
|
|
|
|
|
|
|
var locations = searcher.GetObjectLocations();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(4, locations.Count);
|
2018-01-21 18:08:00 +00:00
|
|
|
|
|
2017-11-09 19:14:09 +00:00
|
|
|
|
var newLocations = searcher.GetObjectLocations();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(4, locations.Count);
|
|
|
|
|
|
|
|
|
|
foreach (var keyValuePair in locations)
|
|
|
|
|
{
|
2018-01-14 14:48:54 +00:00
|
|
|
|
Assert.Contains(newLocations.Keys, x => x.Equals(keyValuePair.Key));
|
2017-11-09 19:14:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|