Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ironlib is able to detect ineffective wipes #135

Merged
Merged
9 changes: 7 additions & 2 deletions utils/watermark_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func ApplyWatermarks(logicalName string) (func() error, error) {
if err != nil {
return nil, err
}

if fileSize == 0 {
return nil, errors.New("No space for watermarking")
}

// Write watermarks on random locations
watermarks, err := writeWatermarks(file, fileSize, numWatermarks)
if err != nil {
Expand All @@ -60,8 +65,8 @@ func ApplyWatermarks(logicalName string) (func() error, error) {
}
// Check if the watermark is still in the disk
if slices.Equal(currentValue, watermark.data) {
ErrorExistingWatermark := errors.New("Error existing watermark in the file")
return fmt.Errorf("%s | %w", logicalName, ErrorExistingWatermark)
ErrorExistingWatermark := errors.New("Error existing watermark in the position: ")
return fmt.Errorf("%s | %w %d", logicalName, ErrorExistingWatermark, watermark.position)
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
Expand Down
87 changes: 87 additions & 0 deletions utils/watermark_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package utils

import (
"crypto/rand"
"os"
"testing"

"gotest.tools/assert"
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
)

func Test_ApplyWatermarks(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer os.Remove(tempFile.Name())

// Close the file since we'll be reopening it in ApplyWatermarks
tempFile.Close()

t.Run("NegativeTest", func(t *testing.T) {
// Create a ~15KB empty file, no room for all watermarks
err := os.WriteFile(tempFile.Name(), make([]byte, 1*1024), 0o600)
if err != nil {
t.Fatalf("Failed to create empty file: %v", err)
}
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved

// Apply watermarks and expect an error
checker, err := ApplyWatermarks(tempFile.Name())

if checker != nil {
err = checker()
if err == nil {
t.Error("Expected error, got none")
}
}
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
assert.ErrorContains(t, err, "invalid argument")
})

t.Run("EmptyFile", func(t *testing.T) {
// Wipe the file
err := os.WriteFile(tempFile.Name(), make([]byte, 0), 0o600)
if err != nil {
t.Fatalf("Failed to truncate file: %v", err)
}
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved

// Apply watermarks and expect no error
checker, err := ApplyWatermarks(tempFile.Name())
assert.Error(t, err, "No space for watermarking")
if checker != nil {
err := checker()
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
}
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved
})
t.Run("PositiveTestWithRandomDataAndWipe", func(t *testing.T) {
// Write the file full of random data
randomData := make([]byte, 15*1024*1024)
_, err := rand.Read(randomData)
if err != nil {
t.Fatalf("Failed to generate random data: %v", err)
}
err = os.WriteFile(tempFile.Name(), randomData, 0o600)
if err != nil {
t.Fatalf("Failed to write random data to file: %v", err)
}

// Apply watermarks and expect no error
checker, err := ApplyWatermarks(tempFile.Name())
if err != nil {
t.Fatalf("Error applying watermarks: %v", err)
}
// simulate wipe
zeroData := make([]byte, 15*1024*1024)
err = os.WriteFile(tempFile.Name(), zeroData, 0o600)
if err != nil {
t.Fatalf("Failed to write zero data to file: %v", err)
}
turegano-equinix marked this conversation as resolved.
Show resolved Hide resolved

err = checker()
if err != nil {
t.Errorf("Expected no error, got: %v", err)
}
})
}
Loading