...

Source file src/runtime/checkptr_test.go

Documentation: runtime

		 1  // Copyright 2020 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 runtime_test
		 6  
		 7  import (
		 8  	"internal/testenv"
		 9  	"os/exec"
		10  	"strings"
		11  	"testing"
		12  )
		13  
		14  func TestCheckPtr(t *testing.T) {
		15  	t.Parallel()
		16  	testenv.MustHaveGoRun(t)
		17  
		18  	exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1")
		19  	if err != nil {
		20  		t.Fatal(err)
		21  	}
		22  
		23  	testCases := []struct {
		24  		cmd	string
		25  		want string
		26  	}{
		27  		{"CheckPtrAlignmentPtr", "fatal error: checkptr: misaligned pointer conversion\n"},
		28  		{"CheckPtrAlignmentNoPtr", ""},
		29  		{"CheckPtrAlignmentNilPtr", ""},
		30  		{"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
		31  		{"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"},
		32  		{"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"},
		33  		{"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"},
		34  		{"CheckPtrSliceOK", ""},
		35  		{"CheckPtrSliceFail", "fatal error: checkptr: unsafe.Slice result straddles multiple allocations\n"},
		36  	}
		37  
		38  	for _, tc := range testCases {
		39  		tc := tc
		40  		t.Run(tc.cmd, func(t *testing.T) {
		41  			t.Parallel()
		42  			got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput()
		43  			if err != nil {
		44  				t.Log(err)
		45  			}
		46  			if tc.want == "" {
		47  				if len(got) > 0 {
		48  					t.Errorf("output:\n%s\nwant no output", got)
		49  				}
		50  				return
		51  			}
		52  			if !strings.HasPrefix(string(got), tc.want) {
		53  				t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want)
		54  			}
		55  		})
		56  	}
		57  }
		58  

View as plain text