Lines 1-12
Link Here
|
1 |
package org.apache.poi.xwpf; |
1 |
package org.apache.poi.xwpf; |
2 |
|
2 |
|
|
|
3 |
import static org.junit.Assert.assertEquals; |
4 |
import static org.junit.Assert.assertNotNull; |
5 |
import static org.junit.Assert.assertTrue; |
6 |
|
3 |
import java.io.File; |
7 |
import java.io.File; |
4 |
import java.io.InputStream; |
8 |
import java.io.InputStream; |
5 |
|
9 |
|
6 |
import junit.framework.TestCase; |
10 |
import javax.crypto.Cipher; |
7 |
|
11 |
|
8 |
import org.apache.poi.POIDataSamples; |
12 |
import org.apache.poi.POIDataSamples; |
9 |
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; |
|
|
10 |
import org.apache.poi.openxml4j.opc.OPCPackage; |
13 |
import org.apache.poi.openxml4j.opc.OPCPackage; |
11 |
import org.apache.poi.poifs.crypt.Decryptor; |
14 |
import org.apache.poi.poifs.crypt.Decryptor; |
12 |
import org.apache.poi.poifs.crypt.EncryptionHeader; |
15 |
import org.apache.poi.poifs.crypt.EncryptionHeader; |
Lines 14-88
Link Here
|
14 |
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; |
17 |
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; |
15 |
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; |
18 |
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; |
16 |
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
19 |
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
20 |
import org.junit.Assume; |
21 |
import org.junit.Test; |
17 |
|
22 |
|
18 |
public class TestXWPFBugs extends TestCase { |
23 |
public class TestXWPFBugs { |
19 |
/** |
24 |
/** |
20 |
* A word document that's encrypted with non-standard |
25 |
* A word document that's encrypted with non-standard |
21 |
* Encryption options, and no cspname section. See bug 53475 |
26 |
* Encryption options, and no cspname section. See bug 53475 |
22 |
*/ |
27 |
*/ |
|
|
28 |
@Test |
23 |
public void test53475NoCSPName() throws Exception { |
29 |
public void test53475NoCSPName() throws Exception { |
24 |
try { |
30 |
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx"); |
25 |
Biff8EncryptionKey.setCurrentUserPassword("solrcell"); |
31 |
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true); |
26 |
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx"); |
|
|
27 |
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true); |
28 |
|
32 |
|
29 |
// Check the encryption details |
33 |
// Check the encryption details |
30 |
EncryptionInfo info = new EncryptionInfo(filesystem); |
34 |
EncryptionInfo info = new EncryptionInfo(filesystem); |
31 |
assertEquals(128, info.getHeader().getKeySize()); |
35 |
assertEquals(128, info.getHeader().getKeySize()); |
32 |
assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm()); |
36 |
assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm()); |
33 |
assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm()); |
37 |
assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm()); |
34 |
|
38 |
|
35 |
// Check it can be decoded |
39 |
// Check it can be decoded |
36 |
Decryptor d = Decryptor.getInstance(info); |
40 |
Decryptor d = Decryptor.getInstance(info); |
37 |
assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell")); |
41 |
assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell")); |
38 |
|
42 |
|
39 |
// Check we can read the word document in that |
43 |
// Check we can read the word document in that |
40 |
InputStream dataStream = d.getDataStream(filesystem); |
44 |
InputStream dataStream = d.getDataStream(filesystem); |
41 |
OPCPackage opc = OPCPackage.open(dataStream); |
45 |
OPCPackage opc = OPCPackage.open(dataStream); |
42 |
XWPFDocument doc = new XWPFDocument(opc); |
46 |
XWPFDocument doc = new XWPFDocument(opc); |
43 |
XWPFWordExtractor ex = new XWPFWordExtractor(doc); |
47 |
XWPFWordExtractor ex = new XWPFWordExtractor(doc); |
44 |
String text = ex.getText(); |
48 |
String text = ex.getText(); |
45 |
assertNotNull(text); |
49 |
assertNotNull(text); |
46 |
assertEquals("This is password protected Word document.", text.trim()); |
50 |
assertEquals("This is password protected Word document.", text.trim()); |
47 |
ex.close(); |
51 |
ex.close(); |
48 |
} finally { |
|
|
49 |
Biff8EncryptionKey.setCurrentUserPassword(null); |
50 |
} |
51 |
} |
52 |
} |
52 |
|
53 |
|
53 |
/** |
54 |
/** |
54 |
* A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size), |
55 |
* A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size), |
55 |
* but the key can be 128/192/256 bits |
56 |
* but the key can be 128/192/256 bits |
56 |
*/ |
57 |
*/ |
|
|
58 |
@Test |
57 |
public void test53475_aes256() throws Exception { |
59 |
public void test53475_aes256() throws Exception { |
58 |
try { |
60 |
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES"); |
59 |
Biff8EncryptionKey.setCurrentUserPassword("pass"); |
61 |
Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647); |
60 |
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx"); |
62 |
File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx"); |
61 |
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true); |
63 |
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true); |
62 |
|
64 |
|
63 |
// Check the encryption details |
65 |
// Check the encryption details |
64 |
EncryptionInfo info = new EncryptionInfo(filesystem); |
66 |
EncryptionInfo info = new EncryptionInfo(filesystem); |
65 |
assertEquals(16, info.getHeader().getBlockSize()); |
67 |
assertEquals(16, info.getHeader().getBlockSize()); |
66 |
assertEquals(256, info.getHeader().getKeySize()); |
68 |
assertEquals(256, info.getHeader().getKeySize()); |
67 |
assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm()); |
69 |
assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm()); |
68 |
assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm()); |
70 |
assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm()); |
69 |
|
71 |
|
70 |
// Check it can be decoded |
72 |
// Check it can be decoded |
71 |
Decryptor d = Decryptor.getInstance(info); |
73 |
Decryptor d = Decryptor.getInstance(info); |
72 |
assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass")); |
74 |
assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass")); |
73 |
|
75 |
|
74 |
// Check we can read the word document in that |
76 |
// Check we can read the word document in that |
75 |
InputStream dataStream = d.getDataStream(filesystem); |
77 |
InputStream dataStream = d.getDataStream(filesystem); |
76 |
OPCPackage opc = OPCPackage.open(dataStream); |
78 |
OPCPackage opc = OPCPackage.open(dataStream); |
77 |
XWPFDocument doc = new XWPFDocument(opc); |
79 |
XWPFDocument doc = new XWPFDocument(opc); |
78 |
XWPFWordExtractor ex = new XWPFWordExtractor(doc); |
80 |
XWPFWordExtractor ex = new XWPFWordExtractor(doc); |
79 |
String text = ex.getText(); |
81 |
String text = ex.getText(); |
80 |
assertNotNull(text); |
82 |
assertNotNull(text); |
81 |
// I know ... a stupid typo, maybe next time ... |
83 |
// I know ... a stupid typo, maybe next time ... |
82 |
assertEquals("The is a password protected document.", text.trim()); |
84 |
assertEquals("The is a password protected document.", text.trim()); |
83 |
ex.close(); |
85 |
ex.close(); |
84 |
} finally { |
|
|
85 |
Biff8EncryptionKey.setCurrentUserPassword(null); |
86 |
} |
87 |
} |
86 |
} |
88 |
} |
87 |
} |