Back to Repositories

Testing Excel Encryption Implementation in alibaba/easyexcel

A comprehensive test suite that validates Excel file encryption capabilities using Apache POI library integration with EasyExcel. The tests focus on both modern OOXML and legacy BIFF8 encryption mechanisms.

Test Coverage Overview

The test suite covers critical encryption functionality for Excel files using Apache POI.

Key areas tested include:
  • Agile encryption for XLSX files with password protection
  • BIFF8 encryption handling for legacy XLS files
  • File system operations with encrypted Excel documents
  • Password-based access control verification

Implementation Analysis

The testing approach utilizes JUnit 5 framework to validate encryption workflows in isolation. The implementation leverages POI’s encryption APIs including POIFSFileSystem, EncryptionInfo, and Biff8EncryptionKey for comprehensive security testing.

Test patterns focus on proper resource handling with try-with-resources and explicit cleanup of file system resources.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Apache POI for Excel manipulation
  • Custom TestFileUtil for file path management
  • SLF4J for logging
  • Support for both XLSX (POI OOXML) and XLS (POI HSSF) formats

Best Practices Demonstrated

The test suite exemplifies robust testing practices for secure document handling.

Notable practices include:
  • Proper resource cleanup and management
  • Isolation of encryption test cases
  • Comprehensive coverage of encryption modes
  • Clear test method naming and organization
  • Appropriate logging implementation

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/Poi3Test.java

            
package com.alibaba.easyexcel.test.temp.poi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.alibaba.easyexcel.test.util.TestFileUtil;

import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试poi
 *
 * @author Jiaju Zhuang
 **/

public class Poi3Test {
    private static final Logger LOGGER = LoggerFactory.getLogger(Poi3Test.class);

    @Test
    public void Encryption() throws Exception {
        String file = TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx";
        POIFSFileSystem fs = new POIFSFileSystem();
        EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
        Encryptor enc = info.getEncryptor();
        enc.confirmPassword("foobaa");
        OPCPackage opc = OPCPackage.open(new File(file), PackageAccess.READ_WRITE);
        OutputStream os = enc.getDataStream(fs);
        opc.save(os);
        opc.close();

        // Write out the encrypted version
        FileOutputStream fos = new FileOutputStream("D:\\test\\99999999999.xlsx");
        fs.writeFilesystem(fos);
        fos.close();
        fs.close();

    }

    @Test
    public void Encryption2() throws Exception {
        Biff8EncryptionKey.setCurrentUserPassword("123456");
        POIFSFileSystem fs = new POIFSFileSystem(new File("d:/test/simple03.xls"), true);
        HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
        Biff8EncryptionKey.setCurrentUserPassword(null);
        System.out.println(hwb.getSheetAt(0).getSheetName());

    }
}