1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 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 "data/test-ini/test02-buggy-property-value.abignore",
53 "data/test-ini/test02-buggy-property-value.abignore.expected",
54 "output/test-ini/test02-buggy-property-value.abignore",
55 ""
56 }
57 ,
58 // This one must always remain the last one.
59 {0, 0, 0, 0}
60 };
61
62 /// @return the test source directory.
63 static string
get_test_src_dir()64 get_test_src_dir()
65 {
66 using abigail::tests::get_src_dir;
67 return string(get_src_dir()) + "/tests";
68 }
69
70 /// @return the test build directory.
71 static string
get_test_build_dir()72 get_test_build_dir()
73 {
74 using abigail::tests::get_build_dir;
75 return string(get_build_dir()) + "/tests";
76 }
77
78 /// @return the tools directory under the build directory;
79 static string
get_tools_build_dir()80 get_tools_build_dir()
81 {
82 using abigail::tests::get_build_dir;
83 return string(get_build_dir()) + "/tools";
84 }
85
86 int
main()87 main()
88 {
89 using abigail::tests::get_build_dir;
90 using abigail::tools_utils::ensure_parent_dir_created;
91 using abigail::tools_utils::abidiff_status;
92
93 unsigned int total_count = 0, passed_count = 0, failed_count = 0;
94
95 string in_ini_path, in_reference_output_path, out_ini_path, cmd, diff_cmd;
96
97 for (InOutSpec *s = in_out_specs; s->in_ini_path; ++s)
98 {
99 bool is_ok = true;
100 in_ini_path = get_test_src_dir() + "/" + s->in_ini_path;
101 in_reference_output_path =
102 get_test_src_dir() + "/" + s->in_reference_output_path;
103 out_ini_path = get_test_build_dir() + "/" + s->out_init_path;
104
105 if (!ensure_parent_dir_created(out_ini_path))
106 {
107 cerr << "could not create parent directory for "
108 << out_ini_path;
109 is_ok = false;
110 continue;
111 }
112
113 cmd = get_tools_build_dir() + "/abinilint";
114 if (s->abinilint_options && strcmp(s->abinilint_options, ""))
115 cmd += " " + string(s->abinilint_options);
116
117 cmd += " " + in_ini_path + " > " + out_ini_path;
118
119 bool cmd_is_ok = true;
120 int code = system(cmd.c_str());
121 if (!WIFEXITED(code))
122 cmd_is_ok = false;
123 else
124 {
125 abigail::tools_utils::abidiff_status status =
126 static_cast<abidiff_status>(WEXITSTATUS(code));
127 if (abigail::tools_utils::abidiff_status_has_error(status))
128 cmd_is_ok = false;
129 }
130
131 if (cmd_is_ok)
132 {
133 diff_cmd = "diff -u " + in_reference_output_path + " " + out_ini_path;
134 if (system(diff_cmd.c_str()))
135 is_ok = false;
136 }
137 else
138 is_ok = false;
139
140 emit_test_status_and_update_counters(is_ok, cmd, passed_count,
141 failed_count, total_count);
142 }
143
144 emit_test_summary(total_count, passed_count, failed_count);
145
146 return failed_count;
147 }
148