diff --git a/Makefile b/Makefile index f64adc4..f2a12fc 100644 --- a/Makefile +++ b/Makefile @@ -30,4 +30,4 @@ lint: golint ./shared/ ./tests/ ./cmd/... clean: - rm -f data/hello data/hello.bc [td]*/.helloworld.c.o [td]*/.helloworld.c.o.bc + rm -f data/*hello data/*.bc [td]*/.*.c.o [td]*/*.o [td]*/.*.c.o.bc data/*.notanextensionthatwerecognize diff --git a/shared/filetypes.go b/shared/filetypes.go index 63e582e..ef9167e 100644 --- a/shared/filetypes.go +++ b/shared/filetypes.go @@ -141,12 +141,15 @@ func machoType2BinaryType(mt macho.Type) (bt BinaryType) { // IsPlainFile returns true if the file is stat-able (i.e. exists etc), and is not a directory, else it returns false. func IsPlainFile(objectFile string) (ok bool) { info, err := os.Stat(objectFile) - if os.IsNotExist(err) || info.IsDir() { + if os.IsNotExist(err) { return } if err != nil { return } + if info.IsDir() { + return + } ok = true return } diff --git a/tests/entry_test.go b/tests/entry_test.go index 41cf5e1..0abd9c3 100644 --- a/tests/entry_test.go +++ b/tests/entry_test.go @@ -7,19 +7,23 @@ import ( "testing" ) +const ( + DEBUG bool = false +) + func Test_basic_functionality(t *testing.T) { args := []string{"../data/helloworld.c", "-o", "../data/hello"} exitCode := shared.Compile(args, "clang") if exitCode != 0 { t.Errorf("Compile of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Compiled OK") } args = []string{"get-bc", "-v", "../data/hello"} exitCode = shared.Extract(args) if exitCode != 0 { t.Errorf("Extraction of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Extraction OK") } } @@ -30,27 +34,27 @@ func Test_more_functionality(t *testing.T) { exitCode := shared.Compile(args, "clang") if exitCode != 0 { t.Errorf("Compile of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Compiled OK") } ok, err := shared.IsObjectFileForOS(objectFile, runtime.GOOS) if !ok { t.Errorf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", objectFile, runtime.GOOS, ok, err) - } else { + } else if DEBUG { fmt.Printf("isObjectFileForOS(%v, %v) = %v\n", objectFile, runtime.GOOS, ok) } args = []string{objectFile, "-o", "../data/bhello"} exitCode = shared.Compile(args, "clang") if exitCode != 0 { t.Errorf("Compile of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Compiled OK") } args = []string{"get-bc", "-v", "../data/bhello"} exitCode = shared.Extract(args) if exitCode != 0 { t.Errorf("Extraction of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Extraction OK") } } @@ -64,39 +68,39 @@ func Test_obscure_functionality(t *testing.T) { exitCode := shared.Compile(args, "clang") if exitCode != 0 { t.Errorf("Compile of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Compiled OK") } ok, err := shared.IsObjectFileForOS(sourceFile, opSys) if ok { t.Errorf("isObjectFileForOS(%v, %v) = %v\n", sourceFile, opSys, ok) - } else { + } else if DEBUG { fmt.Printf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", sourceFile, opSys, ok, err) } ok, err = shared.IsObjectFileForOS(objectFile, opSys) if !ok { t.Errorf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", objectFile, opSys, ok, err) - } else { + } else if DEBUG { fmt.Printf("isObjectFileForOS(%v, %v) = %v\n", objectFile, opSys, ok) } args = []string{objectFile, "-o", exeFile} exitCode = shared.Compile(args, "clang") if exitCode != 0 { t.Errorf("Compile of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Compiled OK") } ok, err = shared.IsObjectFileForOS(exeFile, opSys) if ok { t.Errorf("isObjectFileForOS(%v, %v) = %v\n", exeFile, opSys, ok) - } else { + } else if DEBUG { fmt.Printf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", exeFile, opSys, ok, err) } args = []string{"get-bc", "-v", exeFile} exitCode = shared.Extract(args) if exitCode != 0 { t.Errorf("Extraction of %v returned %v\n", args, exitCode) - } else { + } else if DEBUG { fmt.Println("Extraction OK") } } @@ -107,40 +111,79 @@ func Test_file_type(t *testing.T) { sourceFile := "../data/helloworld.c" objectFile := "../data/bhello.notanextensionthatwerecognize" exeFile := "../data/bhello" - fmt.Printf("GetBinaryType(%v) = %v\n", fictionalFile, shared.GetBinaryType(fictionalFile)) - fmt.Printf("GetBinaryType(%v) = %v\n", dataDir, shared.GetBinaryType(dataDir)) - fmt.Printf("GetBinaryType(%v) = %v\n", sourceFile, shared.GetBinaryType(sourceFile)) - fmt.Printf("GetBinaryType(%v) = %v\n", objectFile, shared.GetBinaryType(objectFile)) - fmt.Printf("GetBinaryType(%v) = %v\n", exeFile, shared.GetBinaryType(exeFile)) - plain := shared.IsPlainFile(fictionalFile) + var binaryFileType shared.BinaryType + binaryFileType = shared.GetBinaryType(fictionalFile) + + if binaryFileType != shared.BinaryUnknown { + t.Errorf("GetBinaryType(%v) = %v\n", fictionalFile, binaryFileType) + } else if DEBUG { + fmt.Printf("GetBinaryType(%v) = %v\n", fictionalFile, binaryFileType) + } + + binaryFileType = shared.GetBinaryType(dataDir) + + if binaryFileType != shared.BinaryUnknown { + t.Errorf("GetBinaryType(%v) = %v\n", dataDir, binaryFileType) + } else if DEBUG { + fmt.Printf("GetBinaryType(%v) = %v\n", dataDir, binaryFileType) + } + + binaryFileType = shared.GetBinaryType(sourceFile) + if binaryFileType != shared.BinaryUnknown { + t.Errorf("GetBinaryType(%v) = %v\n", sourceFile, binaryFileType) + } else if DEBUG { + fmt.Printf("GetBinaryType(%v) = %v\n", sourceFile, binaryFileType) + } + + binaryFileType = shared.GetBinaryType(objectFile) + if binaryFileType != shared.BinaryObject { + t.Errorf("GetBinaryType(%v) = %v\n", objectFile, binaryFileType) + } else if DEBUG { + fmt.Printf("GetBinaryType(%v) = %v\n", objectFile, binaryFileType) + } + + binaryFileType = shared.GetBinaryType(exeFile) + if binaryFileType != shared.BinaryExecutable { + t.Errorf("GetBinaryType(%v) = %v\n", exeFile, binaryFileType) + } else if DEBUG { + fmt.Printf("GetBinaryType(%v) = %v\n", exeFile, binaryFileType) + } + + var plain bool + plain = shared.IsPlainFile(fictionalFile) + if plain { t.Errorf("shared.IsPlainFile(%v) returned %v\n", fictionalFile, plain) - } else { + } else if DEBUG { fmt.Printf("shared.IsPlainFile(%v) returned %v\n", fictionalFile, plain) } + plain = shared.IsPlainFile(dataDir) if plain { t.Errorf("shared.IsPlainFile(%v) returned %v\n", dataDir, plain) - } else { + } else if DEBUG { fmt.Printf("shared.IsPlainFile(%v) returned %v\n", dataDir, plain) } + plain = shared.IsPlainFile(sourceFile) if !plain { t.Errorf("shared.IsPlainFile(%v) returned %v\n", sourceFile, plain) - } else { + } else if DEBUG { fmt.Printf("shared.IsPlainFile(%v) returned %v\n", sourceFile, plain) } + plain = shared.IsPlainFile(objectFile) if !plain { t.Errorf("shared.IsPlainFile(%v) returned %v\n", objectFile, plain) - } else { + } else if DEBUG { fmt.Printf("shared.IsPlainFile(%v) returned %v\n", objectFile, plain) } + plain = shared.IsPlainFile(exeFile) if !plain { t.Errorf("shared.IsPlainFile(%v) returned %v\n", exeFile, plain) - } else { + } else if DEBUG { fmt.Printf("shared.IsPlainFile(%v) returned %v\n", exeFile, plain) }