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