|
1 package com.nokia.cdt.debug.cw.symbian.tests; |
|
2 |
|
3 import java.io.File; |
|
4 import java.io.FileOutputStream; |
|
5 import java.io.IOException; |
|
6 import java.io.PrintStream; |
|
7 import java.util.ArrayList; |
|
8 import java.util.Arrays; |
|
9 |
|
10 import junit.framework.TestCase; |
|
11 |
|
12 import org.eclipse.cdt.utils.debug.dwarf.DwarfReader; |
|
13 |
|
14 public class DwarfReaderTest extends TestCase { |
|
15 |
|
16 private String m_outputFileName = "c:\\temp\\DwarfReaderOutput.txt"; |
|
17 |
|
18 // The Elf file to parse. |
|
19 // |
|
20 private String[] m_dataFileNames; |
|
21 |
|
22 private PrintStream m_outputFileStream = null; |
|
23 |
|
24 protected void setUp() throws Exception { |
|
25 super.setUp(); |
|
26 |
|
27 m_dataFileNames = getDataFileNames(); |
|
28 |
|
29 if (m_outputFileStream == null) { |
|
30 File outputFile = new File(m_outputFileName); |
|
31 if (!outputFile.exists()) |
|
32 outputFile.createNewFile(); |
|
33 |
|
34 m_outputFileStream = new PrintStream(new FileOutputStream( |
|
35 outputFile, false /* append? */)); |
|
36 |
|
37 m_outputFileStream.println("Output of test on CDT DwarfReader"); |
|
38 m_outputFileStream.println("=========================================="); |
|
39 } |
|
40 } |
|
41 |
|
42 protected void tearDown() throws Exception { |
|
43 super.tearDown(); |
|
44 |
|
45 m_outputFileStream.close(); |
|
46 } |
|
47 |
|
48 public final void testGetSourceFiles() { |
|
49 for (String fname: m_dataFileNames) { |
|
50 File f = new File(fname); |
|
51 if (f == null || !f.exists()) |
|
52 fail("Data file not found: " + f.getAbsolutePath()); |
|
53 |
|
54 // Currently this just check if the parsing process succeeds, |
|
55 // not check if the output is correct. |
|
56 // Look at the output file designated by "m_outputFileName" |
|
57 // and see if it's correct. |
|
58 getSourceFilesForBinary(fname); |
|
59 } |
|
60 } |
|
61 |
|
62 // Get data files to test |
|
63 // |
|
64 private String[] getDataFileNames() throws Exception { |
|
65 String[] fnames = null; |
|
66 |
|
67 ArrayList<String> list = new ArrayList<String>(10); |
|
68 |
|
69 // To add individual data files... |
|
70 // |
|
71 /* |
|
72 list.add("M:\\epoc32\\RELEASE\\armv5\\udeb\\agenda.sym"); |
|
73 list.add(TestsPlugin.getPluginPath()+"\\data\\one2.sym"); |
|
74 list.add(TestsPlugin.getPluginPath()+"\\data\\_h2_dma.sym"); |
|
75 */ |
|
76 |
|
77 // To add files under one folder... |
|
78 // |
|
79 // All all .sym files under data folder in the project. |
|
80 list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder( |
|
81 TestsPlugin.getPluginPath()+"\\data\\", ".sym"))); |
|
82 /* |
|
83 list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder( |
|
84 "M:\\epoc32\\RELEASE\\armv5\\udeb\\", ".sym"))); |
|
85 list.addAll(Arrays.asList(TestsPlugin.getFileFullNamesInFolder( |
|
86 "M:\\epoc32\\RELEASE\\gcce\\udeb\\", ".sym"))); |
|
87 */ |
|
88 |
|
89 fnames = new String[list.size()]; |
|
90 list.toArray(fnames); |
|
91 |
|
92 return fnames; |
|
93 } |
|
94 |
|
95 private void getSourceFilesForBinary(String binaryFileName) { |
|
96 DwarfReader reader = null; |
|
97 |
|
98 try { |
|
99 reader = new DwarfReader(binaryFileName); |
|
100 } catch (IOException e) { |
|
101 e.printStackTrace(); |
|
102 fail("File \"" + binaryFileName + "\" is not valid Elf file."); |
|
103 return; |
|
104 } |
|
105 |
|
106 m_outputFileStream.println("\n----------------------------------------------------"); |
|
107 m_outputFileStream.println("Data file to check: "); |
|
108 m_outputFileStream.println("\t" + binaryFileName); |
|
109 m_outputFileStream.println("----------------------------------------------------"); |
|
110 |
|
111 String[] files = reader.getSourceFiles(); |
|
112 |
|
113 m_outputFileStream.print("Number of source files: "); |
|
114 m_outputFileStream.println(files.length); |
|
115 m_outputFileStream.println(); |
|
116 |
|
117 // Sort the file lists. |
|
118 Arrays.sort(files); |
|
119 |
|
120 for (String f : files) { |
|
121 m_outputFileStream.println(f); |
|
122 } |
|
123 } |
|
124 |
|
125 // --- NOTE ------ |
|
126 // This is for my own manual junit test only, not for public |
|
127 // auto-test. |
|
128 public void my_testLineInfoContainsMoreSourceFiles() { |
|
129 |
|
130 // Iterate through those files and check... |
|
131 for (String f : m_dataFileNames) { |
|
132 m_outputFileStream.format("Check [%s] :\n", f); |
|
133 |
|
134 if (lineInfoContainsMoreSourceFiles(f)) |
|
135 m_outputFileStream.println("yes !"); |
|
136 else |
|
137 m_outputFileStream.println("No."); |
|
138 } |
|
139 } |
|
140 |
|
141 /* |
|
142 * This function checks if .debug_line section contains |
|
143 * source files that are not referenced by any TAG_compile_units. |
|
144 * |
|
145 * @param dataFileName |
|
146 */ |
|
147 private boolean lineInfoContainsMoreSourceFiles(String dataFileName) { |
|
148 DwarfReader reader = null; |
|
149 |
|
150 try { |
|
151 reader = new DwarfReader(dataFileName); |
|
152 } catch (IOException e) { |
|
153 e.printStackTrace(); |
|
154 fail("File \"" + dataFileName + "\" is not valid Elf file."); |
|
155 return false; |
|
156 } |
|
157 |
|
158 // Hold on ! |
|
159 // Look at code of getSourceFiles() and add two public |
|
160 // APIs like |
|
161 // "getSourcesFromCompileUnit()" and |
|
162 // "getSourcesFromDebugLine()" |
|
163 // and then test here. |
|
164 String[] filesFromCU = reader.getSourceFiles(); // change to getFromCompileUnit() |
|
165 String[] filesFromLineInfo = reader.getSourceFiles(); // change to getFromDebugLine() |
|
166 |
|
167 /* |
|
168 // Sort the file lists and compare the list if needed. |
|
169 // |
|
170 Arrays.sort(filesFromCU); |
|
171 Arrays.sort(filesFromLineInfo); |
|
172 |
|
173 assertEquals(filesFromCU.length, filesFromLineInfo.length); |
|
174 for (int i = 0; i < filesFromCU.length; i++) { |
|
175 assertEquals(filesFromCU[i], filesFromLineInfo[i]); |
|
176 } |
|
177 */ |
|
178 return (filesFromCU.length > filesFromLineInfo.length); |
|
179 } |
|
180 |
|
181 } |