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