github.com/fisco-bcos/crypto@v0.0.0-20200202032121-bd8ab0b5d4f1/internal/trace/parser_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package trace
     6  
     7  import (
     8  	"bytes"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestCorruptedInputs(t *testing.T) {
    17  	// These inputs crashed parser previously.
    18  	tests := []string{
    19  		"gotrace\x00\x020",
    20  		"gotrace\x00Q00\x020",
    21  		"gotrace\x00T00\x020",
    22  		"gotrace\x00\xc3\x0200",
    23  		"go 1.5 trace\x00\x00\x00\x00\x020",
    24  		"go 1.5 trace\x00\x00\x00\x00Q00\x020",
    25  		"go 1.5 trace\x00\x00\x00\x00T00\x020",
    26  		"go 1.5 trace\x00\x00\x00\x00\xc3\x0200",
    27  	}
    28  	for _, data := range tests {
    29  		res, err := Parse(strings.NewReader(data), "")
    30  		if err == nil || res.Events != nil || res.Stacks != nil {
    31  			t.Fatalf("no error on input: %q", data)
    32  		}
    33  	}
    34  }
    35  
    36  func TestParseCanned(t *testing.T) {
    37  	files, err := ioutil.ReadDir("./testdata")
    38  	if err != nil {
    39  		t.Fatalf("failed to read ./testdata: %v", err)
    40  	}
    41  	for _, f := range files {
    42  		name := filepath.Join("./testdata", f.Name())
    43  		info, err := os.Stat(name)
    44  		if err != nil {
    45  			t.Fatal(err)
    46  		}
    47  		if testing.Short() && info.Size() > 10000 {
    48  			continue
    49  		}
    50  		data, err := ioutil.ReadFile(name)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  		// Instead of Parse that requires a proper binary name for old traces,
    55  		// we use 'parse' that omits symbol lookup if an empty string is given.
    56  		_, _, err = parse(bytes.NewReader(data), "")
    57  		switch {
    58  		case strings.HasSuffix(f.Name(), "_good"):
    59  			if err != nil {
    60  				t.Errorf("failed to parse good trace %v: %v", f.Name(), err)
    61  			}
    62  		case strings.HasSuffix(f.Name(), "_unordered"):
    63  			if err != ErrTimeOrder {
    64  				t.Errorf("unordered trace is not detected %v: %v", f.Name(), err)
    65  			}
    66  		default:
    67  			t.Errorf("unknown input file suffix: %v", f.Name())
    68  		}
    69  	}
    70  }
    71  
    72  func TestParseVersion(t *testing.T) {
    73  	tests := map[string]int{
    74  		"go 1.5 trace\x00\x00\x00\x00": 1005,
    75  		"go 1.7 trace\x00\x00\x00\x00": 1007,
    76  		"go 1.10 trace\x00\x00\x00":    1010,
    77  		"go 1.25 trace\x00\x00\x00":    1025,
    78  		"go 1.234 trace\x00\x00":       1234,
    79  		"go 1.2345 trace\x00":          -1,
    80  		"go 0.0 trace\x00\x00\x00\x00": -1,
    81  		"go a.b trace\x00\x00\x00\x00": -1,
    82  	}
    83  	for header, ver := range tests {
    84  		ver1, err := parseHeader([]byte(header))
    85  		if ver == -1 {
    86  			if err == nil {
    87  				t.Fatalf("no error on input: %q, version %v", header, ver1)
    88  			}
    89  		} else {
    90  			if err != nil {
    91  				t.Fatalf("failed to parse: %q (%v)", header, err)
    92  			}
    93  			if ver != ver1 {
    94  				t.Fatalf("wrong version: %v, want %v, input: %q", ver1, ver, header)
    95  			}
    96  		}
    97  	}
    98  }
    99  
   100  func TestTimestampOverflow(t *testing.T) {
   101  	// Test that parser correctly handles large timestamps (long tracing).
   102  	w := NewWriter()
   103  	w.Emit(EvBatch, 0, 0)
   104  	w.Emit(EvFrequency, 1e9)
   105  	for ts := uint64(1); ts < 1e16; ts *= 2 {
   106  		w.Emit(EvGoCreate, ts, ts, 0, 0)
   107  	}
   108  	if _, err := Parse(w, ""); err != nil {
   109  		t.Fatalf("failed to parse: %v", err)
   110  	}
   111  }