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 15 package main 16 17 import ( 18 "bytes" 19 "flag" 20 "io/ioutil" 21 "path/filepath" 22 "testing" 23 ) 24 25 var ( 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 30 type delocateTest struct { 31 name string 32 in []string 33 out string 34 } 35 36 func (test *delocateTest) Path(file string) string { 37 return filepath.Join(*testDataDir, test.name, file) 38 } 39 40 var delocateTests = []delocateTest{ 41 {"ppc64le-GlobalEntry", []string{"in.s"}, "out.s"}, 42 {"ppc64le-LoadToR0", []string{"in.s"}, "out.s"}, 43 {"ppc64le-Sample2", []string{"in.s"}, "out.s"}, 44 {"ppc64le-Sample", []string{"in.s"}, "out.s"}, 45 {"ppc64le-TOCWithOffset", []string{"in.s"}, "out.s"}, 46 {"x86_64-Basic", []string{"in.s"}, "out.s"}, 47 {"x86_64-BSS", []string{"in.s"}, "out.s"}, 48 {"x86_64-GOTRewrite", []string{"in.s"}, "out.s"}, 49 {"x86_64-LabelRewrite", []string{"in1.s", "in2.s"}, "out.s"}, 50 {"x86_64-Sections", []string{"in.s"}, "out.s"}, 51 } 52 53 func TestDelocate(t *testing.T) { 54 for _, test := range delocateTests { 55 t.Run(test.name, func(t *testing.T) { 56 var inputs []inputFile 57 for i, in := range test.in { 58 inputs = append(inputs, inputFile{ 59 index: i, 60 path: test.Path(in), 61 }) 62 } 63 64 if err := parseInputs(inputs); err != nil { 65 t.Fatalf("parseInputs failed: %s", err) 66 } 67 68 var buf bytes.Buffer 69 if err := transform(&buf, inputs); err != nil { 70 t.Fatalf("transform failed: %s", err) 71 } 72 73 if *update { 74 ioutil.WriteFile(test.Path(test.out), buf.Bytes(), 0666) 75 } else { 76 expected, err := ioutil.ReadFile(test.Path(test.out)) 77 if err != nil { 78 t.Fatalf("could not read %q: %s", test.Path(test.out), err) 79 } 80 if !bytes.Equal(buf.Bytes(), expected) { 81 t.Errorf("delocated output differed. Wanted:\n%s\nGot:\n%s\n", expected, buf.Bytes()) 82 } 83 } 84 }) 85 } 86 } 87