Source file src/internal/trace/parser_test.go

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

View as plain text