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