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