And make the install command work. WTF?
[oweals/opkg-lede.git] / tests / libopkg_test.c
1 #include <opkg.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <libgen.h>
6
7 pkg_t *find_pkg = NULL;
8
9 char *errors[10] = {
10   "No Error",
11   "Unknown Eror",
12   "Download failed",
13   "Dependancies failed",
14   "Package already installed",
15   "Package not available",
16   "Package not found",
17   "Package not installed",
18   "Signature check failed",
19   "MD5 sum failed"
20 };
21
22
23 #define TEST_PACKAGE "aspell"
24
25 void
26 progress_callback (const opkg_progress_data_t *progress, void *data)
27 {
28   printf ("\r%s %3d%%", (char*) data, progress->percentage);
29   fflush (stdout);
30 }
31
32 void
33 package_list_callback (pkg_t *pkg, void *data)
34 {
35   static int install_count = 0;
36   static int total_count = 0;
37
38   if (pkg->state_status == SS_INSTALLED)
39     install_count++;
40
41   total_count++;
42
43   printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
44   fflush (stdout);
45
46   if (!find_pkg)
47   {
48     /* store the first package to print out later */
49     find_pkg = pkg;
50   }
51 }
52
53 void
54 package_list_upgradable_callback (pkg_t *pkg, void *data)
55 {
56   printf ("%s - %s\n", pkg->name, pkg->version);
57 }
58
59 void
60 print_package (pkg_t *pkg)
61 {
62   printf (
63       "Name:         %s\n"
64       "Version:      %s\n"
65       "Repository:   %s\n"
66       "Architecture: %s\n"
67       "Description:  %s\n"
68       "Tags:         %s\n"
69       "Size:         %ld\n"
70       "Status:       %d\n",
71       pkg->name,
72       pkg->version,
73       pkg->src->name,
74       pkg->architecture,
75       pkg->description,
76       pkg->tags,
77       pkg->size,
78       pkg->state_status
79       );
80 }
81
82
83 void
84 opkg_test (void)
85 {
86   int err;
87   pkg_t *pkg;
88
89   err = opkg_update_package_lists (progress_callback, "Updating...");
90   printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
91
92   opkg_list_packages (package_list_callback, NULL);
93   printf ("\n");
94
95   if (find_pkg)
96   {
97     printf ("Finding package \"%s\"\n", find_pkg->name);
98     pkg = opkg_find_package (find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->src->name);
99     if (pkg)
100     {
101       print_package (pkg);
102     }
103     else
104       printf ("Package \"%s\" not found!\n", find_pkg->name);
105   }
106   else
107     printf ("No package available to test find_package.\n");
108
109   err = opkg_install_package (TEST_PACKAGE, progress_callback, "Installing...");
110   printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
111
112   err = opkg_upgrade_package (TEST_PACKAGE, progress_callback, "Upgrading...");
113   printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
114
115   err = opkg_remove_package (TEST_PACKAGE, progress_callback, "Removing...");
116   printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
117
118   printf ("Listing upgradable packages...\n");
119   opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
120
121   err = opkg_upgrade_all (progress_callback, "Upgrading all...");
122   printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
123
124 }
125
126 int
127 main (int argc, char **argv)
128 {
129   pkg_t *pkg;
130   int err;
131
132   if (argc < 2)
133   {
134     printf ("Usage: %s command\n"
135             "\nCommands:\n"
136             "\tupdate - Update package lists\n"
137             "\tfind [package] - Print details of the specified package\n"
138             "\tinstall [package] - Install the specified package\n"
139             "\tupgrade [package] - Upgrade the specified package\n"
140             "\tlist upgrades - List the available upgrades\n"
141             "\tlist all - List all available packages\n"
142             "\tlist installed - List all the installed packages\n"
143             "\tremove [package] - Remove the specified package\n"
144             "\trping - Reposiroties ping, check the accessibility of repositories\n"
145             "\ttest - Run test script\n"
146     , basename (argv[0]));
147     exit (0);
148   }
149
150   setenv("OFFLINE_ROOT", "/tmp", 0);
151  
152   if (opkg_new ()) {
153           printf("opkg_new() failed. This sucks.\n");
154           print_error_list();
155           return 1;
156   }
157
158   switch (argv[1][0])
159   {
160     case 'f':
161       pkg = opkg_find_package (argv[2], NULL, NULL, NULL);
162       if (pkg)
163       {
164         print_package (pkg);
165       }
166       else
167         printf ("Package \"%s\" not found!\n", find_pkg->name);
168       break;
169     case 'i':
170       err = opkg_install_package (argv[2], progress_callback, "Installing...");
171       printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
172       break;
173
174     case 'u':
175       if (argv[1][2] == 'd')
176       {
177         err = opkg_update_package_lists (progress_callback, "Updating...");
178         printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
179         break;
180       }
181       else
182       {
183         if (argc < 3)
184         {
185           err = opkg_upgrade_all (progress_callback, "Upgrading all...");
186           printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
187         }
188         else
189         {
190           err = opkg_upgrade_package (argv[2], progress_callback, "Upgrading...");
191           printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
192         }
193       }
194       break;
195
196     case 'l':
197       if (argc < 3)
198       {
199         printf ("Please specify one either all, installed or upgrades\n");
200       }
201       else
202       {
203         switch (argv[2][0])
204         {
205           case 'u':
206             printf ("Listing upgradable packages...\n");
207             opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
208             break;
209           case 'a':
210             printf ("Listing all packages...\n");
211             opkg_list_packages (package_list_callback, NULL);
212             printf ("\n");
213             break;
214           case 'i':
215             printf ("Listing installed packages...\n");
216             break;
217           default:
218             printf ("Unknown list option \"%s\"", argv[2]);
219         }
220       }
221       break;
222           
223     case 'r':
224       if (argv[1][1] == 'e')
225       {
226         err = opkg_remove_package (argv[2], progress_callback, "Removing...");
227         printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
228         break;
229       }else if (argv[1][1] == 'p')
230       {
231         err = opkg_repository_accessibility_check();
232         printf("\nopkg_repository_accessibility_check returned (%d)\n", err);
233         break;
234       }
235
236     default:
237       printf ("Unknown command \"%s\"\n", argv[1]);
238   }
239
240
241   opkg_free ();
242
243   return 0;
244 }