Back to index

libcitadel  8.12
test-mime-data.c
Go to the documentation of this file.
00001 /* test-mime-data.c: tests for the mime implementation
00002  *
00003  * More info can be found at http://www.freedesktop.org/standards/
00004  * 
00005  * Copyright (C) 2005  Red Hat, Inc.
00006  * Copyright (C) 2005  Matthias Clasen <mclasen@redhat.com>
00007  *
00008  * Licensed under the Academic Free License version 2.0
00009  * Or under the following terms:
00010  * 
00011  * This library is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2 of the License, or (at your option) any later version.
00015  *
00016  * This library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this library; if not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00024  * Boston, MA 02111-1307, USA.
00025  */
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <libgen.h>
00031 
00032 #include "xdgmime.h"
00033 
00034 static int error = 0;
00035 static int total = 0;
00036 static int failed = 0;
00037 static int xfailed = 0;
00038 static int xmatch = 0;
00039 
00040 static int verbose = 0;
00041 
00042 static void
00043 check_mime_type (const char *mt,
00044                const char *mt_expected,
00045                  int         xfail,
00046                const char *test,
00047                const char *filename)
00048 {
00049   total++;
00050 
00051   if (strcmp (mt, mt_expected) != 0)
00052     {
00053       failed++;
00054       
00055       if (xfail)
00056        {
00057          xfailed++;
00058          
00059          if (verbose > 1)
00060          printf ("%s, '%s' test: expected %s, got %s (expected failure)\n", 
00061                 filename, test, mt_expected, mt);
00062        }
00063       else
00064        {
00065          if (verbose > 0)
00066            printf ("%s, '%s' test: expected %s, got %s\n", 
00067                   filename, test, mt_expected, mt);
00068        }
00069 
00070     }
00071   else
00072     {
00073       if (xfail)
00074        {
00075          xmatch++;
00076          
00077          if (verbose > 1)
00078          printf ("%s, '%s' test: got %s (unexpected match)\n", 
00079                 filename, test, mt);
00080        }
00081     }
00082 
00083 }
00084 
00085 static void 
00086 test_by_name (const char *filename,
00087              const char *mt_expected,
00088               int         xfail)
00089 {
00090   const char *mt;
00091 
00092   mt = xdg_mime_get_mime_type_from_file_name (filename);
00093 
00094   check_mime_type (mt, mt_expected, xfail, "name", filename);
00095 }
00096 
00097 static void 
00098 test_by_data (const char *dir,
00099              const char *filename,
00100              const char *mt_expected,
00101               int         xfail)
00102 {
00103   FILE  *file;
00104   const char *mt;
00105   int max_extent;
00106   char *data;
00107   int bytes_read;
00108   char path[1024];
00109 
00110   snprintf (path, 1024, "%s/%s", dir, filename);
00111 
00112   file = fopen (path, "r");
00113 
00114   if (file == NULL)
00115     {
00116       printf ("Could not open %s\n", path);
00117       error++;
00118 
00119       return;
00120     }
00121 
00122   max_extent = xdg_mime_get_max_buffer_extents ();
00123   data = malloc (max_extent);
00124 
00125   if (data == NULL)
00126     {
00127       printf ("Failed to allocate memory for file %s\n", filename);
00128       error++;
00129 
00130       return;
00131     }
00132 
00133   bytes_read = fread (data, 1, max_extent, file);
00134   
00135   if (ferror (file))
00136     {
00137       printf ("Error reading file %s\n", path);
00138       error++;
00139 
00140       free (data);
00141       fclose (file);
00142       
00143      return;
00144     }
00145 
00146   mt = xdg_mime_get_mime_type_for_data (data, bytes_read);
00147   
00148   free (data);
00149   fclose (file);
00150   
00151   check_mime_type (mt, mt_expected, xfail, "data", filename);
00152 }
00153 
00154 static void 
00155 test_by_file (const char *dir,
00156              const char *filename,
00157              const char *mt_expected,
00158               int         xfail)
00159 {
00160   const char *mt;
00161   char path[1024];
00162 
00163   snprintf (path, 1024, "%s/%s", dir, filename);
00164 
00165   mt = xdg_mime_get_mime_type_for_file (path, NULL);
00166   
00167   check_mime_type (mt, mt_expected, xfail, "file", filename);
00168 }
00169 
00170 static void
00171 test_single_file (const char *dir,
00172                 const char *filename,
00173                   const char *mimetype,
00174                   int         xfail_name,
00175                   int         xfail_data,
00176                   int         xfail_file)
00177 {
00178   test_by_name (filename, mimetype, xfail_name);
00179   test_by_data (dir, filename, mimetype, xfail_data);
00180   test_by_file (dir, filename, mimetype, xfail_file);
00181 }
00182 
00183 static void
00184 read_from_file (const char *filename)
00185 {
00186   FILE *file;
00187   char line[255];
00188   int lineno = 0;
00189   char *testfile, *mimetype, *flags;
00190   char *rest, *space, end;
00191   char *dir, *tmp;
00192   int xfail_name, xfail_data, xfail_file;
00193   
00194   file = fopen (filename, "r");
00195   tmp = strdup (filename);
00196   dir = strdup (dirname (tmp));
00197   free (tmp);
00198 
00199   if (file == NULL)
00200     {
00201       printf ("Could not open %s\n", filename);
00202       exit (1);
00203     }
00204 
00205   while (fgets (line, 255, file) != NULL)
00206     {
00207       lineno++;
00208 
00209       rest = line;
00210       while (*rest == ' ') rest++;
00211       
00212       if (*rest == '#' || *rest == '\n')
00213        continue;
00214 
00215       space = strchr (rest, ' ');
00216       if (!space)
00217        {
00218          printf ("Malformed line in %s:%d\n\t%s\n", filename, lineno, line);
00219          continue;
00220        }
00221 
00222       *space = '\0';
00223       testfile = rest;
00224 
00225       rest = space + 1;
00226       while (*rest == ' ') rest++;
00227       space = rest;
00228       while (*space != ' ' && *space != '\n') space++;
00229       end = *space;
00230 
00231       *space = '\0';
00232       mimetype = rest;
00233       
00234       xfail_name = 0;
00235       xfail_data = 0;
00236       xfail_file = 0;
00237 
00238       if (end != '\n')
00239        {
00240          rest = space + 1;
00241          while (*rest == ' ') rest++;
00242          space = rest;
00243          while (*space != ' ' && *space != '\n') space++;
00244          end = *space;
00245          
00246          *space = '\0';
00247           flags = rest;
00248 
00249           switch (strlen (flags))
00250            {
00251              default:
00252                printf ("%s.%d: Extra flags are ignored\n", filename, lineno);
00253                /* Fall thu  */
00254              case 3:
00255                if (flags[2] == 'x')
00256                  xfail_file = 1;
00257                /* Fall thu  */
00258              case 2:
00259                if (flags[1] == 'x')
00260                  xfail_data = 1;
00261                /* Fall thu  */
00262              case 1: 
00263                if (flags[0] == 'x')
00264                  xfail_name = 1;
00265                break;
00266              case 0: ;
00267                /* Should not happen */
00268           }
00269         }
00270 
00271       test_single_file (dir, testfile, mimetype, 
00272                         xfail_name, xfail_data, xfail_file);
00273     }
00274 
00275   fclose (file);
00276 
00277   free (dir);
00278 }
00279 
00280 static void
00281 usage (void)
00282 {
00283   printf ("usage: test-mime-data <FILE>\n\n");
00284   printf ("Tests the mime system.\n");
00285   printf ("Testcases are specified in the following format:\n\n");
00286   printf ("# comment\n");
00287   printf ("<filename1> <mimetype> [<flags>]\n");
00288   printf ("<filename2> <mimetype> [<flags>]\n");
00289   printf ("...\n\n");
00290   printf ("Where an 'x' in the 1st, 2nd or 3rd position in <flags>\n");
00291   printf ("indicates an expected failure when determining the\n");
00292   printf ("mimetype by name, data or file.\n");
00293 
00294   exit (1);
00295 }
00296 
00297 int 
00298 main (int argc, char *argv[])
00299 {
00300   int i;
00301 
00302   if (argc < 2)
00303     usage ();
00304   
00305   for (i = 1; i < argc; i++)
00306     {
00307       if (strcmp (argv[i], "-v") == 0)
00308        verbose++;
00309       else
00310        read_from_file (argv[i]);
00311     }
00312   
00313   if (error > 0 || failed > 0)
00314     {
00315       printf ("%d errors, %d comparisons, %d failed",
00316              error, total, failed);
00317       if (xfailed > 0)
00318        printf (" (%d expected)", xfailed);
00319       if (xmatch > 0)
00320        printf (", %d unexpected successes", xmatch);
00321       printf ("\n");
00322 
00323       return 1;
00324     }
00325 
00326   return 0;
00327 }