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