| Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 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 |  | 
| Dan Willemsen | 2249dc8 | 2018-10-15 00:35:59 -0700 | [diff] [blame] | 15 | package symbol_inject | 
| Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "strconv" | 
|  | 19 | "testing" | 
|  | 20 | ) | 
|  | 21 |  | 
|  | 22 | func TestElfSymbolTable(t *testing.T) { | 
|  | 23 | testCases := []struct { | 
|  | 24 | file         *mockElfFile | 
|  | 25 | symbol       string | 
|  | 26 | offset, size uint64 | 
|  | 27 | }{ | 
|  | 28 | { | 
|  | 29 | file:   elfSymbolTable1, | 
|  | 30 | symbol: "soong_build_number", | 
|  | 31 | offset: 0x1030, | 
|  | 32 | size:   128, | 
|  | 33 | }, | 
|  | 34 | { | 
|  | 35 | file:   elfSymbolTable2, | 
|  | 36 | symbol: "symbol1", | 
|  | 37 | offset: 0x1030, | 
|  | 38 | size:   128, | 
|  | 39 | }, | 
|  | 40 | { | 
|  | 41 | file:   elfSymbolTable2, | 
|  | 42 | symbol: "symbol2", | 
|  | 43 | offset: 0x10b0, | 
|  | 44 | size:   128, | 
|  | 45 | }, | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | for i, testCase := range testCases { | 
|  | 49 | t.Run(strconv.Itoa(i), func(t *testing.T) { | 
|  | 50 | file, err := extractElfSymbols(testCase.file) | 
|  | 51 | if err != nil { | 
|  | 52 | t.Error(err.Error()) | 
|  | 53 | } | 
|  | 54 | offset, size, err := findSymbol(file, testCase.symbol) | 
|  | 55 | if err != nil { | 
|  | 56 | t.Error(err.Error()) | 
|  | 57 | } | 
|  | 58 | if offset != testCase.offset || size != testCase.size { | 
|  | 59 | t.Errorf("expected %x:%x, got %x:%x", testCase.offset, testCase.size, offset, size) | 
|  | 60 | } | 
|  | 61 | }) | 
|  | 62 | } | 
|  | 63 | } |