1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 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 
25 /// This is an aggregate that specifies where a test shall get its
26 /// input from and where it shall write its ouput to.
27 struct InOutSpec
28 {
29   // Where to read the ini file from, with abinilint.
30   const char* in_ini_path;
31   // Where to get the expected output from abinilint.
32   const char* in_reference_output_path;
33   // Where to emit the output of abinilint to.
34   const char* out_init_path;
35   // The options to use with abinilint.
36   const char* abinilint_options;
37 }; // end struct InOutSpec;
38 
39 // An array of test specifications listing the ini files to read and
40 // their expected output.
41 InOutSpec in_out_specs[] =
42 {
43   {
44     "data/test-ini/test01-equal-in-property-string.abignore",
45     "data/test-ini/test01-equal-in-property-string.abignore.expected",
46     "output/test-ini/test01-equal-in-property-string.abignore",
47     ""
48   }
49   ,
50   // This one must always remain the last one.
51   {0, 0, 0, 0}
52 };
53 
54 /// @return the test source directory.
55 static string
get_test_src_dir()56 get_test_src_dir()
57 {
58   using abigail::tests::get_src_dir;
59   return string(get_src_dir()) + "/tests";
60 }
61 
62 /// @return the test build directory.
63 static string
get_test_build_dir()64 get_test_build_dir()
65 {
66   using abigail::tests::get_build_dir;
67   return string(get_build_dir()) + "/tests";
68 }
69 
70 /// @return the tools directory under the build directory;
71 static string
get_tools_build_dir()72 get_tools_build_dir()
73 {
74   using abigail::tests::get_build_dir;
75   return string(get_build_dir()) + "/tools";
76 }
77 
78 int
main()79 main()
80 {
81   using abigail::tests::get_build_dir;
82   using abigail::tools_utils::ensure_parent_dir_created;
83   using abigail::tools_utils::abidiff_status;
84 
85   bool is_ok = true;
86   string in_ini_path, in_reference_output_path, out_ini_path, cmd;
87 
88   for (InOutSpec *s = in_out_specs; s->in_ini_path; ++s)
89     {
90       in_ini_path = get_test_src_dir() + "/" + s->in_ini_path;
91       in_reference_output_path =
92 	get_test_src_dir() + "/" + s->in_reference_output_path;
93       out_ini_path = get_test_build_dir() + "/" + s->out_init_path;
94 
95       if (!ensure_parent_dir_created(out_ini_path))
96 	{
97 	  cerr << "could not create parent directory for "
98 	       << out_ini_path;
99 	  is_ok = false;
100 	  continue;
101 	}
102 
103       cmd = get_tools_build_dir() + "/abinilint";
104       if (s->abinilint_options && strcmp(s->abinilint_options, ""))
105 	cmd += " " + string(s->abinilint_options);
106 
107       cmd += " " + in_ini_path + " > " + out_ini_path;
108 
109       bool cmd_is_ok = true;
110       int code = system(cmd.c_str());
111       if (!WIFEXITED(code))
112 	 cmd_is_ok = false;
113       else
114 	{
115 	  abigail::tools_utils::abidiff_status status =
116 	    static_cast<abidiff_status>(WEXITSTATUS(code));
117 	  if (abigail::tools_utils::abidiff_status_has_error(status))
118 	    cmd_is_ok = false;
119 	}
120 
121       if (cmd_is_ok)
122 	{
123 	  cmd = "diff -u " + in_reference_output_path + " " + out_ini_path;
124 	  if (system(cmd.c_str()))
125 	    is_ok = false;
126 	}
127       else
128 	is_ok = false;
129     }
130 
131   return !is_ok;
132 }
133