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