macho_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "debug/macho"
  17. "strconv"
  18. "testing"
  19. )
  20. func TestMachoSymbolTable(t *testing.T) {
  21. testCases := []struct {
  22. file *macho.File
  23. symbol string
  24. offset, size uint64
  25. }{
  26. {
  27. file: machoSymbolTable1,
  28. symbol: "soong_build_number",
  29. offset: 0x1020,
  30. size: 128,
  31. },
  32. {
  33. file: machoSymbolTable2,
  34. symbol: "symbol1",
  35. offset: 0x1020,
  36. size: 128,
  37. },
  38. {
  39. file: machoSymbolTable2,
  40. symbol: "symbol2",
  41. offset: 0x10a0,
  42. size: 128,
  43. },
  44. }
  45. for i, testCase := range testCases {
  46. t.Run(strconv.Itoa(i), func(t *testing.T) {
  47. file, err := extractMachoSymbols(testCase.file)
  48. if err != nil {
  49. t.Error(err.Error())
  50. }
  51. offset, size, err := findSymbol(file, testCase.symbol)
  52. if err != nil {
  53. t.Error(err.Error())
  54. }
  55. if offset != testCase.offset || size != testCase.size {
  56. t.Errorf("expected %x:%x, got %x:%x", testCase.offset, testCase.size, offset, size)
  57. }
  58. })
  59. }
  60. }