aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestXWPFBugs.java
blob: c39c7fe7cd9e85fc3dab77c9c523fd237f7f9d99 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.xwpf;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.crypto.Cipher;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.poifs.crypt.CipherAlgorithm;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.filesystem.Ole10Native;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJcTable;

class TestXWPFBugs {
    private static final POIDataSamples samples = POIDataSamples.getDocumentInstance();

    @Test()
    void truncatedDocx() {
        //started failing after uptake of commons-compress 1.21
        assertThrows(IOException.class, () -> {
            try (InputStream fis = samples.openResourceAsStream("truncated62886.docx");
                 OPCPackage opc = OPCPackage.open(fis)) {
                assertNotNull(opc);
                //XWPFWordExtractor ext = new XWPFWordExtractor(opc)) {
                //assertNotNull(ext.getText());
            }
        });
    }

    /**
     * A Word document that's encrypted with non-standard
     * Encryption options, and no cspname section. See bug 53475
     */
    @Test
    void bug53475NoCSPName() throws Exception {
        File file = samples.getFile("bug53475-password-is-solrcell.docx");
        POIFSFileSystem filesystem = new POIFSFileSystem(file, true);

        // Check the encryption details
        EncryptionInfo info = new EncryptionInfo(filesystem);
        assertEquals(128, info.getHeader().getKeySize());
        assertEquals(CipherAlgorithm.aes128, info.getHeader().getCipherAlgorithm());
        assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithm());

        // Check it can be decoded
        Decryptor d = Decryptor.getInstance(info);
        assertTrue(d.verifyPassword("solrcell"), "Unable to process: document is encrypted");

        // Check we can read the Word document in that
        InputStream dataStream = d.getDataStream(filesystem);
        OPCPackage opc = OPCPackage.open(dataStream);
        XWPFDocument doc = new XWPFDocument(opc);
        XWPFWordExtractor ex = new XWPFWordExtractor(doc);
        String text = ex.getText();
        assertNotNull(text);
        assertEquals("This is password protected Word document.", text.trim());
        ex.close();

        filesystem.close();
    }

    /**
     * A Word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),
     * but the key can be 128/192/256 bits
     */
    @Test
    void bug53475_aes256() throws Exception {
        int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
        assumeTrue(maxKeyLen == 0x7FFFFFFF, "Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256");

        File file = samples.getFile("bug53475-password-is-pass.docx");
        POIFSFileSystem filesystem = new POIFSFileSystem(file, true);

        // Check the encryption details
        EncryptionInfo info = new EncryptionInfo(filesystem);
        assertEquals(16, info.getHeader().getBlockSize());
        assertEquals(256, info.getHeader().getKeySize());
        assertEquals(CipherAlgorithm.aes256, info.getHeader().getCipherAlgorithm());
        assertEquals(HashAlgorithm.sha1, info.getHeader().getHashAlgorithm());

        // Check it can be decoded
        Decryptor d = Decryptor.getInstance(info);
        assertTrue(d.verifyPassword("pass"), "Unable to process: document is encrypted");

        // Check we can read the Word document in that
        InputStream dataStream = d.getDataStream(filesystem);
        OPCPackage opc = OPCPackage.open(dataStream);
        XWPFDocument doc = new XWPFDocument(opc);
        XWPFWordExtractor ex = new XWPFWordExtractor(doc);
        String text = ex.getText();
        assertNotNull(text);
        // I know ... a stupid typo, maybe next time ...
        assertEquals("The is a password protected document.", text.trim());
        ex.close();

        filesystem.close();
    }


    @Test
    void bug59058() throws IOException, XmlException {
        String[] files = {"bug57031.docx", "bug59058.docx"};
        for (String f : files) {
            ZipFile zf = new ZipFile(samples.getFile(f));
            ZipArchiveEntry entry = zf.getEntry("word/document.xml");
            DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry));
            assertNotNull(document);
            zf.close();
        }
    }

    @Test
    void missingXsbs() throws IOException, XmlException {
        String[] files = {"bib-chernigovka.netdo.ru_download_docs_17459.docx"};
        for (String f : files) {
            ZipFile zf = new ZipFile(samples.getFile(f));
            ZipArchiveEntry entry = zf.getEntry("word/document.xml");
            DocumentDocument document = DocumentDocument.Factory.parse(zf.getInputStream(entry));
            assertNotNull(document);
            zf.close();
        }
    }

    @Test
    void bug65649() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug65649.docx"))) {
            assertEquals(731, document.getParagraphs().size());
        }
    }

    @Test
    void tika3388() throws Exception {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("tika-3388.docx"))) {
            assertEquals(1, document.getParagraphs().size());
            PackagePartName partName = PackagingURIHelper.createPartName("/word/embeddings/oleObject1.bin");
            PackagePart part = document.getPackage().getPart(partName);
            assertNotNull(part);
            try (
                InputStream partStream = part.getInputStream();
                POIFSFileSystem poifs = new POIFSFileSystem(partStream)
            ) {
                Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(poifs);
                String fn = "C:\\Users\\ross\\AppData\\Local\\Microsoft\\Windows\\INetCache\\Content.Word\\約翰的測試文件\uD83D\uDD96.msg";
                assertEquals(fn, ole.getFileName());
                assertEquals(fn, ole.getFileName2());
            }
        }
    }

    @Test
    void insertParagraphDirectlyIntoBody() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug66312.docx"))) {
            XWPFParagraph paragraph = document.getParagraphArray(0);
            insertParagraph(paragraph, document);
            assertEquals("Hello", document.getParagraphArray(0).getText());
            assertEquals("World", document.getParagraphArray(1).getText());
        }
    }

    @Test
    void insertTableDirectlyIntoBody() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug66312.docx"))) {
            XWPFParagraph paragraph = document.getParagraphArray(0);
            insertTable(paragraph, document);
            assertEquals("Hello", document.getTableArray(0).getRow(0).getCell(0).getText());
            assertEquals("World", document.getParagraphArray(0).getText());
        }
    }

    @Test
    void insertParagraphIntoTable() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug66312.docx"))) {
            XWPFTableCell cell = document.getTableArray(0).getRow(0).getCell(0);
            XWPFParagraph paragraph = cell.getParagraphArray(0);
            insertParagraph(paragraph, document);
            assertEquals("Hello", cell.getParagraphArray(0).getText());
            assertEquals("World", cell.getParagraphArray(1).getText());
        }
    }

    @Test
    void insertTableIntoTable() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug66312.docx"))) {
            XWPFTableCell cell = document.getTableArray(0).getRow(0).getCell(0);
            XWPFParagraph paragraph = cell.getParagraphArray(0);
            insertTable(paragraph, document);
            assertEquals("Hello", cell.getTableArray(0).getRow(0).getCell(0).getText());
            assertEquals("World", cell.getParagraphArray(0).getText());
        }
    }


    public static void insertParagraph(XWPFParagraph xwpfParagraph, XWPFDocument document) {
        XmlCursor xmlCursor = xwpfParagraph.getCTP().newCursor();
        XWPFParagraph xwpfParagraph2 = document.insertNewParagraph(xmlCursor);
        xwpfParagraph2.createRun().setText("Hello");
    }

    public static void insertTable(XWPFParagraph xwpfParagraph, XWPFDocument document) {
        XmlCursor xmlCursor = xwpfParagraph.getCTP().newCursor();
        XWPFTable xwpfTable = document.insertNewTbl(xmlCursor);
        xwpfTable.getRow(0).getCell(0).setText("Hello");
    }

    @Test
    void correctParagraphAlignment() throws IOException {
        try (XWPFDocument document = new XWPFDocument(samples.openResourceAsStream("bug-paragraph-alignment.docx"))) {
            XWPFParagraph centeredParagraph = document.getParagraphArray(0);
            assertFalse(centeredParagraph.isAlignmentSet());
            assertEquals(ParagraphAlignment.LEFT, centeredParagraph.getAlignment()); // LEFT is a fallback value here.

            XWPFParagraph leftParagraph = document.getParagraphArray(1);
            assertTrue(leftParagraph.isAlignmentSet());
            assertEquals(ParagraphAlignment.LEFT, leftParagraph.getAlignment()); // LEFT is the real alignment value.
        }
    }

    @Test
    public void testTableRightAlign() throws Exception {
        // Document contains all possible values for table alignment, including null.
        try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table-alignment.docx")) {
            XWPFTable tbl0 = doc.getTableArray(0);
            assertNull(tbl0.getTableAlignment());
            assertFalse(tbl0.getCTTbl().getTblPr().isSetJc());
            XWPFTable tbl1 = doc.getTableArray(1);
            assertEquals(TableRowAlign.LEFT, tbl1.getTableAlignment());
            assertEquals(STJcTable.LEFT, tbl1.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
            XWPFTable tbl2 = doc.getTableArray(2);
            assertEquals(TableRowAlign.START, tbl2.getTableAlignment());
            assertEquals(STJcTable.START, tbl2.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
            XWPFTable tbl3 = doc.getTableArray(3);
            assertEquals(TableRowAlign.CENTER, tbl3.getTableAlignment());
            assertEquals(STJcTable.CENTER, tbl3.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
            XWPFTable tbl4 = doc.getTableArray(4);
            assertEquals(TableRowAlign.RIGHT, tbl4.getTableAlignment());
            assertEquals(STJcTable.RIGHT, tbl4.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
            XWPFTable tbl5 = doc.getTableArray(5);
            assertEquals(TableRowAlign.END, tbl5.getTableAlignment());
            assertEquals(STJcTable.END, tbl5.getCTTbl().getTblPr().getJc().xgetVal().getEnumValue());
        }
    }
}