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