libabigail
test-read-common.cc
Go to the documentation of this file.
1 // -*- Mode: C++ -*-
2 //
3 
4 /// @file
5 ///
6 /// This file implements the common functionality for the tests in
7 /// CTF and DWARF readers, it does the abstraction in the `act` test
8 /// stage.
9 
10 #include <fstream>
11 #include <cstring>
12 #include "test-read-common.h"
13 
14 using std::ofstream;
15 using std::cerr;
16 using std::dynamic_pointer_cast;
17 
19 using abigail::tests::get_build_dir;
23 
24 namespace abigail
25 {
26 namespace tests
27 {
28 namespace read_common
29 {
30 
31 /// Constructor.
32 ///
33 /// Task to be executed for each test entry in @ref
34 /// abigail::tests::read_common::InOutSpec.
35 ///
36 /// @param InOutSpec the set of tests.
37 ///
38 /// @param a_out_abi_base the output base directory for abixml files.
39 ///
40 /// @param a_in_elf_base the input base directory for object files.
41 ///
42 /// @param a_in_elf_base the input base directory for expected
43 /// abixml files.
45  string& a_out_abi_base,
46  string& a_in_elf_base,
47  string& a_in_abi_base)
48  : is_ok(true),
49  spec(s),
50  out_abi_base(a_out_abi_base),
51  in_elf_base(a_in_elf_base),
52  in_abi_base(a_in_abi_base)
53  {}
54 
55 /// Serialize the abixml @p out_abi_path file.
56 ///
57 /// @param out_abi_path the abixml path file.
58 ///
59 /// @param corp the ABI @ref abigail::ir::corpus.
60 ///
61 /// @return true if abixml file was serialized successfully. Otherwise
62 /// `error_message` is set with @p out_abi_path and false is returned.
63 bool
64 test_task::serialize_corpus(const string& out_abi_path,
65  corpus_sptr corp)
66 {
67  ofstream of(out_abi_path.c_str(), std::ios_base::trunc);
68  if (!of.is_open())
69  {
70  error_message = string("failed to read ") + out_abi_path + "\n";
71  return false;
72  }
73 
74  write_context_sptr write_ctxt
75  = create_write_context(corp->get_environment(), of);
76  set_type_id_style(*write_ctxt, spec.type_id_style);
77  is_ok = write_corpus(*write_ctxt, corp, /*indent=*/0);
78  of.close();
79 
80  return is_ok;
81 }
82 
83 /// Spawn `abidw --abidiff` tool appending @p extargs argument.
84 ///
85 /// Thew input file object used by `abidw` will be specified by
86 /// `in_elf_path'.
87 ///
88 /// @param extargs the extra argument(s) passed to `abidw` tool.
89 ///
90 /// @return true if `abidw` tool was executed correctly. Otherwise
91 /// `error_message` shows the full path of the input file object and
92 /// the full output path for the abixml file.
93 bool
94 test_task::run_abidw(const string& extargs)
95 {
96  string abidw = string(get_build_dir()) + "/tools/abidw";
97  string drop_private_types;
98  string spec_options = spec.options ? spec.options : "";
100 
101  if (!in_public_headers_path.empty())
102  drop_private_types += "--headers-dir " + in_public_headers_path +
103  " --drop-private-types";
104  string cmd = abidw + " " + spec_options + drop_private_types +
105  " --abidiff " + extargs + in_elf_path;
106  if (system(cmd.c_str()))
107  {
108  error_message = string("ABIs differ:\n")
109  + in_abi_path
110  + "\nand:\n"
111  + out_abi_path
112  + "\n"
113  + "command was: '" + cmd + "'\n";
114 
115  return false;
116  }
117 
118  return true;
119 }
120 
121 /// Spawn external `diff` command.
122 ///
123 /// The files to be compared are: abixml generated by the input
124 /// object file and the expected abixml file stored in `in_abi_path`.
125 ///
126 /// @return true if `diff` command didn't find defences. Otherwise
127 /// `error_message` shows the full path of the input file object and
128 /// the full output path for the abixml file.
129 bool
131 {
132  set_in_abi_path();
133  string cmd = "diff -u " + in_abi_path + " " + out_abi_path;
134  if (system(cmd.c_str()))
135  {
136  error_message = string("ABI files differ:\n")
137  + in_abi_path
138  + "\nand:\n"
139  + out_abi_path
140  + "\n"
141  + "command was: '" + cmd + "'\n";
142 
143  return false;
144  }
145 
146  return true;
147 }
148 
149 /// Write the usage message to @p out stream object.
150 ///
151 /// @param prog_name the program name.
152 ///
153 /// @param out the stream object to which want to write.
154 void
155 display_usage(const string& prog_name, ostream& out)
156 {
157  emit_prefix(prog_name, out)
158  << "usage: " << prog_name << " [options]\n"
159  << " where options can be: \n"
160  << " --help|-h display this message\n"
161  << " --no-parallel execute testsuite is a sigle thread\n"
162  ;
163 }
164 
165 /// Parse and process test options.
166 ///
167 /// @param argc the arguments number.
168 ///
169 /// @param argv the pointer to the arguments.
170 ///
171 /// @param opts the valid @ref options to be processed/parsed.
172 ///
173 /// @return true if options was processed/parsed successfully. It returns
174 /// false when help is requested or an invalid option is supplied.
175 bool
176 parse_command_line(int argc, char* argv[], options& opts)
177 {
178  for (int i = 1; i < argc; ++i)
179  {
180  if (!strcmp(argv[i], "--no-parallel"))
181  opts.parallel = false;
182  else if (!strcmp(argv[i], "--help")
183  || !strcmp(argv[i], "--h"))
184  return false;
185  else
186  {
187  if (strlen(argv[i]) >= 2 && argv[i][0] == '-' && argv[i][1] == '-')
188  opts.wrong_option = argv[i];
189  return false;
190  }
191  }
192 
193  return true;
194 }
195 
196 /// The main entry point to execute the testsuite.
197 ///
198 /// @param num_tests the number of tests to be executed.
199 ///
200 /// @param specs the @ref abigail::tests::read_common::InOutSpec
201 /// tests container.
202 ///
203 /// @param opts the test execution @ref abigail::tests::read_common::options.
204 ///
205 /// @param new_test the @ref create_new_test callback function to create
206 /// a new test task object.
207 ///
208 /// @return true if `all` tests were performed successfully. Otherwise
209 /// false is returned.
210 bool
211 run_tests(const size_t num_tests, const InOutSpec* specs,
212  const options& opts, create_new_test new_test)
213 {
214  size_t num_workers = (opts.parallel
216  num_tests)
217  : 1);
218 
219  // Create a task queue. The max number of worker threads of the
220  // queue is the number of the concurrent threads supported by the
221  // processor of the machine this code runs on. But if
222  // --no-parallel was provided then the number of worker threads
223  // equals 1.
224  abigail::workers::queue task_queue(num_workers);
225  bool is_ok = true;
226 
227  string out_abi_base = string(get_build_dir()) + "/tests/";
228  string in_elf_base = string(abigail::tests::get_src_dir()) + "/tests/";
229  string in_abi_base = in_elf_base;
230 
231  for (const InOutSpec *s = specs; s->in_elf_path; ++s)
232  {
233  test_task_sptr t(new_test(s, out_abi_base,
234  in_elf_base,
235  in_abi_base));
236  ABG_ASSERT(task_queue.schedule_task(t));
237  }
238 
239  // Wait for all worker threads to finish their job, and wind down.
240  task_queue.wait_for_workers_to_complete();
241 
242  // Now walk the results and print whatever error messages need to be
243  // printed.
244 
245  const vector<abigail::workers::task_sptr>& completed_tasks =
246  task_queue.get_completed_tasks();
247 
248  ABG_ASSERT(completed_tasks.size() == num_tests);
249 
250  for (vector<abigail::workers::task_sptr>::const_iterator ti =
251  completed_tasks.begin();
252  ti != completed_tasks.end();
253  ++ti)
254  {
255  test_task_sptr t = dynamic_pointer_cast<test_task>(*ti);
256  if (!t->is_ok)
257  {
258  is_ok = false;
259  if (!t->error_message.empty())
260  cerr << t->error_message << '\n';
261  }
262  }
263 
264  return !is_ok;
265 }
266 
267 }//end namespace read_common
268 }//end namespace tests
269 }//end namespace abigail
#define ABG_ASSERT(cond)
This is a wrapper around the 'assert' glibc call. It allows for its argument to have side effects,...
Definition: abg-fwd.h:1659
This represents a queue of tasks to be performed.
Definition: abg-workers.h:68
tasks_type & get_completed_tasks() const
Getter of the vector of tasks that got performed.
Definition: abg-workers.cc:347
void wait_for_workers_to_complete()
Suspends the current thread until all worker threads finish performing the tasks they are executing.
Definition: abg-workers.cc:340
bool schedule_task(const task_sptr &)
Submit a task to the queue of tasks to be performed.
Definition: abg-workers.cc:315
ostream & emit_prefix(const string &prog_name, ostream &out)
Emit a prefix made of the name of the program which is emitting a message to an output stream.
size_t get_number_of_threads()
Definition: abg-workers.cc:75
write_context_sptr create_write_context(const environment &env, ostream &default_output_stream)
Create a write_context object that can be used to emit abixml files.
Definition: abg-writer.cc:2105
shared_ptr< write_context > write_context_sptr
A convenience typedef for a shared pointer to write_context.
Definition: abg-writer.h:33
bool write_corpus(write_context &ctxt, const corpus_sptr &corpus, unsigned indent, bool member_of_group)
Serialize an ABI corpus to a single native xml document. The root note of the resulting XML document ...
Definition: abg-writer.cc:4533
void set_type_id_style(write_context &ctxt, type_id_style_kind style)
Set the 'type-id-style' property.
Definition: abg-writer.cc:2243
Toplevel namespace for libabigail.
This is an aggregate that specifies where a test shall get its input from, and where it shall write i...
An abstraction for valid test options.
bool serialize_corpus(const string &out_abi_path, corpus_sptr corp)
Serialize the abixml out_abi_path file.
test_task(const InOutSpec &s, string &a_out_abi_base, string &a_in_elf_base, string &a_in_abi_base)
Constructor.
void set_in_abi_path()
A setter for `in_abi_path` field. The `in_abi_path` is the full path for the expected abixml file.
bool run_abidw(const string &extargs="")
Spawn `abidw –abidiff` tool appending extargs argument.
bool run_diff()
Spawn external `diff` command.
bool run_tests(const size_t num_tests, const InOutSpec *specs, const options &opts, create_new_test new_test)
The main entry point to execute the testsuite.
bool parse_command_line(int argc, char *argv[], options &opts)
Parse and process test options.
void display_usage(const string &prog_name, ostream &out)
Write the usage message to out stream object.
This file declares the common functionality for tests in CTF and DWARF readers, it declares abstracti...