Skip to content

Commit

Permalink
test fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Aug 31, 2024
1 parent 8e46716 commit da4b766
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 86 deletions.
Binary file added assets/testdata/uncompress/TEST.mod
Binary file not shown.
Binary file added assets/testdata/uncompress/TEST.xm
Binary file not shown.
42 changes: 21 additions & 21 deletions internal/magicnumberr/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Defacto2/server/internal/magicnumberr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -29,11 +30,10 @@ func TestArchive(t *testing.T) {
t.Parallel()
t.Log("TestArchive")
r, err := os.Open(td(seaFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
sign, err := magicnumberr.Archive(r)
assert.NoError(t, err)
assert.True(t, sign == magicnumberr.ARChiveSEA)
require.NoError(t, err)
assert.Equal(t, magicnumberr.ARChiveSEA, sign)
assert.Equal(t, "ARC by SEA", sign.String())
assert.Equal(t, "Archive by SEA", sign.Title())
Expand All @@ -43,7 +43,7 @@ func TestZipReduce(t *testing.T) {
t.Parallel()
t.Log("TestZipReduce")
r, err := os.Open(td(zipReduceFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.PkReduce(r))
}
Expand All @@ -52,7 +52,7 @@ func TestZipShrink(t *testing.T) {
t.Parallel()
t.Log("TestZipShrink")
r, err := os.Open(td(zipShrinkFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.PkShrink(r))
}
Expand All @@ -61,7 +61,7 @@ func TestZipImplode(t *testing.T) {
t.Parallel()
t.Log("TestZipImplode")
r, err := os.Open(td(zipImplodeFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.PkImplode(r))
}
Expand All @@ -70,7 +70,7 @@ func TestZipStore(t *testing.T) {
t.Parallel()
t.Log("TestZipStore")
r, err := os.Open(td(zipStoreFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Pkzip(r))
}
Expand All @@ -79,7 +79,7 @@ func TestTar(t *testing.T) {
t.Parallel()
t.Log("TestTar")
r, err := os.Open(td(tarFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Tar(r))
}
Expand All @@ -88,7 +88,7 @@ func TestRarv5(t *testing.T) {
t.Parallel()
t.Log("TestRarv5")
r, err := os.Open(td(rarv5File))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Rarv5(r))
}
Expand All @@ -97,11 +97,11 @@ func TestGzip(t *testing.T) {
t.Parallel()
t.Log("TestGzip")
r, err := os.Open(td(gzFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Gzip(r))
r, err = os.Open(td(b2zFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.False(t, magicnumberr.Gzip(r))
}
Expand All @@ -110,7 +110,7 @@ func TestBzip2(t *testing.T) {
t.Parallel()
t.Log("TestBzip2")
r, err := os.Open(td(b2zFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Bzip2(r))
}
Expand All @@ -119,7 +119,7 @@ func TestX7z(t *testing.T) {
t.Parallel()
t.Log("TestX7z")
r, err := os.Open(td(x7zFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.X7z(r))
}
Expand All @@ -128,7 +128,7 @@ func TestXZ(t *testing.T) {
t.Parallel()
t.Log("TestXZ")
r, err := os.Open(td(xzFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.XZ(r))
}
Expand All @@ -137,12 +137,12 @@ func TestArcFree(t *testing.T) {
t.Parallel()
t.Log("TestArcFree")
r, err := os.Open(td(freeArcFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.ArcFree(r))
assert.False(t, magicnumberr.ArcSEA(r))
b, sign, err := magicnumberr.MatchExt(freeArcFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, b)
assert.Equal(t, magicnumberr.FreeArc, sign)
}
Expand All @@ -151,12 +151,12 @@ func TestArcSEA(t *testing.T) {
t.Parallel()
t.Log("TestArcSEA")
r, err := os.Open(td(seaFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.False(t, magicnumberr.ArcFree(r))
assert.True(t, magicnumberr.ArcSEA(r))
b, sign, err := magicnumberr.MatchExt(seaFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, b)
assert.Equal(t, magicnumberr.ARChiveSEA, sign)
}
Expand All @@ -165,15 +165,15 @@ func TestLHA(t *testing.T) {
t.Parallel()
t.Log("TestLzhLha")
r, err := os.Open(td(lhaFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.LzhLha(r))
sign := magicnumberr.Find(r)
assert.Equal(t, magicnumberr.YoshiLHA, sign)
assert.Equal(t, "LHA by Yoshi", sign.String())
assert.Equal(t, "Yoshi LHA", sign.Title())
b, sign, err := magicnumberr.MatchExt(lhaFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, b)
assert.Equal(t, magicnumberr.YoshiLHA, sign)
}
Expand All @@ -182,7 +182,7 @@ func TestArj(t *testing.T) {
t.Parallel()
t.Log("TestArj")
r, err := os.Open(td(arjFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Arj(r))
}
13 changes: 7 additions & 6 deletions internal/magicnumberr/cdimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Defacto2/server/internal/magicnumberr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -18,15 +19,15 @@ func TestDaa(t *testing.T) {
t.Parallel()
t.Log("TestDaa")
r, err := os.Open(imgfile(DaaFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Daa(r))
sign := magicnumberr.Find(r)
assert.Equal(t, magicnumberr.CDPowerISO, sign)
assert.Equal(t, "CD, PowerISO", sign.String())
assert.Equal(t, "CD PowerISO", sign.Title())
b, sign, err := magicnumberr.MatchExt(DaaFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, b)
assert.Equal(t, magicnumberr.CDPowerISO, sign)
}
Expand All @@ -35,15 +36,15 @@ func TestCDISO(t *testing.T) {
t.Parallel()
t.Log("TestCDISO")
r, err := os.Open(imgfile(ISOFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.ISO(r))
sign := magicnumberr.Find(r)
assert.Equal(t, magicnumberr.CDISO9660, sign)
assert.Equal(t, "CD, ISO 9660", sign.String())
assert.Equal(t, "CD ISO 9660", sign.Title())
b, sign, err := magicnumberr.MatchExt(ISOFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.True(t, b)
assert.Equal(t, magicnumberr.CDISO9660, sign)
}
Expand All @@ -52,13 +53,13 @@ func TestMdf(t *testing.T) {
t.Parallel()
t.Log("TestMdf")
r, err := os.Open(imgfile(MdfFile))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.Mdf(r))
sign := magicnumberr.Find(r)
assert.Equal(t, magicnumberr.CDAlcohol120, sign)
b, sign, err := magicnumberr.MatchExt(DaaFile, r)
assert.NoError(t, err)
require.NoError(t, err)
assert.False(t, b)
assert.Equal(t, magicnumberr.CDAlcohol120, sign)
}
2 changes: 1 addition & 1 deletion internal/magicnumberr/executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const (
func FindExecutable(r io.ReaderAt) (Windows, error) {
win := Default()
if r == nil {
return win, fmt.Errorf("nil reader")
return win, ErrNilReader
}
const size = 1024 * 3
p := make([]byte, size)
Expand Down
18 changes: 9 additions & 9 deletions internal/magicnumberr/executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func TestMSExe(t *testing.T) {
t.Parallel()
t.Log("TestMSExe")
r, err := os.Open(windows("hellojs.com"))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.True(t, magicnumberr.MSExe(r))
}

func TestFindBytesExecutableFreeDOS(t *testing.T) {
t.Parallel()
w, err := magicnumberr.FindExecutable(nil)
assert.Error(t, err)
require.Error(t, err)
assert.Equal(t, magicnumberr.UnknownPE, w.PE)
assert.Equal(t, magicnumberr.NoneNE, w.NE)

Expand All @@ -42,11 +42,11 @@ func TestFindBytesExecutableFreeDOS(t *testing.T) {
require.NoError(t, err)
defer p.Close()
w, err = magicnumberr.FindExecutable(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.UnknownPE, w.PE)
assert.Equal(t, magicnumberr.NoneNE, w.NE)
sign, err := magicnumberr.Program(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.MicrosoftExecutable, sign)
}
}
Expand All @@ -71,7 +71,7 @@ func TestFindBytesExecutableWinVista(t *testing.T) {
assert.Equal(t, "Windows Vista 64-bit", fmt.Sprint(w))
assert.Equal(t, magicnumberr.NoneNE, w.NE)
sign, err := magicnumberr.Program(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.MicrosoftExecutable, sign)
}
}
Expand All @@ -87,14 +87,14 @@ func TestFindBytesExecutableWin3(t *testing.T) {
require.NoError(t, err)
defer p.Close()
w, err := magicnumberr.FindExecutable(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.UnknownPE, w.PE)
assert.Equal(t, magicnumberr.Windows286Exe, w.NE)
assert.Equal(t, 3, w.Major)
assert.Equal(t, 10, w.Minor)
assert.Equal(t, "Windows v3.10 for 286", fmt.Sprint(w))
sign, err := magicnumberr.Program(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.MicrosoftExecutable, sign)
}

Expand All @@ -109,7 +109,7 @@ func TestFindBytesExecutableWin3(t *testing.T) {
assert.Equal(t, 0, w.Minor)
assert.Equal(t, "Windows 2000 32-bit", fmt.Sprint(w))
sign, err := magicnumberr.Program(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.MicrosoftExecutable, sign)

p, err = os.Open(td(filepath.Join("binaries", "windowsXP", "CoreTempv13", "64bit", "Core Temp.exe")))
Expand All @@ -124,7 +124,7 @@ func TestFindBytesExecutableWin3(t *testing.T) {
assert.Equal(t, 2, w.Minor)
assert.Equal(t, "Windows XP Professional x64 Edition 64-bit", fmt.Sprint(w))
sign, err = magicnumberr.Program(p)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, magicnumberr.MicrosoftExecutable, sign)
}

Expand Down
9 changes: 6 additions & 3 deletions internal/magicnumberr/id3.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,28 @@ func MusicID3v1(r io.ReaderAt) string {
if !bytes.Equal(p, []byte{'T', 'A', 'G'}) {
return ""
}
const songOff = 3
const songSize = 30
p = make([]byte, songSize)
sr = io.NewSectionReader(r, offset+3, songSize)
sr = io.NewSectionReader(r, offset+songOff, songSize)
if n, err := sr.Read(p); err != nil || n < size {
return ""
}
song := string(bytes.Trim(p, nul))
song = strings.TrimSpace(song)
const artistOff = 33
const artistSize = 30
p = make([]byte, artistSize)
sr = io.NewSectionReader(r, offset+33, artistSize)
sr = io.NewSectionReader(r, offset+artistOff, artistSize)
if n, err := sr.Read(p); err != nil || n < size {
return ""
}
artist := string(bytes.Trim(p, nul))
artist = strings.TrimSpace(artist)
const yearOff = 93
const yearSize = 4
p = make([]byte, yearSize)
sr = io.NewSectionReader(r, offset+93, yearSize)
sr = io.NewSectionReader(r, offset+yearOff, yearSize)
if n, err := sr.Read(p); err != nil || n < size {
return ""
}
Expand Down
5 changes: 3 additions & 2 deletions internal/magicnumberr/id3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Defacto2/server/internal/magicnumberr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -17,7 +18,7 @@ func TestMusicID3v1(t *testing.T) {
t.Parallel()
t.Log("TestMusicID3v1")
r, err := os.Open(mp3file(IDv1File))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.Equal(t, "Title by Artist (2003)", magicnumberr.MusicID3v1(r))
assert.Equal(t, "", magicnumberr.MusicID3v2(r))
Expand All @@ -27,7 +28,7 @@ func TestMusicID3v2(t *testing.T) {
t.Parallel()
t.Log("TestMusicID3v2")
r, err := os.Open(mp3file(IDv2File))
assert.NoError(t, err)
require.NoError(t, err)
defer r.Close()
assert.Equal(t, "", magicnumberr.MusicID3v1(r))
assert.Equal(t, "Title by Artist (2003)", magicnumberr.MusicID3v2(r))
Expand Down
Loading

0 comments on commit da4b766

Please sign in to comment.