github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/fuzz_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/unidoc/unidoc/pdf/core"
     7  )
     8  
     9  func init() {
    10  	// Uncomment when debugging to get trace logging output - to follow flow.
    11  	// common.SetLogger(common.NewConsoleLogger(common.LogLevelTrace))
    12  }
    13  
    14  // Test for an endless recursive loop in
    15  // func (this *PdfReader) buildPageList(node *PdfIndirectObject, parent *PdfIndirectObject) error
    16  func TestFuzzReaderBuildPageLoop(t *testing.T) {
    17  	/*
    18  		The problem is when there are Pages entries pointing forward and backward (illegal), causing endless
    19  		recursive looping.
    20  
    21  		Example problem data:
    22  			3 0 obj
    23  			<< /Type /Pages /MediaBox [0 0 595 842] /Count 2 /Kids [ 2 0 R 12 0 R ] >>
    24  			endobj
    25  
    26  
    27  			2 0 obj
    28  			<< /Type /Pages
    29  			   /Kids [3 0 R]
    30  			   /Count 1
    31  			   /MediaBox [0 0 300 144]
    32  			>>
    33  			endobj
    34  
    35  			12 0 obj
    36  			<<
    37  				/Type /Page
    38  				/Parent 3 0 R
    39  				/Resources 15 0 R
    40  				/Contents 13 0 R
    41  				/MediaBox [0 0 595 842]
    42  			>>
    43  			endobj
    44  	*/
    45  
    46  	pageDict := core.MakeDict()
    47  	pageDict.Set("Type", core.MakeName("Pages"))
    48  	page := core.MakeIndirectObject(pageDict)
    49  
    50  	pagesDict := core.MakeDict()
    51  	pages := core.MakeIndirectObject(pagesDict)
    52  	pagesDict.Set("Type", core.MakeName("Pages"))
    53  	pagesDict.Set("Kids", core.MakeArray(page))
    54  
    55  	pageDict.Set("Kids", core.MakeArray(pages))
    56  
    57  	// Make a dummy reader to test
    58  	dummyPdfReader := PdfReader{}
    59  	dummyPdfReader.traversed = map[core.PdfObject]bool{}
    60  	dummyPdfReader.modelManager = NewModelManager()
    61  
    62  	traversedPageNodes := map[core.PdfObject]bool{}
    63  	err := dummyPdfReader.buildPageList(pages, nil, traversedPageNodes)
    64  
    65  	// Current behavior is to avoid the recursive endless loop and simply return nil.  Logs a debug message.
    66  
    67  	if err != nil {
    68  		t.Errorf("Fail: %v", err)
    69  	}
    70  
    71  }