github.com/zooyer/miskit@v1.0.71/test/test_test.go (about)

     1  /**
     2   * @Author: zzy
     3   * @Email: zhangzhongyuan@didiglobal.com
     4   * @Description:
     5   * @File: test_test.go
     6   * @Package: test
     7   * @Version: 1.0.0
     8   * @Date: 2021/12/29 4:47 下午
     9   */
    10  
    11  package test
    12  
    13  import (
    14  	"fmt"
    15  	"github.com/brahma-adshonor/gohook"
    16  	"reflect"
    17  	"strconv"
    18  	"syscall"
    19  	"testing"
    20  	"unsafe"
    21  )
    22  
    23  func TestTTT(t *testing.T) {
    24  	var f = 1.00
    25  	addr := uintptr(unsafe.Pointer(&f))
    26  
    27  	var i int64
    28  
    29  	i = *(*int64)(unsafe.Pointer(addr))
    30  
    31  	str := fmt.Sprintf("%064b", i)
    32  
    33  	fmt.Println(str[:12])
    34  	fmt.Println(str[12:])
    35  
    36  	i3 := fmt.Sprintf("%064b", int64(3))
    37  	fmt.Println(i3)
    38  }
    39  
    40  //go:linkname goexit1 runtime.goexit1
    41  func goexit1()
    42  
    43  //go:linkname runtime_beforeExit runtime.runtime_beforeExit
    44  func runtime_beforeExit()
    45  
    46  //go:linkname syscallExit syscall.Exit
    47  func syscallExit()
    48  
    49  //go:linkname exit runtime.exit
    50  func exit(code int32)
    51  
    52  //go:linkname main runtime.main
    53  func main()
    54  
    55  // ago:linkname os_beforeExit runtime.os_beforeExit
    56  //go:linkname os_beforeExit os.runtime_beforeExit
    57  func os_beforeExit()
    58  
    59  func abc() {
    60  	fmt.Println("aaa exit...")
    61  }
    62  
    63  //go:noinline
    64  func aaa() {
    65  	fmt.Println("aaa...")
    66  }
    67  
    68  //go:noinline
    69  func ccc() {
    70  	fmt.Println("ccc...")
    71  }
    72  
    73  //go:noinline
    74  func bbb() {
    75  	fmt.Println("bbb...")
    76  }
    77  
    78  func TestCeil2(t *testing.T) {
    79  
    80  	var a = 3141592653.58979323846264338327950288419716939937510582097494459
    81  	aa := strconv.FormatFloat(a, 'f', 60, 64)
    82  	fmt.Println(aa)
    83  
    84  	var f1 interface{} = syscallExit
    85  	var f2 interface{} = syscall.Exit
    86  
    87  	fmt.Println("f1:", f1)
    88  	fmt.Println("f2:", f2)
    89  
    90  	if reflect.DeepEqual(f1, f2) {
    91  		fmt.Println("f1 == f2")
    92  	} else {
    93  		fmt.Println("f1 != f2")
    94  	}
    95  
    96  	if err := gohook.Hook(syscallExit, abc, syscallExit); err != nil {
    97  		panic(err)
    98  	}
    99  
   100  	return
   101  }
   102  
   103  //go:noinline
   104  func beforeExit() {
   105  	fmt.Println("666 before exit...")
   106  }
   107  
   108  func hook(a, b, c interface{}) {
   109  	if err := gohook.Hook(a, b, c); err != nil {
   110  		panic(err)
   111  	}
   112  }
   113  
   114  func TestHook(t *testing.T) {
   115  	hook(aaa, bbb, ccc)
   116  	aaa()
   117  	bbb()
   118  	ccc()
   119  }
   120  
   121  func TestHookExit(t *testing.T) {
   122  	hook(os_beforeExit, beforeExit, nil)
   123  }