github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/core/crypt_file_test.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  // Integration tests for the PDF crypt support.
     7  
     8  package core_test
     9  
    10  import (
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	pdfcontent "github.com/unidoc/unidoc/pdf/contentstream"
    16  	pdf "github.com/unidoc/unidoc/pdf/model"
    17  )
    18  
    19  const aes3Dir = `./testdata`
    20  
    21  func TestDecryptAES3(t *testing.T) {
    22  	cases := []struct {
    23  		file  string
    24  		pass  string
    25  		R     int
    26  		pages int
    27  		page1 string
    28  	}{
    29  		// See https://github.com/mozilla/pdf.js/issues/6010
    30  		{
    31  			file: "issue6010_1.pdf", pass: "abc", R: 6, pages: 1,
    32  			page1: "\nIssue 6010",
    33  		},
    34  		{
    35  			file: "issue6010_2.pdf", pass: "æøå", R: 6, pages: 10,
    36  			page1: "\nSample PDF Document\nRobert Maron\nGrzegorz Grudzi\n\xb4\nnski\nFebruary 20, 1999",
    37  		},
    38  		// See https://github.com/mozilla/pdf.js/pull/6531
    39  		{
    40  			file: "pr6531_1.pdf", pass: "asdfasdf", R: 6, pages: 1,
    41  		},
    42  		{
    43  			file: "pr6531_2.pdf", pass: "asdfasdf", R: 6, pages: 1,
    44  		},
    45  		// See https://github.com/sumatrapdfreader/sumatrapdf/issues/294
    46  		{
    47  			file: "testcase_encry.pdf", pass: "123", R: 5, pages: 1, // owner pass
    48  			page1: "\n\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\a\x00\b\n\x00\x01\n\x00\t\x00\n\x00\v",
    49  		},
    50  		{
    51  			file: "testcase_encry.pdf", pass: "456", R: 5, pages: 1, // user pass
    52  			page1: "\n\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\a\x00\b\n\x00\x01\n\x00\t\x00\n\x00\v",
    53  		},
    54  		{
    55  			file: "x300.pdf", R: 5, pages: 1,
    56  			pass:  "rnofajrcudiaplhafbqrkrafphehjlvctmwftvpzvachsulmfkjltliftbfpgabustkjfybeqvwgdfawyghoijxgwuxkkrywybpapsswxcnigwwnpttgvfxtrlnbqzberhrnelvcqjaasothqhtzjoxqttlqrmxfqawyhizoslazxhdqffiweruqjrmpdsxutvevceaormydxhregsadphblbaziucrnsbntzptdzfkzfzlwmxhslywusuajwspvabqwopbxdttwbjappgiaxrkgmsuodkzhbqvqiwummcdu",
    57  			page1: " \nTemplate form for pdf_form_add.go\t \nThis PDF is explicitly created as a template\t \tfor adding\t \ta PDF interactive form to.\t \n \nFull \tName: _________________________________________\t \nAddress\t \tLine 1\t: \t__________________\t________________\t____\t \nAddress\t \tLine \t2\t: ________________\t_______\t___________\t____\t \nAge: ______\t \nGender: \t  \t[ ] Male    [ ] Female\t \nCity: ______________\t \nCountry: ______________\t \nFavorite Color:\t \t \t___________________\t \n \n \n ",
    58  		},
    59  	}
    60  	for _, c := range cases {
    61  		c := c
    62  		t.Run(c.file, func(t *testing.T) {
    63  			f, err := os.Open(filepath.Join(aes3Dir, c.file))
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  			defer f.Close()
    68  
    69  			p, err := pdf.NewPdfReader(f)
    70  			if err != nil {
    71  				t.Fatal(err)
    72  			}
    73  			if ok, err := p.IsEncrypted(); err != nil {
    74  				t.Fatal(err)
    75  			} else if !ok {
    76  				t.Fatal("document is not encrypted")
    77  			}
    78  			ok, err := p.Decrypt([]byte(c.pass))
    79  			if err != nil {
    80  				t.Fatal(err)
    81  			} else if !ok {
    82  				t.Fatal("wrong password")
    83  			}
    84  
    85  			numPages, err := p.GetNumPages()
    86  			if err != nil {
    87  				t.Fatal(err)
    88  			} else if numPages != c.pages {
    89  				t.Errorf("wrong number of pages: %d", numPages)
    90  			}
    91  
    92  			page, err := p.GetPage(1)
    93  			if err != nil {
    94  				t.Fatal(err)
    95  			}
    96  
    97  			streams, err := page.GetContentStreams()
    98  			if err != nil {
    99  				t.Fatal(err)
   100  			}
   101  
   102  			content := ""
   103  			for _, cstream := range streams {
   104  				content += cstream
   105  			}
   106  
   107  			cstreamParser := pdfcontent.NewContentStreamParser(content)
   108  			txt, err := cstreamParser.ExtractText()
   109  			if err != nil {
   110  				t.Fatal(err)
   111  			} else if txt != c.page1 {
   112  				t.Fatalf("wrong text: %q", txt)
   113  			}
   114  		})
   115  	}
   116  }