test_extractor.c (4470B)
1 /* 2 This file is part of libextractor. 3 Copyright (C) 2012 Vidyut Samanta and Christian Grothoff 4 5 libextractor is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published 7 by the Free Software Foundation; either version 3, or (at your 8 option) any later version. 9 10 libextractor is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with libextractor; see the file COPYING. If not, write to the 17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 */ 20 /** 21 * @file main/test_extractor.c 22 * @brief plugin for testing GNU libextractor 23 * Data file (or buffer) for this test must be 150 * 1024 bytes long, 24 * first 4 bytes must be "test", all other bytes should be equal to 25 * <FILE_OFFSET> % 256. The test client must return 0 after seeing 26 * "Hello World!" metadata, and return 1 after seeing "Goodbye!" 27 * metadata. 28 * @author Christian Grothoff 29 */ 30 #include "platform.h" 31 #include "extractor.h" 32 #include <string.h> 33 #include <stdio.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <unistd.h> 37 #include <stdlib.h> 38 39 40 /** 41 * Signature of the extract method that each plugin 42 * must provide. 43 * 44 * @param ec extraction context provided to the plugin 45 */ 46 _EXTRACTOR_EXTERN void 47 EXTRACTOR_test_extract_method (struct EXTRACTOR_ExtractContext *ec); 48 49 _EXTRACTOR_EXTERN void 50 EXTRACTOR_test_extract_method (struct EXTRACTOR_ExtractContext *ec) 51 { 52 void *dp; 53 54 if ((NULL == ec->config) || (0 != strcmp (ec->config, "test"))) 55 return; /* only run in test mode */ 56 if (4 != ec->read (ec->cls, &dp, 4)) 57 { 58 fprintf (stderr, "Reading at offset 0 failed\n"); 59 ABORT (); 60 } 61 if (0 != strncmp ("test", dp, 4)) 62 { 63 fprintf (stderr, "Unexpected data at offset 0\n"); 64 ABORT (); 65 } 66 if ( (1024 * 150 != ec->get_size (ec->cls)) && 67 (UINT64_MAX != ec->get_size (ec->cls)) ) 68 { 69 fprintf (stderr, "Unexpected file size returned (expected 150k)\n"); 70 ABORT (); 71 } 72 if (1024 * 100 + 4 != ec->seek (ec->cls, 1024 * 100 + 4, SEEK_SET)) 73 { 74 fprintf (stderr, "Failure to seek (SEEK_SET)\n"); 75 ABORT (); 76 } 77 if (1 != ec->read (ec->cls, &dp, 1)) 78 { 79 fprintf (stderr, "Failure to read at 100k + 4\n"); 80 ABORT (); 81 } 82 if ((1024 * 100 + 4) % 256 != *(unsigned char *) dp) 83 { 84 fprintf (stderr, "Unexpected data at offset 100k + 4\n"); 85 ABORT (); 86 } 87 if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) != 88 ec->seek (ec->cls, -(1024 * 50 + 7), SEEK_CUR)) 89 { 90 fprintf (stderr, "Failure to seek (SEEK_SET)\n"); 91 ABORT (); 92 } 93 if (1 != ec->read (ec->cls, &dp, 1)) 94 { 95 fprintf (stderr, "Failure to read at 50k - 3\n"); 96 ABORT (); 97 } 98 if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) % 256 != *(unsigned char *) dp) 99 { 100 fprintf (stderr, "Unexpected data at offset 100k - 3\n"); 101 ABORT (); 102 } 103 if (1024 * 150 != ec->seek (ec->cls, 0, SEEK_END)) 104 { 105 fprintf (stderr, "Failure to seek (SEEK_END)\n"); 106 ABORT (); 107 } 108 if (0 != ec->read (ec->cls, &dp, 1)) 109 { 110 fprintf (stderr, "Failed to receive EOF at 150k\n"); 111 ABORT (); 112 } 113 if (1024 * 150 - 2 != ec->seek (ec->cls, -2, SEEK_END)) 114 { 115 fprintf (stderr, "Failure to seek (SEEK_END - 2)\n"); 116 ABORT (); 117 } 118 if (1 != ec->read (ec->cls, &dp, 1)) 119 { 120 fprintf (stderr, "Failure to read at 150k - 3\n"); 121 ABORT (); 122 } 123 if ((1024 * 150 - 2) % 256 != *(unsigned char *) dp) 124 { 125 fprintf (stderr, "Unexpected data at offset 150k - 3\n"); 126 ABORT (); 127 } 128 if (0 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT, 129 EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Hello world!", 130 strlen ("Hello world!") + 1)) 131 { 132 fprintf (stderr, "Unexpected return value from 'proc'\n"); 133 ABORT (); 134 } 135 /* The test assumes that client orders us to stop extraction 136 * after seeing "Goodbye!". 137 */ 138 if (1 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT, 139 EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Goodbye!", 140 strlen ("Goodbye!") + 1)) 141 { 142 fprintf (stderr, "Unexpected return value from 'proc'\n"); 143 ABORT (); 144 } 145 } 146 147 148 /* end of test_extractor.c */