123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package tool
- import (
- "bytes"
- "flag"
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "devt.de/krotik/common/errorutil"
- "devt.de/krotik/common/fileutil"
- "devt.de/krotik/common/stringutil"
- )
- const packTestDir = "packtest"
- var testPackOut *bytes.Buffer
- var lastReturnCode = 0
- var lastRuntimeError error
- func setupPackTestDir() {
- if res, _ := fileutil.PathExists(packTestDir); res {
- os.RemoveAll(packTestDir)
- }
- err := os.Mkdir(packTestDir, 0770)
- if err != nil {
- fmt.Print("Could not create test directory:", err.Error())
- os.Exit(1)
- }
- err = os.Mkdir(filepath.Join(packTestDir, "sub"), 0770)
- if err != nil {
- fmt.Print("Could not create test directory:", err.Error())
- os.Exit(1)
- }
- osExit = func(code int) {
- lastReturnCode = code
- }
- handleError = func(err error) {
- lastRuntimeError = err
- }
- }
- func tearDownPackTestDir() {
- err := os.RemoveAll(packTestDir)
- if err != nil {
- fmt.Print("Could not remove test directory:", err.Error())
- }
- }
- func newTestCLIPacker() *CLIPacker {
- clip := NewCLIPacker()
- testPackOut = &bytes.Buffer{}
- clip.LogOut = testPackOut
- return clip
- }
- func TestPackParseArgs(t *testing.T) {
- setupPackTestDir()
- defer tearDownPackTestDir()
- clip := newTestCLIPacker()
- packTestSrcBin := filepath.Join(packTestDir, "source.bin")
- out := bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestSrcBin, "foo", "-help"}
- if ok := clip.ParseArgs(); !ok {
- t.Error("Asking for help should ask to finish the program")
- return
- }
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestSrcBin, "foo", "-help"}
- if err := clip.Pack(); err != nil {
- t.Error("Unexpected result:", err)
- return
- }
- if !strings.Contains(out.String(), "Root directory for ECAL interpreter") {
- t.Error("Unexpected output:", out.String())
- return
- }
- out = bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestSrcBin, "foo", "myentryfile"}
- if ok := clip.ParseArgs(); ok {
- t.Error("Only asking for help should finish the program")
- return
- }
- if ok := clip.ParseArgs(); ok {
- t.Error("Only asking for help should finish the program")
- return
- }
- if clip.EntryFile != "myentryfile" {
- t.Error("Unexpected output:", clip.EntryFile)
- return
- }
- }
- func TestPackPacking(t *testing.T) {
- setupPackTestDir()
- defer tearDownPackTestDir()
- clip := newTestCLIPacker()
- packTestSrcBin := filepath.Join(packTestDir, "source.bin")
- packTestEntry := filepath.Join(packTestDir, "myentry.ecal")
- packAnotherFile := filepath.Join(packTestDir, "sub", "anotherfile.ecal")
- packTestDestBin := filepath.Join(packTestDir, "dest.exe")
- b1 = 5
- b2 = len(packmarker) + 11
- err := ioutil.WriteFile(packTestSrcBin, []byte("mybinaryfilecontent#somemorecontent"+
- stringutil.GenerateRollingString("123", 30)), 0777)
- errorutil.AssertOk(err)
- err = ioutil.WriteFile(packTestEntry, []byte("myvar := 1; 5"), 0777)
- errorutil.AssertOk(err)
- err = ioutil.WriteFile(packAnotherFile, []byte("func f() { raise(123) };f()"), 0777)
- errorutil.AssertOk(err)
- out := bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- // Write a binary with return code
- osArgs = []string{packTestSrcBin, "foo", "-dir", packTestDir, "-target",
- packTestDestBin, packTestEntry}
- // Simulate that whitespaces are added around the pack marker
- oldpackmarker := packmarker
- packmarker = fmt.Sprintf("\n\n\n%v\n\n\n", packmarker)
- if err := clip.Pack(); err != nil {
- t.Error("Unexpected result:", err)
- return
- }
- packmarker = oldpackmarker
- if !strings.Contains(testPackOut.String(), "bytes for intro") {
- t.Error("Unexpected output:", testPackOut.String())
- return
- }
- // Write a binary with which errors
- clip = newTestCLIPacker()
- out = bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestSrcBin, "foo", "-dir", packTestDir, "-target",
- packTestDestBin + ".error", packAnotherFile}
- if err := clip.Pack(); err != nil {
- t.Error("Unexpected result:", err)
- return
- }
- if !strings.Contains(testPackOut.String(), "bytes for intro") {
- t.Error("Unexpected output:", testPackOut.String())
- return
- }
- // Write also a corrupted binary
- err = ioutil.WriteFile(packTestDestBin+".corrupted", []byte(
- "mybinaryfilecontent#somemorecontent"+
- stringutil.GenerateRollingString("123", 30)+
- "\n"+
- packmarker+
- "\n"+
- stringutil.GenerateRollingString("123", 30)), 0777)
- errorutil.AssertOk(err)
- testRunningPackedBinary(t)
- }
- func testRunningPackedBinary(t *testing.T) {
- packTestDestBin := filepath.Join(packTestDir, "dest") // Suffix .exe should be appended
- out := bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestDestBin + ".exe.corrupted"}
- RunPackedBinary()
- if lastRuntimeError == nil || lastRuntimeError.Error() != "zip: not a valid zip file" {
- t.Error("Unexpected result:", lastRuntimeError)
- return
- }
- out = bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osArgs = []string{packTestDestBin}
- RunPackedBinary()
- if lastRuntimeError != nil {
- t.Error("Unexpected result:", lastRuntimeError)
- return
- }
- if lastReturnCode != 5 {
- t.Error("Unexpected result:", lastReturnCode)
- return
- }
- out = bytes.Buffer{}
- flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError) // Reset CLI parsing
- flag.CommandLine.SetOutput(&out)
- osStderr = &out
- osArgs = []string{packTestDestBin + ".exe.error"}
- RunPackedBinary()
- if lastRuntimeError != nil {
- t.Error("Unexpected result:", lastRuntimeError)
- return
- }
- if !strings.HasPrefix(out.String(), "ECAL error in packtest/dest.exe.error") ||
- !strings.Contains(out.String(), "raise(123)") {
- t.Error("Unexpected result:", out.String())
- return
- }
- }
|