- commit current state of testcase
[platform/upstream/libsolv.git] / ext / testcase.c
1 /*
2  * Copyright (c) 2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <limits.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include "pool.h"
18 #include "poolarch.h"
19 #include "poolvendor.h"
20 #include "repo.h"
21 #include "repo_susetags.h"
22 #include "solver.h"
23 #include "solverdebug.h"
24 #include "chksum.h"
25 #include "testcase.h"
26 #include "solv_xfopen.h"
27
28 #ifndef ENABLE_SUSEREPO
29 # define DISABLE_JOIN2
30 # include "tools_util.h"
31 #else
32 # include "repo_susetags.h"
33 #endif
34
35 static struct job2str {
36   Id job;
37   const char *str;
38 } job2str[] = {
39   { SOLVER_NOOP,          "noop" },
40   { SOLVER_INSTALL,       "install" },
41   { SOLVER_ERASE,         "erase" },
42   { SOLVER_UPDATE,        "update" },
43   { SOLVER_WEAKENDEPS,    "weakendeps" },
44   { SOLVER_NOOBSOLETES,   "noobsoletes" },
45   { SOLVER_LOCK,          "lock" },
46   { SOLVER_DISTUPGRADE,   "distupgrade" },
47   { SOLVER_VERIFY,        "verify" },
48   { SOLVER_DROP_ORPHANED, "droporphaned" },
49   { SOLVER_USERINSTALLED, "userinstalled" },
50   { 0, 0 }
51 };
52
53 static struct jobflags2str {
54   Id flag;
55   const char *str;
56 } jobflags2str[] = {
57   { SOLVER_WEAK,      "weak" },
58   { SOLVER_ESSENTIAL, "essential" },
59   { SOLVER_CLEANDEPS, "cleandeps" },
60   { SOLVER_SETEV,     "setev" },
61   { SOLVER_SETEVR,    "setevr" },
62   { SOLVER_SETARCH,   "setarch" },
63   { SOLVER_SETVENDOR, "setvendor" },
64   { SOLVER_SETREPO,   "setrepo" },
65   { SOLVER_NOAUTOSET, "noautoset" },
66   { 0, 0 }
67 };
68
69 static struct resultflags2str {
70   Id flag;
71   const char *str;
72 } resultflags2str[] = {
73   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
74   { TESTCASE_RESULT_PROBLEMS,           "problems" },
75   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
76   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
77   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
78   { 0, 0 }
79 };
80
81 static struct solverflags2str {
82   Id flag;
83   const char *str;
84   int def;
85 } solverflags2str[] = {
86   { SOLVER_FLAG_ALLOW_DOWNGRADE,            "allowdowngrade", 0 },
87   { SOLVER_FLAG_ALLOW_ARCHCHANGE,           "allowarchchange", 0 },
88   { SOLVER_FLAG_ALLOW_VENDORCHANGE,         "allowvendorchange", 0 },
89   { SOLVER_FLAG_ALLOW_UNINSTALL,            "allowuninstall", 0 },
90   { SOLVER_FLAG_NO_UPDATEPROVIDE,           "noupdateprovide", 0 },
91   { SOLVER_FLAG_SPLITPROVIDES,              "splitprovides", 0 },
92   { SOLVER_FLAG_IGNORE_RECOMMENDED,         "ignorerecommended", 0 },
93   { SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED, "ignorealreadyrecommended", 0 },
94   { SOLVER_FLAG_NO_INFARCHCHECK,            "noinfarchcheck", 0 },
95   { 0, 0, 0 }
96 };
97
98
99 typedef struct strqueue {
100   char **str;
101   int nstr;
102 } Strqueue;
103
104 #define STRQUEUE_BLOCK 63
105
106 static void
107 strqueue_init(Strqueue *q)
108 {
109   q->str = 0;
110   q->nstr = 0;
111 }
112
113 static void
114 strqueue_free(Strqueue *q)
115 {
116   int i;
117   for (i = 0; i < q->nstr; i++)
118     solv_free(q->str[i]);
119   q->str = solv_free(q->str);
120   q->nstr = 0;
121 }
122
123 static void
124 strqueue_push(Strqueue *q, const char *s)
125 {
126   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
127   q->str[q->nstr++] = solv_strdup(s);
128 }
129
130 static void
131 strqueue_pushjoin(Strqueue *q, const char *s1, const char *s2, const char *s3)
132 {
133   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
134   q->str[q->nstr++] = solv_dupjoin(s1, s2, s3);
135 }
136
137 static int
138 strqueue_sort_cmp(const void *ap, const void *bp, void *dp)
139 {
140   const char *a = *(const char **)ap;
141   const char *b = *(const char **)bp;
142   return strcmp(a ? a : "", b ? b : "");
143 }
144
145 static void
146 strqueue_sort(Strqueue *q)
147 {
148   if (q->nstr > 1)
149     solv_sort(q->str, q->nstr, sizeof(*q->str), strqueue_sort_cmp, 0);
150 }
151
152 static void
153 strqueue_sort_u(Strqueue *q)
154 {
155   int i, j;
156   strqueue_sort(q);
157   for (i = j = 0; i < q->nstr; i++)
158     if (!j || strqueue_sort_cmp(q->str + i, q->str + j - 1, 0) != 0)
159       q->str[j++] = q->str[i];
160   q->nstr = j;
161 }
162
163 static char *
164 strqueue_join(Strqueue *q)
165 {
166   int i, l = 0;
167   char *r, *rp;
168   for (i = 0; i < q->nstr; i++)
169     if (q->str[i])
170       l += strlen(q->str[i]) + 1;
171   l++;  /* trailing \0 */
172   r = solv_malloc(l);
173   rp = r;
174   for (i = 0; i < q->nstr; i++)
175     if (q->str[i])
176       {
177         strcpy(rp, q->str[i]);
178         rp += strlen(rp);
179         *rp++ = '\n';
180       }
181   *rp = 0;
182   return r;
183 }
184
185 static void
186 strqueue_split(Strqueue *q, const char *s)
187 {
188   const char *p;
189   while ((p = strchr(s, '\n')) != 0)
190     {
191       q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
192       q->str[q->nstr] = solv_malloc(p - s + 1);
193       if (p > s)
194         memcpy(q->str[q->nstr], s, p - s);
195       q->str[q->nstr][p - s] = 0;
196       q->nstr++;
197       s = p + 1;
198     }
199   if (*s)
200     strqueue_push(q, s);
201 }
202
203 static void
204 strqueue_diff(Strqueue *sq1, Strqueue *sq2, Strqueue *osq)
205 {
206   int i = 0, j = 0;
207   while (i < sq1->nstr && j < sq2->nstr)
208     {
209       int r = strqueue_sort_cmp(sq1->str + i, sq2->str + j, 0);
210       if (!r)
211         i++, j++;
212       else if (r < 0)
213         strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
214       else
215         strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
216     }
217   while (i < sq1->nstr)
218     strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
219   while (j < sq2->nstr)
220     strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
221 }
222
223 static inline int
224 pool_isknownarch(Pool *pool, Id id)
225 {
226   if (!id || id == ID_EMPTY)
227     return 0;
228   if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
229     return 1;
230   if (!pool->id2arch || (id > pool->lastarch || !pool->id2arch[id]))
231     return 0;
232   return 1;
233 }
234
235 Id
236 testcase_str2dep(Pool *pool, char *s)
237 {
238   char *n, *a;
239   Id id;
240   int flags;
241
242   if ((n = strchr(s, '|')) != 0)
243     {    
244       id = testcase_str2dep(pool, n + 1);
245       *n = 0; 
246       id = pool_rel2id(pool, testcase_str2dep(pool, s), id, REL_OR, 1);
247       *n = '|'; 
248       return id;
249     }
250   while (*s == ' ' || *s == '\t')
251     s++;
252   n = s;
253   while (*s && *s != ' ' && *s != '\t' && *s != '<' && *s != '=' && *s != '>')
254     {
255       if (*s == '(')
256         {
257           while (*s && *s != ')')
258             s++;
259         }
260       else
261         s++;
262     }
263   if ((a = strchr(n, '.')) != 0 && a + 1 < s && s[-1] != ')')
264     {
265       Id archid = pool_strn2id(pool, a + 1, s - (a + 1), 0);
266       if (pool_isknownarch(pool, archid))
267         {
268           id = pool_strn2id(pool, n, a - n, 1);
269           id = pool_rel2id(pool, id, archid, REL_ARCH, 1);
270         }
271       else
272         id = pool_strn2id(pool, n, s - n, 1);
273     }
274   else
275     id = pool_strn2id(pool, n, s - n, 1);
276   if (!*s)
277     return id;
278   while (*s == ' ' || *s == '\t')
279     s++;
280   flags = 0;
281   for (;;s++)
282     {  
283       if (*s == '<')
284         flags |= REL_LT;
285       else if (*s == '=')
286         flags |= REL_EQ;
287       else if (*s == '>')
288         flags |= REL_GT;
289       else
290         break;
291     }
292   if (!flags)
293     return id;
294   while (*s == ' ' || *s == '\t')
295     s++;
296   n = s;
297   while (*s && *s != ' ' && *s != '\t')
298     s++;
299   return pool_rel2id(pool, id, pool_strn2id(pool, n, s - n, 1), flags, 1);
300 }
301
302 const char *
303 testcase_repoid2str(Pool *pool, Id repoid)
304 {
305   Repo *repo = pool_id2repo(pool, repoid);
306   if (repo->name)
307     {
308       char *r = pool_tmpjoin(pool, repo->name, 0, 0);
309       char *rp;
310       for (rp = r; *rp; rp++)
311         if (*rp == ' ' || *rp == '\t')
312           *rp = '_';
313       return r;
314     }
315   else
316     {
317       char buf[20];
318       sprintf(buf, "@#%d", repoid);
319       return pool_tmpjoin(pool, buf, 0, 0);
320     }
321 }
322
323 const char *
324 testcase_solvid2str(Pool *pool, Id p)
325 {
326   Solvable *s = pool->solvables + p;
327   const char *str;
328   char buf[20];
329
330   if (p == SYSTEMSOLVABLE)
331     return "@SYSTEM";
332   str = pool_solvid2str(pool, p);
333   if (!s->repo)
334     return pool_tmpappend(pool, str, "@", 0);
335   if (s->repo->name)
336     {
337       int l = strlen(str);
338       char *str2 = pool_tmpappend(pool, str, "@", s->repo->name);
339       for (; str2[l]; l++)
340         if (str2[l] == ' ' || str2[l] == '\t')
341           str2[l] = '_';
342       return str2;
343     }
344   sprintf(buf, "@#%d", s->repo->repoid);
345   return pool_tmpappend(pool, str, buf, 0);
346 }
347
348 Repo *
349 testcase_str2repo(Pool *pool, const char *str)
350 {
351   int repoid;
352   Repo *repo = 0;
353   if (str[0] == '#' && (str[1] >= '0' && str[1] <= '9'))
354     {
355       int j;
356       repoid = 0;
357       for (j = 1; str[j] >= '0' && str[j] <= '9'; j++)
358         repoid = repoid * 10 + (str[j] - '0');
359       if (!str[j] && repoid > 0 && repoid < pool->nrepos)
360         repo = pool_id2repo(pool, repoid);
361     }
362   if (!repo)
363     {
364       FOR_REPOS(repoid, repo)
365         {
366           int i, l;
367           if (!repo->name)
368             continue;
369           l = strlen(repo->name);
370           for (i = 0; i < l; i++)
371             {
372               int c = repo->name[i];
373               if (c == ' ' || c == '\t')
374                 c = '_';
375               if (c != str[i])
376                 break;
377             }
378           if (i == l && !str[l])
379             break;
380         }
381       if (repoid >= pool->nrepos)
382         repo = 0;
383     }
384   return repo;
385 }
386
387 Id
388 testcase_str2solvid(Pool *pool, const char *str)
389 {
390   int i, l = strlen(str);
391   int repostart;
392   Repo *repo;
393   Id arch;
394
395   if (!l)
396     return 0;
397   if (*str == '@' && !strcmp(str, "@SYSTEM"))
398     return SYSTEMSOLVABLE;
399   repo = 0;
400   for (i = l - 1; i >= 0; i--)
401     if (str[i] == '@' && (repo = testcase_str2repo(pool, str + i + 1)) != 0)
402       break;
403   if (i < 0)
404     i = l;
405   repostart = i;
406   /* now find the arch (if present) */
407   arch = 0;
408   for (i = repostart - 1; i > 0; i--)
409     if (str[i] == '.')
410       {
411         arch = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
412         if (arch)
413           repostart = i;
414         break;
415       }
416   /* now find the name */
417   for (i = repostart - 1; i > 0; i--)
418     {
419       if (str[i] == '-')
420         {
421           Id nid, evrid, p, pp;
422           nid = pool_strn2id(pool, str, i, 0);
423           if (!nid)
424             continue;
425           evrid = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
426           if (!evrid)
427             continue;
428           FOR_PROVIDES(p, pp, nid)
429             {
430               Solvable *s = pool->solvables + p;
431               if (s->name != nid || s->evr != evrid)
432                 continue;
433               if (repo && s->repo != repo)
434                 continue;
435               if (arch && s->arch != arch)
436                 continue;
437               return p;
438             }
439         }
440     }
441   return 0;
442 }
443
444 const char *
445 testcase_job2str(Pool *pool, Id how, Id what)
446 {
447   char *ret;
448   const char *jobstr;
449   const char *selstr;
450   const char *pkgstr;
451   int i, o;
452   Id select = how & SOLVER_SELECTMASK;
453
454   for (i = 0; job2str[i].str; i++)
455     if ((how & SOLVER_JOBMASK) == job2str[i].job)
456       break;
457   jobstr = job2str[i].str ? job2str[i].str : "unknown";
458   if (select == SOLVER_SOLVABLE)
459     {
460       selstr = " pkg ";
461       pkgstr = testcase_solvid2str(pool, what);
462     }
463   else if (select == SOLVER_SOLVABLE_NAME)
464     {
465       selstr = " name ";
466       pkgstr = pool_dep2str(pool, what);
467     }
468   else if (select == SOLVER_SOLVABLE_PROVIDES)
469     {
470       selstr = " provides ";
471       pkgstr = pool_dep2str(pool, what);
472     }
473   else if (select == SOLVER_SOLVABLE_ONE_OF)
474     {
475       Id p;
476       selstr = " oneof ";
477       pkgstr = 0;
478       while ((p = pool->whatprovidesdata[what++]) != 0)
479         {
480           const char *s = testcase_solvid2str(pool, p);
481           if (pkgstr)
482             {
483               pkgstr = pool_tmpappend(pool, pkgstr, " ", s);
484               pool_freetmpspace(pool, s);
485             }
486           else
487             pkgstr = s;
488         }
489       if (!pkgstr)
490         pkgstr = "nothing";
491     }
492   else if (select == SOLVER_SOLVABLE_REPO)
493     {
494       Repo *repo = pool_id2repo(pool, what);
495       selstr = " repo ";
496       if (!repo->name)
497         {
498           char buf[20];
499           sprintf(buf, "#%d", repo->repoid);
500           pkgstr = pool_tmpjoin(pool, buf, 0, 0);
501         }
502       else
503         pkgstr = pool_tmpjoin(pool, repo->name, 0, 0);
504     }
505   else if (select == SOLVER_SOLVABLE_ALL)
506     {
507       selstr = " all ";
508       pkgstr = "packages";
509     }
510   else
511     {
512       selstr = " unknown ";
513       pkgstr = "unknown";
514     }
515   ret = pool_tmpjoin(pool, jobstr, selstr, pkgstr);
516   o = strlen(ret);
517   ret = pool_tmpappend(pool, ret, " ", 0);
518   for (i = 0; jobflags2str[i].str; i++)
519     if ((how & jobflags2str[i].flag) != 0)
520       ret = pool_tmpappend(pool, ret, ",", jobflags2str[i].str);
521   if (!ret[o + 1])
522     ret[o] = 0;
523   else
524     {
525       ret[o + 1] = '[';
526       ret = pool_tmpappend(pool, ret, "]", 0);
527     }
528   return ret;
529 }
530
531 Id
532 testcase_str2job(Pool *pool, const char *str, Id *whatp)
533 {
534   int i;
535   Id job;
536   Id what;
537   char *s;
538   char **pieces = 0;
539   int npieces = 0;
540
541   *whatp = 0;
542   /* so we can patch it */
543   s = pool_tmpjoin(pool, str, 0, 0);
544   /* split it in pieces */
545   for (;;)
546     {
547       while (*s == ' ' || *s == '\t')
548         s++;
549       if (!*s)
550         break;
551       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
552       pieces[npieces++] = s;
553       while (*s && *s != ' ' && *s != '\t')
554         s++;
555       if (*s)
556         *s++ = 0;
557     }
558   if (npieces < 3)
559     {
560       pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
561       return 0;
562     }
563
564   for (i = 0; job2str[i].str; i++)
565     if (!strcmp(pieces[0], job2str[i].str))
566       break;
567   if (!job2str[i].str)
568     {
569       pool_debug(pool, SOLV_ERROR, "str2job: unknown job '%s'\n", str);
570       return 0;
571     }
572   job = job2str[i].job;
573   if (npieces > 3)
574     {
575       char *flags = pieces[npieces - 1];
576       char *nf;
577       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
578         {
579           npieces--;
580           flags++;
581           flags[strlen(flags) - 1] = ',';
582           while (*flags)
583             {
584               for (nf = flags; *nf != ','; nf++)
585                 ;
586               *nf++ = 0;
587               for (i = 0; jobflags2str[i].str; i++)
588                 if (!strcmp(flags, jobflags2str[i].str))
589                   break;
590               if (!jobflags2str[i].str)
591                 {
592                   pool_debug(pool, SOLV_ERROR, "str2job: unknown jobflags in '%s'\n", str);
593                   return 0;
594                 }
595               job |= jobflags2str[i].flag;
596               flags = nf;
597             }
598         }
599     }
600   if (!strcmp(pieces[1], "pkg"))
601     {
602       if (npieces != 3)
603         {
604           pool_debug(pool, SOLV_ERROR, "str2job: bad pkg selector in '%s'\n", str);
605           return 0;
606         }
607       job |= SOLVER_SOLVABLE;
608       what = testcase_str2solvid(pool, pieces[2]);
609       if (!what)
610         {
611           pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[2]);
612           return 0;
613         }
614     }
615   else if (!strcmp(pieces[1], "name") || !strcmp(pieces[1], "provides"))
616     {
617       /* join em again for dep2str... */
618       char *sp;
619       for (sp = pieces[2]; sp < pieces[npieces - 1]; sp++)
620         if (*sp == 0)
621           *sp = ' ';
622       what = testcase_str2dep(pool, pieces[2]);
623       if (pieces[1][0] == 'n')
624         job |= SOLVER_SOLVABLE_NAME;
625       else
626         job |= SOLVER_SOLVABLE_PROVIDES;
627     }
628   else if (!strcmp(pieces[1], "oneof"))
629     {
630       Queue q;
631       job |= SOLVER_SOLVABLE_ONE_OF;
632       queue_init(&q);
633       if (npieces > 3 && strcmp(pieces[2], "nothing") != 0)
634         {
635           for (i = 2; i < npieces; i++)
636             {
637               Id p = testcase_str2solvid(pool, pieces[i]);
638               if (!p)
639                 {
640                   pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[i]);
641                   queue_free(&q);
642                   return 0;
643                 }
644               queue_push(&q, p);
645             }
646         }
647       what = pool_queuetowhatprovides(pool, &q);
648       queue_free(&q);
649     }
650   else if (!strcmp(pieces[1], "repo"))
651     {
652       Repo *repo;
653       if (npieces != 3)
654         {
655           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
656           return 0;
657         }
658       repo = testcase_str2repo(pool, pieces[2]);
659       if (!repo)
660         {
661           pool_debug(pool, SOLV_ERROR, "str2job: unknown repo '%s'\n", pieces[2]);
662           return 0;
663         }
664       job |= SOLVER_SOLVABLE_REPO;
665       what = repo->repoid;
666     }
667   else if (!strcmp(pieces[1], "all"))
668     {
669       if (npieces != 3 && strcmp(pieces[2], "packages") != 0)
670         {
671           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
672           return 0;
673         }
674       job |= SOLVER_SOLVABLE_ALL;
675       what = 0;
676     }
677   else
678     {
679       pool_debug(pool, SOLV_ERROR, "str2job: unknown selection in '%s'\n", str);
680       return 0;
681     }
682   *whatp = what;
683   return job;
684 }
685
686 static void
687 writedeps(Repo *repo, FILE *fp, const char *tag, Id key, Solvable *s, Offset off)
688 {
689   Pool *pool = repo->pool;
690   Id id, *dp, *prvdp;
691   int tagwritten = 0;
692   const char *idstr;
693
694   if (!off)
695     return;
696   dp = repo->idarraydata + off;
697   prvdp = 0;
698   while ((id = *dp++) != 0)
699     {
700       if (key == SOLVABLE_REQUIRES && id == SOLVABLE_PREREQMARKER)
701         {
702           if (tagwritten)
703             fprintf(fp, "-%s\n", tag);
704           tagwritten = 0;
705           tag = "Prq:";
706           continue;
707         }
708       if (key == SOLVABLE_PROVIDES && id == SOLVABLE_FILEMARKER)
709         {
710           prvdp = dp + 1;
711           continue;
712         }
713       idstr = pool_dep2str(pool, id);
714       if (ISRELDEP(id))
715         {
716           Reldep *rd = GETRELDEP(pool, id);
717           if (key == SOLVABLE_CONFLICTS && rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
718             {
719               if (!strncmp(idstr, "namespace:", 10))
720                 idstr += 10;
721             }
722           if (key == SOLVABLE_SUPPLEMENTS)
723             {
724               if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_FILESYSTEM)
725                 {
726                   if (!strncmp(idstr, "namespace:", 10))
727                     idstr += 10;
728                 }
729               else if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_MODALIAS)
730                 {
731                   if (!strncmp(idstr, "namespace:", 10))
732                     idstr += 10;
733                 }
734               else if (rd->flags == REL_AND)
735                 {
736                   /* either packageand chain or modalias */
737                   idstr = 0;
738                   if (ISRELDEP(rd->evr))
739                     {
740                       Reldep *mrd = GETRELDEP(pool, rd->evr);
741                       if (mrd->flags == REL_NAMESPACE && mrd->name == NAMESPACE_MODALIAS)
742                         {
743                           idstr = pool_tmpjoin(pool, "modalias(", pool_dep2str(pool, rd->name), ":");
744                           idstr = pool_tmpappend(pool, idstr, pool_dep2str(pool, mrd->evr), ")");
745                         }
746                       else if (mrd->flags >= 8)
747                         continue;
748                     }
749                   if (!idstr)
750                     {
751                       /* must be and chain */
752                       idstr = pool_dep2str(pool, rd->evr);
753                       for (;;)
754                         {
755                           id = rd->name;
756                           if (!ISRELDEP(id))
757                             break;
758                           rd = GETRELDEP(pool, id);
759                           if (rd->flags != REL_AND)
760                             break;
761                           idstr = pool_tmpjoin(pool, pool_dep2str(pool, rd->evr), ":", idstr);
762                         }
763                       idstr = pool_tmpjoin(pool, pool_dep2str(pool, id), ":", idstr);
764                       idstr = pool_tmpjoin(pool, "packageand(", idstr, ")");
765                     }
766                 }
767               else if (rd->flags >= 8)
768                 continue;
769             }
770         }
771       if (!tagwritten)
772         {
773           fprintf(fp, "+%s\n", tag);
774           tagwritten = 1;
775         }
776       fprintf(fp, "%s\n", idstr);
777     }
778   if (key == SOLVABLE_PROVIDES)
779     {
780       /* add the filelist */
781       Dataiterator di;
782       dataiterator_init(&di, pool, repo, s - pool->solvables, SOLVABLE_FILELIST, 0, 0);
783       while (dataiterator_step(&di))
784         {
785           const char *s = repodata_dir2str(di.data, di.kv.id, di.kv.str);
786           if (prvdp)
787             {
788               Id id = pool_str2id(pool, s, 0);
789               if (id)
790                 {
791                   for (dp = prvdp; *dp; dp++)
792                     if (*dp == id)
793                       break;
794                   if (*dp)
795                     continue;   /* already included */
796                 }
797             }
798           if (!tagwritten)
799             {
800               fprintf(fp, "+%s", tag);
801               tagwritten = 1;
802             }
803           fprintf(fp, "%s\n", s);
804         }
805     }
806   if (tagwritten)
807     fprintf(fp, "-%s\n", tag);
808 }
809
810 int
811 testcase_write_susetags(Repo *repo, FILE *fp)
812 {
813   Pool *pool = repo->pool;
814   Solvable *s;
815   Id p;
816   const char *name;
817   const char *evr;
818   const char *arch;
819   const char *release;
820   const char *tmp;
821   unsigned int ti;
822
823   fprintf(fp, "=Ver: 2.0\n");
824   FOR_REPO_SOLVABLES(repo, p, s)
825     {
826       name = pool_id2str(pool, s->name);
827       evr = pool_id2str(pool, s->evr);
828       arch = pool_id2str(pool, s->arch);
829       release = strrchr(evr, '-');
830       if (!release)
831         release = evr + strlen(evr);
832       fprintf(fp, "=Pkg: %s %.*s %s %s\n", name, release - evr, evr, *release && release[1] ? release + 1 : "-", arch);
833       tmp = solvable_lookup_str(s, SOLVABLE_SUMMARY);
834       if (tmp)
835         fprintf(fp, "=Sum: %s\n", tmp);
836       writedeps(repo, fp, "Req:", SOLVABLE_REQUIRES, s, s->requires);
837       writedeps(repo, fp, "Prv:", SOLVABLE_PROVIDES, s, s->provides);
838       writedeps(repo, fp, "Obs:", SOLVABLE_OBSOLETES, s, s->obsoletes);
839       writedeps(repo, fp, "Con:", SOLVABLE_CONFLICTS, s, s->conflicts);
840       writedeps(repo, fp, "Rec:", SOLVABLE_RECOMMENDS, s, s->recommends);
841       writedeps(repo, fp, "Sup:", SOLVABLE_SUPPLEMENTS, s, s->supplements);
842       writedeps(repo, fp, "Sug:", SOLVABLE_SUGGESTS, s, s->suggests);
843       writedeps(repo, fp, "Enh:", SOLVABLE_ENHANCES, s, s->enhances);
844       if (s->vendor)
845         fprintf(fp, "=Vnd: %s\n", pool_id2str(pool, s->vendor));
846       ti = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0);
847       if (ti)
848         fprintf(fp, "=Tim: %u\n", ti);
849     }
850   return 0;
851 }
852
853 #ifndef ENABLE_SUSEREPO
854
855 static inline Offset
856 adddep(Repo *repo, Offset olddeps, char *str, Id marker)
857 {
858   Id id = *str == '/' ? pool_str2id(repo->pool, str, 1) : testcase_str2dep(repo->pool, str);
859   return repo_addid_dep(repo, olddeps, id, marker);
860 }
861
862 static void
863 finish_solvable(Pool *pool, Repodata *data, Solvable *s)
864 {
865   /* move file provides to filelist */
866   if (s->provides)
867     {
868       Id *p, *lastreal, did;
869       lastreal = s->repo->idarraydata + s->provides;
870       for (p = lastreal; *p; p++)
871         if (ISRELDEP(*p))
872           lastreal = p + 1;
873       for (p = lastreal; *p; p++)
874         if ((pool_id2str(pool, *p))[0] != '/')
875           lastreal = p + 1;
876       for (p = lastreal; *p; p++)
877         {
878           char *str = pool_tmpjoin(pool, pool_id2str(pool, *p), 0, 0);
879           char *sp = strrchr(str, '/');
880           *sp++ = 0;
881           did = repodata_str2dir(data, str, 1);
882           if (!did)
883             did = repodata_str2dir(data, "/", 1);
884           repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, sp);
885           *p = 0;
886         }
887     }
888   if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
889     s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
890   s->supplements = repo_fix_supplements(s->repo, s->provides, s->supplements, 0);
891   s->conflicts = repo_fix_conflicts(s->repo, s->conflicts);
892 }
893
894 /* stripped down version of susetags parser used for testcases */
895 int
896 testcase_add_susetags(Repo *repo, FILE *fp, int flags)
897 {
898   Pool *pool = repo->pool;
899   char *line, *linep;
900   int aline;
901   int tag;
902   Repodata *data;
903   Solvable *s;
904   char *sp[5];
905   unsigned int t;
906   int intag;
907
908   data = repo_add_repodata(repo, flags);
909   s = 0;
910   intag = 0;
911
912   aline = 1024;
913   line = solv_malloc(aline);
914   linep = line;
915   for (;;)
916     {
917       if (linep - line + 16 > aline)
918         {
919           aline = linep - line;
920           line = solv_realloc(line, aline + 512);
921           linep = line + aline;
922           aline += 512;
923         }
924       if (!fgets(linep, aline - (linep - line), fp))
925         break;
926       linep += strlen(linep);
927       if (linep == line || linep[-1] != '\n')
928         continue;
929       *--linep = 0;
930       linep = line + intag;
931       if (intag)
932         {
933           if (line[intag] == '-' && !strncmp(line + 1, line + intag + 1, intag - 2))
934             {
935               intag = 0;
936               linep = line;
937               continue;
938             }
939         }
940       else if (line[0] == '+' && line[1] && line[1] != ':')
941         {
942           char *tagend = strchr(line, ':');
943           if (!tagend)
944             continue;
945           line[0] = '=';
946           tagend[1] = ' ';
947           intag = tagend + 2 - line;
948           linep = line + intag;
949           continue;
950         }
951       if (*line != '=' || !line[1] || !line[2] || !line[3] || line[4] != ':')
952         continue;
953       tag = line[1] << 16 | line[2] << 8 | line[3];
954       switch(tag)
955         {
956         case 'P' << 16 | 'k' << 8 | 'g':
957           if (s)
958             finish_solvable(pool, data, s);
959           if (split(line + 5, sp, 5) != 4)
960             break;
961           s = pool_id2solvable(pool, repo_add_solvable(repo));
962           s->name = pool_str2id(pool, sp[0], 1);
963           /* join back version and release */
964           if (sp[2] && !(sp[2][0] == '-' && !sp[2][1]))
965             sp[2][-1] = '-';
966           s->evr = makeevr(pool, sp[1]);
967           s->arch = pool_str2id(pool, sp[3], 1);
968           break;
969         case 'S' << 16 | 'u' << 8 | 'm':
970           repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 6);
971           break;
972         case 'V' << 16 | 'n' << 8 | 'd':
973           s->vendor = pool_str2id(pool, line + 6, 1);
974           break;
975         case 'T' << 16 | 'i' << 8 | 'm':
976           t = atoi(line + 6);
977           if (t)
978             repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, t);
979           break;
980         case 'R' << 16 | 'e' << 8 | 'q':
981           s->requires = adddep(repo, s->requires, line + 6, -SOLVABLE_PREREQMARKER);
982           break;
983         case 'P' << 16 | 'r' << 8 | 'q':
984           s->requires = adddep(repo, s->requires, line + 6, SOLVABLE_PREREQMARKER);
985           break;
986         case 'P' << 16 | 'r' << 8 | 'v':
987           s->provides = adddep(repo, s->provides, line + 6, 0);
988           break;
989         case 'O' << 16 | 'b' << 8 | 's':
990           s->obsoletes = adddep(repo, s->obsoletes, line + 6, 0);
991           break;
992         case 'C' << 16 | 'o' << 8 | 'n':
993           s->conflicts = adddep(repo, s->conflicts, line + 6, 0);
994           break;
995         case 'R' << 16 | 'e' << 8 | 'c':
996           s->recommends = adddep(repo, s->recommends, line + 6, 0);
997           break;
998         case 'S' << 16 | 'u' << 8 | 'p':
999           s->supplements = adddep(repo, s->supplements, line + 6, 0);
1000           break;
1001         case 'S' << 16 | 'u' << 8 | 'g':
1002           s->suggests = adddep(repo, s->suggests, line + 6, 0);
1003           break;
1004         case 'E' << 16 | 'n' << 8 | 'h':
1005           s->enhances = adddep(repo, s->enhances, line + 6, 0);
1006           break;
1007         default:
1008           break;
1009         }
1010     }
1011   if (s)
1012     finish_solvable(pool, data, s);
1013   if (!(flags & REPO_NO_INTERNALIZE))
1014     repodata_internalize(data);
1015   solv_free(line);
1016   return 0;
1017 }
1018
1019 #else
1020
1021 int
1022 testcase_add_susetags(Repo *repo, FILE *fp, int flags)
1023 {
1024   return repo_add_susetags(repo, fp, 0, 0, flags);
1025 }
1026
1027 #endif
1028
1029 const char *
1030 testcase_getsolverflags(Solver *solv)
1031 {
1032   Pool *pool = solv->pool;
1033   const char *str = 0;
1034   int i, v;
1035   for (i = 0; solverflags2str[i].str; i++)
1036     {
1037       v = solver_get_flag(solv, solverflags2str[i].flag);
1038       if (v == solverflags2str[i].def)
1039         continue;
1040       str = pool_tmpappend(pool, str, v ? " " : " !", solverflags2str[i].str);
1041     }
1042   return str ? str + 1 : "";
1043 }
1044
1045 int
1046 testcase_setsolverflags(Solver *solv, const char *str)
1047 {
1048   const char *p = str, *s;
1049   int i, v;
1050   for (;;)
1051     {
1052       while (*p == ' ' || *p == '\t' || *p == ',')
1053         p++;
1054       v = 1;
1055       if (*p == '!')
1056         {
1057           p++;
1058           v = 0;
1059         }
1060       if (!*p)
1061         break;
1062       s = p;
1063       while (*p && *p != ' ' && *p != '\t' && *p != ',')
1064         p++;
1065       for (i = 0; solverflags2str[i].str; i++)
1066         if (!strncmp(solverflags2str[i].str, s, p - s) && solverflags2str[i].str[p - s] == 0)
1067           break;
1068       if (!solverflags2str[i].str)
1069         {
1070           pool_debug(solv->pool, SOLV_ERROR, "setsolverflags: unknown flag '%.*s'\n", p - s, s);
1071           return 0;
1072         }
1073       solver_set_flag(solv, solverflags2str[i].flag, v);
1074     }
1075   return 1;
1076 }
1077
1078 void
1079 testcase_resetsolverflags(Solver *solv)
1080 {
1081   int i;
1082   for (i = 0; solverflags2str[i].str; i++)
1083     solver_set_flag(solv, solverflags2str[i].flag, solverflags2str[i].def);
1084 }
1085
1086 static const char *
1087 testcase_ruleid(Solver *solv, Id rid)
1088 {
1089   Strqueue sq;
1090   Queue q;
1091   int i;
1092   void *chk;
1093   const unsigned char *md5;
1094   int md5l;
1095   const char *s;
1096
1097   queue_init(&q);
1098   strqueue_init(&sq);
1099   solver_ruleliterals(solv, rid, &q);
1100   for (i = 0; i < q.count; i++)
1101     {
1102       Id p = q.elements[i];
1103       s = testcase_solvid2str(solv->pool, p > 0 ? p : -p);
1104       if (p < 0)
1105         s = pool_tmpjoin(solv->pool, "!", s, 0);
1106       strqueue_push(&sq, s);
1107     }
1108   queue_free(&q);
1109   strqueue_sort_u(&sq);
1110   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1111   for (i = 0; i < sq.nstr; i++)
1112     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1113   md5 = solv_chksum_get(chk, &md5l);
1114   s = pool_bin2hex(solv->pool, md5, md5l);
1115   chk = solv_chksum_free(chk, 0);
1116   strqueue_free(&sq);
1117   return s;
1118 }
1119
1120 static const char *
1121 testcase_problemid(Solver *solv, Id problem)
1122 {
1123   Strqueue sq;
1124   Queue q;
1125   void *chk;
1126   const unsigned char *md5;
1127   int i, md5l;
1128   const char *s;
1129
1130   /* we build a hash of all rules that define the problem */
1131   queue_init(&q);
1132   strqueue_init(&sq);
1133   solver_findallproblemrules(solv, problem, &q);
1134   for (i = 0; i < q.count; i++)
1135     strqueue_push(&sq, testcase_ruleid(solv, q.elements[i]));
1136   queue_free(&q);
1137   strqueue_sort_u(&sq);
1138   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1139   for (i = 0; i < sq.nstr; i++)
1140     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1141   md5 = solv_chksum_get(chk, &md5l);
1142   s = pool_bin2hex(solv->pool, md5, 4);
1143   chk = solv_chksum_free(chk, 0);
1144   strqueue_free(&sq);
1145   return s;
1146 }
1147
1148 static const char *
1149 testcase_solutionid(Solver *solv, Id problem, Id solution)
1150 {
1151   Id intid;
1152   void *chk;
1153   const unsigned char *md5;
1154   int md5l;
1155   const char *s;
1156
1157   intid = solver_solutionelement_internalid(solv, problem, solution);
1158   /* internal stuff! handle with care! */
1159   if (intid < 0)
1160     {
1161       /* it's a job */
1162       s = testcase_job2str(solv->pool, solv->job.elements[-intid - 1], solv->job.elements[-intid]);
1163     }
1164   else
1165     {
1166       /* it's a rule */
1167       s = testcase_ruleid(solv, intid);
1168     }
1169   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1170   solv_chksum_add(chk, s, strlen(s) + 1);
1171   md5 = solv_chksum_get(chk, &md5l);
1172   s = pool_bin2hex(solv->pool, md5, 4);
1173   chk = solv_chksum_free(chk, 0);
1174   return s;
1175 }
1176
1177 static struct class2str {
1178   Id class;
1179   const char *str;
1180 } class2str[] = {
1181   { SOLVER_TRANSACTION_ERASE,          "erase" },
1182   { SOLVER_TRANSACTION_INSTALL,        "install" },
1183   { SOLVER_TRANSACTION_REINSTALLED,    "reinstall" },
1184   { SOLVER_TRANSACTION_DOWNGRADED,     "downgrade" },
1185   { SOLVER_TRANSACTION_CHANGED,        "change" },
1186   { SOLVER_TRANSACTION_UPGRADED,       "upgrade" },
1187   { SOLVER_TRANSACTION_OBSOLETED,      "obsolete" },
1188   { SOLVER_TRANSACTION_MULTIINSTALL,   "multiinstall" },
1189   { SOLVER_TRANSACTION_MULTIREINSTALL, "multireinstall" },
1190   { 0, 0 }
1191 };
1192
1193 char *
1194 testcase_solverresult(Solver *solv, int resultflags)
1195 {
1196   Pool *pool = solv->pool;
1197   int i, j;
1198   Id p, op;
1199   const char *s;
1200   char *result;
1201   Strqueue sq;
1202
1203   strqueue_init(&sq);
1204   if ((resultflags & TESTCASE_RESULT_TRANSACTION) != 0)
1205     {
1206       Transaction *trans = solver_create_transaction(solv);
1207       Queue q;
1208
1209       queue_init(&q);
1210       for (i = 0; class2str[i].str; i++)
1211         {
1212           queue_empty(&q);
1213           transaction_classify_pkgs(trans, 0, class2str[i].class, 0, 0, &q);
1214           for (j = 0; j < q.count; j++)
1215             {
1216               p = q.elements[j];
1217               op = 0;
1218               if (pool->installed && pool->solvables[p].repo == pool->installed)
1219                 op = transaction_obs_pkg(trans, p);
1220               s = pool_tmpjoin(pool, class2str[i].str, " ", testcase_solvid2str(pool, p));
1221               if (op)
1222                 s = pool_tmpjoin(pool, s, " ", testcase_solvid2str(pool, op));
1223               strqueue_push(&sq, s);
1224             }
1225         }
1226       queue_free(&q);
1227       transaction_free(trans);
1228     }
1229   if ((resultflags & TESTCASE_RESULT_PROBLEMS) != 0)
1230     {
1231       char *probprefix, *solprefix;
1232       int problem, solution, element;
1233       int pcnt, scnt;
1234
1235       pcnt = solver_problem_count(solv);
1236       for (problem = 1; problem <= pcnt; problem++)
1237         {
1238           Id rid, from, to, dep;
1239           SolverRuleinfo rinfo;
1240           rid = solver_findproblemrule(solv, problem);
1241           s = testcase_problemid(solv, problem);
1242           probprefix = solv_dupjoin("problem ", s, 0);
1243           rinfo = solver_ruleinfo(solv, rid, &from, &to, &dep);
1244           s = pool_tmpjoin(pool, probprefix, " info ", solver_problemruleinfo2str(solv, rinfo, from, to, dep));
1245           strqueue_push(&sq, s);
1246           scnt = solver_solution_count(solv, problem);
1247           for (solution = 1; solution <= scnt; solution++)
1248             {
1249               s = testcase_solutionid(solv, problem, solution);
1250               solprefix = solv_dupjoin(probprefix, " solution ", s);
1251               element = 0;
1252               while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &op)) != 0)
1253                 {
1254                   if (p == SOLVER_SOLUTION_JOB)
1255                     s = pool_tmpjoin(pool, solprefix, " deljob ", testcase_job2str(pool, solv->job.elements[op - 1], solv->job.elements[op]));
1256                   else if (p > 0 && op == 0)
1257                     s = pool_tmpjoin(pool, solprefix, " erase ", testcase_solvid2str(pool, p));
1258                   else if (p > 0 && op > 0)
1259                     {
1260                       s = pool_tmpjoin(pool, solprefix, " replace ", testcase_solvid2str(pool, p));
1261                       s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, op));
1262                     }
1263                   else if (p < 0 && op > 0)
1264                     s = pool_tmpjoin(pool, solprefix, " allow ", testcase_solvid2str(pool, op));
1265                   else
1266                     s = pool_tmpjoin(pool, solprefix, " unknown", 0);
1267                   strqueue_push(&sq, s);
1268                 }
1269               solv_free(solprefix);
1270             }
1271           solv_free(probprefix);
1272         }
1273     }
1274
1275   if ((resultflags & TESTCASE_RESULT_ORPHANED) != 0)
1276     {
1277       Queue q;
1278
1279       queue_init(&q);
1280       solver_get_orphaned(solv, &q);
1281       for (i = 0; i < q.count; i++)
1282         {
1283           s = pool_tmpjoin(pool, "orphaned ", testcase_solvid2str(pool, q.elements[i]), 0);
1284           strqueue_push(&sq, s);
1285         }
1286       queue_free(&q);
1287     }
1288
1289   if ((resultflags & TESTCASE_RESULT_RECOMMENDED) != 0)
1290     {
1291       Queue qr, qs;
1292
1293       queue_init(&qr);
1294       queue_init(&qs);
1295       solver_get_recommendations(solv, &qr, &qs, 0);
1296       for (i = 0; i < qr.count; i++)
1297         {
1298           s = pool_tmpjoin(pool, "recommended ", testcase_solvid2str(pool, qr.elements[i]), 0);
1299           strqueue_push(&sq, s);
1300         }
1301       for (i = 0; i < qs.count; i++)
1302         {
1303           s = pool_tmpjoin(pool, "suggested ", testcase_solvid2str(pool, qs.elements[i]), 0);
1304           strqueue_push(&sq, s);
1305         }
1306       queue_free(&qr);
1307       queue_free(&qs);
1308     }
1309
1310   strqueue_sort(&sq);
1311   result = strqueue_join(&sq);
1312   strqueue_free(&sq);
1313   return result;
1314 }
1315
1316
1317 int
1318 testcase_write(Solver *solv, char *dir, int resultflags)
1319 {
1320   Pool *pool = solv->pool;
1321   Repo *repo;
1322   int i;
1323   Id arch, repoid;
1324   Id lowscore;
1325   FILE *fp;
1326   Strqueue sq;
1327   char *cmd, *out;
1328   const char *s;
1329
1330   if (mkdir(dir, 0777) && errno != EEXIST)
1331     {
1332       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not create directory '%s'\n", dir);
1333       return 0;
1334     }
1335   strqueue_init(&sq);
1336   FOR_REPOS(repoid, repo)
1337     {
1338       const char *name = testcase_repoid2str(pool, repoid);
1339       char priobuf[50];
1340       if (repo->subpriority)
1341         sprintf(priobuf, "%d.%d", repo->priority, repo->subpriority);
1342       else
1343         sprintf(priobuf, "%d", repo->priority);
1344       out = pool_tmpjoin(pool, name, ".repo", ".gz");
1345       cmd = pool_tmpjoin(pool, "repo ", name, " ");
1346       cmd = pool_tmpappend(pool, cmd, priobuf, " ");
1347       cmd = pool_tmpappend(pool, cmd, "susetags ", out);
1348       strqueue_push(&sq, cmd);
1349       out = pool_tmpjoin(pool, dir, "/", out);
1350       if (!(fp = solv_xfopen(out, "w")))
1351         {
1352           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1353           strqueue_free(&sq);
1354           return 0;
1355         }
1356       testcase_write_susetags(repo, fp);
1357       if (fclose(fp))
1358         {
1359           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1360           strqueue_free(&sq);
1361           return 0;
1362         }
1363     }
1364   /* hmm, this is not optimal... we currently search for the lowest score */
1365   lowscore = 0;
1366   arch = ARCH_NOARCH;
1367   for (i = 0; i < pool->lastarch; i++)
1368     {
1369       if (pool->id2arch[i] == 1 && !lowscore)
1370         arch = i;
1371       if (pool->id2arch[i] > 0x10000 && (!lowscore || pool->id2arch[i] < lowscore))
1372         {
1373           arch = i;
1374           lowscore = pool->id2arch[i];
1375         }
1376     }
1377   cmd = pool_tmpjoin(pool, "system ", pool_id2str(pool, arch), pool->disttype == DISTTYPE_DEB ? " deb" : " rpm");
1378   if (pool->installed)
1379     cmd = pool_tmpappend(pool, cmd, " ", testcase_repoid2str(pool, pool->installed->repoid));
1380   strqueue_push(&sq, cmd);
1381
1382   if (pool->vendorclasses)
1383     {
1384       cmd = 0;
1385       for (i = 0; pool->vendorclasses[i]; i++)
1386         {
1387           cmd = pool_tmpappend(pool, cmd ? cmd : "vendorclass", " ", pool->vendorclasses[i]);
1388           if (!pool->vendorclasses[i + 1])
1389             {
1390               strqueue_push(&sq, cmd);
1391               cmd = 0;
1392               i++;
1393             }
1394         }
1395     }
1396
1397   s = testcase_getsolverflags(solv);
1398   if (*s)
1399     {
1400       cmd = pool_tmpjoin(pool, "solverflags ", s, 0);
1401       strqueue_push(&sq, cmd);
1402     }
1403
1404   /* now dump all the ns callback values we know */
1405   if (pool->nscallback)
1406     {
1407       Id rid;
1408       int d;
1409       for (rid = 1; rid < pool->nrels; rid++)
1410         {
1411           Reldep *rd = pool->rels + rid;
1412           if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1413             continue;
1414           /* check if we evaluated it, also skip empty results */
1415           if (!(d = pool->whatprovides_rel[rid]) || !pool->whatprovidesdata[d])
1416             continue;
1417           cmd = pool_tmpjoin(pool, "namespace ", pool_id2str(pool, rd->name), "(");
1418           cmd = pool_tmpappend(pool, cmd, pool_id2str(pool, rd->evr), ")");
1419           for (;  pool->whatprovidesdata[d]; d++)
1420             cmd = pool_tmpappend(pool, cmd, " ", testcase_solvid2str(pool, pool->whatprovidesdata[d]));
1421           strqueue_push(&sq, cmd);
1422         }
1423     }
1424
1425   for (i = 0; i < solv->job.count; i += 2)
1426     {
1427       cmd = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
1428       cmd = pool_tmpjoin(pool, "job ", cmd, 0);
1429       strqueue_push(&sq, cmd);
1430     }
1431
1432   if (resultflags)
1433     {
1434       char *result;
1435       out = pool_tmpjoin(pool, dir, "/", "solver.result");
1436       if (!(fp = fopen(out, "w")))
1437         {
1438           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1439           strqueue_free(&sq);
1440           return 0;
1441         }
1442       result = testcase_solverresult(solv, resultflags);
1443       if (fwrite(result, strlen(result), 1, fp) != 1)
1444         {
1445           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1446           solv_free(result);
1447           strqueue_free(&sq);
1448           return 0;
1449         }
1450       solv_free(result);
1451       if (fclose(fp))
1452         {
1453           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1454           strqueue_free(&sq);
1455           return 0;
1456         }
1457       cmd = 0;
1458       for (i = 0; resultflags2str[i].str; i++)
1459         if ((resultflags & resultflags2str[i].flag) != 0)
1460           cmd = pool_tmpappend(pool, cmd, cmd ? "," : 0, resultflags2str[i].str);
1461       cmd = pool_tmpjoin(pool, "result ", cmd ? cmd : "?", " solver.result");
1462       strqueue_push(&sq, cmd);
1463     }
1464
1465   cmd = strqueue_join(&sq);
1466   out = pool_tmpjoin(pool, dir, "/", "testcase.t");
1467   if (!(fp = fopen(out, "w")))
1468     {
1469       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1470       strqueue_free(&sq);
1471       return 0;
1472     }
1473   if (fwrite(cmd, strlen(cmd), 1, fp) != 1)
1474     {
1475       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1476       strqueue_free(&sq);
1477       return 0;
1478     }
1479   if (fclose(fp))
1480     {
1481       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1482       strqueue_free(&sq);
1483       return 0;
1484     }
1485   solv_free(cmd);
1486   strqueue_free(&sq);
1487   return 1;
1488 }
1489
1490 Solver *
1491 testcase_read(Pool *pool, FILE *fp, char *testcase, Queue *job, char **resultp, int *resultflagsp)
1492 {
1493   Solver *solv;
1494   char *buf, *bufp;
1495   int bufl;
1496   char *testcasedir, *s;
1497   int l;
1498   char **pieces = 0;
1499   int npieces = 0;
1500   int prepared = 0;
1501   int closefp = !fp;
1502
1503   if (!fp && !(fp = fopen(testcase, "r")))
1504     {
1505       pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", testcase);
1506       return 0;
1507     }
1508   testcasedir = solv_strdup(testcase);
1509   if ((s = strchr(testcasedir, '/')) != 0)
1510     s[1] = 0;
1511   else
1512     *testcasedir = 0;
1513   bufl = 1024;
1514   buf = solv_malloc(bufl);
1515   bufp = buf;
1516   solv = 0;
1517   for (;;)
1518     {
1519       if (bufp - buf + 16 > bufl)
1520         {
1521           bufl = bufp - buf;
1522           buf = solv_realloc(buf, bufl + 512);
1523           bufp = buf + bufl;
1524           bufl += 512;
1525         }
1526       if (!fgets(bufp, bufl - (bufp - buf), fp))
1527         break;
1528       bufp = buf;
1529       l = strlen(buf);
1530       if (!l || buf[l - 1] != '\n')
1531         {
1532           bufp += l;
1533           continue;
1534         }
1535       buf[--l] = 0;
1536       s = buf;
1537       while (*s && (*s == ' ' || *s == '\t'))
1538         s++;
1539       if (!*s || *s == '#')
1540         continue;
1541       npieces = 0;
1542       /* split it in pieces */
1543       for (;;)
1544         {
1545           while (*s == ' ' || *s == '\t')
1546             s++;
1547           if (!*s)
1548             break;
1549           pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1550           pieces[npieces++] = s;
1551           while (*s && *s != ' ' && *s != '\t')
1552             s++;
1553           if (*s)
1554             *s++ = 0;
1555         }
1556       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1557       pieces[npieces] = 0;
1558       if (!strcmp(pieces[0], "repo") && npieces >= 4)
1559         {
1560           Repo *repo = repo_create(pool, pieces[1]);
1561           FILE *rfp;
1562           int prio, subprio;
1563           const char *rdata;
1564
1565           prepared = 0;
1566           if (sscanf(pieces[2], "%d.%d", &prio, &subprio) != 2)
1567             {
1568               subprio = 0;
1569               prio = atoi(pieces[2]);
1570             }
1571           repo->priority = prio;
1572           repo->subpriority = subprio;
1573           rdata = pool_tmpjoin(pool, testcasedir, pieces[4], 0);
1574           if ((rfp = solv_xfopen(rdata, "r")) == 0)
1575             {
1576               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1577             }
1578           else if (!strcmp(pieces[3], "susetags"))
1579             {
1580               testcase_add_susetags(repo, rfp, 0);
1581               fclose(rfp);
1582             }
1583           else
1584             {
1585               fclose(rfp);
1586               pool_debug(pool, SOLV_ERROR, "testcase_read: unknown repo type for repo '%s'\n", repo->name);
1587             }
1588         }
1589       else if (!strcmp(pieces[0], "system") && npieces >= 3)
1590         {
1591           prepared = 0;
1592           pool_setarch(pool, pieces[1]);
1593           if (npieces > 3)
1594             {
1595               Repo *repo = testcase_str2repo(pool, pieces[3]);
1596               if (!repo)
1597                 pool_debug(pool, SOLV_ERROR, "testcase_read: system: unknown repo '%s'\n", pieces[3]);
1598               else
1599                 pool_set_installed(pool, repo);
1600             }
1601         }
1602       else if (!strcmp(pieces[0], "job") && npieces > 1)
1603         {
1604           char *sp;
1605           Id how, what;
1606           if (!prepared)
1607             {
1608               pool_addfileprovides(pool);
1609               pool_createwhatprovides(pool);
1610               prepared = 1;
1611             }
1612           /* rejoin */
1613           for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
1614             if (*sp == 0)
1615               *sp = ' ';
1616           how = testcase_str2job(pool, pieces[1], &what);
1617           if (how >= 0 && job)
1618             queue_push2(job, how, what);
1619         }
1620       else if (!strcmp(pieces[0], "vendorclass") && npieces > 1)
1621         {
1622           pool_addvendorclass(pool, (const char **)(pieces + 1));
1623         }
1624       else if (!strcmp(pieces[0], "namespace") && npieces > 1)
1625         {
1626           int i = strlen(pieces[1]);
1627           s = strchr(pieces[1], '(');
1628           if (!s && pieces[1][i - 1] != ')')
1629             {
1630               pool_debug(pool, SOLV_ERROR, "testcase_read: bad namespace '%s'\n", pieces[1]);
1631             }
1632           else
1633             {
1634               Id name, evr, id;
1635               Queue q;
1636               queue_init(&q);
1637               *s = 0;
1638               pieces[1][i - 1] = 0;
1639               name = pool_str2id(pool, pieces[1], 1);
1640               evr = pool_str2id(pool, s + 1, 1);
1641               *s = '(';
1642               pieces[1][i - 1] = ')';
1643               id = pool_rel2id(pool, name, evr, REL_NAMESPACE, 1);
1644               for (i = 2; i < npieces; i++)
1645                 queue_push(&q, testcase_str2solvid(pool, pieces[i]));
1646               /* now do the callback */
1647               if (!prepared)
1648                 {
1649                   pool_addfileprovides(pool);
1650                   pool_createwhatprovides(pool);
1651                   prepared = 1;
1652                 }
1653               pool->whatprovides_rel[GETRELID(id)] = pool_queuetowhatprovides(pool, &q);
1654               queue_free(&q);
1655             }
1656         }
1657       else if (!strcmp(pieces[0], "solverflags") && npieces > 1)
1658         {
1659           int i;
1660           if (!solv)
1661             {
1662               solv = solver_create(pool);
1663               testcase_resetsolverflags(solv);
1664             }
1665           for (i = 1; i < npieces; i++)
1666             testcase_setsolverflags(solv, pieces[i]);
1667         }
1668       else if (!strcmp(pieces[0], "result") && npieces > 1)
1669         {
1670           FILE *rfp;
1671           const char *rdata;
1672           int resultflags = 0;
1673           char *s = pieces[1];
1674           int i;
1675           while (s)
1676             {
1677               char *se = strchr(s, ',');
1678               if (se)
1679                 *se++ = 0;
1680               for (i = 0; resultflags2str[i].str; i++)
1681                 if (!strcmp(s, resultflags2str[i].str))
1682                   {
1683                     resultflags |= resultflags2str[i].flag;
1684                     break;
1685                   }
1686               if (!resultflags2str[i].str)
1687                 pool_debug(pool, SOLV_ERROR, "result: unknown flag '%s'\n", s);
1688               s = se;
1689             }
1690
1691           rdata = pool_tmpjoin(pool, testcasedir, pieces[2], 0);
1692           if (!strcmp(pieces[1], "<inline>"))
1693             rfp = fp;
1694           else
1695             rfp = fopen(rdata, "r");
1696           if (!rfp)
1697             {
1698               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1699             }
1700           else
1701             {
1702               /* slurp it in... */
1703               char *result = solv_malloc(1024);
1704               char *rp = result;
1705               int resultl = 1024;
1706               for (;;)
1707                 {
1708                   size_t rl;
1709                   if (rp - result + 256 >= resultl)
1710                     {
1711                       resultl = rp - result;
1712                       result = solv_realloc(result, resultl + 1024);
1713                       rp = result + resultl;
1714                       resultl += 1024;
1715                     }
1716                   if (fp == rfp)
1717                     {
1718                       if (!fgets(rp, resultl - (rp - result), fp))
1719                         rl = 0;
1720                       else
1721                         {
1722                           rl = strlen(rp);
1723                           if (rl && (rp == result || rp[-1] == '\n'))
1724                             {
1725                               if (rl > 1 && rp[0] == '#' && rp[1] == '>')
1726                                 {
1727                                   memmove(rp, rp + 2, rl - 2);
1728                                   rl -= 2;
1729                                 }
1730                               else
1731                                 {
1732                                   while (rl + 16 > bufl)
1733                                     {
1734                                       buf = solv_realloc(buf, bufl + 512);
1735                                       bufl += 512;
1736                                     }
1737                                   memmove(buf, rp, rl);
1738                                   if (buf[rl - 1] == '\n')
1739                                     {
1740                                       ungetc('\n', fp);
1741                                       rl--;
1742                                     }
1743                                   bufp = buf + rl;
1744                                   rl = 0;
1745                                 }
1746                             }
1747                         }
1748                     }
1749                   else
1750                     rl = fread(rp, 1, resultl - (rp - result), rfp);
1751                   if (rl <= 0)
1752                     {
1753                       *rp = 0;
1754                       break;
1755                     }
1756                   rp += rl;
1757                 }
1758               if (rfp != fp)
1759                 fclose(rfp);
1760               if (resultp)
1761                 *resultp = result;
1762               else
1763                 solv_free(result);
1764               if (resultflagsp)
1765                 *resultflagsp = resultflags;
1766             }
1767         }
1768       else
1769         {
1770           pool_debug(pool, SOLV_ERROR, "testcase_read: cannot parse command '%s'\n", pieces[0]);
1771         }
1772     }
1773   buf = solv_free(buf);
1774   pieces = solv_free(pieces);
1775   solv_free(testcasedir);
1776   if (!prepared)
1777     {
1778       pool_addfileprovides(pool);
1779       pool_createwhatprovides(pool);
1780     }
1781   if (!solv)
1782     {
1783       solv = solver_create(pool);
1784       testcase_resetsolverflags(solv);
1785     }
1786   if (closefp)
1787     fclose(fp);
1788   return solv;
1789 }
1790
1791 char *
1792 testcase_resultdiff(char *result1, char *result2)
1793 {
1794   Strqueue sq1, sq2, osq;
1795   char *r;
1796   strqueue_init(&sq1);
1797   strqueue_init(&sq2);
1798   strqueue_init(&osq);
1799   strqueue_split(&sq1, result1);
1800   strqueue_split(&sq2, result2);
1801   strqueue_sort(&sq1);
1802   strqueue_sort(&sq2);
1803   strqueue_diff(&sq1, &sq2, &osq);
1804   r = osq.nstr ? strqueue_join(&osq) : 0;
1805   strqueue_free(&sq1);
1806   strqueue_free(&sq2);
1807   strqueue_free(&osq);
1808   return r;
1809 }
1810