Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbojangles3 committed Sep 25, 2024
1 parent 4c96cdb commit 58384a8
Showing 1 changed file with 83 additions and 43 deletions.
126 changes: 83 additions & 43 deletions filesystem/fat32/fat32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ package fat32_test

import (
"bytes"
"crypto/rand"
"fmt"
"io"
mRand "math/rand"
"math/rand/v2"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -249,6 +248,8 @@ func TestFat32Read(t *testing.T) {
}
//nolint:thelper // this is not a helper function
runTest := func(t *testing.T, pre, post int64) {
seed := [32]byte{}
chacha := rand.NewChaCha8(seed)
for _, t2 := range tests {
tt := t2
t.Run(fmt.Sprintf("blocksize %d filesize %d bytechange %d", tt.filesize, tt.blocksize, tt.bytechange), func(t *testing.T) {
Expand All @@ -262,7 +263,7 @@ func TestFat32Read(t *testing.T) {
corrupted := ""
if tt.bytechange >= 0 {
b := make([]byte, 1)
_, _ = rand.Read(b)
_, _ = chacha.Read(b)
_, _ = f.WriteAt(b, tt.bytechange+pre)
corrupted = fmt.Sprintf("corrupted %d", tt.bytechange+pre)
}
Expand Down Expand Up @@ -633,14 +634,16 @@ func TestFat32OpenFile(t *testing.T) {
bWrite := make([]byte, size)
header := fmt.Sprintf("OpenFile(%s, %s)", path, getOpenMode(mode))
readWriter, err := fs.OpenFile(path, mode)
seed := [32]byte{}
chacha := rand.NewChaCha8(seed)
switch {
case err != nil:
t.Errorf("%s: unexpected error: %v", header, err)
case readWriter == nil:
t.Errorf("%s: Unexpected nil output", header)
default:
// write and then read
_, _ = rand.Read(bWrite)
_, _ = chacha.Read(bWrite)
written, writeErr := readWriter.Write(bWrite)
_, _ = readWriter.Seek(0, 0)
bRead, readErr := io.ReadAll(readWriter)
Expand Down Expand Up @@ -693,14 +696,16 @@ func TestFat32OpenFile(t *testing.T) {
bWrite := make([]byte, size)
header := fmt.Sprintf("OpenFile(%s, %s)", path, getOpenMode(mode))
readWriter, err := fs.OpenFile(path, mode)
seed := [32]byte{}
chacha := rand.NewChaCha8(seed)
switch {
case err != nil:
t.Fatalf("%s: unexpected error: %v", header, err)
case readWriter == nil:
t.Fatalf("%s: Unexpected nil output", header)
default:
// write and then read
_, _ = rand.Read(bWrite)
_, _ = chacha.Read(bWrite)
written, writeErr := readWriter.Write(bWrite)
_, _ = readWriter.Seek(0, 0)

Expand Down Expand Up @@ -760,6 +765,7 @@ func TestFat32OpenFile(t *testing.T) {
bWrite := make([]byte, size)
header := fmt.Sprintf("OpenFile(%s, %s)", path, getOpenMode(mode))
readWriter, err := fs.OpenFile(path, mode)

switch {
case err != nil:
t.Errorf("%s: unexpected error: %v", header, err)
Expand All @@ -769,7 +775,9 @@ func TestFat32OpenFile(t *testing.T) {
// success
}

_, _ = rand.Read(bWrite)
seed := [32]byte{}
chacha := rand.NewChaCha8(seed)
_, _ = chacha.Read(bWrite)
writeSizes := []int{512, 1024, 256}
low := 0
for i := 0; low < len(bWrite); i++ {
Expand Down Expand Up @@ -1064,7 +1072,7 @@ func TestOpenFileCaseInsensitive(t *testing.T) {
}

// small helper function to make a large fat32 filesystem
func mkfs(name string) filesystem.FileSystem {
func mkfs(name string) (filesystem.FileSystem, error) {
size := int64(6 * 1024 * 1024 * 1024)
d, err := diskfs.Create(name, size, diskfs.Raw, diskfs.SectorSizeDefault)
if err != nil {
Expand All @@ -1078,87 +1086,119 @@ func mkfs(name string) filesystem.FileSystem {
}

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

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

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

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

size := 5 * 1024 * 1024
smallFile := make([]byte, size, size)
smallFile := make([]byte, size)
_, err = rw.Write(smallFile)
if err != nil {
panic(err)
return err
}
return nil
}
func mkRandFile(fs filesystem.FileSystem, name string, rSize int) {
func mkRandFile(fs filesystem.FileSystem, name string, rSize int32) error {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
return err
}

size := rSize * 1024 * 1024
randFile := make([]byte, size, size)
randFile := make([]byte, size)
_, err = rw.Write(randFile)
if err != nil {
panic(err)
return err
}
return nil
}
func mkGigFile(fs filesystem.FileSystem, name string) {
func mkGigFile(fs filesystem.FileSystem, name string) error {
rw, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR)
if err != nil {
panic(err)
return err
}

size := 1024 * 1024 * 1024
smallFile := make([]byte, size, size)
smallFile := make([]byte, size)
_, err = rw.Write(smallFile)
if err != nil {
panic(err)
return err
}
return nil
}
func TestCreateFileTree(t *testing.T) {
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))
f, err := mkfs(fileName)

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

ineffectual assignment to err (ineffassign)

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

ineffectual assignment to err (ineffassign)
err = mkdir(f, "/A")
if err != nil {
t.Errorf("Error making dir /A in root: %v", err)
}
err = mkdir(f, "/b")
if err != nil {
t.Errorf("Error making dir /b in root: %v", err)
}
err = mkMicroFile(f, "/rootfile")
if err != nil {
t.Errorf("Error making microfile in root: %v", err)
}
//r := rand.New(rand.Uint64())

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)
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")
err = mkdir(f, "/b/sub"+inc)
if err != nil {
t.Errorf("Error making directory,"+"/b/sub"+inc+": %v", err)
}
err = mkdir(f, "/b/sub"+inc+"/blob/")
if err != nil {
t.Errorf("Error making directory,"+"/b/sub"+inc+"/blob/: %v", err)
}
err = mkMicroFile(f, "/b/sub"+inc+"/blob/microfile")
if err != nil {
t.Errorf("Error making directory,"+"/b/sub"+inc+"/blob/microfile: %v", err)
}
err = mkRandFile(f, "/b/sub"+inc+"/blob/randfile", rand.Int32N(73))

Check failure on line 1182 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)

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand) (gosec)
if err != nil {
t.Errorf("Error making directory,"+"/b/sub"+inc+"/blob/randfile: %v", err)
}
err = mkSmallFile(f, "/b/sub"+inc+"/blob/smallfile")
if err != nil {
t.Errorf("Error making directory,"+"/b/sub"+inc+"/blob/smallfile: %v", err)
}

}

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

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unnecessary trailing newline (whitespace)

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unnecessary trailing newline (whitespace)
mkGigFile(f, "/b/sub49/blob/gigfile1")
mkGigFile(f, "/b/sub50/blob/gigfile1")
mkGigFile(f, "/b/sub55/blob/gigfile1")
err = mkGigFile(f, "/b/sub49/blob/gigfile1")
if err != nil {
t.Errorf("Error making gigfile1 /b/sub49/blob/gigfile1: %v", err)
}
err = mkGigFile(f, "/b/sub50/blob/gigfile1")
if err != nil {
t.Errorf("Error making gigfile1 /b/sub50/blob/gigfile1: %v", err)
}
err = mkGigFile(f, "/b/sub55/blob/gigfile1")
if err != nil {
t.Errorf("Error making gigfile1 /b/sub55/blob/gigfile1: %v", err)
}
}

0 comments on commit 58384a8

Please sign in to comment.