Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0dcb0300b5613dfe608be0342d6ef815a5e29dd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
 * Copyright (c) 2018 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Hansruedi Patzen (IFS) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.utils.elf;

import java.io.FileInputStream;
import java.io.IOException;

import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.utils.elf.parser.ElfBinaryExecutable;
import org.eclipse.cdt.utils.elf.parser.ElfBinaryShared;
import org.eclipse.cdt.utils.elf.parser.ElfParser;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ElfParserTest extends TestCase {

	ElfParser elfParser;

	public static Test suite() {
		return new TestSuite(ElfParserTest.class);
	}

	@Override
	protected void setUp() throws Exception {
		elfParser = new ElfParser();
		super.setUp();
	}

	private byte[] readHints(final IPath path) throws IOException {
		int bytesToRead = elfParser.getHintBufferSize();
		byte[] hints = new byte[bytesToRead];
		try (FileInputStream is = new FileInputStream(path.toFile())) {
			is.read(hints);
		}
		return hints;
	}

	/*
	 * The most probable cause for failing one of the following tests
	 * 
	 * testLE64DYNwithInterpELFoutsideHintsError_Bug512822
	 * testLE32DYNwithInterpELFoutsideHintsError_Bug512822
	 * 
	 * is that the hint buffer size has been changed and the test binaries
	 * are too small and need to be updated to reflect this change as well.
	 * 
	 * The original files have been manually created using nasm. See the
	 * *.asm files inside resources/elf/{inside_hints,outside_hints}/ for
	 * further details.
	 */
	public void testLE64DYNwithInterpELFoutsideHintsError_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le64");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertFalse("Binary should not be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE32DYNwithInterpELFoutsideHintsError_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le32");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertFalse("Binary should not be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE64DYNwithInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/le64");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE32DYNwithInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/le32");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE64DYNwithInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le64");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE32DYNwithInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le32");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testLE32DYNnoInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/le32lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testLE64DYNnoInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/le64lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testLE32DYNnoInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le32lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testLE64DYNnoInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/le64lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testBE64DYNwithInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/be64");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testBE32DYNwithInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/be32");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testBE64DYNwithInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/be64");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testBE32DYNwithInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/be32");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be an executable", binary instanceof ElfBinaryExecutable);
	}

	public void testBE32DYNnoInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/be32lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testBE64DYNnoInterpELFinsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/inside_hints/be64lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), new Path(""));
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testBE32DYNnoInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/be32lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}

	public void testBE64DYNnoInterpELFoutsideHints_Bug512822() throws CModelException, IOException {
		IPath path = new Path("resources/elf/outside_hints/be64lib");
		IBinaryFile binary = elfParser.getBinary(readHints(path), path);
		assertTrue("Binary should be a library", binary instanceof ElfBinaryShared);
	}
}

Back to the top