plugins/check-xor-filt.c

Go to the documentation of this file.
00001 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <dlfcn.h>
00022 
00023 #include "mysqlfs-plugin.h"
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 #include <mysqlfs.h>            /* to grab the DATA_BLOCK_SIZE */
00028 
00032 unsigned char checksum(const unsigned char *x, size_t size)
00033 {
00034     size_t i;
00035     unsigned char s;
00036 
00037     for (i = 0, s = 0; i < size; s = (s + x[i++]) % 256);
00038     return s;
00039 }
00040 
00044 int main(int argc, char **argv)
00045 {
00046     void *handle;
00047     void *(*bufferfilter)(unsigned char, unsigned char, unsigned char);
00048     mysqlfs_plugin *filterentries;
00049     char *error;
00050     int errorcode;
00051     unsigned char ver[] = {0, 0, 0};
00052     unsigned char testdatablock [DATA_BLOCK_SIZE];
00053     unsigned char cksum;
00054 
00055     handle = dlopen ("dummy-xor.so", RTLD_LAZY);
00056     /* already checked by check-xor.c */
00057     if (!handle) { fprintf (stderr, "%s\n", dlerror()); exit(1); }
00058 
00059     dlerror();    /* Clear any existing error */
00060 
00061     bufferfilter = dlsym(handle, "mysqlfs_init");
00062     /* already checked by check-xor.c */
00063     if (NULL != (error = dlerror())) { fprintf (stderr, "%s\n", error); exit(1); }
00064 
00065     /* already checked by check-xor.c */
00066     if (NULL == bufferfilter) { fprintf (stderr, "NULL bufferfilter\n"); exit(1); }
00067 
00068     /* grab the version data */
00069     {
00070         int i;
00071         char *a = strdup (VERSION);
00072         char *v;
00073         if (NULL != a)
00074         {
00075             v = strtok (a, ".");
00076 
00077             for (i = 0; NULL != v && i < (sizeof(ver)/sizeof(ver[0])); i++)
00078             {
00079                 ver[i] = atoi(v);
00080                 v = strtok (NULL, ".");
00081             }
00082             free (a);
00083         }
00084     }
00085 
00086     /* already checked by check-xor.c */
00087     if (NULL == (filterentries = bufferfilter(ver[0], ver[1], ver[2]))) { fprintf (stderr, "NULL jump points\n"); exit(1); }
00088 
00089     /* already checked by check-xor.c */
00090     if (NULL == filterentries->identity) { fprintf (stderr, "NULL identity function\n"); exit(1); }
00091 
00092 
00093     /*
00094      * Configure the plugin stack (in this case, only one)
00095      *
00096      * If this testcase is expanded to help test another more complex pluging, perhaps you'll need some
00097      * additional plugin configuration than just this one attribute.  This part of the code makes a
00098      * good place to stick that :)
00099      */
00100     if (NULL != filterentries->setblocksize)
00101         filterentries->setblocksize(DATA_BLOCK_SIZE);
00102 
00103 
00104     /* fill a data block with trash */
00105     {
00106         int i;          /* yeah, I like how in C++ ypu can type "for (int x = ..." for a local variable */
00107 
00108         /* I'm committing a host of errors here: I am not seeding random, and I'm using the lower-half of the returned values, not the MSB.  I only need kinda-random, so meh. */
00109         for (i = 0; i < sizeof(testdatablock); i++)
00110             testdatablock[i] = (char) random();
00111     }
00112 
00113     /* do we have read/write hooks to test? */
00114     if ((NULL == filterentries->read) || (NULL == filterentries->write))
00115     { fprintf (stderr, "Both read and write hooks must be non-NULL\n"); exit(1); }
00116 
00117     /* grab an initial checksum */
00118     cksum = checksum (testdatablock, sizeof(testdatablock));
00119     
00120     /* munge inbound */
00121     if (0 != (errorcode = filterentries->write (testdatablock, sizeof(testdatablock), 0)))
00122     { fprintf (stderr, "Write hook returned nonzero %d\n", errorcode); exit(1); }
00123 
00124     /* compare checksum, XFAIL: expect failure/inequal */
00125     if (cksum == checksum (testdatablock, sizeof(testdatablock)))
00126     { fprintf (stderr, "Checksum matched, expected mismatch\n"); exit(1); }
00127 
00128     /* munge outbound */
00129     if (0 != (errorcode = filterentries->read (testdatablock, sizeof(testdatablock), 0)))
00130     { fprintf (stderr, "Read hook returned nonzero %d\n", errorcode); exit(1); }
00131 
00132     /* compare checksum */
00133     if (cksum != checksum (testdatablock, sizeof(testdatablock)))
00134     { fprintf (stderr, "Checksum mismatched\n"); exit(1); }
00135 
00136     printf ("OK %s\n", filterentries->identity());
00137     dlclose(handle);
00138     return 0;
00139 }

Generated on Sun Jul 12 20:25:26 2009 for mysqlfs by  doxygen 1.4.7