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 "os" 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 {"x86_64-Basic", []string{"in.s"}, "out.s"}, 43 {"x86_64-BSS", []string{"in.s"}, "out.s"}, 44 {"x86_64-GOTRewrite", []string{"in.s"}, "out.s"}, 45 {"x86_64-LargeMemory", []string{"in.s"}, "out.s"}, 46 {"x86_64-LabelRewrite", []string{"in1.s", "in2.s"}, "out.s"}, 47 {"x86_64-Sections", []string{"in.s"}, "out.s"}, 48 {"x86_64-ThreeArg", []string{"in.s"}, "out.s"}, 49 {"aarch64-Basic", []string{"in.s"}, "out.s"}, 50} 51 52func TestDelocate(t *testing.T) { 53 for _, test := range delocateTests { 54 t.Run(test.name, func(t *testing.T) { 55 var inputs []inputFile 56 for i, in := range test.in { 57 inputs = append(inputs, inputFile{ 58 index: i, 59 path: test.Path(in), 60 }) 61 } 62 63 if err := parseInputs(inputs, nil); err != nil { 64 t.Fatalf("parseInputs failed: %s", err) 65 } 66 67 var buf bytes.Buffer 68 if err := transform(&buf, inputs); err != nil { 69 t.Fatalf("transform failed: %s", err) 70 } 71 72 if *update { 73 os.WriteFile(test.Path(test.out), buf.Bytes(), 0666) 74 } else { 75 expected, err := os.ReadFile(test.Path(test.out)) 76 if err != nil { 77 t.Fatalf("could not read %q: %s", test.Path(test.out), err) 78 } 79 if !bytes.Equal(buf.Bytes(), expected) { 80 t.Errorf("delocated output differed. Wanted:\n%s\nGot:\n%s\n", expected, buf.Bytes()) 81 } 82 } 83 }) 84 } 85} 86