From d4f8217f815ca7a7773bb6c867541afe8afe45c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Tue, 23 Apr 2024 16:17:03 +0200 Subject: [PATCH 1/6] ironlib is able to detect ineffective wipes using watermarks --- actions/storage_controller.go | 20 ++++++- examples/diskwipe/main.go | 2 +- utils/watermark_disk.go | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 utils/watermark_disk.go diff --git a/actions/storage_controller.go b/actions/storage_controller.go index a2b1db02..040b5c78 100644 --- a/actions/storage_controller.go +++ b/actions/storage_controller.go @@ -13,7 +13,10 @@ import ( "github.com/sirupsen/logrus" ) -var ErrVirtualDiskManagerUtilNotIdentified = errors.New("virtual disk management utility not identifed") +var ( + ErrVirtualDiskManagerUtilNotIdentified = errors.New("virtual disk management utility not identifed") + ErrWipeVerificationNotSatisfactory = errors.New("ineffective wipe") +) type StorageControllerAction struct { Logger *logrus.Logger @@ -100,6 +103,19 @@ func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName stri if err != nil { return err } + // Watermark disk + check, err := utils.PrepWatermarks(logicalName) + if err != nil { + return err + } - return util.WipeDisk(ctx, logicalName) + err = util.WipeDisk(ctx, logicalName) + if err != nil { + return err + } + + if check() { + return nil + } + return errors.Wrap(ErrWipeVerificationNotSatisfactory, "Watermark still present, disk not wipped") } diff --git a/examples/diskwipe/main.go b/examples/diskwipe/main.go index be2b4116..b15c5097 100644 --- a/examples/diskwipe/main.go +++ b/examples/diskwipe/main.go @@ -22,5 +22,5 @@ func main() { if err != nil { logger.Fatal(err) } - fmt.Println("Wiped") + fmt.Println("Wiped successfully!") } diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go new file mode 100644 index 00000000..3162b5a0 --- /dev/null +++ b/utils/watermark_disk.go @@ -0,0 +1,105 @@ +package utils + +import ( + "crypto/rand" + "io" + "log" + "math/big" + "os" + "slices" +) + +const ( + bufferSize = 512 + numWatermarks = 10 +) + +type watermark struct { + position uint64 + data []byte +} + +func PrepWatermarks(disk string) (func() bool, error) { + log.Printf("%s | Initiating watermarking process", disk) + watermarks := make([]watermark, numWatermarks) + + // Write open + file, err := os.OpenFile(disk, os.O_WRONLY, 0) + if err != nil { + log.Printf("%s | Failed to open disk, err: %s", disk, err) + return nil, err + } + defer file.Close() + + // Get disk or partition size + fileSize, err := file.Seek(0, io.SeekEnd) + if err != nil { + log.Printf("%s | Failed to get size, err: %s", disk, err) + return nil, err + } + // Write watermarks on random locations + err = writeWatermarks(file, fileSize, watermarks) + if err != nil { + log.Printf("%s | Failed to write watermarks, err: %s", disk, err) + return nil, err + } + + checker := func() bool { + log.Printf("%s | Checking if the watermark has been removed", disk) + + file, err := os.OpenFile(disk, os.O_RDONLY, 0) + if err != nil { + log.Printf("%s | Failed to open disk for reading: %s", disk, err) + return false + } + defer file.Close() + + for _, watermark := range watermarks { + _, err = file.Seek(int64(watermark.position), io.SeekStart) + if err != nil { + log.Printf("%s | Error moving rw pointer: %s", disk, err) + return false + } + // Read the watermark written to the position + currentValue := make([]byte, bufferSize) + _, err = file.Read(currentValue) + if err != nil { + log.Printf("%s | Error reading from file: %s", disk, err) + return false + } + // Check if the watermark is still in the disk + if slices.Equal(currentValue, watermark.data) { + log.Printf("%s | Error existing watermark in the file", disk) + return false + } + } + log.Printf("%s | Watermarks has been removed", disk) + return true + } + return checker, nil +} + +func writeWatermarks(file *os.File, fileSize int64, watermarks []watermark) error { + for i := 0; i < numWatermarks; i++ { + data := make([]byte, bufferSize) + _, err := rand.Read(data) + if err != nil { + log.Println("error:", err) + return err + } + randomPosition, err := rand.Int(rand.Reader, big.NewInt(fileSize)) + if err != nil { + log.Println(err) + return err + } + + _, err = file.Seek(int64(randomPosition.Uint64()), io.SeekStart) + if err != nil { + log.Println("Error moving rw pointer:", err) + return err + } + watermarks[i].position = randomPosition.Uint64() + watermarks[i].data = data + } + return nil +} From b222b884ef15c49dc8a8d8b4afacd92755026336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Thu, 25 Apr 2024 13:15:00 +0200 Subject: [PATCH 2/6] Watermarks scattered evenly --- actions/storage_controller.go | 21 +++---- utils/watermark_disk.go | 101 ++++++++++++++++++---------------- 2 files changed, 64 insertions(+), 58 deletions(-) diff --git a/actions/storage_controller.go b/actions/storage_controller.go index 040b5c78..c7bfebf7 100644 --- a/actions/storage_controller.go +++ b/actions/storage_controller.go @@ -13,10 +13,7 @@ import ( "github.com/sirupsen/logrus" ) -var ( - ErrVirtualDiskManagerUtilNotIdentified = errors.New("virtual disk management utility not identifed") - ErrWipeVerificationNotSatisfactory = errors.New("ineffective wipe") -) +var ErrVirtualDiskManagerUtilNotIdentified = errors.New("virtual disk management utility not identifed") type StorageControllerAction struct { Logger *logrus.Logger @@ -104,18 +101,18 @@ func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName stri return err } // Watermark disk - check, err := utils.PrepWatermarks(logicalName) - if err != nil { - return err - } + log.Printf("%s | Initiating watermarking process", logicalName) + check := utils.ApplyWatermarks(logicalName) err = util.WipeDisk(ctx, logicalName) if err != nil { return err } - - if check() { - return nil + log.Printf("%s | Checking if the watermark has been removed", logicalName) + err = check() + if err != nil { + return err } - return errors.Wrap(ErrWipeVerificationNotSatisfactory, "Watermark still present, disk not wipped") + log.Printf("%s | Watermarks has been removed", logicalName) + return nil } diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go index 3162b5a0..97ca10be 100644 --- a/utils/watermark_disk.go +++ b/utils/watermark_disk.go @@ -2,11 +2,13 @@ package utils import ( "crypto/rand" + "fmt" "io" - "log" "math/big" "os" "slices" + + "github.com/pkg/errors" ) const ( @@ -15,91 +17,98 @@ const ( ) type watermark struct { - position uint64 + position int64 data []byte } -func PrepWatermarks(disk string) (func() bool, error) { - log.Printf("%s | Initiating watermarking process", disk) - watermarks := make([]watermark, numWatermarks) - +func ApplyWatermarks(logicalName string) func() error { // Write open - file, err := os.OpenFile(disk, os.O_WRONLY, 0) + file, err := os.OpenFile(logicalName, os.O_WRONLY, 0) if err != nil { - log.Printf("%s | Failed to open disk, err: %s", disk, err) - return nil, err + return func() error { + return err + } } defer file.Close() // Get disk or partition size fileSize, err := file.Seek(0, io.SeekEnd) if err != nil { - log.Printf("%s | Failed to get size, err: %s", disk, err) - return nil, err + return func() error { + return err + } } // Write watermarks on random locations - err = writeWatermarks(file, fileSize, watermarks) - if err != nil { - log.Printf("%s | Failed to write watermarks, err: %s", disk, err) - return nil, err + watermarks := writeWatermarks(file, 0, fileSize, numWatermarks) + if len(watermarks) != numWatermarks { + return func() error { + ErrorWritingWatermarks := errors.New("Error writing watermarks in the file") + return fmt.Errorf("%s | %w", logicalName, ErrorWritingWatermarks) + } } - checker := func() bool { - log.Printf("%s | Checking if the watermark has been removed", disk) - - file, err := os.OpenFile(disk, os.O_RDONLY, 0) + checker := func() error { + file, err := os.OpenFile(logicalName, os.O_RDONLY, 0) if err != nil { - log.Printf("%s | Failed to open disk for reading: %s", disk, err) - return false + return err } defer file.Close() for _, watermark := range watermarks { - _, err = file.Seek(int64(watermark.position), io.SeekStart) + _, err = file.Seek(watermark.position, io.SeekStart) if err != nil { - log.Printf("%s | Error moving rw pointer: %s", disk, err) - return false + return err } // Read the watermark written to the position currentValue := make([]byte, bufferSize) - _, err = file.Read(currentValue) + _, err = io.ReadFull(file, currentValue) if err != nil { - log.Printf("%s | Error reading from file: %s", disk, err) - return false + return err } // Check if the watermark is still in the disk if slices.Equal(currentValue, watermark.data) { - log.Printf("%s | Error existing watermark in the file", disk) - return false + ErrorExistingWatermark := errors.New("Error existing watermark in the file") + return fmt.Errorf("%s | %w", logicalName, ErrorExistingWatermark) } } - log.Printf("%s | Watermarks has been removed", disk) - return true + return nil } - return checker, nil + return checker } -func writeWatermarks(file *os.File, fileSize int64, watermarks []watermark) error { - for i := 0; i < numWatermarks; i++ { +func writeWatermarks(file *os.File, a, b int64, count int) []watermark { + if count == 1 { data := make([]byte, bufferSize) _, err := rand.Read(data) if err != nil { - log.Println("error:", err) - return err + return nil } - randomPosition, err := rand.Int(rand.Reader, big.NewInt(fileSize)) + offset, err := rand.Int(rand.Reader, big.NewInt(b-a-bufferSize)) if err != nil { - log.Println(err) - return err + return nil } - - _, err = file.Seek(int64(randomPosition.Uint64()), io.SeekStart) + randomPosition := int64(offset.Uint64()) + a + _, err = file.Seek(randomPosition, io.SeekStart) if err != nil { - log.Println("Error moving rw pointer:", err) - return err + return nil + } + _, err = file.Write(data) + if err != nil { + return nil + } + + w := watermark{ + position: randomPosition, + data: data, } - watermarks[i].position = randomPosition.Uint64() - watermarks[i].data = data + return []watermark{w} } - return nil + // Divide the intervals into two equal parts (approximately) + mid := (a + b) / 2 + + // Return recursively the call of function with the two remaining intervals + leftCount := count / 2 + rightCount := count - leftCount + + return append(writeWatermarks(file, a, mid-1, leftCount), writeWatermarks(file, mid, b, rightCount)...) } From 42370c2d714001ad1ebb9189959b3c46db0259eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Thu, 25 Apr 2024 17:06:09 +0200 Subject: [PATCH 3/6] return err propagation to ApplyWatermarks and more documentation to the divide and conquer writeWatermarks function --- actions/storage_controller.go | 6 ++++-- utils/watermark_disk.go | 17 +++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/actions/storage_controller.go b/actions/storage_controller.go index c7bfebf7..2890cc62 100644 --- a/actions/storage_controller.go +++ b/actions/storage_controller.go @@ -102,8 +102,10 @@ func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName stri } // Watermark disk log.Printf("%s | Initiating watermarking process", logicalName) - check := utils.ApplyWatermarks(logicalName) - + check, err := utils.ApplyWatermarks(logicalName) + if err != nil { + return err + } err = util.WipeDisk(ctx, logicalName) if err != nil { return err diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go index 97ca10be..35351d32 100644 --- a/utils/watermark_disk.go +++ b/utils/watermark_disk.go @@ -21,30 +21,23 @@ type watermark struct { data []byte } -func ApplyWatermarks(logicalName string) func() error { +func ApplyWatermarks(logicalName string) (func() error, error) { // Write open file, err := os.OpenFile(logicalName, os.O_WRONLY, 0) if err != nil { - return func() error { - return err - } + return nil, err } defer file.Close() // Get disk or partition size fileSize, err := file.Seek(0, io.SeekEnd) if err != nil { - return func() error { - return err - } + return nil, err } // Write watermarks on random locations watermarks := writeWatermarks(file, 0, fileSize, numWatermarks) if len(watermarks) != numWatermarks { - return func() error { - ErrorWritingWatermarks := errors.New("Error writing watermarks in the file") - return fmt.Errorf("%s | %w", logicalName, ErrorWritingWatermarks) - } + return nil, err } checker := func() error { @@ -73,7 +66,7 @@ func ApplyWatermarks(logicalName string) func() error { } return nil } - return checker + return checker, nil } func writeWatermarks(file *os.File, a, b int64, count int) []watermark { From 8e5771db776daeb9bde9384d19fbddc581302ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Thu, 25 Apr 2024 17:43:00 +0200 Subject: [PATCH 4/6] rewrite writeWatermarks function from recursive to iterative --- utils/watermark_disk.go | 42 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go index 35351d32..00a939c5 100644 --- a/utils/watermark_disk.go +++ b/utils/watermark_disk.go @@ -35,8 +35,8 @@ func ApplyWatermarks(logicalName string) (func() error, error) { return nil, err } // Write watermarks on random locations - watermarks := writeWatermarks(file, 0, fileSize, numWatermarks) - if len(watermarks) != numWatermarks { + watermarks, err := writeWatermarks(file, fileSize, numWatermarks) + if err != nil { return nil, err } @@ -69,39 +69,33 @@ func ApplyWatermarks(logicalName string) (func() error, error) { return checker, nil } -func writeWatermarks(file *os.File, a, b int64, count int) []watermark { - if count == 1 { +// writeWatermarks creates random watermarks and writes them uniformly into a given file. +func writeWatermarks(file *os.File, fileSize, count int64) ([]watermark, error) { + origin := int64(0) + intervalSize := fileSize / count + watermarks := make([]watermark, count) + for i := 0; i < numWatermarks; i++ { data := make([]byte, bufferSize) _, err := rand.Read(data) if err != nil { - return nil + return nil, err } - offset, err := rand.Int(rand.Reader, big.NewInt(b-a-bufferSize)) + offset, err := rand.Int(rand.Reader, big.NewInt(intervalSize)) if err != nil { - return nil + return nil, err } - randomPosition := int64(offset.Uint64()) + a + randomPosition := int64(offset.Uint64()) + origin - bufferSize _, err = file.Seek(randomPosition, io.SeekStart) if err != nil { - return nil + return nil, err } _, err = file.Write(data) if err != nil { - return nil - } - - w := watermark{ - position: randomPosition, - data: data, + return nil, err } - return []watermark{w} + watermarks[i].position = randomPosition + watermarks[i].data = data + origin += intervalSize } - // Divide the intervals into two equal parts (approximately) - mid := (a + b) / 2 - - // Return recursively the call of function with the two remaining intervals - leftCount := count / 2 - rightCount := count - leftCount - - return append(writeWatermarks(file, a, mid-1, leftCount), writeWatermarks(file, mid, b, rightCount)...) + return watermarks, nil } From 3029047477f2077b72b04cb5681fca17b49d09e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Fri, 26 Apr 2024 00:34:57 +0200 Subject: [PATCH 5/6] Added tests to ApplyWatermarks func and more verbose err --- utils/watermark_disk.go | 9 +++- utils/watermark_disk_test.go | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 utils/watermark_disk_test.go diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go index 00a939c5..5ece41bc 100644 --- a/utils/watermark_disk.go +++ b/utils/watermark_disk.go @@ -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 { @@ -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) } } return nil diff --git a/utils/watermark_disk_test.go b/utils/watermark_disk_test.go new file mode 100644 index 00000000..fc2f294e --- /dev/null +++ b/utils/watermark_disk_test.go @@ -0,0 +1,87 @@ +package utils + +import ( + "crypto/rand" + "os" + "testing" + + "gotest.tools/assert" +) + +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) + } + + // 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") + } + } + 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) + } + + // 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) + } + } + }) + 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) + } + + err = checker() + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } + }) +} From e354379d1a0dd245a0beb9c024f5c9224c61f72c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Tur=C3=A9gano?= Date: Fri, 26 Apr 2024 11:27:31 +0200 Subject: [PATCH 6/6] Added more comments for code understanding and function usage. Also reduced test code using assert functions --- actions/storage_controller.go | 4 ++++ examples/diskwipe/main.go | 3 +-- utils/watermark_disk.go | 5 ++++- utils/watermark_disk_test.go | 35 +++++++++-------------------------- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/actions/storage_controller.go b/actions/storage_controller.go index 2890cc62..cecf9f21 100644 --- a/actions/storage_controller.go +++ b/actions/storage_controller.go @@ -101,20 +101,24 @@ func (s *StorageControllerAction) WipeDisk(ctx context.Context, logicalName stri return err } // Watermark disk + // Before wiping the disk, we apply watermarks to later verify successful deletion log.Printf("%s | Initiating watermarking process", logicalName) check, err := utils.ApplyWatermarks(logicalName) if err != nil { return err } + // Wipe the disk err = util.WipeDisk(ctx, logicalName) if err != nil { return err } + // Check if the watermark has been removed after wiping log.Printf("%s | Checking if the watermark has been removed", logicalName) err = check() if err != nil { return err } + // Watermarks have been successfully removed, indicating successful deletion log.Printf("%s | Watermarks has been removed", logicalName) return nil } diff --git a/examples/diskwipe/main.go b/examples/diskwipe/main.go index b15c5097..43124f05 100644 --- a/examples/diskwipe/main.go +++ b/examples/diskwipe/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "fmt" "time" "github.com/metal-toolbox/ironlib/actions" @@ -22,5 +21,5 @@ func main() { if err != nil { logger.Fatal(err) } - fmt.Println("Wiped successfully!") + logger.Println("Wiped successfully!") } diff --git a/utils/watermark_disk.go b/utils/watermark_disk.go index 5ece41bc..62838186 100644 --- a/utils/watermark_disk.go +++ b/utils/watermark_disk.go @@ -21,6 +21,9 @@ type watermark struct { data []byte } +// ApplyWatermarks applies watermarks to the specified disk. +// It returns a function that checks if the applied watermarks still exist on the file. +// It relies on the writeWatermarks function to uniformly write watermarks across the disk. func ApplyWatermarks(logicalName string) (func() error, error) { // Write open file, err := os.OpenFile(logicalName, os.O_WRONLY, 0) @@ -66,7 +69,7 @@ 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 position: ") - return fmt.Errorf("%s | %w %d", logicalName, ErrorExistingWatermark, watermark.position) + return fmt.Errorf("%s@%d | %w", logicalName, watermark.position, ErrorExistingWatermark) } } return nil diff --git a/utils/watermark_disk_test.go b/utils/watermark_disk_test.go index fc2f294e..481e4bb9 100644 --- a/utils/watermark_disk_test.go +++ b/utils/watermark_disk_test.go @@ -5,7 +5,7 @@ import ( "os" "testing" - "gotest.tools/assert" + "github.com/stretchr/testify/assert" ) func Test_ApplyWatermarks(t *testing.T) { @@ -25,35 +25,21 @@ func Test_ApplyWatermarks(t *testing.T) { if err != nil { t.Fatalf("Failed to create empty file: %v", err) } - + // Create a 1KB empty file, no room for all watermarks + assert.NoError(t, os.Truncate(tempFile.Name(), 1*1024)) // 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") - } - } - assert.ErrorContains(t, err, "invalid argument") + checker, _ := ApplyWatermarks(tempFile.Name()) + assert.Nil(t, checker) }) 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) - } + assert.NoError(t, os.Truncate(tempFile.Name(), 0)) // 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) - } - } + assert.Nil(t, checker) }) t.Run("PositiveTestWithRandomDataAndWipe", func(t *testing.T) { // Write the file full of random data @@ -73,11 +59,8 @@ func Test_ApplyWatermarks(t *testing.T) { 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) - } + assert.NoError(t, os.Truncate(tempFile.Name(), 0)) + assert.NoError(t, os.Truncate(tempFile.Name(), 15*1024*1024)) err = checker() if err != nil {