2019-10-20 22:42:26 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2019-10-22 19:53:26 +02:00
|
|
|
"fmt"
|
2019-10-20 22:42:26 +02:00
|
|
|
"github.com/SRI-CSL/gllvm/shared"
|
2020-10-30 23:04:23 +01:00
|
|
|
"runtime"
|
2020-10-31 00:04:58 +01:00
|
|
|
"testing"
|
2019-10-20 22:42:26 +02:00
|
|
|
)
|
|
|
|
|
2020-11-02 03:55:14 +01:00
|
|
|
const (
|
|
|
|
DEBUG bool = false
|
|
|
|
)
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func Test_basic_functionality(t *testing.T) {
|
2019-10-22 19:53:26 +02:00
|
|
|
args := []string{"../data/helloworld.c", "-o", "../data/hello"}
|
2019-10-20 22:42:26 +02:00
|
|
|
exitCode := shared.Compile(args, "clang")
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Compile of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2019-10-20 22:42:26 +02:00
|
|
|
fmt.Println("Compiled OK")
|
|
|
|
}
|
2019-10-25 22:22:52 +02:00
|
|
|
args = []string{"get-bc", "-v", "../data/hello"}
|
|
|
|
exitCode = shared.Extract(args)
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Extraction of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2019-10-25 22:22:52 +02:00
|
|
|
fmt.Println("Extraction OK")
|
|
|
|
}
|
2020-10-30 23:04:23 +01:00
|
|
|
}
|
2019-10-26 02:37:23 +02:00
|
|
|
|
2020-10-30 23:04:23 +01:00
|
|
|
func Test_more_functionality(t *testing.T) {
|
|
|
|
objectFile := "../data/bhello.o"
|
|
|
|
args := []string{"../data/helloworld.c", "-c", "-o", objectFile}
|
|
|
|
exitCode := shared.Compile(args, "clang")
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Compile of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:04:23 +01:00
|
|
|
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)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:04:23 +01:00
|
|
|
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)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:04:23 +01:00
|
|
|
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)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:04:23 +01:00
|
|
|
fmt.Println("Extraction OK")
|
|
|
|
}
|
2019-10-20 22:42:26 +02:00
|
|
|
}
|
2020-10-30 23:11:29 +01:00
|
|
|
|
|
|
|
func Test_obscure_functionality(t *testing.T) {
|
2020-10-31 00:53:00 +01:00
|
|
|
sourceFile := "../data/helloworld.c"
|
2020-10-30 23:11:29 +01:00
|
|
|
objectFile := "../data/bhello.notanextensionthatwerecognize"
|
2020-10-31 00:53:00 +01:00
|
|
|
exeFile := "../data/bhello"
|
|
|
|
opSys := runtime.GOOS
|
|
|
|
args := []string{sourceFile, "-c", "-o", objectFile}
|
2020-10-30 23:11:29 +01:00
|
|
|
exitCode := shared.Compile(args, "clang")
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Compile of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:11:29 +01:00
|
|
|
fmt.Println("Compiled OK")
|
|
|
|
}
|
2020-10-31 00:53:00 +01:00
|
|
|
ok, err := shared.IsObjectFileForOS(sourceFile, opSys)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("isObjectFileForOS(%v, %v) = %v\n", sourceFile, opSys, ok)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-31 00:53:00 +01:00
|
|
|
fmt.Printf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", sourceFile, opSys, ok, err)
|
|
|
|
}
|
|
|
|
ok, err = shared.IsObjectFileForOS(objectFile, opSys)
|
2020-10-30 23:11:29 +01:00
|
|
|
if !ok {
|
2020-10-31 00:53:00 +01:00
|
|
|
t.Errorf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", objectFile, opSys, ok, err)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-31 00:53:00 +01:00
|
|
|
fmt.Printf("isObjectFileForOS(%v, %v) = %v\n", objectFile, opSys, ok)
|
2020-10-30 23:11:29 +01:00
|
|
|
}
|
2020-10-31 00:53:00 +01:00
|
|
|
args = []string{objectFile, "-o", exeFile}
|
2020-10-30 23:11:29 +01:00
|
|
|
exitCode = shared.Compile(args, "clang")
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Compile of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:11:29 +01:00
|
|
|
fmt.Println("Compiled OK")
|
|
|
|
}
|
2020-10-31 00:53:00 +01:00
|
|
|
ok, err = shared.IsObjectFileForOS(exeFile, opSys)
|
|
|
|
if ok {
|
|
|
|
t.Errorf("isObjectFileForOS(%v, %v) = %v\n", exeFile, opSys, ok)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-31 00:53:00 +01:00
|
|
|
fmt.Printf("isObjectFileForOS(%v, %v) = %v (err = %v)\n", exeFile, opSys, ok, err)
|
|
|
|
}
|
|
|
|
args = []string{"get-bc", "-v", exeFile}
|
2020-10-30 23:11:29 +01:00
|
|
|
exitCode = shared.Extract(args)
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Errorf("Extraction of %v returned %v\n", args, exitCode)
|
2020-11-02 03:55:14 +01:00
|
|
|
} else if DEBUG {
|
2020-10-30 23:11:29 +01:00
|
|
|
fmt.Println("Extraction OK")
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 19:49:22 +01:00
|
|
|
|
2022-09-07 23:46:09 +02:00
|
|
|
func BinaryFile(t *testing.T, fictionalFile string, dataDir string, sourceFile string, objectFile string, exeFile string) {
|
2020-11-02 03:55:14 +01:00
|
|
|
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)
|
|
|
|
}
|
2022-09-07 23:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func PlainFile(t *testing.T, fictionalFile string, dataDir string, sourceFile string, objectFile string, exeFile string) {
|
|
|
|
var plain bool
|
|
|
|
plain = shared.IsPlainFile(fictionalFile)
|
|
|
|
|
|
|
|
if plain {
|
|
|
|
t.Errorf("shared.IsPlainFile(%v) returned %v\n", fictionalFile, plain)
|
|
|
|
} 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 if DEBUG {
|
|
|
|
fmt.Printf("shared.IsPlainFile(%v) returned %v\n", dataDir, plain)
|
|
|
|
}
|
2020-11-01 19:49:22 +01:00
|
|
|
|
2022-09-07 23:42:53 +02:00
|
|
|
plain = shared.IsPlainFile(sourceFile)
|
|
|
|
if !plain {
|
|
|
|
t.Errorf("shared.IsPlainFile(%v) returned %v\n", sourceFile, plain)
|
|
|
|
} 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 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 if DEBUG {
|
|
|
|
fmt.Printf("shared.IsPlainFile(%v) returned %v\n", exeFile, plain)
|
|
|
|
}
|
2020-11-01 19:49:22 +01:00
|
|
|
}
|
2022-09-07 23:48:50 +02:00
|
|
|
|
|
|
|
func Test_file_type(t *testing.T) {
|
|
|
|
fictionalFile := "HopefullyThereIsNotAFileCalledThisNearBy.txt"
|
|
|
|
dataDir := "../data"
|
|
|
|
sourceFile := "../data/helloworld.c"
|
|
|
|
objectFile := "../data/bhello.notanextensionthatwerecognize"
|
|
|
|
exeFile := "../data/bhello"
|
|
|
|
|
|
|
|
BinaryFile(t, fictionalFile, dataDir, sourceFile, objectFile, exeFile)
|
|
|
|
PlainFile(t, fictionalFile, dataDir, sourceFile, objectFile, exeFile)
|
|
|
|
|
|
|
|
}
|
|
|
|
|