[Forensics-changes] [yara] 229/368: tests: Add code for working with external files

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:30:42 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.5.0
in repository yara.

commit d68a0968a14e331d51f0aac1a9bff476499c746e
Author: Hilko Bengen <bengen at hilluzination.de>
Date:   Tue Mar 15 22:29:02 2016 +0100

    tests: Add code for working with external files
---
 tests/util.c | 33 +++++++++++++++++++++++++++++++++
 tests/util.h | 21 +++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/tests/util.c b/tests/util.c
index 32af34d..1719578 100644
--- a/tests/util.c
+++ b/tests/util.c
@@ -17,6 +17,10 @@ limitations under the License.
 #include <stdio.h>
 #include <unistd.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
 #include <yara.h>
 
 char compile_error[1024];
@@ -188,3 +192,32 @@ int capture_string(
 
   return f.found;
 }
+
+
+int read_file(
+    char* filename, char** buf)
+{
+  int fd;
+  if ((fd = open(filename, O_RDONLY)) < 0) {
+    return -1;
+  }
+  size_t sz = lseek(fd, 0, SEEK_END);
+  int rc = -1;
+  if (sz == -1) {
+    goto _exit;
+  }
+  if (lseek(fd, 0, SEEK_SET) != 0) {
+    goto _exit;
+  }
+  if ((*buf = malloc(sz)) == NULL) {
+    goto _exit;
+  }
+  if ((rc = read(fd, *buf, sz)) != sz) {
+    rc = -1;
+    free(*buf);
+  }
+
+_exit:
+  close(fd);
+  return rc;
+}
diff --git a/tests/util.h b/tests/util.h
index d43c9f6..8ecb168 100644
--- a/tests/util.h
+++ b/tests/util.h
@@ -40,6 +40,10 @@ int capture_string(
     char* expected_string);
 
 
+int read_file(
+    char* filename, char** buf);
+
+
 #define assert_true_rule(rule, string)                                  \
   do {                                                                  \
     if (!matches_string(rule, string)) {                                \
@@ -58,6 +62,23 @@ int capture_string(
     }                                                                   \
   } while (0);
 
+#define assert_true_rule_file(rule, filename)                           \
+  do {                                                                  \
+    char* buf;                                                          \
+    size_t sz;                                                          \
+    if ((sz = read_file(filename, &buf)) == -1) {                       \
+      fprintf(stderr, "%s:%d: cannot read file '%s'\n",                 \
+              __FILE__, __LINE__, filename);                            \
+      exit(EXIT_FAILURE);                                               \
+    }                                                                   \
+    if (!matches_blob(rule, (uint8_t*) (buf), sz)) {                    \
+      fprintf(stderr, "%s:%d: rule does not match contents of"          \
+              "'%s' (but should)\n",                                    \
+              __FILE__, __LINE__, filename);                            \
+      exit(EXIT_FAILURE);                                               \
+    }                                                                   \
+  } while (0);
+
 #define assert_false_rule(rule, string)                                 \
   do {                                                                  \
     if (matches_string(rule, string)) {                                 \

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list