gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/aqua/misc_test.go (about)

     1  // Copyright 2018,2022 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package aqua
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestDecodeExtra(t *testing.T) {
    27  	b := makeExtraData(nil)
    28  	t.Logf("extra: %s\n", gohex(b)) // print if -test.v flag
    29  	version, o, extra, err := DecodeExtraData(b)
    30  	if err != nil {
    31  		t.Log("got err:", err)
    32  		t.Fail()
    33  	}
    34  	t.Log("version:", version)
    35  	t.Log("OS:", o)
    36  	t.Log("raw extra string:", string(extra[6:]))
    37  	t.Logf("raw first6: %#02x", extra[:6])
    38  }
    39  
    40  func TestDecodeExtra2(t *testing.T) {
    41  	var (
    42  		wants = [][3]uint8{
    43  			{1, 7, 12},
    44  			{1, 7, 7},
    45  			{1, 7, 12},
    46  		}
    47  
    48  		bufs = [][]byte{
    49  			{0xd9, 0x83, 0x01, 0x07, 0x0c, 0x84, 0x61, 0x71,
    50  				0x75, 0x61, 0x85, 0x6c, 0x69, 0x6e, 0x75, 0x78,
    51  				0x89, 0x67, 0x6f, 0x64, 0x65, 0x76, 0x31, 0x2e, 0x32, 0x30},
    52  			{0xd4, 0x83, 0x1, 0x7, 0x7, 0x89, 0x61, 0x71, 0x75,
    53  				0x61, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x85, 0x6c, 0x69,
    54  				0x6e, 0x75, 0x7},
    55  			{0xd6, 0x83, 0x01, 0x07, 0x0c, 0x84, 0x61, 0x71,
    56  				0x75, 0x61, 0x85, 0x6c, 0x69, 0x6e, 0x75, 0x78,
    57  				0x86, 0x67, 0x6f, 0x31, 0x2e, 0x32, 0x30}}
    58  	)
    59  	for i := 0; i < len(bufs); i++ {
    60  		var b []byte = bufs[i]
    61  		var wantVersion [3]uint8 = wants[i]
    62  		version, osname, extra, err := DecodeExtraData(b)
    63  		if err != nil {
    64  			t.Log("err non-nil", err)
    65  			t.FailNow()
    66  		}
    67  		t.Log("Detected OS:", osname)
    68  		for i := 0; i < 3; i++ {
    69  
    70  			if version[i] != wantVersion[i] {
    71  				t.Log("version mismatch digit", i, ":", version, "wanted:", wantVersion)
    72  				t.Fail()
    73  			}
    74  		}
    75  		t.Log("Detected Version:", version[0], version[1], version[2])
    76  		if 0 != bytes.Compare(extra, b) {
    77  			t.Log("extra mismatch:", gohex(extra), "wanted:", gohex(b))
    78  			t.Fail()
    79  		}
    80  		t.Log("extra:", string(b[6:]))
    81  	}
    82  
    83  }
    84  
    85  func gohex(b []byte) string {
    86  	return strings.Replace(fmt.Sprintf("%# 02x", b), " ", ", ", -1)
    87  }