github.com/aquanetwork/aquachain@v1.7.8/aqua/misc_test.go (about)

     1  package aqua
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  func TestDecodeExtra(t *testing.T) {
    10  	b := makeExtraData(nil)
    11  	fmt.Printf("extra: %s\n", gohex(b))
    12  	version, _, extra, err := DecodeExtraData(makeExtraData(nil))
    13  	if err != nil {
    14  		fmt.Println("got err:", err)
    15  		t.Fail()
    16  	}
    17  	fmt.Println("version:", version)
    18  	fmt.Println("extra:", string(extra[6:]))
    19  }
    20  
    21  func TestDecodeExtra2(t *testing.T) {
    22  	var (
    23  		wantVersion = [3]uint8{1, 7, 7}
    24  		b           = []byte{0xd4, 0x83, 0x1, 0x7, 0x7, 0x89, 0x61, 0x71, 0x75, 0x61, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x85, 0x6c, 0x69, 0x6e, 0x75, 0x7}
    25  	)
    26  	version, osname, extra, err := DecodeExtraData(b)
    27  	if err != nil {
    28  		t.Log("err non-nil", err)
    29  		t.FailNow()
    30  	}
    31  	fmt.Println("Detected OS:", osname)
    32  	if version[1] != wantVersion[1] {
    33  		t.Log("version mismatch:", version, "wanted:", wantVersion)
    34  		t.Fail()
    35  	}
    36  	if 0 != bytes.Compare(extra, b) {
    37  		t.Log("extra mismatch:", gohex(extra), "wanted:", gohex(b))
    38  		t.Fail()
    39  	}
    40  
    41  	fmt.Println("extra:", string(b[6:]))
    42  
    43  }
    44  
    45  func gohex(b []byte) (s string) {
    46  	if len(b) == 0 {
    47  		return "nil"
    48  	}
    49  	for i := range b {
    50  		if len(b)-1 == i {
    51  			s += fmt.Sprintf("0x%x", b[i])
    52  			break
    53  		}
    54  		s += fmt.Sprintf("0x%x, ", b[i])
    55  	}
    56  	return s
    57  }