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