elf_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2018 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package symbol_inject
  15. import (
  16. "strconv"
  17. "testing"
  18. )
  19. func TestElfSymbolTable(t *testing.T) {
  20. testCases := []struct {
  21. file *mockElfFile
  22. symbol string
  23. offset, size uint64
  24. }{
  25. {
  26. file: elfSymbolTable1,
  27. symbol: "soong_build_number",
  28. offset: 0x1030,
  29. size: 128,
  30. },
  31. {
  32. file: elfSymbolTable2,
  33. symbol: "symbol1",
  34. offset: 0x1030,
  35. size: 128,
  36. },
  37. {
  38. file: elfSymbolTable2,
  39. symbol: "symbol2",
  40. offset: 0x10b0,
  41. size: 128,
  42. },
  43. }
  44. for i, testCase := range testCases {
  45. t.Run(strconv.Itoa(i), func(t *testing.T) {
  46. file, err := extractElfSymbols(testCase.file)
  47. if err != nil {
  48. t.Error(err.Error())
  49. }
  50. offset, size, err := findSymbol(file, testCase.symbol)
  51. if err != nil {
  52. t.Error(err.Error())
  53. }
  54. if offset != testCase.offset || size != testCase.size {
  55. t.Errorf("expected %x:%x, got %x:%x", testCase.offset, testCase.size, offset, size)
  56. }
  57. })
  58. }
  59. }