File: test-string.c

package info (click to toggle)
yara 4.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,884 kB
  • sloc: ansic: 52,295; yacc: 2,895; lex: 2,019; cpp: 863; makefile: 479; javascript: 85; sh: 47; python: 35
file content (143 lines) | stat: -rw-r--r-- 3,322 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <yara.h>

#include "util.h"

int main(int argc, char** argv)
{
  int result = 0;

  YR_DEBUG_INITIALIZE();
  YR_DEBUG_FPRINTF(1, stderr, "+ %s() { // in %s\n", __FUNCTION__, argv[0]);

  yr_initialize();

  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"1234\") == 1234 \
      }",
      NULL);

  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"-1\") == -1 \
      }",
      NULL);

  // Leading spaces and + are allowed.
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\" +1\") == 1 \
      }",
      NULL);

  // Strings can be prefixed with 0x and will be interpreted as hexadecimal.
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"0x10\") == 16 \
      }",
      NULL);

  // Strings prefixed with 0 will be interpreted as octal.
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"010\") == 8 \
      }",
      NULL);

  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"10\", 8) == 8 \
      }",
      NULL);

  // Base 0 is a special case that tries to interpret the string by prefix, or
  // default to decimal. We aren't doing anything special to get this, it is
  // part of strtoll by default.
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.to_int(\"010\", 0) == 8 and \
          string.to_int(\"0x10\", 0) == 16 and \
          string.to_int(\"10\", 0) == 10 \
      }",
      NULL);

  // Test undefined cases

  // on invalid base value
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          not defined string.to_int(\"1\", -1) and \
          not defined string.to_int(\"1\", 1) and \
          not defined string.to_int(\"1\", 37) \
      }",
      NULL);

  // on underflow or underflow
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          not defined string.to_int(\"9223372036854775808\") \
      }",
      NULL);
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          not defined string.to_int(\"-9223372036854775809\") \
      }",
      NULL);

  // if parsing does not use all the string
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          not defined string.to_int(\"FOO\") and \
          not defined string.to_int(\"10A20\") \
      }",
      NULL);

  // if parsing does not consume any digits
  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          not defined string.to_int(\"\") and \
          not defined string.to_int(\"   -\") and \
          not defined string.to_int(\" +0x\") \
      }",
      NULL);

  assert_true_rule(
      "import \"string\" \
      rule test { \
        condition: \
          string.length(\"AXS\\x00ERS\") == 7 \
      }",
      NULL);

  yr_finalize();

  YR_DEBUG_FPRINTF(
      1, stderr, "} = %d // %s() in %s\n", result, __FUNCTION__, argv[0]);

  return result;
}