• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2022 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// This test harness program runs a diff between the output of
11 /// abinilint on an ini file and a reference expected output.
12 
13 #include <sys/wait.h>
14 #include <cstring>
15 #include <string>
16 #include <fstream>
17 #include <iostream>
18 #include <cstdlib>
19 #include "abg-tools-utils.h"
20 #include "test-utils.h"
21 
22 using std::string;
23 using std::cerr;
24 using abigail::tests::emit_test_status_and_update_counters;
25 using abigail::tests::emit_test_summary;
26 
27 /// This is an aggregate that specifies where a test shall get its
28 /// input from and where it shall write its ouput to.
29 struct InOutSpec
30 {
31   // Where to read the ini file from, with abinilint.
32   const char* in_ini_path;
33   // Where to get the expected output from abinilint.
34   const char* in_reference_output_path;
35   // Where to emit the output of abinilint to.
36   const char* out_init_path;
37   // The options to use with abinilint.
38   const char* abinilint_options;
39 }; // end struct InOutSpec;
40 
41 // An array of test specifications listing the ini files to read and
42 // their expected output.
43 InOutSpec in_out_specs[] =
44 {
45   {
46     "data/test-ini/test01-equal-in-property-string.abignore",
47     "data/test-ini/test01-equal-in-property-string.abignore.expected",
48     "output/test-ini/test01-equal-in-property-string.abignore",
49     ""
50   }
51   ,
52   // This one must always remain the last one.
53   {0, 0, 0, 0}
54 };
55 
56 /// @return the test source directory.
57 static string
get_test_src_dir()58 get_test_src_dir()
59 {
60   using abigail::tests::get_src_dir;
61   return string(get_src_dir()) + "/tests";
62 }
63 
64 /// @return the test build directory.
65 static string
get_test_build_dir()66 get_test_build_dir()
67 {
68   using abigail::tests::get_build_dir;
69   return string(get_build_dir()) + "/tests";
70 }
71 
72 /// @return the tools directory under the build directory;
73 static string
get_tools_build_dir()74 get_tools_build_dir()
75 {
76   using abigail::tests::get_build_dir;
77   return string(get_build_dir()) + "/tools";
78 }
79 
80 int
main()81 main()
82 {
83   using abigail::tests::get_build_dir;
84   using abigail::tools_utils::ensure_parent_dir_created;
85   using abigail::tools_utils::abidiff_status;
86 
87   unsigned int total_count = 0, passed_count = 0, failed_count = 0;
88 
89   string in_ini_path, in_reference_output_path, out_ini_path, cmd, diff_cmd;
90 
91   for (InOutSpec *s = in_out_specs; s->in_ini_path; ++s)
92     {
93       bool is_ok = true;
94       in_ini_path = get_test_src_dir() + "/" + s->in_ini_path;
95       in_reference_output_path =
96 	get_test_src_dir() + "/" + s->in_reference_output_path;
97       out_ini_path = get_test_build_dir() + "/" + s->out_init_path;
98 
99       if (!ensure_parent_dir_created(out_ini_path))
100 	{
101 	  cerr << "could not create parent directory for "
102 	       << out_ini_path;
103 	  is_ok = false;
104 	  continue;
105 	}
106 
107       cmd = get_tools_build_dir() + "/abinilint";
108       if (s->abinilint_options && strcmp(s->abinilint_options, ""))
109 	cmd += " " + string(s->abinilint_options);
110 
111       cmd += " " + in_ini_path + " > " + out_ini_path;
112 
113       bool cmd_is_ok = true;
114       int code = system(cmd.c_str());
115       if (!WIFEXITED(code))
116 	 cmd_is_ok = false;
117       else
118 	{
119 	  abigail::tools_utils::abidiff_status status =
120 	    static_cast<abidiff_status>(WEXITSTATUS(code));
121 	  if (abigail::tools_utils::abidiff_status_has_error(status))
122 	    cmd_is_ok = false;
123 	}
124 
125       if (cmd_is_ok)
126 	{
127 	  diff_cmd = "diff -u " + in_reference_output_path + " " + out_ini_path;
128 	  if (system(diff_cmd.c_str()))
129 	    is_ok = false;
130 	}
131       else
132 	is_ok = false;
133 
134       emit_test_status_and_update_counters(is_ok, cmd, passed_count,
135 					   failed_count, total_count);
136     }
137 
138   emit_test_summary(total_count, passed_count, failed_count);
139 
140   return failed_count;
141 }
142