Merged in feature/memory (pull request #155)

Memory Scripts

* tests

* cleanermemtest

* files

* tests
This commit is contained in:
Michael McGuinness
2025-05-26 09:24:38 +00:00
parent c2d06fc5a7
commit 007c32a58d
16 changed files with 925 additions and 689 deletions
+27 -679
View File
@@ -3,6 +3,8 @@ package documenttypes
import (
"context"
"math"
"os"
"path/filepath"
"strings"
"testing"
@@ -211,60 +213,32 @@ func TestIsCorrupt(t *testing.T) {
reason := pdf.IsCorrupt(ctx)
assert.Equal(t, InvalidDocumentLargeDimensions, *reason)
})
t.Run("version 1.0", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_0))
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.1", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_1))
dirPath := "../../../assets/original"
entries, err := os.ReadDir(dirPath)
require.NoError(t, err)
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.2", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_2))
for _, entry := range entries {
if entry.IsDir() {
continue
}
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.3", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_3))
fileName := entry.Name()
if strings.ToLower(filepath.Ext(fileName)) != ".pdf" {
continue
}
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.4", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_4))
t.Run(fileName, func(t *testing.T) {
fullPath := filepath.Join(dirPath, fileName)
file, err := os.Open(fullPath)
require.NoError(t, err)
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.5", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_5))
pdf := NewPDF(file)
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.6", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_6))
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 1.7", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_7))
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
t.Run("version 2.0", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF2_0))
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
reason := pdf.IsCorrupt(ctx)
assert.Nil(t, reason)
})
}
}
func TestNewPDF(t *testing.T) {
@@ -283,643 +257,17 @@ func TestNewPDF(t *testing.T) {
func TestPDFGetPage(t *testing.T) {
ctx := context.Background()
t.Run("hello world", func(t *testing.T) {
pdf := NewPDF(strings.NewReader(PDF1_7))
file, err := os.Open("../../../assets/original/version1_7.pdf")
require.NoError(t, err)
pdf := NewPDF(file)
pageZero, err := pdf.GetPage(ctx, 0)
assert.NoError(t, err)
assert.Contains(t, string(pageZero), "PDF 1.7 Example")
})
}
const PDF1_0 = `%PDF-1.0
%¥±ë % Binary comment for PDF parsers
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.0 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 6
0000000000 65535 f
0000000018 00000 n
0000000072 00000 n
0000000134 00000 n
0000000309 00000 n
0000000411 00000 n
trailer
<< /Size 6
/Root 1 0 R
>>
startxref
485
%%EOF`
const PDF1_1 = `%PDF-1.1
%âãÏÓ
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.1 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 6
0000000000 65535 f
0000000018 00000 n
0000000072 00000 n
0000000134 00000 n
0000000309 00000 n
0000000411 00000 n
trailer
<< /Size 6
/Root 1 0 R
>>
startxref
485
%%EOF`
const PDF1_2 = `%PDF-1.2
%¥±ë
1 0 obj
<</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj
<</Type/Pages/Kids[3 0 R]/Count 1>>
endobj
3 0 obj
<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 4 0 R>>>>/MediaBox[0 0 612 792]/Contents 5 0 R>>
endobj
4 0 obj
<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>
endobj
5 0 obj
<</Length 44>>
stream
BT
/F1 24 Tf
100 700 Td
(hello version 1.2) Tj
ET
endstream
endobj
xref
0 6
0000000000 65535 f
0000000018 00000 n
0000000063 00000 n
0000000114 00000 n
0000000223 00000 n
0000000285 00000 n
trailer
<</Size 6/Root 1 0 R>>
startxref
377
%%EOF`
const PDF1_3 = `%PDF-1.3
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.3 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 6
0000000000 65535 f
0000000009 00000 n
0000000063 00000 n
0000000125 00000 n
0000000300 00000 n
0000000402 00000 n
trailer
<< /Size 6
/Root 1 0 R
>>
startxref
476
%%EOF`
const PDF1_4 = `%PDF-1.4
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
/ExtGState << /GS1 6 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 78 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.4 Example with Transparency) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
6 0 obj
<< /Type /ExtGState
/ca 0.5
/CA 0.5
>>
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000063 00000 n
0000000125 00000 n
0000000342 00000 n
0000000470 00000 n
0000000544 00000 n
trailer
<< /Size 7
/Root 1 0 R
>>
startxref
603
%%EOF`
const PDF1_5 = `%PDF-1.5
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
/Version /1.5
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.5 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
6 0 obj
<< /Type /ObjStm
/N 1
/First 4
/Length 19
>>
stream
7 0 << /Im 8 0 R >>
endstream
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000075 00000 n
0000000137 00000 n
0000000312 00000 n
0000000414 00000 n
0000000488 00000 n
trailer
<< /Size 7
/Root 1 0 R
>>
startxref
585
%%EOF`
const PDF1_6 = `%PDF-1.6
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
/Version /1.6
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
/Annots [6 0 R]
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.6 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
6 0 obj
<< /Type /Annot
/Subtype /3D
/Rect [100 100 400 500]
/3DD 7 0 R
/AP << /N 8 0 R >>
>>
endobj
7 0 obj
<< /Type /3DStream
/Subtype /U3D
/Length 14
>>
stream
Placeholder3D
endstream
endobj
8 0 obj
<< /Type /XObject
/Subtype /Form
/BBox [0 0 300 400]
/Length 25
>>
stream
q
1 0 0 1 0 0 cm
/Fm1 Do
Q
endstream
endobj
xref
0 9
0000000000 65535 f
0000000009 00000 n
0000000075 00000 n
0000000137 00000 n
0000000332 00000 n
0000000434 00000 n
0000000508 00000 n
0000000621 00000 n
0000000708 00000 n
trailer
<< /Size 9
/Root 1 0 R
>>
startxref
835
%%EOF`
const PDF1_7 = `%PDF-1.7
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
/Version /1.7
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
/Annots [6 0 R]
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 1.7 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
6 0 obj
<< /Type /Annot
/Subtype /RichMedia
/Rect [100 100 400 500]
/RichMediaContent 7 0 R
>>
endobj
7 0 obj
<< /Type /RichMediaContent
/Assets 8 0 R
/Configurations [9 0 R]
>>
endobj
8 0 obj
<< /Names [(asset1) 10 0 R]
>>
endobj
9 0 obj
<< /Type /RichMediaConfiguration
/Instances [<< /Asset (asset1) >>]
>>
endobj
10 0 obj
<< /Type /Filespec
/F (media.mp4)
/EF << /F 11 0 R >>
>>
endobj
11 0 obj
<< /Length 22 >>
stream
PlaceholderMediaContent
endstream
endobj
xref
0 12
0000000000 65535 f
0000000009 00000 n
0000000075 00000 n
0000000137 00000 n
0000000332 00000 n
0000000434 00000 n
0000000508 00000 n
0000000612 00000 n
0000000696 00000 n
0000000741 00000 n
0000000826 00000 n
0000000901 00000 n
trailer
<< /Size 12
/Root 1 0 R
>>
startxref
975
%%EOF`
const PDF2_0 = `%PDF-2.0
%âãÏÓ
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/MediaBox [0 0 612 792]
/Contents 4 0 R
/Resources << /ProcSet [/PDF /Text]
/Font << /F1 5 0 R >>
>>
>>
endobj
4 0 obj
<< /Length 52 >>
stream
BT
/F1 24 Tf
100 700 Td
(PDF 2.0 Example) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
xref
0 6
0000000000 65535 f
0000000018 00000 n
0000000072 00000 n
0000000134 00000 n
0000000309 00000 n
0000000411 00000 n
trailer
<< /Size 6
/Root 1 0 R
>>
startxref
485
%%EOF`
const pdfTwoPages = `%PDF-1.4
%âãÏÓ
1 0 obj