Ensure that the ThreadLocal-Biff-Password is reset correctly after each usage, we had some unit tests failing sporadically due to this

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1704710 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-09-22 19:50:37 +00:00
parent ef8cf4bdcc
commit 5e6b975b1c
8 changed files with 39 additions and 5 deletions

View File

@ -55,8 +55,6 @@ public final class RecordFactoryInputStream {
private final Record _lastRecord; private final Record _lastRecord;
private final boolean _hasBOFRecord; private final boolean _hasBOFRecord;
private static POILogger log = POILogFactory.getLogger(StreamEncryptionInfo.class);
public StreamEncryptionInfo(RecordInputStream rs, List<Record> outputRecs) { public StreamEncryptionInfo(RecordInputStream rs, List<Record> outputRecs) {
Record rec; Record rec;
rs.nextRecord(); rs.nextRecord();

View File

@ -121,9 +121,11 @@ public class WorkbookFactory {
if (password != null) { if (password != null) {
Biff8EncryptionKey.setCurrentUserPassword(password); Biff8EncryptionKey.setCurrentUserPassword(password);
} }
Workbook wb = new HSSFWorkbook(root, true); try {
return new HSSFWorkbook(root, true);
} finally {
Biff8EncryptionKey.setCurrentUserPassword(null); Biff8EncryptionKey.setCurrentUserPassword(null);
return wb; }
} }
/** /**

View File

@ -47,6 +47,7 @@ import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader; import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptionHeader;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -57,6 +58,7 @@ public class TestDocumentEncryption {
POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
@Before @Before
@After // also afterwards to not affect other tests running in the same JVM
public void resetPassword() { public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null); Biff8EncryptionKey.setCurrentUserPassword(null);
} }

View File

@ -36,6 +36,7 @@ import org.apache.poi.hssf.record.SelectionRecord;
import org.apache.poi.hssf.record.WindowTwoRecord; import org.apache.poi.hssf.record.WindowTwoRecord;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.After;
/** /**
* Testing for {@link HSSFEventFactory} * Testing for {@link HSSFEventFactory}
@ -45,6 +46,12 @@ public final class TestHSSFEventFactory extends TestCase {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName); return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
} }
// to not affect other tests running in the same JVM
@After
public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
public void testWithMissingRecords() throws Exception { public void testWithMissingRecords() throws Exception {
HSSFRequest req = new HSSFRequest(); HSSFRequest req = new HSSFRequest();

View File

@ -32,12 +32,18 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
/** /**
* *
*/ */
public final class TestExcelExtractor { public final class TestExcelExtractor {
// to not affect other tests running in the same JVM
@After
public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
@SuppressWarnings("resource") @SuppressWarnings("resource")
private static ExcelExtractor createExtractor(String sampleFileName) throws IOException { private static ExcelExtractor createExtractor(String sampleFileName) throws IOException {

View File

@ -25,6 +25,7 @@ import java.io.ByteArrayInputStream;
import org.apache.poi.EncryptedDocumentException; import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.util.HexRead; import org.apache.poi.util.HexRead;
import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
@ -35,6 +36,11 @@ import org.junit.rules.ExpectedException;
* @author Josh Micich * @author Josh Micich
*/ */
public final class TestRecordFactoryInputStream { public final class TestRecordFactoryInputStream {
// to not affect other tests running in the same JVM
@After
public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
/** /**
* Hex dump of a BOF record and most of a FILEPASS record. * Hex dump of a BOF record and most of a FILEPASS record.

View File

@ -27,12 +27,19 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.HexRead; import org.apache.poi.util.HexRead;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
public class TestXorEncryption { public class TestXorEncryption {
private static HSSFTestDataSamples samples = new HSSFTestDataSamples(); private static HSSFTestDataSamples samples = new HSSFTestDataSamples();
// to not affect other tests running in the same JVM
@After
public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
@Test @Test
public void testXorEncryption() throws Exception { public void testXorEncryption() throws Exception {
// Xor-Password: abc // Xor-Password: abc

View File

@ -74,6 +74,7 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.junit.After;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -85,6 +86,11 @@ import org.junit.Test;
* define the test in the base class {@link BaseTestBugzillaIssues}</b> * define the test in the base class {@link BaseTestBugzillaIssues}</b>
*/ */
public final class TestBugs extends BaseTestBugzillaIssues { public final class TestBugs extends BaseTestBugzillaIssues {
// to not affect other tests running in the same JVM
@After
public void resetPassword() {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
public TestBugs() { public TestBugs() {
super(HSSFITestDataProvider.instance); super(HSSFITestDataProvider.instance);