Skip to content

Commit 1763088

Browse files
committed
Add more unit test for crypt, improve test test coverage
1 parent d421697 commit 1763088

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

crypt_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package excelize
1313

1414
import (
1515
"bytes"
16+
"encoding/base64"
1617
"encoding/binary"
1718
"os"
1819
"path/filepath"
@@ -94,6 +95,101 @@ func TestEncrypt(t *testing.T) {
9495
compoundFile.put("EncryptedPackage", []byte{})
9596
_, err = OpenReader(bytes.NewReader(compoundFile.write()))
9697
assert.Equal(t, ErrWorkbookFileFormat, err)
98+
// Test createIV when iv length is less than block size
99+
_, err = createIV(0, Encryption{
100+
KeyData: KeyData{
101+
HashAlgorithm: "md5",
102+
BlockSize: 32,
103+
SaltValue: base64.StdEncoding.EncodeToString([]byte("")),
104+
},
105+
})
106+
assert.NoError(t, err)
107+
// Test decryptPackage error with padding
108+
input := make([]byte, 18)
109+
binary.LittleEndian.PutUint64(input[:8], 10)
110+
for i := 8; i < 18; i++ {
111+
input[i] = byte(i)
112+
}
113+
_, err = decryptPackage(make([]byte, 32), input, Encryption{
114+
KeyData: KeyData{
115+
HashAlgorithm: "sha256",
116+
BlockSize: 16,
117+
SaltValue: base64.StdEncoding.EncodeToString([]byte("")),
118+
},
119+
})
120+
assert.NoError(t, err)
121+
// Test IV creation error with invalid salt
122+
input = make([]byte, 4104)
123+
binary.LittleEndian.PutUint64(input[:8], 4096)
124+
_, err = decryptPackage(make([]byte, 32), input, Encryption{
125+
KeyData: KeyData{
126+
HashAlgorithm: "sha256",
127+
BlockSize: 16,
128+
SaltValue: "==",
129+
},
130+
})
131+
assert.Error(t, err)
132+
// Test decrypt error with invalid key
133+
_, err = decryptPackage([]byte{}, input, Encryption{
134+
KeyData: KeyData{
135+
HashAlgorithm: "sha256",
136+
BlockSize: 16,
137+
SaltValue: base64.StdEncoding.EncodeToString([]byte("")),
138+
},
139+
})
140+
assert.Error(t, err)
141+
// Test put with path that is a prefix of name
142+
compoundFile = &cfb{
143+
paths: []string{"Root Entry/"},
144+
sectors: []sector{{name: "Root Entry", typeID: 5}},
145+
}
146+
compoundFile.put("Root Entry/Test", []byte(""))
147+
compoundFile = &cfb{
148+
paths: []string{"Root"},
149+
sectors: []sector{{name: "Root", typeID: 5}},
150+
}
151+
compoundFile.put("Test", []byte(""))
152+
// Test compare function with different scenarios
153+
compoundFile = &cfb{}
154+
assert.Equal(t, -1, compoundFile.compare("Root/A", "Root/B"))
155+
assert.Equal(t, 1, compoundFile.compare("Root/B", "Root/A"))
156+
assert.Equal(t, -1, compoundFile.compare("Root", "Root/Child"))
157+
// Test prepare with typeID == 0 sector
158+
compoundFile = &cfb{
159+
paths: []string{"Root Entry/", "Skip/", "Valid/"},
160+
sectors: []sector{
161+
{name: "Root Entry", typeID: 5},
162+
{name: "Skip", typeID: 0},
163+
{name: "Valid", typeID: 2},
164+
},
165+
}
166+
compoundFile.prepare()
167+
// Test locate with FATSectors incrementing but staying <= 109
168+
compoundFile = &cfb{
169+
paths: []string{"Root Entry/"},
170+
sectors: []sector{{name: "Root Entry", typeID: 5, content: []byte{}}},
171+
}
172+
numFiles := 1533
173+
for i := range numFiles {
174+
name := strings.Repeat("F", i%50+1)
175+
compoundFile.paths = append(compoundFile.paths, name+"/")
176+
compoundFile.sectors = append(compoundFile.sectors, sector{
177+
name: name,
178+
typeID: 2,
179+
content: make([]byte, 4096),
180+
})
181+
}
182+
compoundFile.locate()
183+
// Test writeDirectoryEntry with empty clsID
184+
compoundFile = &cfb{
185+
paths: []string{"Root Entry/", "File1/"},
186+
sectors: []sector{
187+
{name: "Root Entry", typeID: 5, content: []byte{}, clsID: headerCLSID},
188+
{name: "File1", typeID: 2, content: []byte("test"), clsID: []byte{}},
189+
},
190+
}
191+
compoundFile.stream = make([]byte, 10000)
192+
compoundFile.writeDirectoryEntry([]int{1, 0, 1, 0, 1, 0, 0, 0})
97193
}
98194

99195
func TestEncryptionMechanism(t *testing.T) {

0 commit comments

Comments
 (0)