Skip to content

Commit

Permalink
added test for file tree creation with large files
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbojangles3 committed Sep 25, 2024
1 parent 1c5247c commit 4c96cdb
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions filesystem/fat32/fat32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"crypto/rand"
"fmt"
"io"
mRand "math/rand"
"os"
"strconv"
"strings"
"testing"

"github.com/diskfs/go-diskfs"
"github.com/diskfs/go-diskfs/disk"
"github.com/diskfs/go-diskfs/filesystem"
"github.com/diskfs/go-diskfs/filesystem/fat32"
"github.com/diskfs/go-diskfs/testhelper"
Expand Down Expand Up @@ -1058,3 +1062,103 @@ func TestOpenFileCaseInsensitive(t *testing.T) {
}
}
}

// small helper function to make a large fat32 filesystem
func mkfs(name string) filesystem.FileSystem {
size := int64(6 * 1024 * 1024 * 1024)
d, err := diskfs.Create(name, size, diskfs.Raw, diskfs.SectorSizeDefault)
if err != nil {
fmt.Printf("error creating disk: %v", err)
os.Exit(1)
}

spec := disk.FilesystemSpec{
Partition: 0,
FSType: filesystem.TypeFat32,
}

fs, err := d.CreateFilesystem(spec)
if err != nil {
panic(err)
}
return fs
}

func mkdir(fs filesystem.FileSystem, name string) {
err := fs.Mkdir(name)
if err != nil {
panic(err)
}
}

func mkMicroFile(fs filesystem.FileSystem, name string) {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
}

_, err = rw.Write([]byte("hello World"))
if err != nil {
panic(err)
}
}
func mkSmallFile(fs filesystem.FileSystem, name string) {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
}

size := 5 * 1024 * 1024
smallFile := make([]byte, size, size)

Check failure on line 1112 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

S1019: should use make([]byte, size) instead (gosimple)
_, err = rw.Write(smallFile)
if err != nil {
panic(err)
}
}
func mkRandFile(fs filesystem.FileSystem, name string, rSize int) {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
}

size := rSize * 1024 * 1024
randFile := make([]byte, size, size)

Check failure on line 1125 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

S1019: should use make([]byte, size) instead (gosimple)
_, err = rw.Write(randFile)
if err != nil {
panic(err)
}
}
func mkGigFile(fs filesystem.FileSystem, name string) {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
}

size := 1024 * 1024 * 1024
smallFile := make([]byte, size, size)

Check failure on line 1138 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

S1019: should use make([]byte, size) instead (gosimple)
_, err = rw.Write(smallFile)
if err != nil {
panic(err)
}
}
func TestCreateFileTree(t *testing.T) {

Check failure on line 1144 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
fileName := "file-tree.img"
os.Remove(fileName)
f := mkfs(fileName)
mkdir(f, "/A")
mkdir(f, "/b")
mkMicroFile(f, "/rootfile")
r := mRand.New(mRand.NewSource(37))

Check failure on line 1151 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
for i := 0; i < 100; i++ {
inc := strconv.Itoa(i)
mkdir(f, "/b/sub"+inc)
mkdir(f, "/b/sub"+inc+"/blob/")
mkMicroFile(f, "/b/sub"+inc+"/blob/microfile")
mkRandFile(f, "/b/sub"+inc+"/blob/randfile", r.Intn(73))
mkSmallFile(f, "/b/sub"+inc+"/blob/smallfile")

}

Check failure on line 1160 in filesystem/fat32/fat32_test.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unnecessary trailing newline (whitespace)
mkGigFile(f, "/b/sub49/blob/gigfile1")
mkGigFile(f, "/b/sub50/blob/gigfile1")
mkGigFile(f, "/b/sub55/blob/gigfile1")
}

0 comments on commit 4c96cdb

Please sign in to comment.