aboutsummaryrefslogtreecommitdiff
path: root/src/main/test_extractor.c
blob: 8c15fca72110bfdfa0b5a104b90673ed65175125 (plain) (blame)
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
144
145
/*
     This file is part of libextractor.
     Copyright (C) 2012 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */
/**
 * @file main/test_extractor.c
 * @brief plugin for testing GNU libextractor
 * Data file (or buffer) for this test must be 150 * 1024 bytes long,
 * first 4 bytes must be "test", all other bytes should be equal to
 * <FILE_OFFSET> % 256. The test client must return 0 after seeing
 * "Hello World!" metadata, and return 1 after seeing "Goodbye!"
 * metadata.
 * @author Christian Grothoff
 */
#include "platform.h"
#include "extractor.h"
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>



/**
 * Signature of the extract method that each plugin
 * must provide.
 *
 * @param ec extraction context provided to the plugin
 */
_EXTRACTOR_EXTERN void
EXTRACTOR_test_extract_method (struct EXTRACTOR_ExtractContext *ec)
{
  void *dp;

  if ((NULL == ec->config) || (0 != strcmp (ec->config, "test")))
    return; /* only run in test mode */
  if (4 != ec->read (ec->cls, &dp, 4))
    {
      fprintf (stderr, "Reading at offset 0 failed\n");
      ABORT ();
    }
  if (0 != strncmp ("test", dp, 4))
    {
      fprintf (stderr, "Unexpected data at offset 0\n");
      ABORT ();
    }
  if ( (1024 * 150 != ec->get_size (ec->cls)) &&
       (UINT64_MAX != ec->get_size (ec->cls)) )
    {
      fprintf (stderr, "Unexpected file size returned (expected 150k)\n");
      ABORT ();
    }
  if (1024 * 100 + 4 != ec->seek (ec->cls, 1024 * 100 + 4, SEEK_SET))
    {
      fprintf (stderr, "Failure to seek (SEEK_SET)\n");
      ABORT ();
    }
  if (1 != ec->read (ec->cls, &dp, 1))
    {
      fprintf (stderr, "Failure to read at 100k + 4\n");
      ABORT ();
    }
  if ((1024 * 100 + 4) % 256 != * (unsigned char *) dp)
    {
      fprintf (stderr, "Unexpected data at offset 100k + 4\n");
      ABORT ();
    }
  if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) !=
      ec->seek (ec->cls, - (1024 * 50 + 7), SEEK_CUR))
    {
      fprintf (stderr, "Failure to seek (SEEK_SET)\n");
      ABORT ();
    }
  if (1 != ec->read (ec->cls, &dp, 1))
    {
      fprintf (stderr, "Failure to read at 50k - 3\n");
      ABORT ();
    }
  if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) % 256 != * (unsigned char *) dp)
    {
      fprintf (stderr, "Unexpected data at offset 100k - 3\n");
      ABORT ();
    }
  if (1024 * 150 != ec->seek (ec->cls, 0, SEEK_END))
    {
      fprintf (stderr, "Failure to seek (SEEK_END)\n");
      ABORT ();
    }
  if (0 != ec->read (ec->cls, &dp, 1))
    {
      fprintf (stderr, "Failed to receive EOF at 150k\n");
      ABORT ();
    }
  if (1024 * 150 - 2 != ec->seek (ec->cls, -2, SEEK_END))
    {
      fprintf (stderr, "Failure to seek (SEEK_END - 2)\n");
      ABORT ();
    }
  if (1 != ec->read (ec->cls, &dp, 1))
    {
      fprintf (stderr, "Failure to read at 150k - 3\n");
      ABORT ();
    }
  if ((1024 * 150 - 2) % 256 != * (unsigned char *) dp)
    {
      fprintf (stderr, "Unexpected data at offset 150k - 3\n");
      ABORT ();
    }
  if (0 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT,
      EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Hello world!",
      strlen ("Hello world!") + 1))
    {
      fprintf (stderr, "Unexpected return value from 'proc'\n");
      ABORT ();
    }
  /* The test assumes that client orders us to stop extraction
   * after seeing "Goodbye!".
   */
  if (1 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT,
      EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Goodbye!",
      strlen ("Goodbye!") + 1))
    {
      fprintf (stderr, "Unexpected return value from 'proc'\n");
      ABORT ();
    }
}

/* end of test_extractor.c */