Imported Upstream version 0.7.7
[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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include "pool.h"
16 #include "poolarch.h"
17 #include "poolvendor.h"
18 #include "evr.h"
19 #include "repo.h"
20 #include "repo_solv.h"
21 #include "solver.h"
22 #include "solverdebug.h"
23 #include "chksum.h"
24 #include "testcase.h"
25 #include "selection.h"
26 #include "solv_xfopen.h"
27 #if ENABLE_TESTCASE_HELIXREPO
28 #include "ext/repo_helix.h"
29 #endif
30
31 #ifdef _WIN32
32   #include <direct.h>
33 #endif
34
35 /* see repo_testcase.c */
36 struct oplist {
37   Id flags;
38   const char *opname;
39 };
40 extern struct oplist oplist[];
41
42
43 static struct job2str {
44   Id job;
45   const char *str;
46 } job2str[] = {
47   { SOLVER_NOOP,           "noop" },
48   { SOLVER_INSTALL,        "install" },
49   { SOLVER_ERASE,          "erase" },
50   { SOLVER_UPDATE,         "update" },
51   { SOLVER_WEAKENDEPS,     "weakendeps" },
52   { SOLVER_MULTIVERSION,   "multiversion" },
53   { SOLVER_MULTIVERSION,   "noobsoletes" },     /* old name */
54   { SOLVER_LOCK,           "lock" },
55   { SOLVER_DISTUPGRADE,    "distupgrade" },
56   { SOLVER_VERIFY,         "verify" },
57   { SOLVER_DROP_ORPHANED,  "droporphaned" },
58   { SOLVER_USERINSTALLED,  "userinstalled" },
59   { SOLVER_ALLOWUNINSTALL, "allowuninstall" },
60   { SOLVER_FAVOR,          "favor" },
61   { SOLVER_DISFAVOR,       "disfavor" },
62   { 0, 0 }
63 };
64
65 static struct jobflags2str {
66   Id flag;
67   const char *str;
68 } jobflags2str[] = {
69   { SOLVER_WEAK,      "weak" },
70   { SOLVER_ESSENTIAL, "essential" },
71   { SOLVER_CLEANDEPS, "cleandeps" },
72   { SOLVER_ORUPDATE,  "orupdate" },
73   { SOLVER_FORCEBEST, "forcebest" },
74   { SOLVER_TARGETED,  "targeted" },
75   { SOLVER_NOTBYUSER, "notbyuser" },
76   { SOLVER_SETEV,     "setev" },
77   { SOLVER_SETEVR,    "setevr" },
78   { SOLVER_SETARCH,   "setarch" },
79   { SOLVER_SETVENDOR, "setvendor" },
80   { SOLVER_SETREPO,   "setrepo" },
81   { SOLVER_NOAUTOSET, "noautoset" },
82   { 0, 0 }
83 };
84
85 static struct resultflags2str {
86   Id flag;
87   const char *str;
88 } resultflags2str[] = {
89   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
90   { TESTCASE_RESULT_PROBLEMS,           "problems" },
91   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
92   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
93   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
94   { TESTCASE_RESULT_ALTERNATIVES,       "alternatives" },
95   { TESTCASE_RESULT_RULES,              "rules" },
96   { TESTCASE_RESULT_GENID,              "genid" },
97   { TESTCASE_RESULT_REASON,             "reason" },
98   { TESTCASE_RESULT_CLEANDEPS,          "cleandeps" },
99   { TESTCASE_RESULT_JOBS,               "jobs" },
100   { TESTCASE_RESULT_USERINSTALLED,      "userinstalled" },
101   { 0, 0 }
102 };
103
104 static struct solverflags2str {
105   Id flag;
106   const char *str;
107   int def;
108 } solverflags2str[] = {
109   { SOLVER_FLAG_ALLOW_DOWNGRADE,            "allowdowngrade", 0 },
110   { SOLVER_FLAG_ALLOW_NAMECHANGE,           "allownamechange", 1 },
111   { SOLVER_FLAG_ALLOW_ARCHCHANGE,           "allowarchchange", 0 },
112   { SOLVER_FLAG_ALLOW_VENDORCHANGE,         "allowvendorchange", 0 },
113   { SOLVER_FLAG_ALLOW_UNINSTALL,            "allowuninstall", 0 },
114   { SOLVER_FLAG_NO_UPDATEPROVIDE,           "noupdateprovide", 0 },
115   { SOLVER_FLAG_SPLITPROVIDES,              "splitprovides", 0 },
116   { SOLVER_FLAG_IGNORE_RECOMMENDED,         "ignorerecommended", 0 },
117   { SOLVER_FLAG_ADD_ALREADY_RECOMMENDED,    "addalreadyrecommended", 0 },
118   { SOLVER_FLAG_NO_INFARCHCHECK,            "noinfarchcheck", 0 },
119   { SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES,    "keepexplicitobsoletes", 0 },
120   { SOLVER_FLAG_BEST_OBEY_POLICY,           "bestobeypolicy", 0 },
121   { SOLVER_FLAG_NO_AUTOTARGET,              "noautotarget", 0 },
122   { SOLVER_FLAG_DUP_ALLOW_DOWNGRADE,        "dupallowdowngrade", 1 },
123   { SOLVER_FLAG_DUP_ALLOW_ARCHCHANGE,       "dupallowarchchange", 1 },
124   { SOLVER_FLAG_DUP_ALLOW_VENDORCHANGE,     "dupallowvendorchange", 1 },
125   { SOLVER_FLAG_DUP_ALLOW_NAMECHANGE,       "dupallownamechange", 1 },
126   { SOLVER_FLAG_KEEP_ORPHANS,               "keeporphans", 0 },
127   { SOLVER_FLAG_BREAK_ORPHANS,              "breakorphans", 0 },
128   { SOLVER_FLAG_FOCUS_INSTALLED,            "focusinstalled", 0 },
129   { SOLVER_FLAG_YUM_OBSOLETES,              "yumobsoletes", 0 },
130   { SOLVER_FLAG_NEED_UPDATEPROVIDE,         "needupdateprovide", 0 },
131   { SOLVER_FLAG_URPM_REORDER,               "urpmreorder", 0 },
132   { SOLVER_FLAG_FOCUS_BEST,                 "focusbest", 0 },
133   { SOLVER_FLAG_STRONG_RECOMMENDS,          "strongrecommends", 0 },
134   { SOLVER_FLAG_INSTALL_ALSO_UPDATES,       "installalsoupdates", 0 },
135   { SOLVER_FLAG_ONLY_NAMESPACE_RECOMMENDED, "onlynamespacerecommended", 0 },
136   { 0, 0, 0 }
137 };
138
139 static struct poolflags2str {
140   Id flag;
141   const char *str;
142   int def;
143 } poolflags2str[] = {
144   { POOL_FLAG_PROMOTEEPOCH,                 "promoteepoch", 0 },
145   { POOL_FLAG_FORBIDSELFCONFLICTS,          "forbidselfconflicts", 0 },
146   { POOL_FLAG_OBSOLETEUSESPROVIDES,         "obsoleteusesprovides", 0 },
147   { POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES, "implicitobsoleteusesprovides", 0 },
148   { POOL_FLAG_OBSOLETEUSESCOLORS,           "obsoleteusescolors", 0 },
149   { POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS,   "implicitobsoleteusescolors", 0 },
150   { POOL_FLAG_NOINSTALLEDOBSOLETES,         "noinstalledobsoletes", 0 },
151   { POOL_FLAG_HAVEDISTEPOCH,                "havedistepoch", 0 },
152   { POOL_FLAG_NOOBSOLETESMULTIVERSION,      "noobsoletesmultiversion", 0 },
153   { POOL_FLAG_ADDFILEPROVIDESFILTERED,      "addfileprovidesfiltered", 0 },
154   { POOL_FLAG_NOWHATPROVIDESAUX,            "nowhatprovidesaux", 0 },
155   { POOL_FLAG_WHATPROVIDESWITHDISABLED,     "whatprovideswithdisabled", 0 },
156   { 0, 0, 0 }
157 };
158
159 static struct disttype2str {
160   Id type;
161   const char *str;
162 } disttype2str[] = {
163   { DISTTYPE_RPM,  "rpm" },
164   { DISTTYPE_DEB,  "deb" },
165   { DISTTYPE_ARCH, "arch" },
166   { DISTTYPE_HAIKU, "haiku" },
167   { DISTTYPE_CONDA, "conda" },
168   { 0, 0 }
169 };
170
171 static struct selflags2str {
172   Id flag;
173   const char *str;
174 } selflags2str[] = {
175   { SELECTION_NAME, "name" },
176   { SELECTION_PROVIDES, "provides" },
177   { SELECTION_FILELIST, "filelist" },
178   { SELECTION_CANON, "canon" },
179   { SELECTION_DOTARCH, "dotarch" },
180   { SELECTION_REL, "rel" },
181   { SELECTION_INSTALLED_ONLY, "installedonly" },
182   { SELECTION_GLOB, "glob" },
183   { SELECTION_FLAT, "flat" },
184   { SELECTION_NOCASE, "nocase" },
185   { SELECTION_SOURCE_ONLY, "sourceonly" },
186   { SELECTION_WITH_SOURCE, "withsource" },
187   { SELECTION_SKIP_KIND, "skipkind" },
188   { SELECTION_MATCH_DEPSTR, "depstr" },
189   { SELECTION_WITH_DISABLED, "withdisabled" },
190   { SELECTION_WITH_BADARCH, "withbadarch" },
191   { SELECTION_ADD, "add" },
192   { SELECTION_SUBTRACT, "subtract" },
193   { SELECTION_FILTER, "filter" },
194   { 0, 0 }
195 };
196
197 static const char *features[] = {
198 #ifdef ENABLE_LINKED_PKGS
199   "linked_packages",
200 #endif
201 #ifdef ENABLE_COMPLEX_DEPS
202   "complex_deps",
203 #endif
204 #if ENABLE_TESTCASE_HELIXREPO
205   "testcase_helixrepo",
206 #endif
207   0
208 };
209
210 typedef struct strqueue {
211   char **str;
212   int nstr;
213 } Strqueue;
214
215 #define STRQUEUE_BLOCK 63
216
217 static void
218 strqueue_init(Strqueue *q)
219 {
220   q->str = 0;
221   q->nstr = 0;
222 }
223
224 static void
225 strqueue_free(Strqueue *q)
226 {
227   int i;
228   for (i = 0; i < q->nstr; i++)
229     solv_free(q->str[i]);
230   q->str = solv_free(q->str);
231   q->nstr = 0;
232 }
233
234 static void
235 strqueue_push(Strqueue *q, const char *s)
236 {
237   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
238   q->str[q->nstr++] = solv_strdup(s);
239 }
240
241 static void
242 strqueue_pushjoin(Strqueue *q, const char *s1, const char *s2, const char *s3)
243 {
244   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
245   q->str[q->nstr++] = solv_dupjoin(s1, s2, s3);
246 }
247
248 static int
249 strqueue_sort_cmp(const void *ap, const void *bp, void *dp)
250 {
251   const char *a = *(const char **)ap;
252   const char *b = *(const char **)bp;
253   return strcmp(a ? a : "", b ? b : "");
254 }
255
256 static void
257 strqueue_sort(Strqueue *q)
258 {
259   if (q->nstr > 1)
260     solv_sort(q->str, q->nstr, sizeof(*q->str), strqueue_sort_cmp, 0);
261 }
262
263 static void
264 strqueue_sort_u(Strqueue *q)
265 {
266   int i, j;
267   strqueue_sort(q);
268   for (i = j = 0; i < q->nstr; i++)
269     if (!j || strqueue_sort_cmp(q->str + i, q->str + j - 1, 0) != 0)
270       q->str[j++] = q->str[i];
271   q->nstr = j;
272 }
273
274 static char *
275 strqueue_join(Strqueue *q)
276 {
277   int i, l = 0;
278   char *r, *rp;
279   for (i = 0; i < q->nstr; i++)
280     if (q->str[i])
281       l += strlen(q->str[i]) + 1;
282   l++;  /* trailing \0 */
283   r = solv_malloc(l);
284   rp = r;
285   for (i = 0; i < q->nstr; i++)
286     if (q->str[i])
287       {
288         strcpy(rp, q->str[i]);
289         rp += strlen(rp);
290         *rp++ = '\n';
291       }
292   *rp = 0;
293   return r;
294 }
295
296 static void
297 strqueue_split(Strqueue *q, const char *s)
298 {
299   const char *p;
300   if (!s)
301     return;
302   while ((p = strchr(s, '\n')) != 0)
303     {
304       q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
305       q->str[q->nstr] = solv_malloc(p - s + 1);
306       if (p > s)
307         memcpy(q->str[q->nstr], s, p - s);
308       q->str[q->nstr][p - s] = 0;
309       q->nstr++;
310       s = p + 1;
311     }
312   if (*s)
313     strqueue_push(q, s);
314 }
315
316 static void
317 strqueue_diff(Strqueue *sq1, Strqueue *sq2, Strqueue *osq)
318 {
319   int i = 0, j = 0;
320   while (i < sq1->nstr && j < sq2->nstr)
321     {
322       int r = strqueue_sort_cmp(sq1->str + i, sq2->str + j, 0);
323       if (!r)
324         i++, j++;
325       else if (r < 0)
326         strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
327       else
328         strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
329     }
330   while (i < sq1->nstr)
331     strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
332   while (j < sq2->nstr)
333     strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
334 }
335
336 /**********************************************************/
337
338 const char *
339 testcase_repoid2str(Pool *pool, Id repoid)
340 {
341   Repo *repo = pool_id2repo(pool, repoid);
342   if (repo->name)
343     {
344       char *r = pool_tmpjoin(pool, repo->name, 0, 0);
345       char *rp;
346       for (rp = r; *rp; rp++)
347         if (*rp == ' ' || *rp == '\t')
348           *rp = '_';
349       return r;
350     }
351   else
352     {
353       char buf[20];
354       sprintf(buf, "#%d", repoid);
355       return pool_tmpjoin(pool, buf, 0, 0);
356     }
357 }
358
359 const char *
360 testcase_solvid2str(Pool *pool, Id p)
361 {
362   Solvable *s = pool->solvables + p;
363   const char *n, *e, *a;
364   char *str, buf[20];
365
366   if (p == SYSTEMSOLVABLE)
367     return "@SYSTEM";
368   n = pool_id2str(pool, s->name);
369   e = pool_id2str(pool, s->evr);
370   a = pool_id2str(pool, s->arch);
371   str = pool_alloctmpspace(pool, strlen(n) + strlen(e) + strlen(a) + 3);
372   sprintf(str, "%s-%s", n, e);
373   if (solvable_lookup_type(s, SOLVABLE_BUILDFLAVOR))
374     {
375       Queue flavorq;
376       int i;
377
378       queue_init(&flavorq);
379       solvable_lookup_idarray(s, SOLVABLE_BUILDFLAVOR, &flavorq);
380       for (i = 0; i < flavorq.count; i++)
381         str = pool_tmpappend(pool, str, "-", pool_id2str(pool, flavorq.elements[i]));
382       queue_free(&flavorq);
383     }
384   if (s->arch)
385     str = pool_tmpappend(pool, str, ".", a);
386   if (!s->repo)
387     return pool_tmpappend(pool, str, "@", 0);
388   if (s->repo->name)
389     {
390       int l = strlen(str);
391       str = pool_tmpappend(pool, str, "@", s->repo->name);
392       for (; str[l]; l++)
393         if (str[l] == ' ' || str[l] == '\t')
394           str[l] = '_';
395       return str;
396     }
397   sprintf(buf, "@#%d", s->repo->repoid);
398   return pool_tmpappend(pool, str, buf, 0);
399 }
400
401 Repo *
402 testcase_str2repo(Pool *pool, const char *str)
403 {
404   int repoid;
405   Repo *repo = 0;
406   if (str[0] == '#' && (str[1] >= '0' && str[1] <= '9'))
407     {
408       int j;
409       repoid = 0;
410       for (j = 1; str[j] >= '0' && str[j] <= '9'; j++)
411         repoid = repoid * 10 + (str[j] - '0');
412       if (!str[j] && repoid > 0 && repoid < pool->nrepos)
413         repo = pool_id2repo(pool, repoid);
414     }
415   if (!repo)
416     {
417       FOR_REPOS(repoid, repo)
418         {
419           int i, l;
420           if (!repo->name)
421             continue;
422           l = strlen(repo->name);
423           for (i = 0; i < l; i++)
424             {
425               int c = repo->name[i];
426               if (c == ' ' || c == '\t')
427                 c = '_';
428               if (c != str[i])
429                 break;
430             }
431           if (i == l && !str[l])
432             break;
433         }
434       if (repoid >= pool->nrepos)
435         repo = 0;
436     }
437   return repo;
438 }
439
440 /* check evr and buildflavors */
441 static int
442 str2solvid_check(Pool *pool, Solvable *s, const char *start, const char *end, Id evrid)
443 {
444   if (!solvable_lookup_type(s, SOLVABLE_BUILDFLAVOR))
445     {
446       /* just check the evr */
447       return evrid && s->evr == evrid;
448     }
449   else
450     {
451       Queue flavorq;
452       int i;
453
454       queue_init(&flavorq);
455       solvable_lookup_idarray(s, SOLVABLE_BUILDFLAVOR, &flavorq);
456       queue_unshift(&flavorq, s->evr);
457       for (i = 0; i < flavorq.count; i++)
458         {
459           const char *part = pool_id2str(pool, flavorq.elements[i]);
460           size_t partl = strlen(part);
461           if (start + partl > end || strncmp(start, part, partl) != 0)
462             break;
463           start += partl;
464           if (i + 1 < flavorq.count)
465             {
466               if (start >= end || *start != '-')
467                 break;
468               start++;
469             }
470         }
471       if (i < flavorq.count)
472         {
473           queue_free(&flavorq);
474           return 0;
475         }
476       queue_free(&flavorq);
477       return start == end;
478     }
479 }
480
481 Id
482 testcase_str2solvid(Pool *pool, const char *str)
483 {
484   int i, l = strlen(str);
485   int repostart;
486   Repo *repo;
487   Id arch;
488
489   if (!l)
490     return 0;
491   if (*str == '@' && !strcmp(str, "@SYSTEM"))
492     return SYSTEMSOLVABLE;
493   repo = 0;
494   for (i = l - 1; i >= 0; i--)
495     if (str[i] == '@' && (repo = testcase_str2repo(pool, str + i + 1)) != 0)
496       break;
497   if (i < 0)
498     i = l;
499   repostart = i;
500   /* now find the arch (if present) */
501   arch = 0;
502   for (i = repostart - 1; i > 0; i--)
503     if (str[i] == '.')
504       {
505         arch = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
506         if (arch)
507           repostart = i;
508         break;
509       }
510   /* now find the name */
511   for (i = repostart - 1; i > 0; i--)
512     {
513       if (str[i] == '-')
514         {
515           Id nid, evrid, p, pp;
516           nid = pool_strn2id(pool, str, i, 0);
517           if (!nid)
518             continue;
519           evrid = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
520           /* first check whatprovides */
521           FOR_PROVIDES(p, pp, nid)
522             {
523               Solvable *s = pool->solvables + p;
524               if (s->name != nid)
525                 continue;
526               if (repo && s->repo != repo)
527                 continue;
528               if (arch && s->arch != arch)
529                 continue;
530               if (str2solvid_check(pool, s, str + i + 1, str + repostart, evrid))
531                 return p;
532             }
533           /* maybe it's not installable and thus not in whatprovides. do a slow search */
534           if (repo)
535             {
536               Solvable *s;
537               FOR_REPO_SOLVABLES(repo, p, s)
538                 {
539                   if (s->name != nid)
540                     continue;
541                   if (arch && s->arch != arch)
542                     continue;
543                   if (str2solvid_check(pool, s, str + i + 1, str + repostart, evrid))
544                     return p;
545                 }
546             }
547           else
548             {
549               FOR_POOL_SOLVABLES(p)
550                 {
551                   Solvable *s = pool->solvables + p;
552                   if (s->name != nid)
553                     continue;
554                   if (arch && s->arch != arch)
555                     continue;
556                   if (str2solvid_check(pool, s, str + i + 1, str + repostart, evrid))
557                     return p;
558                 }
559             }
560         }
561     }
562   return 0;
563 }
564
565 const char *
566 testcase_job2str(Pool *pool, Id how, Id what)
567 {
568   char *ret;
569   const char *jobstr;
570   const char *selstr;
571   const char *pkgstr;
572   int i, o;
573   Id select = how & SOLVER_SELECTMASK;
574
575   for (i = 0; job2str[i].str; i++)
576     if ((how & SOLVER_JOBMASK) == job2str[i].job)
577       break;
578   jobstr = job2str[i].str ? job2str[i].str : "unknown";
579   if (select == SOLVER_SOLVABLE)
580     {
581       selstr = " pkg ";
582       pkgstr = testcase_solvid2str(pool, what);
583     }
584   else if (select == SOLVER_SOLVABLE_NAME)
585     {
586       selstr = " name ";
587       pkgstr = testcase_dep2str(pool, what);
588     }
589   else if (select == SOLVER_SOLVABLE_PROVIDES)
590     {
591       selstr = " provides ";
592       pkgstr = testcase_dep2str(pool, what);
593     }
594   else if (select == SOLVER_SOLVABLE_ONE_OF)
595     {
596       Id p;
597       selstr = " oneof ";
598       pkgstr = 0;
599       while ((p = pool->whatprovidesdata[what++]) != 0)
600         {
601           const char *s = testcase_solvid2str(pool, p);
602           if (pkgstr)
603             {
604               pkgstr = pool_tmpappend(pool, pkgstr, " ", s);
605               pool_freetmpspace(pool, s);
606             }
607           else
608             pkgstr = s;
609         }
610       if (!pkgstr)
611         pkgstr = "nothing";
612     }
613   else if (select == SOLVER_SOLVABLE_REPO)
614     {
615       Repo *repo = pool_id2repo(pool, what);
616       selstr = " repo ";
617       if (!repo->name)
618         {
619           char buf[20];
620           sprintf(buf, "#%d", repo->repoid);
621           pkgstr = pool_tmpjoin(pool, buf, 0, 0);
622         }
623       else
624         pkgstr = pool_tmpjoin(pool, repo->name, 0, 0);
625     }
626   else if (select == SOLVER_SOLVABLE_ALL)
627     {
628       selstr = " all ";
629       pkgstr = "packages";
630     }
631   else
632     {
633       selstr = " unknown ";
634       pkgstr = "unknown";
635     }
636   ret = pool_tmpjoin(pool, jobstr, selstr, pkgstr);
637   o = strlen(ret);
638   ret = pool_tmpappend(pool, ret, " ", 0);
639   for (i = 0; jobflags2str[i].str; i++)
640     if ((how & jobflags2str[i].flag) != 0)
641       ret = pool_tmpappend(pool, ret, ",", jobflags2str[i].str);
642   if (!ret[o + 1])
643     ret[o] = 0;
644   else
645     {
646       ret[o + 1] = '[';
647       ret = pool_tmpappend(pool, ret, "]", 0);
648     }
649   return ret;
650 }
651
652 static int
653 str2selflags(Pool *pool, char *s)       /* modifies the string! */
654 {
655   int i, selflags = 0;
656   while (s)
657     {
658       char *se = strchr(s, ',');
659       if (se)
660         *se++ = 0;
661       for (i = 0; selflags2str[i].str; i++)
662         if (!strcmp(s, selflags2str[i].str))
663           {
664             selflags |= selflags2str[i].flag;
665             break;
666           }
667       if (!selflags2str[i].str)
668         pool_error(pool, 0, "str2job: unknown selection flag '%s'", s);
669       s = se;
670     }
671   return selflags;
672 }
673
674 static int
675 str2jobflags(Pool *pool, char *s)       /* modifies the string */
676 {
677   int i, jobflags = 0;
678   while (s)
679     {
680       char *se = strchr(s, ',');
681       if (se)
682         *se++ = 0;
683       for (i = 0; jobflags2str[i].str; i++)
684         if (!strcmp(s, jobflags2str[i].str))
685           {
686             jobflags |= jobflags2str[i].flag;
687             break;
688           }
689       if (!jobflags2str[i].str)
690         pool_error(pool, 0, "str2job: unknown job flag '%s'", s);
691       s = se;
692     }
693   return jobflags;
694 }
695
696 static Id
697 testcase_str2jobsel(Pool *pool, const char *caller, char **pieces, int npieces, Id *whatp)
698 {
699   Id job, what;
700   if (!strcmp(pieces[0], "pkg") && npieces == 2)
701     {
702       job = SOLVER_SOLVABLE;
703       what = testcase_str2solvid(pool, pieces[1]);
704       if (!what)
705         return pool_error(pool, -1, "%s: unknown package '%s'", caller, pieces[1]);
706     }
707   else if (!strcmp(pieces[0], "name") || !strcmp(pieces[0], "provides"))
708     {
709       /* join em again for dep2str... */
710       char *sp;
711       for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
712         if (*sp == 0)
713           *sp = ' ';
714       what = 0;
715       if (pieces[0][0] == 'p' && strncmp(pieces[1], "namespace:", 10) == 0)
716         {
717           char *spe = strchr(pieces[1], '(');
718           int l = strlen(pieces[1]);
719           if (spe && pieces[1][l - 1] == ')')
720             {
721               /* special namespace provides */
722               if (strcmp(spe, "(<NULL>)") != 0)
723                 {
724                   pieces[1][l - 1] = 0;
725                   what = testcase_str2dep(pool, spe + 1);
726                   pieces[1][l - 1] = ')';
727                 }
728               what = pool_rel2id(pool, pool_strn2id(pool, pieces[1], spe - pieces[1], 1), what, REL_NAMESPACE, 1);
729             }
730         }
731       if (!what)
732         what = testcase_str2dep(pool, pieces[1]);
733       if (pieces[0][0] == 'n')
734         job = SOLVER_SOLVABLE_NAME;
735       else
736         job = SOLVER_SOLVABLE_PROVIDES;
737     }
738   else if (!strcmp(pieces[0], "oneof"))
739     {
740       Queue q;
741       job = SOLVER_SOLVABLE_ONE_OF;
742       queue_init(&q);
743       if (npieces > 1 && strcmp(pieces[1], "nothing") != 0)
744         {
745           int i;
746           for (i = 1; i < npieces; i++)
747             {
748               Id p = testcase_str2solvid(pool, pieces[i]);
749               if (!p)
750                 {
751                   queue_free(&q);
752                   return pool_error(pool, -1, "%s: unknown package '%s'", caller, pieces[i]);
753                 }
754               queue_push(&q, p);
755             }
756         }
757       what = pool_queuetowhatprovides(pool, &q);
758       queue_free(&q);
759     }
760   else if (!strcmp(pieces[0], "repo") && npieces == 2)
761     {
762       Repo *repo = testcase_str2repo(pool, pieces[1]);
763       if (!repo)
764         return pool_error(pool, -1, "%s: unknown repo '%s'", caller, pieces[1]);
765       job = SOLVER_SOLVABLE_REPO;
766       what = repo->repoid;
767     }
768   else if (!strcmp(pieces[0], "all") && npieces == 2 && !strcmp(pieces[1], "packages"))
769     {
770       job = SOLVER_SOLVABLE_ALL;
771       what = 0;
772     }
773   else
774     {
775       /* join em again for the error message... */
776       char *sp;
777       for (sp = pieces[0]; sp < pieces[npieces - 1]; sp++)
778         if (*sp == 0)
779           *sp = ' ';
780       return pool_error(pool, -1, "%s: bad line '%s'", caller, pieces[0]);
781     }
782   *whatp = what;
783   return job;
784 }
785
786 Id
787 testcase_str2job(Pool *pool, const char *str, Id *whatp)
788 {
789   int i;
790   Id job, jobsel;
791   Id what;
792   char *s;
793   char **pieces = 0;
794   int npieces = 0;
795
796   *whatp = 0;
797   /* so we can patch it */
798   s = pool_tmpjoin(pool, str, 0, 0);
799   /* split it in pieces */
800   for (;;)
801     {
802       while (*s == ' ' || *s == '\t')
803         s++;
804       if (!*s)
805         break;
806       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
807       pieces[npieces++] = s;
808       while (*s && *s != ' ' && *s != '\t')
809         s++;
810       if (*s)
811         *s++ = 0;
812     }
813   if (npieces < 3)
814     {
815       pool_error(pool, -1, "str2job: bad line '%s'", str);
816       solv_free(pieces);
817       return -1;
818     }
819
820   for (i = 0; job2str[i].str; i++)
821     if (!strcmp(pieces[0], job2str[i].str))
822       break;
823   if (!job2str[i].str)
824     {
825       pool_error(pool, -1, "str2job: unknown job '%s'", str);
826       solv_free(pieces);
827       return -1;
828     }
829   job = job2str[i].job;
830   what = 0;
831   if (npieces > 3)
832     {
833       char *flags = pieces[npieces - 1];
834       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
835         {
836           npieces--;
837           flags++;
838           flags[strlen(flags) - 1] = 0;
839           job |= str2jobflags(pool, flags);
840         }
841     }
842   jobsel = testcase_str2jobsel(pool, "str2job", pieces + 1, npieces - 1, &what);
843   solv_free(pieces);
844   if (jobsel == -1)
845     return -1;
846   *whatp = what;
847   return job | jobsel;
848 }
849
850 #define SELECTIONJOB_MATCHDEPS          1
851 #define SELECTIONJOB_MATCHDEPID         2
852 #define SELECTIONJOB_MATCHSOLVABLE      3
853
854 static int
855 addselectionjob(Pool *pool, char **pieces, int npieces, Queue *jobqueue, int type, int keyname)
856 {
857   Id job;
858   int i, r = 0;
859   int selflags;
860   Queue sel;
861   char *sp;
862
863   for (i = 0; job2str[i].str; i++)
864     if (!strcmp(pieces[0], job2str[i].str))
865       break;
866   if (!job2str[i].str)
867     return pool_error(pool, -1, "selstr2job: unknown job '%s'", pieces[0]);
868   job = job2str[i].job;
869   if (npieces > 3)
870     {
871       char *flags = pieces[npieces - 1];
872       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
873         {
874           npieces--;
875           flags++;
876           flags[strlen(flags) - 1] = 0;
877           job |= str2jobflags(pool, flags);
878         }
879     }
880   if (npieces < 4)
881     return pool_error(pool, -1, "selstr2job: no selection flags");
882   selflags = str2selflags(pool, pieces[npieces - 1]);
883   /* re-join pieces */
884   for (sp = pieces[2]; sp < pieces[npieces - 2]; sp++)
885     if (*sp == 0)
886       *sp = ' ';
887   queue_init(&sel);
888   if (selflags & (SELECTION_ADD | SELECTION_SUBTRACT | SELECTION_FILTER))
889     {
890       for (i = 0; i < jobqueue->count; i += 2)
891         queue_push2(&sel, jobqueue->elements[i] & (SOLVER_SELECTMASK | SOLVER_SETMASK), jobqueue->elements[i + 1]);
892       queue_empty(jobqueue);
893     }
894   if (!type)
895     r = selection_make(pool, &sel, pieces[2], selflags);
896   else if (type == SELECTIONJOB_MATCHDEPS)
897     r = selection_make_matchdeps(pool, &sel, pieces[2], selflags, keyname, 0);
898   else if (type == SELECTIONJOB_MATCHDEPID)
899     r = selection_make_matchdepid(pool, &sel, testcase_str2dep(pool, pieces[2]), selflags, keyname, 0);
900   else if (type == SELECTIONJOB_MATCHSOLVABLE)
901     r = selection_make_matchsolvable(pool, &sel, testcase_str2solvid(pool, pieces[2]), selflags, keyname, 0);
902   for (i = 0; i < sel.count; i += 2)
903     queue_push2(jobqueue, job | sel.elements[i], sel.elements[i + 1]);
904   queue_free(&sel);
905   return r;
906 }
907
908 const char *
909 testcase_getpoolflags(Pool *pool)
910 {
911   const char *str = 0;
912   int i, v;
913   for (i = 0; poolflags2str[i].str; i++)
914     {
915       v = pool_get_flag(pool, poolflags2str[i].flag);
916       if (v == poolflags2str[i].def)
917         continue;
918       str = pool_tmpappend(pool, str, v ? " " : " !", poolflags2str[i].str);
919     }
920   return str ? str + 1 : "";
921 }
922
923 int
924 testcase_setpoolflags(Pool *pool, const char *str)
925 {
926   const char *p = str, *s;
927   int i, v;
928   for (;;)
929     {
930       while (*p == ' ' || *p == '\t' || *p == ',')
931         p++;
932       v = 1;
933       if (*p == '!')
934         {
935           p++;
936           v = 0;
937         }
938       if (!*p)
939         break;
940       s = p;
941       while (*p && *p != ' ' && *p != '\t' && *p != ',')
942         p++;
943       for (i = 0; poolflags2str[i].str; i++)
944         if (!strncmp(poolflags2str[i].str, s, p - s) && poolflags2str[i].str[p - s] == 0)
945           break;
946       if (!poolflags2str[i].str)
947         return pool_error(pool, 0, "setpoolflags: unknown flag '%.*s'", (int)(p - s), s);
948       pool_set_flag(pool, poolflags2str[i].flag, v);
949     }
950   return 1;
951 }
952
953 void
954 testcase_resetpoolflags(Pool *pool)
955 {
956   int i;
957   for (i = 0; poolflags2str[i].str; i++)
958     pool_set_flag(pool, poolflags2str[i].flag, poolflags2str[i].def);
959 }
960
961 const char *
962 testcase_getsolverflags(Solver *solv)
963 {
964   Pool *pool = solv->pool;
965   const char *str = 0;
966   int i, v;
967   for (i = 0; solverflags2str[i].str; i++)
968     {
969       v = solver_get_flag(solv, solverflags2str[i].flag);
970       if (v == solverflags2str[i].def)
971         continue;
972       str = pool_tmpappend(pool, str, v ? " " : " !", solverflags2str[i].str);
973     }
974   return str ? str + 1 : "";
975 }
976
977 int
978 testcase_setsolverflags(Solver *solv, const char *str)
979 {
980   const char *p = str, *s;
981   int i, v;
982   for (;;)
983     {
984       while (*p == ' ' || *p == '\t' || *p == ',')
985         p++;
986       v = 1;
987       if (*p == '!')
988         {
989           p++;
990           v = 0;
991         }
992       if (!*p)
993         break;
994       s = p;
995       while (*p && *p != ' ' && *p != '\t' && *p != ',')
996         p++;
997       for (i = 0; solverflags2str[i].str; i++)
998         if (!strncmp(solverflags2str[i].str, s, p - s) && solverflags2str[i].str[p - s] == 0)
999           break;
1000       if (!solverflags2str[i].str)
1001         return pool_error(solv->pool, 0, "setsolverflags: unknown flag '%.*s'", (int)(p - s), s);
1002       if (solver_set_flag(solv, solverflags2str[i].flag, v) == -1)
1003         return pool_error(solv->pool, 0, "setsolverflags: unsupported flag '%s'", solverflags2str[i].str);
1004     }
1005   return 1;
1006 }
1007
1008 void
1009 testcase_resetsolverflags(Solver *solv)
1010 {
1011   int i;
1012   for (i = 0; solverflags2str[i].str; i++)
1013     solver_set_flag(solv, solverflags2str[i].flag, solverflags2str[i].def);
1014 }
1015
1016 static const char *
1017 testcase_ruleid(Solver *solv, Id rid)
1018 {
1019   Strqueue sq;
1020   Queue q;
1021   int i;
1022   Chksum *chk;
1023   const unsigned char *md5;
1024   int md5l;
1025   const char *s;
1026
1027   queue_init(&q);
1028   strqueue_init(&sq);
1029   solver_ruleliterals(solv, rid, &q);
1030   for (i = 0; i < q.count; i++)
1031     {
1032       Id p = q.elements[i];
1033       s = testcase_solvid2str(solv->pool, p > 0 ? p : -p);
1034       if (p < 0)
1035         s = pool_tmpjoin(solv->pool, "!", s, 0);
1036       strqueue_push(&sq, s);
1037     }
1038   queue_free(&q);
1039   strqueue_sort_u(&sq);
1040   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1041   for (i = 0; i < sq.nstr; i++)
1042     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1043   md5 = solv_chksum_get(chk, &md5l);
1044   s = pool_bin2hex(solv->pool, md5, md5l);
1045   chk = solv_chksum_free(chk, 0);
1046   strqueue_free(&sq);
1047   return s;
1048 }
1049
1050 static const char *
1051 testcase_problemid(Solver *solv, Id problem)
1052 {
1053   Strqueue sq;
1054   Queue q;
1055   Chksum *chk;
1056   const unsigned char *md5;
1057   int i, md5l;
1058   const char *s;
1059
1060   /* we build a hash of all rules that define the problem */
1061   queue_init(&q);
1062   strqueue_init(&sq);
1063   solver_findallproblemrules(solv, problem, &q);
1064   for (i = 0; i < q.count; i++)
1065     strqueue_push(&sq, testcase_ruleid(solv, q.elements[i]));
1066   queue_free(&q);
1067   strqueue_sort_u(&sq);
1068   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1069   for (i = 0; i < sq.nstr; i++)
1070     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1071   md5 = solv_chksum_get(chk, &md5l);
1072   s = pool_bin2hex(solv->pool, md5, 4);
1073   chk = solv_chksum_free(chk, 0);
1074   strqueue_free(&sq);
1075   return s;
1076 }
1077
1078 static const char *
1079 testcase_solutionid(Solver *solv, Id problem, Id solution)
1080 {
1081   Id intid;
1082   Chksum *chk;
1083   const unsigned char *md5;
1084   int md5l;
1085   const char *s;
1086
1087   intid = solver_solutionelement_internalid(solv, problem, solution);
1088   /* internal stuff! handle with care! */
1089   if (intid < 0)
1090     {
1091       /* it's a job */
1092       s = testcase_job2str(solv->pool, solv->job.elements[-intid - 1], solv->job.elements[-intid]);
1093     }
1094   else
1095     {
1096       /* it's a rule */
1097       s = testcase_ruleid(solv, intid);
1098     }
1099   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1100   solv_chksum_add(chk, s, strlen(s) + 1);
1101   md5 = solv_chksum_get(chk, &md5l);
1102   s = pool_bin2hex(solv->pool, md5, 4);
1103   chk = solv_chksum_free(chk, 0);
1104   return s;
1105 }
1106
1107 static const char *
1108 testcase_alternativeid(Solver *solv, int type, Id id, Id from)
1109 {
1110   const char *s;
1111   Pool *pool = solv->pool;
1112   Chksum *chk;
1113   const unsigned char *md5;
1114   int md5l;
1115   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1116   if (type == SOLVER_ALTERNATIVE_TYPE_RECOMMENDS)
1117     {
1118       s = testcase_solvid2str(pool, from);
1119       solv_chksum_add(chk, s, strlen(s) + 1);
1120       s = testcase_dep2str(pool, id);
1121       solv_chksum_add(chk, s, strlen(s) + 1);
1122     }
1123   else if (type == SOLVER_ALTERNATIVE_TYPE_RULE)
1124     {
1125       s = testcase_ruleid(solv, id);
1126       solv_chksum_add(chk, s, strlen(s) + 1);
1127     }
1128   md5 = solv_chksum_get(chk, &md5l);
1129   s = pool_bin2hex(pool, md5, 4);
1130   chk = solv_chksum_free(chk, 0);
1131   return s;
1132 }
1133
1134 static struct class2str {
1135   Id class;
1136   const char *str;
1137 } class2str[] = {
1138   { SOLVER_TRANSACTION_ERASE,          "erase" },
1139   { SOLVER_TRANSACTION_INSTALL,        "install" },
1140   { SOLVER_TRANSACTION_REINSTALLED,    "reinstall" },
1141   { SOLVER_TRANSACTION_DOWNGRADED,     "downgrade" },
1142   { SOLVER_TRANSACTION_CHANGED,        "change" },
1143   { SOLVER_TRANSACTION_UPGRADED,       "upgrade" },
1144   { SOLVER_TRANSACTION_OBSOLETED,      "obsolete" },
1145   { SOLVER_TRANSACTION_MULTIINSTALL,   "multiinstall" },
1146   { SOLVER_TRANSACTION_MULTIREINSTALL, "multireinstall" },
1147   { 0, 0 }
1148 };
1149
1150 static struct reason2str {
1151   Id reason;
1152   const char *str;
1153 } reason2str[] = {
1154   { SOLVER_REASON_UNRELATED,            "unrelated" },
1155   { SOLVER_REASON_UNIT_RULE,            "unit" },
1156   { SOLVER_REASON_KEEP_INSTALLED,       "keep" },
1157   { SOLVER_REASON_RESOLVE_JOB,          "job" },
1158   { SOLVER_REASON_UPDATE_INSTALLED,     "update" },
1159   { SOLVER_REASON_CLEANDEPS_ERASE,      "cleandeps" },
1160   { SOLVER_REASON_RESOLVE,              "resolve" },
1161   { SOLVER_REASON_WEAKDEP,              "weakdep" },
1162   { SOLVER_REASON_RESOLVE_ORPHAN,       "orphan" },
1163
1164   { SOLVER_REASON_RECOMMENDED,          "recommended" },
1165   { SOLVER_REASON_SUPPLEMENTED,         "supplemented" },
1166   { 0, 0 }
1167 };
1168
1169 static const char *
1170 testcase_reason2str(Id reason)
1171 {
1172   int i;
1173   for (i = 0; reason2str[i].str; i++)
1174     if (reason == reason2str[i].reason)
1175       return reason2str[i].str;
1176   return "?";
1177 }
1178
1179 static struct rclass2str {
1180   Id rclass;
1181   const char *str;
1182 } rclass2str[] = {
1183   { SOLVER_RULE_PKG, "pkg" },
1184   { SOLVER_RULE_UPDATE, "update" },
1185   { SOLVER_RULE_FEATURE, "feature" },
1186   { SOLVER_RULE_JOB, "job" },
1187   { SOLVER_RULE_DISTUPGRADE, "distupgrade" },
1188   { SOLVER_RULE_INFARCH, "infarch" },
1189   { SOLVER_RULE_CHOICE, "choice" },
1190   { SOLVER_RULE_LEARNT, "learnt" },
1191   { SOLVER_RULE_BEST, "best" },
1192   { SOLVER_RULE_YUMOBS, "yumobs" },
1193   { SOLVER_RULE_RECOMMENDS, "recommends" },
1194   { 0, 0 }
1195 };
1196
1197 static const char *
1198 testcase_rclass2str(Id rclass)
1199 {
1200   int i;
1201   for (i = 0; rclass2str[i].str; i++)
1202     if (rclass == rclass2str[i].rclass)
1203       return rclass2str[i].str;
1204   return "unknown";
1205 }
1206
1207 static int
1208 dump_genid(Pool *pool, Strqueue *sq, Id id, int cnt)
1209 {
1210   struct oplist *op;
1211   char cntbuf[20];
1212   const char *s;
1213
1214   if (ISRELDEP(id))
1215     {
1216       Reldep *rd = GETRELDEP(pool, id);
1217       for (op = oplist; op->flags; op++)
1218         if (rd->flags == op->flags)
1219           break;
1220       cnt = dump_genid(pool, sq, rd->name, cnt);
1221       cnt = dump_genid(pool, sq, rd->evr, cnt);
1222       sprintf(cntbuf, "genid %2d: genid ", cnt++);
1223       s = pool_tmpjoin(pool, cntbuf, "op ", op->flags ? op->opname : "unknown");
1224     }
1225   else
1226     {
1227       sprintf(cntbuf, "genid %2d: genid ", cnt++);
1228       s = pool_tmpjoin(pool, cntbuf, id ? "lit " : "null", id ? pool_id2str(pool, id) : 0);
1229     }
1230   strqueue_push(sq, s);
1231   return cnt;
1232 }
1233
1234 char *
1235 testcase_solverresult(Solver *solv, int resultflags)
1236 {
1237   Pool *pool = solv->pool;
1238   int i, j;
1239   Id p, op;
1240   const char *s;
1241   char *result;
1242   Strqueue sq;
1243
1244   strqueue_init(&sq);
1245   if ((resultflags & TESTCASE_RESULT_TRANSACTION) != 0)
1246     {
1247       Transaction *trans = solver_create_transaction(solv);
1248       Queue q;
1249
1250       queue_init(&q);
1251       for (i = 0; class2str[i].str; i++)
1252         {
1253           queue_empty(&q);
1254           transaction_classify_pkgs(trans, SOLVER_TRANSACTION_KEEP_PSEUDO, class2str[i].class, 0, 0, &q);
1255           for (j = 0; j < q.count; j++)
1256             {
1257               p = q.elements[j];
1258               op = 0;
1259               if (pool->installed && pool->solvables[p].repo == pool->installed)
1260                 op = transaction_obs_pkg(trans, p);
1261               s = pool_tmpjoin(pool, class2str[i].str, " ", testcase_solvid2str(pool, p));
1262               if (op)
1263                 s = pool_tmpjoin(pool, s, " ", testcase_solvid2str(pool, op));
1264               strqueue_push(&sq, s);
1265             }
1266         }
1267       queue_free(&q);
1268       transaction_free(trans);
1269     }
1270   if ((resultflags & TESTCASE_RESULT_PROBLEMS) != 0)
1271     {
1272       char *probprefix, *solprefix;
1273       int problem, solution, element;
1274       int pcnt, scnt;
1275
1276       pcnt = solver_problem_count(solv);
1277       for (problem = 1; problem <= pcnt; problem++)
1278         {
1279           Id rid, from, to, dep;
1280           SolverRuleinfo rinfo;
1281           rid = solver_findproblemrule(solv, problem);
1282           s = testcase_problemid(solv, problem);
1283           probprefix = solv_dupjoin("problem ", s, 0);
1284           rinfo = solver_ruleinfo(solv, rid, &from, &to, &dep);
1285           s = pool_tmpjoin(pool, probprefix, " info ", solver_problemruleinfo2str(solv, rinfo, from, to, dep));
1286           strqueue_push(&sq, s);
1287           scnt = solver_solution_count(solv, problem);
1288           for (solution = 1; solution <= scnt; solution++)
1289             {
1290               s = testcase_solutionid(solv, problem, solution);
1291               solprefix = solv_dupjoin(probprefix, " solution ", s);
1292               element = 0;
1293               while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &op)) != 0)
1294                 {
1295                   if (p == SOLVER_SOLUTION_JOB)
1296                     s = pool_tmpjoin(pool, solprefix, " deljob ", testcase_job2str(pool, solv->job.elements[op - 1], solv->job.elements[op]));
1297                   else if (p > 0 && op == 0)
1298                     s = pool_tmpjoin(pool, solprefix, " erase ", testcase_solvid2str(pool, p));
1299                   else if (p > 0 && op > 0)
1300                     {
1301                       s = pool_tmpjoin(pool, solprefix, " replace ", testcase_solvid2str(pool, p));
1302                       s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, op));
1303                     }
1304                   else if (p < 0 && op > 0)
1305                     s = pool_tmpjoin(pool, solprefix, " allow ", testcase_solvid2str(pool, op));
1306                   else
1307                     s = pool_tmpjoin(pool, solprefix, " unknown", 0);
1308                   strqueue_push(&sq, s);
1309                 }
1310               solv_free(solprefix);
1311             }
1312           solv_free(probprefix);
1313         }
1314     }
1315
1316   if ((resultflags & TESTCASE_RESULT_ORPHANED) != 0)
1317     {
1318       Queue q;
1319
1320       queue_init(&q);
1321       solver_get_orphaned(solv, &q);
1322       for (i = 0; i < q.count; i++)
1323         {
1324           s = pool_tmpjoin(pool, "orphaned ", testcase_solvid2str(pool, q.elements[i]), 0);
1325           strqueue_push(&sq, s);
1326         }
1327       queue_free(&q);
1328     }
1329
1330   if ((resultflags & TESTCASE_RESULT_RECOMMENDED) != 0)
1331     {
1332       Queue qr, qs;
1333
1334       queue_init(&qr);
1335       queue_init(&qs);
1336       solver_get_recommendations(solv, &qr, &qs, 0);
1337       for (i = 0; i < qr.count; i++)
1338         {
1339           s = pool_tmpjoin(pool, "recommended ", testcase_solvid2str(pool, qr.elements[i]), 0);
1340           strqueue_push(&sq, s);
1341         }
1342       for (i = 0; i < qs.count; i++)
1343         {
1344           s = pool_tmpjoin(pool, "suggested ", testcase_solvid2str(pool, qs.elements[i]), 0);
1345           strqueue_push(&sq, s);
1346         }
1347       queue_free(&qr);
1348       queue_free(&qs);
1349     }
1350
1351   if ((resultflags & TESTCASE_RESULT_UNNEEDED) != 0)
1352     {
1353       Queue q, qf;
1354
1355       queue_init(&q);
1356       queue_init(&qf);
1357       solver_get_unneeded(solv, &q, 0);
1358       solver_get_unneeded(solv, &qf, 1);
1359       for (i = j = 0; i < q.count; i++)
1360         {
1361           /* we rely on qf containing a subset of q in the same order */
1362           if (j < qf.count && q.elements[i] == qf.elements[j])
1363             {
1364               s = pool_tmpjoin(pool, "unneeded_filtered ", testcase_solvid2str(pool, q.elements[i]), 0);
1365               j++;
1366             }
1367           else
1368             s = pool_tmpjoin(pool, "unneeded ", testcase_solvid2str(pool, q.elements[i]), 0);
1369           strqueue_push(&sq, s);
1370         }
1371       queue_free(&q);
1372       queue_free(&qf);
1373     }
1374   if ((resultflags & TESTCASE_RESULT_USERINSTALLED) != 0)
1375     {
1376       Queue q;
1377       solver_get_userinstalled(solv, &q, 0);
1378       for (i = 0; i < q.count; i++)
1379         {
1380           s = pool_tmpjoin(pool, "userinstalled pkg ", testcase_solvid2str(pool, q.elements[i]), 0);
1381           strqueue_push(&sq, s);
1382         }
1383       queue_empty(&q);
1384       solver_get_userinstalled(solv, &q, GET_USERINSTALLED_NAMES | GET_USERINSTALLED_INVERTED);
1385       for (i = 0; i < q.count; i++)
1386         {
1387           s = pool_tmpjoin(pool, "autoinst name ", pool_id2str(pool, q.elements[i]), 0);
1388           strqueue_push(&sq, s);
1389         }
1390       queue_free(&q);
1391     }
1392   if ((resultflags & TESTCASE_RESULT_ALTERNATIVES) != 0)
1393     {
1394       char *altprefix;
1395       Queue q, rq;
1396       int cnt;
1397       Id alternative;
1398       queue_init(&q);
1399       queue_init(&rq);
1400       cnt = solver_alternatives_count(solv);
1401       for (alternative = 1; alternative <= cnt; alternative++)
1402         {
1403           Id id, from, chosen;
1404           char num[20];
1405           int type = solver_get_alternative(solv, alternative, &id, &from, &chosen, &q, 0);
1406           altprefix = solv_dupjoin("alternative ", testcase_alternativeid(solv, type, id, from), " ");
1407           strcpy(num, " 0 ");
1408           if (type == SOLVER_ALTERNATIVE_TYPE_RECOMMENDS)
1409             {
1410               char *s = pool_tmpjoin(pool, altprefix, num, testcase_solvid2str(pool, from));
1411               s = pool_tmpappend(pool, s, " recommends ", testcase_dep2str(pool, id));
1412               strqueue_push(&sq, s);
1413             }
1414           else if (type == SOLVER_ALTERNATIVE_TYPE_RULE)
1415             {
1416               /* map choice rules back to pkg rules */
1417               if (solver_ruleclass(solv, id) == SOLVER_RULE_CHOICE)
1418                 id = solver_rule2pkgrule(solv, id);
1419               if (solver_ruleclass(solv, id) == SOLVER_RULE_RECOMMENDS)
1420                 id = solver_rule2pkgrule(solv, id);
1421               solver_allruleinfos(solv, id, &rq);
1422               for (i = 0; i < rq.count; i += 4)
1423                 {
1424                   int rtype = rq.elements[i];
1425                   if ((rtype & SOLVER_RULE_TYPEMASK) == SOLVER_RULE_JOB)
1426                     {
1427                       const char *js = testcase_job2str(pool, rq.elements[i + 2], rq.elements[i + 3]);
1428                       char *s = pool_tmpjoin(pool, altprefix, num, "job ");
1429                       s = pool_tmpappend(pool, s, js, 0);
1430                       strqueue_push(&sq, s);
1431                     }
1432                   else if (rtype == SOLVER_RULE_PKG_REQUIRES)
1433                     {
1434                       char *s = pool_tmpjoin(pool, altprefix, num, testcase_solvid2str(pool, rq.elements[i + 1]));
1435                       s = pool_tmpappend(pool, s, " requires ", testcase_dep2str(pool, rq.elements[i + 3]));
1436                       strqueue_push(&sq, s);
1437                     }
1438                   else if (rtype == SOLVER_RULE_UPDATE || rtype == SOLVER_RULE_FEATURE)
1439                     {
1440                       const char *js = testcase_solvid2str(pool, rq.elements[i + 1]);
1441                       char *s = pool_tmpjoin(pool, altprefix, num, "update ");
1442                       s = pool_tmpappend(pool, s, js, 0);
1443                       strqueue_push(&sq, s);
1444                     }
1445                 }
1446             }
1447           for (i = 0; i < q.count; i++)
1448             {
1449               Id p = q.elements[i];
1450               if (i >= 9)
1451                 num[0] = '0' + (i + 1) / 10;
1452               num[1] = '0' + (i + 1) % 10;
1453               if (-p == chosen)
1454                 s = pool_tmpjoin(pool, altprefix, num, "+ ");
1455               else if (p < 0)
1456                 s = pool_tmpjoin(pool, altprefix, num, "- ");
1457               else if (p >= 0)
1458                 s = pool_tmpjoin(pool, altprefix, num, "  ");
1459               s = pool_tmpappend(pool, s,  testcase_solvid2str(pool, p < 0 ? -p : p), 0);
1460               strqueue_push(&sq, s);
1461             }
1462           solv_free(altprefix);
1463         }
1464       queue_free(&q);
1465       queue_free(&rq);
1466     }
1467   if ((resultflags & TESTCASE_RESULT_RULES) != 0)
1468     {
1469       /* dump all rules */
1470       Id rid;
1471       SolverRuleinfo rclass;
1472       Queue q;
1473       int i;
1474
1475       queue_init(&q);
1476       for (rid = 1; (rclass = solver_ruleclass(solv, rid)) != SOLVER_RULE_UNKNOWN; rid++)
1477         {
1478           char *prefix = solv_dupjoin("rule ", testcase_rclass2str(rclass), " ");
1479           prefix = solv_dupappend(prefix, testcase_ruleid(solv, rid), 0);
1480           solver_ruleliterals(solv, rid, &q);
1481           if (rclass == SOLVER_RULE_FEATURE && q.count == 1 && q.elements[0] == -SYSTEMSOLVABLE)
1482             continue;
1483           for (i = 0; i < q.count; i++)
1484             {
1485               Id p = q.elements[i];
1486               const char *s;
1487               if (p < 0)
1488                 s = pool_tmpjoin(pool, prefix, " -", testcase_solvid2str(pool, -p));
1489               else
1490                 s = pool_tmpjoin(pool, prefix, "  ", testcase_solvid2str(pool, p));
1491               strqueue_push(&sq, s);
1492             }
1493           solv_free(prefix);
1494         }
1495       queue_free(&q);
1496     }
1497   if ((resultflags & TESTCASE_RESULT_GENID) != 0)
1498     {
1499       for (i = 0 ; i < solv->job.count; i += 2)
1500         {
1501           Id id, id2;
1502           if (solv->job.elements[i] != (SOLVER_NOOP | SOLVER_SOLVABLE_PROVIDES))
1503             continue;
1504           id = solv->job.elements[i + 1];
1505           s = testcase_dep2str(pool, id);
1506           strqueue_push(&sq, pool_tmpjoin(pool, "genid dep ", s, 0));
1507           if ((id2 = testcase_str2dep(pool, s)) != id)
1508             {
1509               s = pool_tmpjoin(pool, "genid roundtrip error: ", testcase_dep2str(pool, id2), 0);
1510               strqueue_push(&sq, s);
1511             }
1512           dump_genid(pool, &sq, id, 1);
1513         }
1514     }
1515   if ((resultflags & TESTCASE_RESULT_REASON) != 0)
1516     {
1517       Queue whyq;
1518       queue_init(&whyq);
1519       FOR_POOL_SOLVABLES(p)
1520         {
1521           Id info, p2, id;
1522           int reason = solver_describe_decision(solv, p, &info);
1523           if (reason == SOLVER_REASON_UNRELATED)
1524             continue;
1525           if (reason == SOLVER_REASON_WEAKDEP)
1526             {
1527               solver_describe_weakdep_decision(solv, p, &whyq);
1528               if (whyq.count)
1529                 {
1530                   for (i = 0; i < whyq.count; i += 3)
1531                     {
1532                       reason = whyq.elements[i];
1533                       p2 = whyq.elements[i + 1];
1534                       id = whyq.elements[i + 2];
1535                       s = pool_tmpjoin(pool, "reason ", testcase_solvid2str(pool, p), 0);
1536                       s = pool_tmpappend(pool, s, " ", testcase_reason2str(reason));
1537                       s = pool_tmpappend(pool, s, " ", testcase_dep2str(pool, id));
1538                       if (p2)
1539                         s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, p2));
1540                       strqueue_push(&sq, s);
1541                     }
1542                   continue;
1543                 }
1544             }
1545           s = pool_tmpjoin(pool, "reason ", testcase_solvid2str(pool, p), 0);
1546           s = pool_tmpappend(pool, s, " ", testcase_reason2str(reason));
1547           if (info)
1548             s = pool_tmpappend(pool, s, " ", testcase_ruleid(solv, info));
1549           strqueue_push(&sq, s);
1550         }
1551       queue_free(&whyq);
1552     }
1553   if ((resultflags & TESTCASE_RESULT_CLEANDEPS) != 0)
1554     {
1555       Queue q;
1556
1557       queue_init(&q);
1558       solver_get_cleandeps(solv, &q);
1559       for (i = 0; i < q.count; i++)
1560         {
1561           s = pool_tmpjoin(pool, "cleandeps ", testcase_solvid2str(pool, q.elements[i]), 0);
1562           strqueue_push(&sq, s);
1563         }
1564       queue_free(&q);
1565     }
1566   if ((resultflags & TESTCASE_RESULT_JOBS) != 0)
1567     {
1568       for (i = 0; i < solv->job.count; i += 2)
1569         {
1570           s = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
1571           s = pool_tmpjoin(pool, "job ", s, 0);
1572           strqueue_push(&sq, s);
1573         }
1574     }
1575   strqueue_sort(&sq);
1576   result = strqueue_join(&sq);
1577   strqueue_free(&sq);
1578   return result;
1579 }
1580
1581
1582 static int
1583 testcase_write_mangled(Solver *solv, const char *dir, int resultflags, const char *testcasename, const char *resultname)
1584 {
1585   Pool *pool = solv->pool;
1586   Repo *repo;
1587   int i;
1588   Id arch, repoid;
1589   Id lowscore;
1590   FILE *fp;
1591   Strqueue sq;
1592   char *cmd, *out, *result;
1593   const char *s;
1594
1595   if (!testcasename)
1596     testcasename = "testcase.t";
1597   if (!resultname)
1598     resultname = "solver.result";
1599
1600 #ifdef _WIN32
1601   if (mkdir(dir) && errno != EEXIST)
1602 #else
1603   if (mkdir(dir, 0777) && errno != EEXIST)
1604 #endif
1605     return pool_error(solv->pool, 0, "testcase_write: could not create directory '%s'", dir);
1606   strqueue_init(&sq);
1607   FOR_REPOS(repoid, repo)
1608     {
1609       const char *name = testcase_repoid2str(pool, repoid);
1610       char priobuf[50];
1611       if (repo->subpriority)
1612         sprintf(priobuf, "%d.%d", repo->priority, repo->subpriority);
1613       else
1614         sprintf(priobuf, "%d", repo->priority);
1615 #if !defined(WITHOUT_COOKIEOPEN) && defined(ENABLE_ZLIB_COMPRESSION)
1616       out = pool_tmpjoin(pool, name, ".repo", ".gz");
1617 #else
1618       out = pool_tmpjoin(pool, name, ".repo", 0);
1619 #endif
1620       for (i = 0; out[i]; i++)
1621         if (out[i] == '/')
1622           out[i] = '_';
1623       cmd = pool_tmpjoin(pool, "repo ", name, " ");
1624       cmd = pool_tmpappend(pool, cmd, priobuf, " ");
1625       cmd = pool_tmpappend(pool, cmd, "testtags ", out);
1626       strqueue_push(&sq, cmd);
1627       out = pool_tmpjoin(pool, dir, "/", out);
1628       if (!(fp = solv_xfopen(out, "w")))
1629         {
1630           pool_error(solv->pool, 0, "testcase_write: could not open '%s' for writing", out);
1631           strqueue_free(&sq);
1632           return 0;
1633         }
1634       testcase_write_testtags(repo, fp);
1635       if (fclose(fp))
1636         {
1637           pool_error(solv->pool, 0, "testcase_write: write error");
1638           strqueue_free(&sq);
1639           return 0;
1640         }
1641     }
1642   /* hmm, this is not optimal... we currently search for the lowest score */
1643   lowscore = 0;
1644   arch = pool->solvables[SYSTEMSOLVABLE].arch;
1645   for (i = 0; i < pool->lastarch; i++)
1646     {
1647       if (pool->id2arch[i] == 1 && !lowscore)
1648         arch = i;
1649       if (pool->id2arch[i] > 0x10000 && (!lowscore || pool->id2arch[i] < lowscore))
1650         {
1651           arch = i;
1652           lowscore = pool->id2arch[i];
1653         }
1654     }
1655   cmd = pool_tmpjoin(pool, "system ", pool->lastarch ? pool_id2str(pool, arch) : "-", 0);
1656   for (i = 0; disttype2str[i].str != 0; i++)
1657     if (pool->disttype == disttype2str[i].type)
1658       break;
1659   pool_tmpappend(pool, cmd, " ", disttype2str[i].str ? disttype2str[i].str : "unknown");
1660   if (pool->installed)
1661     cmd = pool_tmpappend(pool, cmd, " ", testcase_repoid2str(pool, pool->installed->repoid));
1662   strqueue_push(&sq, cmd);
1663   s = testcase_getpoolflags(solv->pool);
1664   if (*s)
1665     {
1666       cmd = pool_tmpjoin(pool, "poolflags ", s, 0);
1667       strqueue_push(&sq, cmd);
1668     }
1669
1670   if (pool->vendorclasses)
1671     {
1672       cmd = 0;
1673       for (i = 0; pool->vendorclasses[i]; i++)
1674         {
1675           cmd = pool_tmpappend(pool, cmd ? cmd : "vendorclass", " ", pool->vendorclasses[i]);
1676           if (!pool->vendorclasses[i + 1])
1677             {
1678               strqueue_push(&sq, cmd);
1679               cmd = 0;
1680               i++;
1681             }
1682         }
1683     }
1684
1685   /* dump disabled packages (must come before the namespace/job lines) */
1686   if (pool->considered)
1687     {
1688       Id p;
1689       FOR_POOL_SOLVABLES(p)
1690         if (!MAPTST(pool->considered, p))
1691           {
1692             cmd = pool_tmpjoin(pool, "disable pkg ", testcase_solvid2str(pool, p), 0);
1693             strqueue_push(&sq, cmd);
1694           }
1695     }
1696
1697   s = testcase_getsolverflags(solv);
1698   if (*s)
1699     {
1700       cmd = pool_tmpjoin(pool, "solverflags ", s, 0);
1701       strqueue_push(&sq, cmd);
1702     }
1703
1704   /* now dump all the ns callback values we know */
1705   if (pool->nscallback)
1706     {
1707       Id rid;
1708       int d;
1709       for (rid = 1; rid < pool->nrels; rid++)
1710         {
1711           Reldep *rd = pool->rels + rid;
1712           if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1713             continue;
1714           /* evaluate all namespace ids, skip empty results */
1715           d = pool_whatprovides(pool, MAKERELDEP(rid));
1716           if (!d || !pool->whatprovidesdata[d])
1717             continue;
1718           cmd = pool_tmpjoin(pool, "namespace ", pool_id2str(pool, rd->name), "(");
1719           cmd = pool_tmpappend(pool, cmd, pool_id2str(pool, rd->evr), ")");
1720           for (;  pool->whatprovidesdata[d]; d++)
1721             cmd = pool_tmpappend(pool, cmd, " ", testcase_solvid2str(pool, pool->whatprovidesdata[d]));
1722           strqueue_push(&sq, cmd);
1723         }
1724     }
1725
1726   for (i = 0; i < solv->job.count; i += 2)
1727     {
1728       cmd = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
1729       cmd = pool_tmpjoin(pool, "job ", cmd, 0);
1730       strqueue_push(&sq, cmd);
1731     }
1732
1733   if ((resultflags & ~TESTCASE_RESULT_REUSE_SOLVER) != 0)
1734     {
1735       cmd = 0;
1736       for (i = 0; resultflags2str[i].str; i++)
1737         if ((resultflags & resultflags2str[i].flag) != 0)
1738           cmd = pool_tmpappend(pool, cmd, cmd ? "," : 0, resultflags2str[i].str);
1739       cmd = pool_tmpjoin(pool, "result ", cmd ? cmd : "?", 0);
1740       cmd = pool_tmpappend(pool, cmd, " ", resultname);
1741       strqueue_push(&sq, cmd);
1742       result = testcase_solverresult(solv, resultflags);
1743       if (!strcmp(resultname, "<inline>"))
1744         {
1745           Strqueue rsq;
1746           strqueue_init(&rsq);
1747           strqueue_split(&rsq, result);
1748           for (i = 0; i < rsq.nstr; i++)
1749             {
1750               cmd = pool_tmpjoin(pool, "#>", rsq.str[i], 0);
1751               strqueue_push(&sq, cmd);
1752             }
1753           strqueue_free(&rsq);
1754         }
1755       else
1756         {
1757           out = pool_tmpjoin(pool, dir, "/", resultname);
1758           if (!(fp = fopen(out, "w")))
1759             {
1760               pool_error(solv->pool, 0, "testcase_write: could not open '%s' for writing", out);
1761               solv_free(result);
1762               strqueue_free(&sq);
1763               return 0;
1764             }
1765           if (result && *result && fwrite(result, strlen(result), 1, fp) != 1)
1766             {
1767               pool_error(solv->pool, 0, "testcase_write: write error");
1768               solv_free(result);
1769               strqueue_free(&sq);
1770               fclose(fp);
1771               return 0;
1772             }
1773           if (fclose(fp))
1774             {
1775               pool_error(solv->pool, 0, "testcase_write: write error");
1776               solv_free(result);
1777               strqueue_free(&sq);
1778               return 0;
1779             }
1780         }
1781       solv_free(result);
1782     }
1783
1784   result = strqueue_join(&sq);
1785   strqueue_free(&sq);
1786   out = pool_tmpjoin(pool, dir, "/", testcasename);
1787   if (!(fp = fopen(out, "w")))
1788     {
1789       pool_error(solv->pool, 0, "testcase_write: could not open '%s' for writing", out);
1790       solv_free(result);
1791       return 0;
1792     }
1793   if (*result && fwrite(result, strlen(result), 1, fp) != 1)
1794     {
1795       pool_error(solv->pool, 0, "testcase_write: write error");
1796       solv_free(result);
1797       fclose(fp);
1798       return 0;
1799     }
1800   if (fclose(fp))
1801     {
1802       pool_error(solv->pool, 0, "testcase_write: write error");
1803       solv_free(result);
1804       return 0;
1805     }
1806   solv_free(result);
1807   return 1;
1808 }
1809
1810 int
1811 testcase_write(Solver *solv, const char *dir, int resultflags, const char *testcasename, const char *resultname)
1812 {
1813   Pool *pool = solv->pool;
1814   int i, r, repoid;
1815   int mangle = 1;
1816   const char **orignames;
1817
1818   /* mangle repo names so that there are no conflicts */
1819   orignames = solv_calloc(pool->nrepos, sizeof(char *));
1820   for (repoid = 1; repoid < pool->nrepos; repoid++)
1821     {
1822       Repo *repo = pool_id2repo(pool, repoid);
1823       char *buf = solv_malloc((repo->name ? strlen(repo->name) : 0) + 40);
1824       char *mp;
1825       orignames[repoid] = repo->name;
1826       if (!repo->name || !repo->name[0])
1827         sprintf(buf, "#%d", repoid);
1828       else
1829         strcpy(buf, repo->name);
1830       for (i = 0; buf[i]; i++)
1831         if (buf[i] == ' ' || buf[i] == '\t' || buf[i] == '/')
1832           buf[i] = '_';
1833       mp = buf + strlen(buf);
1834       for (;;)
1835         {
1836           for (i = 1; i < repoid; i++)
1837             if (!strcmp(buf, pool_id2repo(pool, i)->name))
1838               break;
1839           if (i == repoid)
1840             break;
1841           sprintf(mp, "_%d", mangle++);
1842         }
1843       repo->name = buf;
1844     }
1845   r = testcase_write_mangled(solv, dir, resultflags, testcasename, resultname);
1846   for (repoid = 1; repoid < pool->nrepos; repoid++)
1847     {
1848       Repo *repo = pool_id2repo(pool, repoid);
1849       solv_free((void *)repo->name);
1850       repo->name = orignames[repoid];
1851     }
1852   solv_free(orignames);
1853   return r;
1854 }
1855
1856 static char *
1857 read_inline_file(FILE *fp, char **bufp, char **bufpp, int *buflp)
1858 {
1859   char *result = solv_malloc(1024);
1860   char *rp = result;
1861   int resultl = 1024;
1862
1863   for (;;)
1864     {
1865       size_t rl;
1866       if (rp - result + 256 >= resultl)
1867         {
1868           resultl = rp - result;
1869           result = solv_realloc(result, resultl + 1024);
1870           rp = result + resultl;
1871           resultl += 1024;
1872         }
1873       if (!fgets(rp, resultl - (rp - result), fp))
1874         *rp = 0;
1875       rl = strlen(rp);
1876       if (rl && (rp == result || rp[-1] == '\n'))
1877         {
1878           if (rl > 1 && rp[0] == '#' && rp[1] == '>')
1879             {
1880               memmove(rp, rp + 2, rl - 2);
1881               rl -= 2;
1882             }
1883           else
1884             {
1885               while (rl + 16 > *buflp)
1886                 {
1887                   *bufp = solv_realloc(*bufp, *buflp + 512);
1888                   *buflp += 512;
1889                 }
1890               memmove(*bufp, rp, rl);
1891               if ((*bufp)[rl - 1] == '\n')
1892                 {
1893                   ungetc('\n', fp);
1894                   rl--;
1895                 }
1896               (*bufp)[rl] = 0;
1897               (*bufpp) = *bufp + rl;
1898               rl = 0;
1899             }
1900         }
1901       if (rl <= 0)
1902         {
1903           *rp = 0;
1904           break;
1905         }
1906       rp += rl;
1907     }
1908   return result;
1909 }
1910
1911 static char *
1912 read_file(FILE *fp)
1913 {
1914   char *result = solv_malloc(1024);
1915   char *rp = result;
1916   int resultl = 1024;
1917
1918   for (;;)
1919     {
1920       size_t rl;
1921       if (rp - result + 256 >= resultl)
1922         {
1923           resultl = rp - result;
1924           result = solv_realloc(result, resultl + 1024);
1925           rp = result + resultl;
1926           resultl += 1024;
1927         }
1928       rl = fread(rp, 1, resultl - (rp - result), fp);
1929       if (rl <= 0)
1930         {
1931           *rp = 0;
1932           break;
1933         }
1934       rp += rl;
1935     }
1936   return result;
1937 }
1938
1939 static int
1940 str2resultflags(Pool *pool, char *s)    /* modifies the string! */
1941 {
1942   int i, resultflags = 0;
1943   while (s)
1944     {
1945       char *se = strchr(s, ',');
1946       if (se)
1947         *se++ = 0;
1948       for (i = 0; resultflags2str[i].str; i++)
1949         if (!strcmp(s, resultflags2str[i].str))
1950           {
1951             resultflags |= resultflags2str[i].flag;
1952             break;
1953           }
1954       if (!resultflags2str[i].str)
1955         pool_error(pool, 0, "result: unknown flag '%s'", s);
1956       s = se;
1957     }
1958   return resultflags;
1959 }
1960
1961 Solver *
1962 testcase_read(Pool *pool, FILE *fp, const char *testcase, Queue *job, char **resultp, int *resultflagsp)
1963 {
1964   Solver *solv;
1965   char *buf, *bufp;
1966   int bufl;
1967   char *testcasedir, *s;
1968   int l;
1969   char **pieces = 0;
1970   int npieces = 0;
1971   int prepared = 0;
1972   int closefp = !fp;
1973   int poolflagsreset = 0;
1974   int missing_features = 0;
1975   Id *genid = 0;
1976   int ngenid = 0;
1977   Queue autoinstq;
1978
1979   if (resultp)
1980     *resultp = 0;
1981   if (resultflagsp)
1982     *resultflagsp = 0;
1983   if (!fp && !(fp = fopen(testcase, "r")))
1984     {
1985       pool_error(pool, 0, "testcase_read: could not open '%s'", testcase);
1986       return 0;
1987     }
1988   testcasedir = solv_strdup(testcase);
1989   s = strrchr(testcasedir, '/');
1990 #ifdef _WIN32
1991   buf = strrchr(testcasedir, '\\');
1992   if (!s || (buf && buf > s))
1993     s = buf;
1994 #endif
1995   if (s)
1996     s[1] = 0;
1997   else
1998     *testcasedir = 0;
1999   bufl = 1024;
2000   buf = solv_malloc(bufl);
2001   bufp = buf;
2002   solv = 0;
2003   queue_init(&autoinstq);
2004   for (;;)
2005     {
2006       if (bufp - buf + 16 > bufl)
2007         {
2008           bufl = bufp - buf;
2009           buf = solv_realloc(buf, bufl + 512);
2010           bufp = buf + bufl;
2011           bufl += 512;
2012         }
2013       if (!fgets(bufp, bufl - (bufp - buf), fp))
2014         break;
2015       bufp = buf;
2016       l = strlen(buf);
2017       if (!l || buf[l - 1] != '\n')
2018         {
2019           bufp += l;
2020           continue;
2021         }
2022       buf[--l] = 0;
2023       s = buf;
2024       while (*s && (*s == ' ' || *s == '\t'))
2025         s++;
2026       if (!*s || *s == '#')
2027         continue;
2028       npieces = 0;
2029       /* split it in pieces */
2030       for (;;)
2031         {
2032           while (*s == ' ' || *s == '\t')
2033             s++;
2034           if (!*s)
2035             break;
2036           pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
2037           pieces[npieces++] = s;
2038           while (*s && *s != ' ' && *s != '\t')
2039             s++;
2040           if (*s)
2041             *s++ = 0;
2042         }
2043       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
2044       pieces[npieces] = 0;
2045       if (!strcmp(pieces[0], "repo") && npieces >= 4)
2046         {
2047           Repo *repo = repo_create(pool, pieces[1]);
2048           FILE *rfp;
2049           int prio, subprio;
2050           const char *rdata;
2051
2052           prepared = 0;
2053           if (!poolflagsreset)
2054             {
2055               poolflagsreset = 1;
2056               testcase_resetpoolflags(pool);    /* hmm */
2057             }
2058           if (sscanf(pieces[2], "%d.%d", &prio, &subprio) != 2)
2059             {
2060               subprio = 0;
2061               prio = atoi(pieces[2]);
2062             }
2063           repo->priority = prio;
2064           repo->subpriority = subprio;
2065           if (strcmp(pieces[3], "empty") != 0)
2066             {
2067               const char *repotype = pool_tmpjoin(pool, pieces[3], 0, 0);       /* gets overwritten in <inline> case */
2068               if (!strcmp(pieces[4], "<inline>"))
2069                 {
2070                   char *idata = read_inline_file(fp, &buf, &bufp, &bufl);
2071                   rdata = "<inline>";
2072                   rfp = solv_xfopen_buf(rdata, &idata, 0, "rf");
2073                 }
2074               else
2075                 {
2076                   rdata = pool_tmpjoin(pool, testcasedir, pieces[4], 0);
2077                   rfp = solv_xfopen(rdata, "r");
2078                 }
2079               if (!rfp)
2080                 {
2081                   pool_error(pool, 0, "testcase_read: could not open '%s'", rdata);
2082                 }
2083               else if (!strcmp(repotype, "testtags"))
2084                 {
2085                   testcase_add_testtags(repo, rfp, 0);
2086                   fclose(rfp);
2087                 }
2088               else if (!strcmp(repotype, "solv"))
2089                 {
2090                   repo_add_solv(repo, rfp, 0);
2091                   fclose(rfp);
2092                 }
2093 #if ENABLE_TESTCASE_HELIXREPO
2094               else if (!strcmp(repotype, "helix"))
2095                 {
2096                   repo_add_helix(repo, rfp, 0);
2097                   fclose(rfp);
2098                 }
2099 #endif
2100               else
2101                 {
2102                   fclose(rfp);
2103                   pool_error(pool, 0, "testcase_read: unknown repo type for repo '%s'", repo->name);
2104                 }
2105             }
2106         }
2107       else if (!strcmp(pieces[0], "system") && npieces >= 3)
2108         {
2109           int i;
2110
2111           /* must set the disttype before the arch */
2112           prepared = 0;
2113           if (strcmp(pieces[2], "*") != 0)
2114             {
2115               char *dp = pieces[2];
2116               while (dp && *dp)
2117                 {
2118                   char *dpe = strchr(dp, ',');
2119                   if (dpe)
2120                     *dpe = 0;
2121                   for (i = 0; disttype2str[i].str != 0; i++)
2122                     if (!strcmp(disttype2str[i].str, dp))
2123                       break;
2124                   if (dpe)
2125                     *dpe++ = ',';
2126                   if (disttype2str[i].str)
2127                     {
2128 #ifdef MULTI_SEMANTICS
2129                       if (pool->disttype != disttype2str[i].type)
2130                         pool_setdisttype(pool, disttype2str[i].type);
2131 #endif
2132                       if (pool->disttype == disttype2str[i].type)
2133                         break;
2134                     }
2135                   dp = dpe;
2136                 }
2137               if (!(dp && *dp))
2138                 {
2139                   pool_error(pool, 0, "testcase_read: system: could not change disttype to '%s'", pieces[2]);
2140                   missing_features = 1;
2141                 }
2142             }
2143           if (strcmp(pieces[1], "unset") == 0 || strcmp(pieces[1], "-") == 0)
2144             pool_setarch(pool, 0);
2145           else if (pieces[1][0] == ':')
2146             pool_setarchpolicy(pool, pieces[1] + 1);
2147           else
2148             pool_setarch(pool, pieces[1]);
2149           if (npieces > 3)
2150             {
2151               Repo *repo = testcase_str2repo(pool, pieces[3]);
2152               if (!repo)
2153                 pool_error(pool, 0, "testcase_read: system: unknown repo '%s'", pieces[3]);
2154               else
2155                 pool_set_installed(pool, repo);
2156             }
2157         }
2158       else if (!strcmp(pieces[0], "job") && npieces > 1)
2159         {
2160           char *sp;
2161           Id how, what;
2162           if (prepared <= 0)
2163             {
2164               pool_addfileprovides(pool);
2165               pool_createwhatprovides(pool);
2166               prepared = 1;
2167             }
2168           if (npieces >= 3 && !strcmp(pieces[2], "selection"))
2169             {
2170               addselectionjob(pool, pieces + 1, npieces - 1, job, 0, 0);
2171               continue;
2172             }
2173           if (npieces >= 4 && !strcmp(pieces[2], "selection_matchdeps"))
2174             {
2175               pieces[2] = pieces[1];
2176               addselectionjob(pool, pieces + 2, npieces - 2, job, SELECTIONJOB_MATCHDEPS, pool_str2id(pool, pieces[3], 1));
2177               continue;
2178             }
2179           if (npieces >= 4 && !strcmp(pieces[2], "selection_matchdepid"))
2180             {
2181               pieces[2] = pieces[1];
2182               addselectionjob(pool, pieces + 2, npieces - 2, job, SELECTIONJOB_MATCHDEPID, pool_str2id(pool, pieces[3], 1));
2183               continue;
2184             }
2185           if (npieces >= 4 && !strcmp(pieces[2], "selection_matchsolvable"))
2186             {
2187               pieces[2] = pieces[1];
2188               addselectionjob(pool, pieces + 2, npieces - 2, job, SELECTIONJOB_MATCHSOLVABLE, pool_str2id(pool, pieces[3], 1));
2189               continue;
2190             }
2191           /* rejoin */
2192           for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
2193             if (*sp == 0)
2194               *sp = ' ';
2195           how = testcase_str2job(pool, pieces[1], &what);
2196           if (how >= 0 && job)
2197             queue_push2(job, how, what);
2198         }
2199       else if (!strcmp(pieces[0], "vendorclass") && npieces > 1)
2200         {
2201           pool_addvendorclass(pool, (const char **)(pieces + 1));
2202         }
2203       else if (!strcmp(pieces[0], "namespace") && npieces > 1)
2204         {
2205           int i = strlen(pieces[1]);
2206           s = strchr(pieces[1], '(');
2207           if (!s || pieces[1][i - 1] != ')')
2208             {
2209               pool_error(pool, 0, "testcase_read: bad namespace '%s'", pieces[1]);
2210             }
2211           else
2212             {
2213               Id name, evr, id;
2214               Queue q;
2215               queue_init(&q);
2216               *s = 0;
2217               pieces[1][i - 1] = 0;
2218               name = pool_str2id(pool, pieces[1], 1);
2219               evr = pool_str2id(pool, s + 1, 1);
2220               *s = '(';
2221               pieces[1][i - 1] = ')';
2222               id = pool_rel2id(pool, name, evr, REL_NAMESPACE, 1);
2223               for (i = 2; i < npieces; i++)
2224                 queue_push(&q, testcase_str2solvid(pool, pieces[i]));
2225               /* now do the callback */
2226               if (prepared <= 0)
2227                 {
2228                   pool_addfileprovides(pool);
2229                   pool_createwhatprovides(pool);
2230                   prepared = 1;
2231                 }
2232               pool->whatprovides_rel[GETRELID(id)] = pool_queuetowhatprovides(pool, &q);
2233               queue_free(&q);
2234             }
2235         }
2236       else if (!strcmp(pieces[0], "poolflags"))
2237         {
2238           int i;
2239           if (!poolflagsreset)
2240             {
2241               poolflagsreset = 1;
2242               testcase_resetpoolflags(pool);    /* hmm */
2243             }
2244           for (i = 1; i < npieces; i++)
2245             testcase_setpoolflags(pool, pieces[i]);
2246         }
2247       else if (!strcmp(pieces[0], "solverflags") && npieces > 1)
2248         {
2249           int i;
2250           if (!solv)
2251             {
2252               solv = solver_create(pool);
2253               testcase_resetsolverflags(solv);
2254             }
2255           for (i = 1; i < npieces; i++)
2256             testcase_setsolverflags(solv, pieces[i]);
2257         }
2258       else if (!strcmp(pieces[0], "result") && npieces > 1)
2259         {
2260           char *result = 0;
2261           int resultflags = str2resultflags(pool, pieces[1]);
2262           const char *rdata;
2263           if (npieces > 2)
2264             {
2265               rdata = pool_tmpjoin(pool, testcasedir, pieces[2], 0);
2266               if (!strcmp(pieces[2], "<inline>"))
2267                 result = read_inline_file(fp, &buf, &bufp, &bufl);
2268               else
2269                 {
2270                   FILE *rfp = fopen(rdata, "r");
2271                   if (!rfp)
2272                     pool_error(pool, 0, "testcase_read: could not open '%s'", rdata);
2273                   else
2274                     {
2275                       result = read_file(rfp);
2276                       fclose(rfp);
2277                     }
2278                 }
2279             }
2280           if (resultp)
2281             *resultp = result;
2282           else
2283             solv_free(result);
2284           if (resultflagsp)
2285             *resultflagsp = resultflags;
2286         }
2287       else if (!strcmp(pieces[0], "nextjob"))
2288         {
2289           if (npieces == 2 && resultflagsp && !strcmp(pieces[1], "reusesolver"))
2290             *resultflagsp |= TESTCASE_RESULT_REUSE_SOLVER;
2291           break;
2292         }
2293       else if (!strcmp(pieces[0], "disable") && npieces == 3)
2294         {
2295           Id p, pp, jobsel, what = 0;
2296           if (!prepared)
2297             pool_createwhatprovides(pool);
2298           prepared = -1;
2299           if (!pool->considered)
2300             {
2301               pool->considered = solv_calloc(1, sizeof(Map));
2302               map_init(pool->considered, pool->nsolvables);
2303               map_setall(pool->considered);
2304             }
2305           jobsel = testcase_str2jobsel(pool, "disable", pieces + 1, npieces - 1, &what);
2306           if (jobsel < 0)
2307             continue;
2308           if (jobsel == SOLVER_SOLVABLE_ALL)
2309             map_empty(pool->considered);
2310           else if (jobsel == SOLVER_SOLVABLE_REPO)
2311             {
2312               Repo *repo = pool_id2repo(pool, what);
2313               Solvable *s;
2314               FOR_REPO_SOLVABLES(repo, p, s)
2315                 MAPCLR(pool->considered, p);
2316             }
2317           FOR_JOB_SELECT(p, pp, jobsel, what)
2318             MAPCLR(pool->considered, p);
2319         }
2320       else if (!strcmp(pieces[0], "feature"))
2321         {
2322           int i, j;
2323           for (i = 1; i < npieces; i++)
2324             {
2325               for (j = 0; features[j]; j++)
2326                 if (!strcmp(pieces[i], features[j]))
2327                   break;
2328               if (!features[j])
2329                 {
2330                   pool_error(pool, 0, "testcase_read: missing feature '%s'", pieces[i]);
2331                   missing_features++;
2332                 }
2333             }
2334           if (missing_features)
2335             break;
2336         }
2337       else if (!strcmp(pieces[0], "genid") && npieces > 1)
2338         {
2339           Id id;
2340           /* rejoin */
2341           if (npieces > 2)
2342             {
2343               char *sp;
2344               for (sp = pieces[2]; sp < pieces[npieces - 1]; sp++)
2345                 if (*sp == 0)
2346                   *sp = ' ';
2347             }
2348           genid = solv_extend(genid, ngenid, 1, sizeof(*genid), 7);
2349           if (!strcmp(pieces[1], "op") && npieces > 2)
2350             {
2351               struct oplist *op;
2352               for (op = oplist; op->flags; op++)
2353                 if (!strncmp(pieces[2], op->opname, strlen(op->opname)))
2354                   break;
2355               if (!op->flags)
2356                 {
2357                   pool_error(pool, 0, "testcase_read: genid: unknown op '%s'", pieces[2]);
2358                   break;
2359                 }
2360               if (ngenid < 2)
2361                 {
2362                   pool_error(pool, 0, "testcase_read: genid: out of stack");
2363                   break;
2364                 }
2365               ngenid -= 2;
2366               id = pool_rel2id(pool, genid[ngenid] , genid[ngenid + 1], op->flags, 1);
2367             }
2368           else if (!strcmp(pieces[1], "lit"))
2369             id = pool_str2id(pool, npieces > 2 ? pieces[2] : "", 1);
2370           else if (!strcmp(pieces[1], "null"))
2371             id = 0;
2372           else if (!strcmp(pieces[1], "dep"))
2373             id = testcase_str2dep(pool, pieces[2]);
2374           else
2375             {
2376               pool_error(pool, 0, "testcase_read: genid: unknown command '%s'", pieces[1]);
2377               break;
2378             }
2379           genid[ngenid++] = id;
2380         }
2381       else if (!strcmp(pieces[0], "autoinst") && npieces > 2)
2382         {
2383           if (strcmp(pieces[1], "name"))
2384             {
2385               pool_error(pool, 0, "testcase_read: autoinst: illegal mode");
2386               break;
2387             }
2388           queue_push(&autoinstq, pool_str2id(pool, pieces[2], 1));
2389         }
2390       else if (!strcmp(pieces[0], "evrcmp") && npieces == 3)
2391         {
2392           Id evr1 = pool_str2id(pool, pieces[1], 1);
2393           Id evr2 = pool_str2id(pool, pieces[2], 1);
2394           int r = pool_evrcmp(pool, evr1, evr2, EVRCMP_COMPARE);
2395           r = r < 0 ? REL_LT : r > 0 ? REL_GT : REL_EQ;
2396           queue_push2(job, SOLVER_NOOP | SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, evr1, evr2, r, 1));
2397         }
2398       else
2399         {
2400           pool_error(pool, 0, "testcase_read: cannot parse command '%s'", pieces[0]);
2401         }
2402     }
2403   while (job && ngenid > 0)
2404     queue_push2(job, SOLVER_NOOP | SOLVER_SOLVABLE_PROVIDES, genid[--ngenid]);
2405   if (autoinstq.count)
2406     pool_add_userinstalled_jobs(pool, &autoinstq, job, GET_USERINSTALLED_NAMES | GET_USERINSTALLED_INVERTED);
2407   queue_free(&autoinstq);
2408   genid = solv_free(genid);
2409   buf = solv_free(buf);
2410   pieces = solv_free(pieces);
2411   solv_free(testcasedir);
2412   if (!prepared)
2413     {
2414       pool_addfileprovides(pool);
2415       pool_createwhatprovides(pool);
2416     }
2417   if (!solv)
2418     {
2419       solv = solver_create(pool);
2420       testcase_resetsolverflags(solv);
2421     }
2422   if (closefp)
2423     fclose(fp);
2424   if (missing_features)
2425     {
2426       solver_free(solv);
2427       solv = 0;
2428       if (resultflagsp)
2429         *resultflagsp = 77;     /* hack for testsolv */
2430     }
2431   return solv;
2432 }
2433
2434 char *
2435 testcase_resultdiff(const char *result1, const char *result2)
2436 {
2437   Strqueue sq1, sq2, osq;
2438   char *r;
2439   strqueue_init(&sq1);
2440   strqueue_init(&sq2);
2441   strqueue_init(&osq);
2442   strqueue_split(&sq1, result1);
2443   strqueue_split(&sq2, result2);
2444   strqueue_sort(&sq1);
2445   strqueue_sort(&sq2);
2446   strqueue_diff(&sq1, &sq2, &osq);
2447   r = osq.nstr ? strqueue_join(&osq) : 0;
2448   strqueue_free(&sq1);
2449   strqueue_free(&sq2);
2450   strqueue_free(&osq);
2451   return r;
2452 }
2453