github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/pack/pack_test.go (about)

     1  // Copyright 2020 Readium Foundation. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license
     3  // that can be found in the LICENSE file exposed on Github (readium) in the project repository.
     4  
     5  package pack
     6  
     7  import (
     8  	"archive/zip"
     9  	"bytes"
    10  	"compress/flate"
    11  	"io/ioutil"
    12  	"testing"
    13  
    14  	"github.com/readium/readium-lcp-server/crypto"
    15  	"github.com/readium/readium-lcp-server/epub"
    16  	"github.com/readium/readium-lcp-server/xmlenc"
    17  )
    18  
    19  func TestPacking(t *testing.T) {
    20  	z, err := zip.OpenReader("../test/samples/sample.epub")
    21  	if err != nil {
    22  		t.Error(err)
    23  		t.FailNow()
    24  	}
    25  
    26  	input, _ := epub.Read(&z.Reader)
    27  
    28  	// keep a raw html file for future use
    29  	htmlFilePath := "OPS/chapter_001.xhtml"
    30  	inputRes, ok := FindFile(htmlFilePath, input)
    31  	if !ok {
    32  		t.Fatalf("Could not find %s in input", htmlFilePath)
    33  	}
    34  	inputBytes, err := ioutil.ReadAll(inputRes.Contents)
    35  	if err != nil {
    36  		t.Fatalf("Could not find %s in input", htmlFilePath)
    37  	}
    38  
    39  	inputRes.Contents = bytes.NewReader(inputBytes)
    40  
    41  	buf := new(bytes.Buffer)
    42  	encrypter := crypto.NewAESEncrypter_PUBLICATION_RESOURCES()
    43  	encryption, key, err := Do(encrypter, "", input, buf)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	if encryption == nil {
    48  		t.Fatal("Expected an xmlenc file")
    49  	}
    50  
    51  	if len(encryption.Data) == 0 {
    52  		t.Error("Expected some encrypted data")
    53  	}
    54  
    55  	if key == nil {
    56  		t.Error("expected a key")
    57  	}
    58  
    59  	for _, item := range encryption.Data {
    60  		if !input.CanEncrypt(string(item.CipherData.CipherReference.URI)) {
    61  			t.Errorf("Should not have encrypted %s\n", item.CipherData.CipherReference.URI)
    62  		}
    63  	}
    64  
    65  	zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	output, _ := epub.Read(zr)
    70  
    71  	if res, ok := FindFile("OPS/images/Moby-Dick_FE_title_page.jpg", output); !ok {
    72  		t.Errorf("Could not find image")
    73  	} else {
    74  		if res.Compressed {
    75  			t.Errorf("Did not expect image to be compressed")
    76  		}
    77  	}
    78  
    79  	//fmt.Printf("%#v\n", output.Encryption)
    80  	var encryptionData xmlenc.Data
    81  	for _, data := range output.Encryption.Data {
    82  		if data.CipherData.CipherReference.URI == xmlenc.URI(htmlFilePath) {
    83  			encryptionData = data
    84  			break
    85  		}
    86  	}
    87  
    88  	if l := encryptionData.Properties.Properties[0].Compression.OriginalLength; l != 13877 {
    89  		t.Errorf("Expected %s to have an original length of %d, got %d", htmlFilePath, 13877, l)
    90  	}
    91  
    92  	if res, ok := FindFile(htmlFilePath, output); !ok {
    93  		t.Errorf("Could not find html file")
    94  	} else {
    95  		if !res.Compressed {
    96  			t.Errorf("Expected html to be compressed")
    97  		}
    98  
    99  		var buf bytes.Buffer
   100  		if decrypter, ok := encrypter.(crypto.Decrypter); !ok {
   101  			t.Errorf("Could not decrypt file")
   102  		} else {
   103  			decrypter.Decrypt(key, res.Contents, &buf)
   104  			if outputBytes, err := ioutil.ReadAll(flate.NewReader(&buf)); err != nil {
   105  				t.Fatalf("Could not decompress data from %s", htmlFilePath)
   106  			} else {
   107  				if !bytes.Equal(inputBytes, outputBytes) {
   108  					t.Errorf("Expected the files to be equal before and after")
   109  				}
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  func TestPackingWithSpace(t *testing.T) {
   116  	z, err := zip.OpenReader("../test/samples/sample-with-space.epub")
   117  	if err != nil {
   118  		t.Error(err)
   119  		t.FailNow()
   120  	}
   121  
   122  	input, _ := epub.Read(&z.Reader)
   123  
   124  	// keep a raw html file for future use
   125  	htmlEncodedFilePath := "OPS/chapter%20136.xhtml"
   126  	fontFilePath := "OPS/fonts/MinionPro Regular.otf"
   127  
   128  	_, alreadyEncrypted := input.Encryption.DataForFile(fontFilePath)
   129  	if alreadyEncrypted == false {
   130  		t.Errorf("Cannot find already encrypted item %s in encryption.xml file", fontFilePath)
   131  	}
   132  
   133  	buf := new(bytes.Buffer)
   134  	encrypter := crypto.NewAESEncrypter_PUBLICATION_RESOURCES()
   135  	encryption, key, err := Do(encrypter, "", input, buf)
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	if encryption == nil {
   140  		t.Fatal("Expected an xmlenc file")
   141  	}
   142  
   143  	if len(encryption.Data) == 0 {
   144  		t.Error("Expected some encrypted data")
   145  	}
   146  
   147  	if key == nil {
   148  		t.Error("expected a key")
   149  	}
   150  
   151  	var foundEncodedSpace = false
   152  	for _, item := range encryption.Data {
   153  		if item.CipherData.CipherReference.URI == xmlenc.URI(htmlEncodedFilePath) {
   154  			foundEncodedSpace = true
   155  			break
   156  		}
   157  	}
   158  	if foundEncodedSpace == false {
   159  		t.Errorf("Cannot find %s encoded file name in encryption.xml file", htmlEncodedFilePath)
   160  	}
   161  
   162  }