test_extractor.c (4377B)
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 void *dp; 50 51 if ((NULL == ec->config) || (0 != strcmp (ec->config, "test"))) 52 return; /* only run in test mode */ 53 if (4 != ec->read (ec->cls, &dp, 4)) 54 { 55 fprintf (stderr, "Reading at offset 0 failed\n"); 56 ABORT (); 57 } 58 if (0 != strncmp ("test", dp, 4)) 59 { 60 fprintf (stderr, "Unexpected data at offset 0\n"); 61 ABORT (); 62 } 63 if ( (1024 * 150 != ec->get_size (ec->cls)) && 64 (UINT64_MAX != ec->get_size (ec->cls)) ) 65 { 66 fprintf (stderr, "Unexpected file size returned (expected 150k)\n"); 67 ABORT (); 68 } 69 if (1024 * 100 + 4 != ec->seek (ec->cls, 1024 * 100 + 4, SEEK_SET)) 70 { 71 fprintf (stderr, "Failure to seek (SEEK_SET)\n"); 72 ABORT (); 73 } 74 if (1 != ec->read (ec->cls, &dp, 1)) 75 { 76 fprintf (stderr, "Failure to read at 100k + 4\n"); 77 ABORT (); 78 } 79 if ((1024 * 100 + 4) % 256 != *(unsigned char *) dp) 80 { 81 fprintf (stderr, "Unexpected data at offset 100k + 4\n"); 82 ABORT (); 83 } 84 if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) != 85 ec->seek (ec->cls, -(1024 * 50 + 7), SEEK_CUR)) 86 { 87 fprintf (stderr, "Failure to seek (SEEK_SET)\n"); 88 ABORT (); 89 } 90 if (1 != ec->read (ec->cls, &dp, 1)) 91 { 92 fprintf (stderr, "Failure to read at 50k - 3\n"); 93 ABORT (); 94 } 95 if (((1024 * 100 + 4) + 1 - (1024 * 50 + 7)) % 256 != *(unsigned char *) dp) 96 { 97 fprintf (stderr, "Unexpected data at offset 100k - 3\n"); 98 ABORT (); 99 } 100 if (1024 * 150 != ec->seek (ec->cls, 0, SEEK_END)) 101 { 102 fprintf (stderr, "Failure to seek (SEEK_END)\n"); 103 ABORT (); 104 } 105 if (0 != ec->read (ec->cls, &dp, 1)) 106 { 107 fprintf (stderr, "Failed to receive EOF at 150k\n"); 108 ABORT (); 109 } 110 if (1024 * 150 - 2 != ec->seek (ec->cls, -2, SEEK_END)) 111 { 112 fprintf (stderr, "Failure to seek (SEEK_END - 2)\n"); 113 ABORT (); 114 } 115 if (1 != ec->read (ec->cls, &dp, 1)) 116 { 117 fprintf (stderr, "Failure to read at 150k - 3\n"); 118 ABORT (); 119 } 120 if ((1024 * 150 - 2) % 256 != *(unsigned char *) dp) 121 { 122 fprintf (stderr, "Unexpected data at offset 150k - 3\n"); 123 ABORT (); 124 } 125 if (0 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT, 126 EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Hello world!", 127 strlen ("Hello world!") + 1)) 128 { 129 fprintf (stderr, "Unexpected return value from 'proc'\n"); 130 ABORT (); 131 } 132 /* The test assumes that client orders us to stop extraction 133 * after seeing "Goodbye!". 134 */ 135 if (1 != ec->proc (ec->cls, "test", EXTRACTOR_METATYPE_COMMENT, 136 EXTRACTOR_METAFORMAT_UTF8, "<no mime>", "Goodbye!", 137 strlen ("Goodbye!") + 1)) 138 { 139 fprintf (stderr, "Unexpected return value from 'proc'\n"); 140 ABORT (); 141 } 142 } 143 144 145 /* end of test_extractor.c */