Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dsoprea committed Sep 6, 2019
1 parent 292ba01 commit 9e57a61
Show file tree
Hide file tree
Showing 9 changed files with 1,001 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmd/exfat_print_boot_sector_header/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func main() {
err = er.Parse()
log.PanicIf(err)

er.ActiveBootRegion().Dump()
er.ActiveBootSectorHeader().Dump()
}
2 changes: 1 addition & 1 deletion navigator_entry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ type ExfatVolumeGuidDirectoryEntry struct {

// String returns a descriptive string.
func (vgde ExfatVolumeGuidDirectoryEntry) String() string {
return fmt.Sprintf("VolumeGuidDirectoryEntry<SECONDARY-COUNT=(%d) SET-CHECKSUM=(0x%04x) GENERAL-PRIMARY-FLAGS=(%016b) GUID=[0x%064x]>", vgde.SecondaryCountRaw, vgde.SetChecksum, vgde.GeneralPrimaryFlags, vgde.VolumeGuid)
return fmt.Sprintf("VolumeGuidDirectoryEntry<SECONDARY-COUNT=(%d) SET-CHECKSUM=(0x%04x) GENERAL-PRIMARY-FLAGS=(0x%04x) GUID=[0x%016x...]>", vgde.SecondaryCountRaw, vgde.SetChecksum, vgde.GeneralPrimaryFlags, vgde.VolumeGuid[:4])
}

// SecondaryCount returns the count of associated secondary-records.
Expand Down
111 changes: 111 additions & 0 deletions navigator_entry_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package exfat

import (
"testing"
)

func TestEntryType_Dump(t *testing.T) {
EntryType(0xab).Dump()
}

func TestEntryType_String(t *testing.T) {
s := EntryType(0xab).String()
if s != "EntryType<TYPE-CODE=(11) IS-CRITICAL=[false] IS-PRIMARY=[true] IS-IN-USE=[true] X-IS-REGULAR=[true] X-IS-UNUSED=[false] X-IS-END=[false]>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatFileDirectoryEntry_Dump(t *testing.T) {
fdf := ExfatFileDirectoryEntry{}
fdf.Dump()
}

func TestExfatStreamExtensionDirectoryEntry_Dump(t *testing.T) {
sede := ExfatStreamExtensionDirectoryEntry{}
sede.Dump()
}

func TestDirectoryEntryParserKey_String(t *testing.T) {
depk := DirectoryEntryParserKey{}
s := depk.String()
if s != "DirectoryEntryParserKey<TYPE-CODE=(0) IS-CRITICAL=[false] IS-PRIMARY=[false]>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestFileAttributes_String(t *testing.T) {
s := FileAttributes(0x1234).String()
if s != "FileAttributes<IS-READONLY=[false] IS-HIDDEN=[false] IS-SYSTEM=[true] IS-DIRECTORY=[true] IS-ARCHIVE=[true]>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatVolumeGuidDirectoryEntry_String(t *testing.T) {
vgde := ExfatVolumeGuidDirectoryEntry{}
s := vgde.String()
if s != "VolumeGuidDirectoryEntry<SECONDARY-COUNT=(0) SET-CHECKSUM=(0x0000) GENERAL-PRIMARY-FLAGS=(0x0000) GUID=[0x0000000000000000...]>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatVolumeGuidDirectoryEntry_SecondaryCount(t *testing.T) {
vgde := ExfatVolumeGuidDirectoryEntry{
SecondaryCountRaw: 99,
}

if vgde.SecondaryCount() != 99 {
t.Fatalf("SecondaryCount not correct.")
}
}

func TestExfatVolumeGuidDirectoryEntry_TypeName(t *testing.T) {
vgde := ExfatVolumeGuidDirectoryEntry{}
if vgde.TypeName() != "VolumeGuid" {
t.Fatalf("TypeName not correct.")
}
}

func TestExfatTexFATDirectoryEntry_String(t *testing.T) {
tfde := ExfatTexFATDirectoryEntry{}
s := tfde.String()
if s != "TexFATDirectoryEntry<>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatTexFATDirectoryEntry_TypeName(t *testing.T) {
tfde := ExfatTexFATDirectoryEntry{}
if tfde.TypeName() != "TexFAT" {
t.Fatalf("TypeName not correct.")
}
}

func TestExfatVendorExtensionDirectoryEntry_String(t *testing.T) {
vede := ExfatVendorExtensionDirectoryEntry{}
s := vede.String()
if s != "VendorExtensionDirectoryEntry<GENERAL-SECONDARY-FLAGS=(00000000) GUID=(0x00000000000000000000000000000000)>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatVendorExtensionDirectoryEntry_TypeName(t *testing.T) {
vede := ExfatVendorExtensionDirectoryEntry{}
if vede.TypeName() != "VendorExtension" {
t.Fatalf("TypeName not correct.")
}
}

func TestExfatVendorAllocationDirectoryEntry_String(t *testing.T) {
vade := ExfatVendorAllocationDirectoryEntry{}
s := vade.String()
if s != "VendorAllocationDirectoryEntry<GENERAL-SECONDARY-FLAGS=(00000000) GUID=(0x00000000000000000000000000000000) VENDOR-DEFINED=(0x00000000) FIRST-CLUSTER=(0) DATA-LENGTH=(0)>" {
t.Fatalf("String not correct: [%s]", s)
}
}

func TestExfatVendorAllocationDirectoryEntry_TypeName(t *testing.T) {
vade := ExfatVendorAllocationDirectoryEntry{}
if vade.TypeName() != "VendorAllocation" {
t.Fatalf("TypeName not correct.")
}
}
134 changes: 131 additions & 3 deletions navigator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestDirectoryEntryIndex_GetFile(t *testing.T) {
}
}

func TestDirectoryEntryIndex_FindIndexedFile(t *testing.T) {
func TestDirectoryEntryIndex_FindIndexedFile__Hit(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()
Expand Down Expand Up @@ -326,7 +326,27 @@ func TestDirectoryEntryIndex_FindIndexedFile(t *testing.T) {
}
}

func TestDirectoryEntryIndex_FindIndexedFileFileDirectoryEntry(t *testing.T) {
func TestDirectoryEntryIndex_FindIndexedFile__Miss(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

_, found := index.FindIndexedFile("invalid-file")
if found != false {
t.Fatalf("Expected invalid-file to not be found.")
}
}

func TestDirectoryEntryIndex_FindIndexedFileFileDirectoryEntry__Hit(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()
Expand All @@ -351,7 +371,27 @@ func TestDirectoryEntryIndex_FindIndexedFileFileDirectoryEntry(t *testing.T) {
}
}

func TestDirectoryEntryIndex_FindIndexedFileStreamExtensionDirectoryEntry(t *testing.T) {
func TestDirectoryEntryIndex_FindIndexedFileFileDirectoryEntry__Miss(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

fdf := index.FindIndexedFileFileDirectoryEntry("invalid-file")
if fdf != nil {
t.Fatalf("Expected file miss.")
}
}

func TestDirectoryEntryIndex_FindIndexedFileStreamExtensionDirectoryEntry__Hit(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()
Expand All @@ -370,3 +410,91 @@ func TestDirectoryEntryIndex_FindIndexedFileStreamExtensionDirectoryEntry(t *tes
t.Fatalf("Stream-extension entry-type not found: (%d)", sede.FirstCluster)
}
}

func TestDirectoryEntryIndex_FindIndexedFileDirectoryEntry__Hit(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

de := index.FindIndexedFileDirectoryEntry("2-delahaye-type-165-cabriolet-dsc_8025.jpg", "File", 0)
if de == nil {
t.Fatalf("Could not find entry.")
}

fdf := de.(*ExfatFileDirectoryEntry)

fdfExpected := index.FindIndexedFileFileDirectoryEntry("2-delahaye-type-165-cabriolet-dsc_8025.jpg")

if fdf != fdfExpected {
t.Fatalf("Entry not found.")
}
}

func TestDirectoryEntryIndex_FindIndexedFileDirectoryEntry__MissOnFile(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

de := index.FindIndexedFileDirectoryEntry("invalid-file", "File", 0)
if de != nil {
t.Fatalf("Expected lookup miss.")
}
}

func TestDirectoryEntryIndex_FindIndexedFileDirectoryEntry__MissOnType(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

de := index.FindIndexedFileDirectoryEntry("2-delahaye-type-165-cabriolet-dsc_8025.jpg", "InvalidType", 0)
if de != nil {
t.Fatalf("Expected lookup miss.")
}
}

func TestDirectoryEntryIndex_FindIndexedFileDirectoryEntry__MissOnIndex(t *testing.T) {
f, er := getTestFileAndParser()

defer f.Close()

err := er.Parse()
log.PanicIf(err)

firstClusterNumber := er.FirstClusterOfRootDirectory()
en := NewExfatNavigator(er, firstClusterNumber)

index, _, _, err := en.IndexDirectoryEntries()
log.PanicIf(err)

de := index.FindIndexedFileDirectoryEntry("2-delahaye-type-165-cabriolet-dsc_8025.jpg", "FileName", 4)
if de != nil {
t.Fatalf("Expected lookup miss.")
}
}
6 changes: 3 additions & 3 deletions structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (er *ExfatReader) readExtendedBootSector(sectorSize uint32) (extendedBootCo
log.PanicIf(err)

if extendedBootSignature != requiredExtendedBootSignature {
panic(fmt.Errorf("extended boot-signature not correct: %x", extendedBootSignature))
log.Panicf("extended boot-signature not correct: %x", extendedBootSignature)
}

return extendedBootCode, nil
Expand Down Expand Up @@ -880,9 +880,9 @@ func (er *ExfatReader) SectorsPerCluster() uint32 {
return er.bootRegion.bsh.SectorsPerCluster()
}

// ActiveBootRegion returns the active boot-sector struct (whether main or
// ActiveBootSectorHeader returns the active boot-sector struct (whether main or
// backup).
func (er *ExfatReader) ActiveBootRegion() BootSectorHeader {
func (er *ExfatReader) ActiveBootSectorHeader() BootSectorHeader {

// TODO(dustin): !! Add test.

Expand Down

0 comments on commit 9e57a61

Please sign in to comment.