opkg: implment active_list_clear()
[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 "\trping - Reposiroties ping, check the accessibility of repositories\n"
152 "\ttest - Run test script\n"
153 , basename (argv[0]));
154 exit (0);
155 }
156
157 opkg = opkg_new ();
158
159 opkg_set_option (opkg, "offline_root", "/tmp/");
160
161 opkg_re_read_config_files (opkg);
162
163 switch (argv[1][0])
164 {
165 case 'f':
166 pkg = opkg_find_package (opkg, argv[2], NULL, NULL, NULL);
167 if (pkg)
168 {
169 print_package (pkg);
170 opkg_package_free (pkg);
171 }
172 else
173 printf ("Package \"%s\" not found!\n", find_pkg->name);
174 opkg_package_free (pkg);
175 break;
176 case 'i':
177 err = opkg_install_package (opkg, argv[1], progress_callback, "Installing...");
178 printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
179 break;
180
181 case 'u':
182 if (strlen (argv[1]) < 4)
183 printf ("");
184 if (argv[1][3] == 'd')
185 {
186 err = opkg_update_package_lists (opkg, progress_callback, "Updating...");
187 printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
188 break;
189 }
190 else
191 {
192 if (argc < 3)
193 {
194 err = opkg_upgrade_all (opkg, progress_callback, "Upgrading all...");
195 printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
196 }
197 else
198 {
199 err = opkg_upgrade_package (opkg, argv[2], progress_callback, "Upgrading...");
200 printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
201 }
202 }
203 break;
204
205 case 'l':
206 if (argc < 3)
207 {
208 printf ("Please specify one either all, installed or upgrades\n");
209 }
210 else
211 {
212 switch (argv[2][0])
213 {
214 case 'u':
215 printf ("Listing upgradable packages...\n");
216 opkg_list_upgradable_packages (opkg, package_list_upgradable_callback, NULL);
217 break;
218 case 'a':
219 printf ("Listing all packages...\n");
220 opkg_list_packages (opkg, package_list_callback, NULL);
221 printf ("\n");
222 break;
223 case 'i':
224 printf ("Listing installed packages...\n");
225 break;
226 default:
227 printf ("Unknown list option \"%s\"", argv[2]);
228 }
229 }
230 break;
231
232 case 'r':
233 if (argv[1][1] == 'e')
234 {
235 err = opkg_remove_package (opkg, argv[2], progress_callback, "Removing...");
236 printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
237 break;
238 }else if (argv[1][1] == 'p')
239 {
240 err = opkg_repository_accessibility_check(opkg);
241 printf("\nopkg_repository_accessibility_check returned (%d)\n", err);
242 break;
243 }
244
245 default:
246 printf ("Unknown command \"%s\"\n", argv[1]);
247 }
248
249
250 opkg_free (opkg);
251
252 return 0;
253 }