diff options
Diffstat (limited to 'excelize_test.go')
-rw-r--r-- | excelize_test.go | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/excelize_test.go b/excelize_test.go index 9aaaae9..0edfe11 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -1160,13 +1160,44 @@ func TestHSL(t *testing.T) { func TestProtectSheet(t *testing.T) { f := NewFile() - assert.NoError(t, f.ProtectSheet("Sheet1", nil)) - assert.NoError(t, f.ProtectSheet("Sheet1", &FormatSheetProtection{ + sheetName := f.GetSheetName(0) + assert.NoError(t, f.ProtectSheet(sheetName, nil)) + // Test protect worksheet with XOR hash algorithm + assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{ Password: "password", EditScenarios: false, })) - + ws, err := f.workSheetReader(sheetName) + assert.NoError(t, err) + assert.Equal(t, "83AF", ws.SheetProtection.Password) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestProtectSheet.xlsx"))) + // Test protect worksheet with SHA-512 hash algorithm + assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{ + AlgorithmName: "SHA-512", + Password: "password", + })) + ws, err = f.workSheetReader(sheetName) + assert.NoError(t, err) + assert.Equal(t, 24, len(ws.SheetProtection.SaltValue)) + assert.Equal(t, 88, len(ws.SheetProtection.HashValue)) + assert.Equal(t, int(sheetProtectionSpinCount), ws.SheetProtection.SpinCount) + // Test remove sheet protection with an incorrect password + assert.EqualError(t, f.UnprotectSheet(sheetName, "wrongPassword"), ErrUnprotectSheetPassword.Error()) + // Test remove sheet protection with password verification + assert.NoError(t, f.UnprotectSheet(sheetName, "password")) + // Test protect worksheet with empty password + assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{})) + assert.Equal(t, "", ws.SheetProtection.Password) + // Test protect worksheet with password exceeds the limit length + assert.EqualError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{ + AlgorithmName: "MD4", + Password: strings.Repeat("s", MaxFieldLength+1), + }), ErrPasswordLengthInvalid.Error()) + // Test protect worksheet with unsupported hash algorithm + assert.EqualError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{ + AlgorithmName: "RIPEMD-160", + Password: "password", + }), ErrUnsupportedHashAlgorithm.Error()) // Test protect not exists worksheet. assert.EqualError(t, f.ProtectSheet("SheetN", nil), "sheet SheetN is not exist") } @@ -1176,12 +1207,30 @@ func TestUnprotectSheet(t *testing.T) { if !assert.NoError(t, err) { t.FailNow() } - // Test unprotect not exists worksheet. + // Test remove protection on not exists worksheet. assert.EqualError(t, f.UnprotectSheet("SheetN"), "sheet SheetN is not exist") assert.NoError(t, f.UnprotectSheet("Sheet1")) + assert.EqualError(t, f.UnprotectSheet("Sheet1", "password"), ErrUnprotectSheet.Error()) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnprotectSheet.xlsx"))) assert.NoError(t, f.Close()) + + f = NewFile() + sheetName := f.GetSheetName(0) + assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{Password: "password"})) + // Test remove sheet protection with an incorrect password + assert.EqualError(t, f.UnprotectSheet(sheetName, "wrongPassword"), ErrUnprotectSheetPassword.Error()) + // Test remove sheet protection with password verification + assert.NoError(t, f.UnprotectSheet(sheetName, "password")) + // Test with invalid salt value + assert.NoError(t, f.ProtectSheet(sheetName, &FormatSheetProtection{ + AlgorithmName: "SHA-512", + Password: "password", + })) + ws, err := f.workSheetReader(sheetName) + assert.NoError(t, err) + ws.SheetProtection.SaltValue = "YWJjZA=====" + assert.EqualError(t, f.UnprotectSheet(sheetName, "wrongPassword"), "illegal base64 data at input byte 8") } func TestSetDefaultTimeStyle(t *testing.T) { |