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.
21 // testUnnamed tests that two plugins built with .go files passed on
22 // the command line do not have overlapping symbols. That is,
23 // unnamed1.so/FuncInt and unnamed2.so/FuncInt should be distinct functions.
25 p, err := plugin.Open("unnamed1.so")
27 log.Fatalf(`plugin.Open("unnamed1.so"): %v`, err)
29 fn, err := p.Lookup("FuncInt")
31 log.Fatalf(`unnamed1.so: Lookup("FuncInt") failed: %v`, err)
33 if got, want := fn.(func() int)(), 1; got != want {
34 log.Fatalf("unnamed1.so: FuncInt()=%d, want %d", got, want)
37 p, err = plugin.Open("unnamed2.so")
39 log.Fatalf(`plugin.Open("unnamed2.so"): %v`, err)
41 fn, err = p.Lookup("FuncInt")
43 log.Fatalf(`unnamed2.so: Lookup("FuncInt") failed: %v`, err)
45 if got, want := fn.(func() int)(), 2; got != want {
46 log.Fatalf("unnamed2.so: FuncInt()=%d, want %d", got, want)
51 if got, want := common.X, 3*5; got != want {
52 log.Fatalf("before plugin load common.X=%d, want %d", got, want)
55 p, err := plugin.Open("plugin1.so")
57 log.Fatalf("plugin.Open failed: %v", err)
60 const wantX = 3 * 5 * 7
61 if got := common.X; got != wantX {
62 log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
65 seven, err := p.Lookup("Seven")
67 log.Fatalf(`Lookup("Seven") failed: %v`, err)
69 if got, want := *seven.(*int), 7; got != want {
70 log.Fatalf("plugin1.Seven=%d, want %d", got, want)
73 readFunc, err := p.Lookup("ReadCommonX")
75 log.Fatalf(`plugin1.Lookup("ReadCommonX") failed: %v`, err)
77 if got := readFunc.(func() int)(); got != wantX {
78 log.Fatalf("plugin1.ReadCommonX()=%d, want %d", got, wantX)
81 // sub/plugin1.so is a different plugin with the same name as
82 // the already loaded plugin. It also depends on common. Test
83 // that we can load the different plugin, it is actually
84 // different, and that it sees the same common package.
85 subpPath, err := filepath.Abs("sub/plugin1.so")
87 log.Fatalf("filepath.Abs(%q) failed: %v", subpPath, err)
89 subp, err := plugin.Open(subpPath)
91 log.Fatalf("plugin.Open(%q) failed: %v", subpPath, err)
94 funcVar, err := subp.Lookup("FuncVar")
96 log.Fatalf(`sub/plugin1.Lookup("FuncVar") failed: %v`, err)
99 *funcVar.(*func()) = func() {
103 readFunc, err = subp.Lookup("ReadCommonX")
105 log.Fatalf(`sub/plugin1.Lookup("ReadCommonX") failed: %v`, err)
107 if got := readFunc.(func() int)(); got != wantX {
108 log.Fatalf("sub/plugin1.ReadCommonX()=%d, want %d", got, wantX)
111 log.Fatal("calling ReadCommonX did not call FuncVar")
114 subf, err := subp.Lookup("F")
116 log.Fatalf(`sub/plugin1.Lookup("F") failed: %v`, err)
118 if gotf := subf.(func() int)(); gotf != 17 {
119 log.Fatalf(`sub/plugin1.F()=%d, want 17`, gotf)
121 f, err := p.Lookup("F")
123 log.Fatalf(`plugin1.Lookup("F") failed: %v`, err)
125 if gotf := f.(func() int)(); gotf != 3 {
126 log.Fatalf(`plugin1.F()=%d, want 17`, gotf)
129 p2, err := plugin.Open("plugin2.so")
131 log.Fatalf("plugin.Open failed: %v", err)
133 // Check that plugin2's init function was called, and
134 // that it modifies the same global variable as the host.
135 if got, want := common.X, 2; got != want {
136 log.Fatalf("after loading plugin2, common.X=%d, want %d", got, want)
139 _, err = plugin.Open("plugin2-dup.so")
141 log.Fatal(`plugin.Open("plugin2-dup.so"): duplicate open should have failed`)
143 if s := err.Error(); !strings.Contains(s, "already loaded") {
144 log.Fatal(`plugin.Open("plugin2.so"): error does not mention "already loaded"`)
147 _, err = plugin.Open("plugin-mismatch.so")
149 log.Fatal(`plugin.Open("plugin-mismatch.so"): should have failed`)
151 if s := err.Error(); !strings.Contains(s, "different version") {
152 log.Fatalf(`plugin.Open("plugin-mismatch.so"): error does not mention "different version": %v`, s)
155 _, err = plugin.Open("plugin2-dup.so")
157 log.Fatal(`plugin.Open("plugin2-dup.so"): duplicate open after bad plugin should have failed`)
159 _, err = plugin.Open("plugin2.so")
161 log.Fatalf(`plugin.Open("plugin2.so"): second open with same name failed: %v`, err)
164 // Test that unexported types with the same names in
165 // different plugins do not interfere with each other.
168 UnexportedNameReuse, _ := p.Lookup("UnexportedNameReuse")
169 UnexportedNameReuse.(func())()
170 UnexportedNameReuse, _ = p2.Lookup("UnexportedNameReuse")
171 UnexportedNameReuse.(func())()