Skip to content

Commit 8eb7304

Browse files
committed
debug/pe: added windows-specific test against atmfd imports
This adds a test against atmfd as suggested by Alex Brainman that proves that file.go has the ability to parse an import table residing in a section not named .idata. This test is windows-only and since the atmfd driver itself is actually provided by Adobe but bundled with all versions of windows, we're forced to search for the file in the current PATH and then try to read the symbols from it. If the file isn't found, the test fails. src/debug/pe/file_test.go: added test
1 parent c9a44b8 commit 8eb7304

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/debug/pe/file_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"reflect"
1515
"regexp"
1616
"runtime"
17+
"strings"
1718
"strconv"
1819
"testing"
1920
"text/template"
@@ -532,3 +533,50 @@ func TestBuildingWindowsGUI(t *testing.T) {
532533
t.Fatalf("unexpected OptionalHeader type: have %T, but want *pe.OptionalHeader32 or *pe.OptionalHeader64", oh)
533534
}
534535
}
536+
537+
// go through each directory in dirs looking for the file identified by name.
538+
// returns the index or -1 on error.
539+
func filenameInDirectories(name string, dirs []string) string {
540+
for i, path := range dirs {
541+
rp := filepath.Join(path, name)
542+
if _, err := os.Stat(rp); !os.IsNotExist(err) {
543+
return rp
544+
}
545+
}
546+
return ""
547+
}
548+
549+
// split an environment variable into its individual paths
550+
// more likely to be used with "PATH" and nothing else
551+
func listOfEnvironment(key string) []string {
552+
if path, ok := os.LookupEnv(key); ok {
553+
return strings.Split(path, string(filepath.ListSeparator))
554+
}
555+
return []string{"."}
556+
}
557+
558+
func TestImportTableInUnknownSection(t *testing.T) {
559+
if runtime.GOOS != "windows" {
560+
t.Skip("skipping windows only test")
561+
}
562+
563+
// first we need to find this font driver
564+
const filename = "atmfd.dll"
565+
path := filenameInDirectories(filename, listOfEnvironment("PATH"))
566+
if path == "" {
567+
t.Fatalf("unable to locate required file (%s) in search path.", filename)
568+
}
569+
570+
// now we can open it with debug/pe like so
571+
f, err := Open(path)
572+
if err != nil {
573+
t.Error(err)
574+
}
575+
defer f.Close()
576+
577+
// so let's ask for symbols and if it burns, then complain
578+
symbols, err := f.ImportedSymbols()
579+
if err != nil {
580+
t.Error(err)
581+
}
582+
}

0 commit comments

Comments
 (0)