generated from ekristen/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add some tests for compare pkg
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package checksum | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestComputeFileHash(t *testing.T) { | ||
// Create a temporary file | ||
tmpFile, err := os.CreateTemp("", "testfile-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
// Write random content to the file | ||
content := []byte("random content for testing") | ||
_, err = tmpFile.Write(content) | ||
assert.NoError(t, err) | ||
tmpFile.Close() | ||
|
||
// Compute the hash of the file | ||
hash, err := ComputeFileHash(tmpFile.Name(), sha256.New) | ||
assert.NoError(t, err) | ||
|
||
// Compute the expected hash | ||
expectedHash := fmt.Sprintf("%x", sha256.Sum256(content)) | ||
|
||
// Assert that the computed hash matches the expected hash | ||
assert.Equal(t, expectedHash, hash) | ||
} | ||
|
||
func TestCompareHashWithChecksumFile(t *testing.T) { | ||
// Create a temporary file | ||
tmpFile, err := os.CreateTemp("", "testfile-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
// Write random content to the file | ||
content := []byte("random content for testing") | ||
_, err = tmpFile.Write(content) | ||
assert.NoError(t, err) | ||
tmpFile.Close() | ||
|
||
// Compute the hash of the file | ||
hash := fmt.Sprintf("%x", sha256.Sum256(content)) | ||
|
||
// Create a temporary checksum file | ||
checksumFile, err := os.CreateTemp("", "checksum-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(checksumFile.Name()) | ||
|
||
// Write the hash and filename to the checksum file | ||
_, err = checksumFile.WriteString(fmt.Sprintf("%s %s\n", hash, tmpFile.Name())) | ||
assert.NoError(t, err) | ||
checksumFile.Close() | ||
|
||
// Compare the hash with the checksum file | ||
match, err := CompareHashWithChecksumFile(tmpFile.Name(), tmpFile.Name(), checksumFile.Name(), sha256.New) | ||
assert.NoError(t, err) | ||
assert.True(t, match) | ||
|
||
// Test with a different hash function | ||
hash = fmt.Sprintf("%x", sha512.Sum512(content)) | ||
checksumFile, err = os.CreateTemp("", "checksum-*.txt") | ||
assert.NoError(t, err) | ||
defer os.Remove(checksumFile.Name()) | ||
|
||
_, err = checksumFile.WriteString(fmt.Sprintf("%s %s\n", hash, tmpFile.Name())) | ||
assert.NoError(t, err) | ||
checksumFile.Close() | ||
|
||
match, err = CompareHashWithChecksumFile(tmpFile.Name(), tmpFile.Name(), checksumFile.Name(), sha512.New) | ||
assert.NoError(t, err) | ||
assert.True(t, match) | ||
} |