1// Copyright (c) 2017, Google Inc. 2// 3// Permission to use, copy, modify, and/or distribute this software for any 4// purpose with or without fee is hereby granted, provided that the above 5// copyright notice and this permission notice appear in all copies. 6// 7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15package main 16 17import ( 18 "bytes" 19 "flag" 20 "io/ioutil" 21 "path/filepath" 22 "testing" 23) 24 25var ( 26 testDataDir = flag.String("testdata", "testdata", "The path to the test data directory.") 27 update = flag.Bool("update", false, "If true, update output files rather than compare them.") 28) 29 30type delocateTest struct { 31 name string 32 in []string 33 out string 34} 35 36func (test *delocateTest) Path(file string) string { 37 return filepath.Join(*testDataDir, test.name, file) 38} 39 40var delocateTests = []delocateTest{ 41 {"generic-FileDirectives", []string{"in.s"}, "out.s"}, 42 {"ppc64le-GlobalEntry", []string{"in.s"}, "out.s"}, 43 {"ppc64le-LoadToR0", []string{"in.s"}, "out.s"}, 44 {"ppc64le-Sample2", []string{"in.s"}, "out.s"}, 45 {"ppc64le-Sample", []string{"in.s"}, "out.s"}, 46 {"ppc64le-TOCWithOffset", []string{"in.s"}, "out.s"}, 47 {"x86_64-Basic", []string{"in.s"}, "out.s"}, 48 {"x86_64-BSS", []string{"in.s"}, "out.s"}, 49 {"x86_64-GOTRewrite", []string{"in.s"}, "out.s"}, 50 {"x86_64-LargeMemory", []string{"in.s"}, "out.s"}, 51 {"x86_64-LabelRewrite", []string{"in1.s", "in2.s"}, "out.s"}, 52 {"x86_64-Sections", []string{"in.s"}, "out.s"}, 53 {"x86_64-ThreeArg", []string{"in.s"}, "out.s"}, 54 {"aarch64-Basic", []string{"in.s"}, "out.s"}, 55} 56 57func TestDelocate(t *testing.T) { 58 for _, test := range delocateTests { 59 t.Run(test.name, func(t *testing.T) { 60 var inputs []inputFile 61 for i, in := range test.in { 62 inputs = append(inputs, inputFile{ 63 index: i, 64 path: test.Path(in), 65 }) 66 } 67 68 if err := parseInputs(inputs); err != nil { 69 t.Fatalf("parseInputs failed: %s", err) 70 } 71 72 var buf bytes.Buffer 73 if err := transform(&buf, inputs); err != nil { 74 t.Fatalf("transform failed: %s", err) 75 } 76 77 if *update { 78 ioutil.WriteFile(test.Path(test.out), buf.Bytes(), 0666) 79 } else { 80 expected, err := ioutil.ReadFile(test.Path(test.out)) 81 if err != nil { 82 t.Fatalf("could not read %q: %s", test.Path(test.out), err) 83 } 84 if !bytes.Equal(buf.Bytes(), expected) { 85 t.Errorf("delocated output differed. Wanted:\n%s\nGot:\n%s\n", expected, buf.Bytes()) 86 } 87 } 88 }) 89 } 90} 91