github.com/primecitizens/pcz/std@v0.2.1/core/abi/abi_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2021 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package abi_test
     9  
    10  import (
    11  	"os/exec"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/primecitizens/pcz/std/core/abi"
    17  	"github.com/primecitizens/pcz/std/core/testenv"
    18  )
    19  
    20  func TestFuncPC(t *testing.T) {
    21  	// Test that FuncPC* can get correct function PC.
    22  	pcFromAsm := abi.FuncPCTestFnAddr
    23  
    24  	// Test FuncPC for locally defined function
    25  	pcFromGo := abi.FuncPCTest()
    26  	if pcFromGo != pcFromAsm {
    27  		t.Errorf("FuncPC returns wrong PC, want %x, got %x", pcFromAsm, pcFromGo)
    28  	}
    29  
    30  	// Test FuncPC for imported function
    31  	pcFromGo = abi.FuncPCABI0(abi.FuncPCTestFn)
    32  	if pcFromGo != pcFromAsm {
    33  		t.Errorf("FuncPC returns wrong PC, want %x, got %x", pcFromAsm, pcFromGo)
    34  	}
    35  }
    36  
    37  func TestFuncPCCompileError(t *testing.T) {
    38  	// Test that FuncPC* on a function of a mismatched ABI is rejected.
    39  	testenv.MustHaveGoBuild(t)
    40  
    41  	// We want to test internal package, which we cannot normally import.
    42  	// Run the assembler and compiler manually.
    43  	tmpdir := t.TempDir()
    44  	asmSrc := filepath.Join("testdata", "x.s")
    45  	goSrc := filepath.Join("testdata", "x.go")
    46  	symabi := filepath.Join(tmpdir, "symabi")
    47  	obj := filepath.Join(tmpdir, "x.o")
    48  
    49  	importcfgfile := filepath.Join(tmpdir, "hello.importcfg")
    50  	testenv.WriteImportcfg(t, importcfgfile, nil)
    51  
    52  	// parse assembly code for symabi.
    53  	cmd := exec.Command(testenv.GoToolPath(t), "tool", "asm", "-gensymabis", "-o", symabi, asmSrc)
    54  	out, err := cmd.CombinedOutput()
    55  	if err != nil {
    56  		t.Fatalf("go tool asm -gensymabis failed: %v\n%s", err, out)
    57  	}
    58  
    59  	// compile go code.
    60  	cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-symabis", symabi, "-o", obj, goSrc)
    61  	out, err = cmd.CombinedOutput()
    62  	if err == nil {
    63  		t.Fatalf("go tool compile did not fail")
    64  	}
    65  
    66  	// Expect errors in line 17, 18, 20, no errors on other lines.
    67  	want := []string{"x.go:17", "x.go:18", "x.go:20"}
    68  	got := strings.Split(string(out), "\n")
    69  	if got[len(got)-1] == "" {
    70  		got = got[:len(got)-1] // remove last empty line
    71  	}
    72  	for i, s := range got {
    73  		if !strings.Contains(s, want[i]) {
    74  			t.Errorf("did not error on line %s", want[i])
    75  		}
    76  	}
    77  	if len(got) != len(want) {
    78  		t.Errorf("unexpected number of errors, want %d, got %d", len(want), len(got))
    79  	}
    80  	if t.Failed() {
    81  		t.Logf("output:\n%s", string(out))
    82  	}
    83  }