You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

87 lines
2.6 KiB

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