-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pdfsignature_test.go
82 lines (70 loc) · 3.18 KB
/
pdfsignature_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sign
import (
"fmt"
"os"
"testing"
"time"
"github.com/digitorus/pdf"
)
var signatureTests = []struct {
file string
expectedSignatures map[CertType]string
}{
{
file: "../testfiles/testfile20.pdf",
expectedSignatures: map[CertType]string{
CertificationSignature: "<<\n /Type /Sig\n /Filter /Adobe.PPKLite\n /SubFilter /adbe.pkcs7.detached\n /Prop_Build <<\n /App << /Name /Digitorus#20PDFSign >>\n >>\n /ByteRange[0 ********** ********** **********] /Contents<>\n /Reference [\n << /Type /SigRef\n /TransformMethod /DocMDP\n /TransformParams <<\n /Type /TransformParams\n /P 2 /V /1.2\n >>\n >> ] /Name (John Doe)\n /Location (Somewhere)\n /Reason (Test)\n /ContactInfo (None)\n /M (D:20170923143900+03'00')\n>>\n",
UsageRightsSignature: "<<\n /Type /Sig\n /Filter /Adobe.PPKLite\n /SubFilter /adbe.pkcs7.detached\n /Prop_Build <<\n /App << /Name /Digitorus#20PDFSign >>\n >>\n /ByteRange[0 ********** ********** **********] /Contents<>\n /Reference [\n << /Type /SigRef\n /TransformMethod /UR3\n /TransformParams <<\n /Type /TransformParams\n /V /2.2\n >>\n >> ] /Name (John Doe)\n /Location (Somewhere)\n /Reason (Test)\n /ContactInfo (None)\n /M (D:20170923143900+03'00')\n>>\n",
ApprovalSignature: "<<\n /Type /Sig\n /Filter /Adobe.PPKLite\n /SubFilter /adbe.pkcs7.detached\n /Prop_Build <<\n /App << /Name /Digitorus#20PDFSign >>\n >>\n /ByteRange[0 ********** ********** **********] /Contents<>\n /TransformMethod /FieldMDP\n /TransformParams <<\n /Type /TransformParams\n /Action /All\n /V /1.2\n >>\n /Name (John Doe)\n /Location (Somewhere)\n /Reason (Test)\n /ContactInfo (None)\n /M (D:20170923143900+03'00')\n>>\n",
},
},
}
func TestCreateSignaturePlaceholder(t *testing.T) {
for _, testFile := range signatureTests {
for certType, expectedSignature := range testFile.expectedSignatures {
t.Run(fmt.Sprintf("%s_certType-%d", testFile.file, certType), func(st *testing.T) {
inputFile, err := os.Open(testFile.file)
if err != nil {
st.Errorf("Failed to load test PDF")
return
}
finfo, err := inputFile.Stat()
if err != nil {
st.Errorf("Failed to load test PDF")
return
}
size := finfo.Size()
rdr, err := pdf.NewReader(inputFile, size)
if err != nil {
st.Errorf("Failed to load test PDF")
return
}
timezone, _ := time.LoadLocation("Europe/Tallinn")
now := time.Date(2017, 9, 23, 14, 39, 0, 0, timezone)
sign_data := SignData{
Signature: SignDataSignature{
Info: SignDataSignatureInfo{
Name: "John Doe",
Location: "Somewhere",
Reason: "Test",
ContactInfo: "None",
Date: now,
},
CertType: certType,
DocMDPPerm: AllowFillingExistingFormFieldsAndSignaturesPerms,
},
}
sign_data.objectId = uint32(rdr.XrefInformation.ItemCount) + 3
context := SignContext{
PDFReader: rdr,
InputFile: inputFile,
SignData: sign_data,
}
signature := context.createSignaturePlaceholder()
if string(signature) != expectedSignature {
st.Errorf("Signature mismatch, expected:\n%q\nbut got:\n%q", expectedSignature, signature)
}
})
}
}
}