Coverage Report

Created: 2025-11-24 06:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/yara/libyara/modules/tests/tests.c
Line
Count
Source
1
/*
2
Copyright (c) 2014. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <assert.h>
31
#include <yara/modules.h>
32
33
#define MODULE_NAME tests
34
35
define_function(fsum_2)
36
0
{
37
0
  double a = float_argument(1);
38
0
  double b = float_argument(2);
39
40
0
  return_float(a + b);
41
0
}
42
43
define_function(fsum_3)
44
0
{
45
0
  double a = float_argument(1);
46
0
  double b = float_argument(2);
47
0
  double c = float_argument(3);
48
49
0
  return_float(a + b + c);
50
0
}
51
52
define_function(isum_2)
53
0
{
54
0
  int64_t a = integer_argument(1);
55
0
  int64_t b = integer_argument(2);
56
57
0
  return_integer(a + b);
58
0
}
59
60
define_function(isum_3)
61
0
{
62
0
  int64_t a = integer_argument(1);
63
0
  int64_t b = integer_argument(2);
64
0
  int64_t c = integer_argument(3);
65
66
0
  return_integer(a + b + c);
67
0
}
68
69
define_function(length)
70
0
{
71
0
  char* s = string_argument(1);
72
73
0
  return_integer(strlen(s));
74
0
}
75
76
define_function(empty)
77
0
{
78
0
  return_string("");
79
0
}
80
81
define_function(match)
82
0
{
83
0
  return_integer(
84
0
      yr_re_match(yr_scan_context(), regexp_argument(1), string_argument(2)));
85
0
}
86
87
define_function(foobar)
88
0
{
89
0
  int64_t arg = integer_argument(1);
90
91
0
  switch (arg)
92
0
  {
93
0
  case 1:
94
0
    return_string("foo");
95
0
    break;
96
0
  case 2:
97
0
    return_string("bar");
98
0
    break;
99
0
  }
100
101
0
  return_string("oops")
102
0
}
103
104
12
begin_declarations
105
24
  begin_struct("constants")
106
12
    declare_integer("one");
107
12
    declare_integer("two");
108
12
    declare_string("foo");
109
12
    declare_string("empty");
110
24
  end_struct("constants")
111
112
24
  begin_struct("undefined")
113
12
    declare_integer("i");
114
12
    declare_float("f");
115
24
  end_struct("undefined")
116
117
12
  declare_string("module_data");
118
24
  declare_integer_array("integer_array");
119
24
  declare_string_array("string_array");
120
121
24
  declare_integer_dictionary("integer_dict");
122
24
  declare_string_dictionary("string_dict");
123
124
36
  begin_struct_array("struct_array")
125
12
    declare_integer("i");
126
12
    declare_string("s");
127
24
  end_struct_array("struct_array")
128
129
36
  begin_struct_dictionary("struct_dict")
130
12
    declare_integer("i");
131
12
    declare_string("s");
132
24
  end_struct_dictionary("struct_dict");
133
134
36
  begin_struct_dictionary("empty_struct_dict")
135
12
    declare_integer("unused");
136
24
  end_struct_dictionary("empty_struct_dict")
137
138
36
  begin_struct_array("empty_struct_array")
139
36
    begin_struct_array("struct_array")
140
12
      declare_string("unused");
141
24
    end_struct_array("struct_array");
142
36
    begin_struct_dictionary("struct_dict")
143
12
      declare_string("unused");
144
24
    end_struct_dictionary("struct_dict");
145
24
  end_struct_array("empty_struct_array");
146
147
24
  declare_function("match", "rs", "i", match);
148
12
  declare_function("isum", "ii", "i", isum_2);
149
12
  declare_function("isum", "iii", "i", isum_3);
150
12
  declare_function("fsum", "ff", "f", fsum_2);
151
12
  declare_function("fsum", "fff", "f", fsum_3);
152
12
  declare_function("length", "s", "i", length);
153
12
  declare_function("empty", "", "s", empty);
154
12
  declare_function("foobar", "i", "s", foobar);
155
12
end_declarations
156
157
int module_initialize(YR_MODULE* module)
158
11
{
159
11
  return ERROR_SUCCESS;
160
11
}
161
162
int module_finalize(YR_MODULE* module)
163
0
{
164
0
  return ERROR_SUCCESS;
165
0
}
166
167
int module_load(
168
    YR_SCAN_CONTEXT* context,
169
    YR_OBJECT* module_object,
170
    void* module_data,
171
    size_t module_data_size)
172
0
{
173
0
  yr_set_integer(1, module_object, "constants.one");
174
0
  yr_set_integer(2, module_object, "constants.two");
175
0
  yr_set_string("foo", module_object, "constants.foo");
176
0
  yr_set_string("", module_object, "constants.empty");
177
178
0
  yr_set_integer(1, module_object, "struct_array[1].i");
179
180
0
  yr_set_integer(0, module_object, "integer_array[%i]", 0);
181
0
  yr_set_integer(1, module_object, "integer_array[%i]", 1);
182
0
  yr_set_integer(2, module_object, "integer_array[%i]", 2);
183
0
  yr_set_integer(256, module_object, "integer_array[%i]", 256);
184
185
0
  yr_set_string("foo", module_object, "string_array[%i]", 0);
186
0
  yr_set_string("bar", module_object, "string_array[%i]", 1);
187
0
  yr_set_string("baz", module_object, "string_array[%i]", 2);
188
189
0
  yr_set_sized_string("foo\0bar", 7, module_object, "string_array[%i]", 3);
190
191
0
  yr_set_string("foo", module_object, "string_dict[%s]", "foo");
192
0
  yr_set_string("bar", module_object, "string_dict[\"bar\"]");
193
194
0
  yr_set_string("foo", module_object, "struct_dict[%s].s", "foo");
195
0
  yr_set_integer(1, module_object, "struct_dict[%s].i", "foo");
196
197
0
  if (module_data_size > 0 && module_data != NULL)
198
0
  {
199
0
    yr_set_sized_string(
200
0
        (const char*) module_data,
201
0
        module_data_size,
202
0
        module_object,
203
0
        "module_data");
204
0
  }
205
206
0
  return ERROR_SUCCESS;
207
0
}
208
209
int module_unload(YR_OBJECT* module_object)
210
0
{
211
  // Fail if module_unload is called twice with the same module_object
212
0
  if (module_object->data == (void*) 0xFABADA)
213
0
    assert(false);
214
215
0
  module_object->data = (void*) 0xFABADA;
216
0
  return ERROR_SUCCESS;
217
0
}