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