Merge commit 'grg' into HEAD
[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 (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_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_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 "Size: %d\n"
72 "Installed: %s\n",
73 pkg->name,
74 pkg->version,
75 pkg->repository,
76 pkg->architecture,
77 pkg->description,
78 pkg->tags,
79 pkg->size,
80 (pkg->installed ? "True" : "False")
81 );
82 }
83
84
85 void
86 opkg_test (void)
87 {
88 int err;
89 opkg_package_t *pkg;
90
91 err = opkg_update_package_lists (progress_callback, "Updating...");
92 printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
93
94 opkg_list_packages (package_list_callback, NULL);
95 printf ("\n");
96
97 if (find_pkg)
98 {
99 printf ("Finding package \"%s\"\n", find_pkg->name);
100 pkg = opkg_find_package (find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->repository);
101 if (pkg)
102 {
103 print_package (pkg);
104 opkg_package_free (pkg);
105 }
106 else
107 printf ("Package \"%s\" not found!\n", find_pkg->name);
108 opkg_package_free (find_pkg);
109 }
110 else
111 printf ("No package available to test find_package.\n");
112
113 err = opkg_install_package (TEST_PACKAGE, progress_callback, "Installing...");
114 printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
115
116 err = opkg_upgrade_package (TEST_PACKAGE, progress_callback, "Upgrading...");
117 printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
118
119 err = opkg_remove_package (TEST_PACKAGE, progress_callback, "Removing...");
120 printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
121
122 printf ("Listing upgradable packages...\n");
123 opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
124
125 err = opkg_upgrade_all (progress_callback, "Upgrading all...");
126 printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
127
128 }
129
130 int
131 main (int argc, char **argv)
132 {
133 opkg_package_t *pkg;
134 int err;
135
136 if (argc < 2)
137 {
138 printf ("Usage: %s command\n"
139 "\nCommands:\n"
140 "\tupdate - Update package lists\n"
141 "\tfind [package] - Print details of the specified package\n"
142 "\tinstall [package] - Install the specified package\n"
143 "\tupgrade [package] - Upgrade the specified package\n"
144 "\tlist upgrades - List the available upgrades\n"
145 "\tlist all - List all available packages\n"
146 "\tlist installed - List all the installed packages\n"
147 "\tremove [package] - Remove the specified package\n"
148 "\trping - Reposiroties ping, check the accessibility of repositories\n"
149 "\ttest - Run test script\n"
150 , basename (argv[0]));
151 exit (0);
152 }
153
154 opkg_new ();
155
156 opkg_set_option ("offline_root", "/tmp/");
157
158 opkg_re_read_config_files ();
159
160 switch (argv[1][0])
161 {
162 case 'f':
163 pkg = opkg_find_package (argv[2], NULL, NULL, NULL);
164 if (pkg)
165 {
166 print_package (pkg);
167 opkg_package_free (pkg);
168 }
169 else
170 printf ("Package \"%s\" not found!\n", find_pkg->name);
171 opkg_package_free (pkg);
172 break;
173 case 'i':
174 err = opkg_install_package (argv[1], progress_callback, "Installing...");
175 printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
176 break;
177
178 case 'u':
179 if (strlen (argv[1]) < 4)
180 printf ("");
181 if (argv[1][3] == 'd')
182 {
183 err = opkg_update_package_lists (progress_callback, "Updating...");
184 printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
185 break;
186 }
187 else
188 {
189 if (argc < 3)
190 {
191 err = opkg_upgrade_all (progress_callback, "Upgrading all...");
192 printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
193 }
194 else
195 {
196 err = opkg_upgrade_package (argv[2], progress_callback, "Upgrading...");
197 printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
198 }
199 }
200 break;
201
202 case 'l':
203 if (argc < 3)
204 {
205 printf ("Please specify one either all, installed or upgrades\n");
206 }
207 else
208 {
209 switch (argv[2][0])
210 {
211 case 'u':
212 printf ("Listing upgradable packages...\n");
213 opkg_list_upgradable_packages (package_list_upgradable_callback, NULL);
214 break;
215 case 'a':
216 printf ("Listing all packages...\n");
217 opkg_list_packages (package_list_callback, NULL);
218 printf ("\n");
219 break;
220 case 'i':
221 printf ("Listing installed packages...\n");
222 break;
223 default:
224 printf ("Unknown list option \"%s\"", argv[2]);
225 }
226 }
227 break;
228
229 case 'r':
230 if (argv[1][1] == 'e')
231 {
232 err = opkg_remove_package (argv[2], progress_callback, "Removing...");
233 printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
234 break;
235 }else if (argv[1][1] == 'p')
236 {
237 err = opkg_repository_accessibility_check();
238 printf("\nopkg_repository_accessibility_check returned (%d)\n", err);
239 break;
240 }
241
242 default:
243 printf ("Unknown command \"%s\"\n", argv[1]);
244 }
245
246
247 opkg_free ();
248
249 return 0;
250 }