Source file src/cmd/compile/internal/noder/lex_test.go

     1  // Copyright 2016 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 noder
     6  
     7  import (
     8  	"internal/buildcfg"
     9  	"reflect"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"cmd/compile/internal/syntax"
    14  )
    15  
    16  func eq(a, b []string) bool {
    17  	if len(a) != len(b) {
    18  		return false
    19  	}
    20  	for i := 0; i < len(a); i++ {
    21  		if a[i] != b[i] {
    22  			return false
    23  		}
    24  	}
    25  	return true
    26  }
    27  
    28  func TestPragmaFields(t *testing.T) {
    29  	var tests = []struct {
    30  		in   string
    31  		want []string
    32  	}{
    33  		{"", []string{}},
    34  		{" \t ", []string{}},
    35  		{`""""`, []string{`""`, `""`}},
    36  		{"  a'b'c  ", []string{"a'b'c"}},
    37  		{"1 2 3 4", []string{"1", "2", "3", "4"}},
    38  		{"\n☺\t☹\n", []string{"☺", "☹"}},
    39  		{`"1 2 "  3  " 4 5"`, []string{`"1 2 "`, `3`, `" 4 5"`}},
    40  		{`"1""2 3""4"`, []string{`"1"`, `"2 3"`, `"4"`}},
    41  		{`12"34"`, []string{`12`, `"34"`}},
    42  		{`12"34 `, []string{`12`}},
    43  	}
    44  
    45  	for _, tt := range tests {
    46  		got := pragmaFields(tt.in)
    47  		if !eq(got, tt.want) {
    48  			t.Errorf("pragmaFields(%q) = %v; want %v", tt.in, got, tt.want)
    49  			continue
    50  		}
    51  	}
    52  }
    53  
    54  func TestPragcgo(t *testing.T) {
    55  	type testStruct struct {
    56  		in   string
    57  		want []string
    58  	}
    59  
    60  	var tests = []testStruct{
    61  		{`go:cgo_export_dynamic local`, []string{`cgo_export_dynamic`, `local`}},
    62  		{`go:cgo_export_dynamic local remote`, []string{`cgo_export_dynamic`, `local`, `remote`}},
    63  		{`go:cgo_export_dynamic local' remote'`, []string{`cgo_export_dynamic`, `local'`, `remote'`}},
    64  		{`go:cgo_export_static local`, []string{`cgo_export_static`, `local`}},
    65  		{`go:cgo_export_static local remote`, []string{`cgo_export_static`, `local`, `remote`}},
    66  		{`go:cgo_export_static local' remote'`, []string{`cgo_export_static`, `local'`, `remote'`}},
    67  		{`go:cgo_import_dynamic local`, []string{`cgo_import_dynamic`, `local`}},
    68  		{`go:cgo_import_dynamic local remote`, []string{`cgo_import_dynamic`, `local`, `remote`}},
    69  		{`go:cgo_import_static local`, []string{`cgo_import_static`, `local`}},
    70  		{`go:cgo_import_static local'`, []string{`cgo_import_static`, `local'`}},
    71  		{`go:cgo_dynamic_linker "/path/"`, []string{`cgo_dynamic_linker`, `/path/`}},
    72  		{`go:cgo_dynamic_linker "/p ath/"`, []string{`cgo_dynamic_linker`, `/p ath/`}},
    73  		{`go:cgo_ldflag "arg"`, []string{`cgo_ldflag`, `arg`}},
    74  		{`go:cgo_ldflag "a rg"`, []string{`cgo_ldflag`, `a rg`}},
    75  		{`go:cgo_import_dynamic _cgo_panic _cgo_panic,--plugin=/tmp/pwn.so`, []string{`<unknown position>: invalid symbol name "_cgo_panic,--plugin=/tmp/pwn.so" in //go:cgo_import_dynamic`}},
    76  		{`go:cgo_export_dynamic main main,--plugin=/tmp/pwn.so`, []string{`<unknown position>: invalid symbol name "main,--plugin=/tmp/pwn.so" in //go:cgo_export_dynamic`}},
    77  		{`go:cgo_dynamic_linker "/lib64/ld.so,--plugin=/tmp/pwn.so"`, []string{`<unknown position>: invalid path "/lib64/ld.so,--plugin=/tmp/pwn.so" in //go:cgo_dynamic_linker`}},
    78  		{`go:cgo_ldflag "-L\"foo\""`, []string{`cgo_ldflag`, `-L"foo"`}},
    79  		{`go:cgo_ldflag "-L\q\"foo\""`, []string{`<unknown position>: usage: //go:cgo_ldflag "arg"`}},
    80  	}
    81  
    82  	if runtime.GOOS != "aix" {
    83  		tests = append(tests, []testStruct{
    84  			{`go:cgo_import_dynamic local remote "library"`, []string{`cgo_import_dynamic`, `local`, `remote`, `library`}},
    85  			{`go:cgo_import_dynamic local' remote' "lib rary"`, []string{`cgo_import_dynamic`, `local'`, `remote'`, `lib rary`}},
    86  		}...)
    87  	} else {
    88  		// cgo_import_dynamic with a library is slightly different on AIX
    89  		// as the library field must follow the pattern [libc.a/object.o].
    90  		tests = append(tests, []testStruct{
    91  			{`go:cgo_import_dynamic local remote "lib.a/obj.o"`, []string{`cgo_import_dynamic`, `local`, `remote`, `lib.a/obj.o`}},
    92  			// This test must fail.
    93  			{`go:cgo_import_dynamic local' remote' "library"`, []string{`<unknown position>: usage: //go:cgo_import_dynamic local [remote ["lib.a/object.o"]]`}},
    94  		}...)
    95  
    96  	}
    97  
    98  	for _, tt := range tests {
    99  		var p noder
   100  		var nopos syntax.Pos
   101  
   102  		p.err = make(chan syntax.Error)
   103  		gotch := make(chan [][]string, 1)
   104  		go func() {
   105  			p.pragcgobuf = nil
   106  			p.pragcgo(nopos, tt.in)
   107  			if p.pragcgobuf != nil {
   108  				gotch <- p.pragcgobuf
   109  			}
   110  		}()
   111  
   112  		select {
   113  		case e := <-p.err:
   114  			want := tt.want[0]
   115  			if e.Error() != want {
   116  				t.Errorf("pragcgo(%q) = %q; want %q", tt.in, e, want)
   117  				continue
   118  			}
   119  		case got := <-gotch:
   120  			want := [][]string{tt.want}
   121  			if !reflect.DeepEqual(got, want) {
   122  				t.Errorf("pragcgo(%q) = %q; want %q", tt.in, got, want)
   123  				continue
   124  			}
   125  		}
   126  
   127  	}
   128  }
   129  
   130  func TestSafeArg(t *testing.T) {
   131  	good := []string{
   132  		"libc.so.6",
   133  		"libstdc++.so.6",
   134  		"libc++.1.dylib",
   135  		"libSystem.B.dylib",
   136  		"kernel32.dll",
   137  		"/usr/lib/libSystem.B.dylib",
   138  		"lib.a/obj.o",
   139  	}
   140  	if runtime.GOOS != "windows" {
   141  		good = append(good, "@rpath/libfoo.dylib")
   142  	}
   143  	for _, s := range good {
   144  		if !safeArg(s) {
   145  			t.Errorf("safeArg(%q) = false, want true", s)
   146  		}
   147  	}
   148  
   149  	bad := []string{
   150  		"",
   151  		"-lfoo",
   152  		"../../../../../../tmp/evil.dll",
   153  		"foo/../bar.dll",
   154  		`..\..\evil.dll`,
   155  		":evil.dll",
   156  		"foo bar.dll",
   157  		"foo,bar.so",
   158  	}
   159  	if runtime.GOOS == "windows" {
   160  		bad = append(bad, "@resp")
   161  	}
   162  	for _, s := range bad {
   163  		if safeArg(s) {
   164  			t.Errorf("safeArg(%q) = true, want false", s)
   165  		}
   166  	}
   167  }
   168  
   169  func TestPragmaCgoImportDynamic(t *testing.T) {
   170  	origGOOS := buildcfg.GOOS
   171  	defer func() { buildcfg.GOOS = origGOOS }()
   172  
   173  	cgoPos := syntax.MakePos(syntax.NewTrimmedFileBase("/tmp/_cgo_import.go", true), 1, 1)
   174  	userPos := syntax.MakePos(syntax.NewTrimmedFileBase("/tmp/main.go", true), 1, 1)
   175  	spoofedPos := syntax.MakePos(syntax.NewLineBase(userPos, "/tmp/_cgo_import.go", true, 1, 1), 2, 1)
   176  
   177  	libs := []string{
   178  		"/tmp/r/app/../lib/libfoo.so",
   179  		"$ORIGIN/libfoo.so",
   180  		"/Library/Application Support/Foo/libfoo.dylib",
   181  	}
   182  
   183  	check := func(pos syntax.Pos, lib string) error {
   184  		p := noder{err: make(chan syntax.Error, 4)}
   185  		p.pragma(pos, true, `go:cgo_import_dynamic _ _ "`+lib+`"`, nil)
   186  		close(p.err)
   187  		for err := range p.err {
   188  			return err
   189  		}
   190  		return nil
   191  	}
   192  
   193  	for _, goos := range []string{"linux", "darwin", "windows"} {
   194  		buildcfg.GOOS = goos
   195  		for _, lib := range libs {
   196  			if err := check(userPos, lib); err == nil {
   197  				t.Errorf("GOOS=%s user file: expected error for library %q, got nil", goos, lib)
   198  			}
   199  			if err := check(spoofedPos, lib); err == nil {
   200  				t.Errorf("GOOS=%s //line-spoofed file: expected error for library %q, got nil", goos, lib)
   201  			}
   202  			err := check(cgoPos, lib)
   203  			if goos == "windows" {
   204  				if err == nil {
   205  					t.Errorf("GOOS=%s _cgo_import.go: expected error for library %q, got nil", goos, lib)
   206  				}
   207  			} else if err != nil {
   208  				t.Errorf("GOOS=%s _cgo_import.go: unexpected error for library %q: %v", goos, lib, err)
   209  			}
   210  		}
   211  	}
   212  }
   213  

View as plain text