opkg: improve opkg_install error reporting and include a check to verify repository...
[oweals/opkg-lede.git] / tests / libopkg_test.c
1 #include <opkg.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 opkg_package_t *find_pkg = NULL;
6
7 char *errors[10] = {
8   "No Error",
9   "Unknown Eror",
10   "Download failed",
11   "Dependancies failed",
12   "Package already installed",
13   "Package not available",
14   "Package not found",
15   "Package not installed",
16   "Signature check failed",
17   "MD5 sum failed"
18 };
19
20
21 #define TEST_PACKAGE "aspell"
22
23 void
24 progress_callback (opkg_t *opkg, const opkg_progress_data_t *progress, void *data)
25 {
26   printf ("\r%s %3d%%", (char*) data, progress->percentage);
27   fflush (stdout);
28 }
29
30 void
31 package_list_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
32 {
33   static install_count = 0;
34   static total_count = 0;
35
36   if (pkg->installed)
37     install_count++;
38
39   total_count++;
40
41   printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
42   fflush (stdout);
43
44   if (!find_pkg)
45   {
46     /* store the first package to print out later */
47     find_pkg = pkg;
48   }
49   else
50     opkg_package_free (pkg);
51 }
52
53 void
54 package_list_upgradable_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
55 {
56   printf ("%s - %s\n", pkg->name, pkg->version);
57   opkg_package_free (pkg);
58 }
59
60 void
61 print_package (opkg_package_t *pkg)
62 {
63   printf (
64       "Name:         %s\n"
65       "Version:      %s\n"
66       "Repository:   %s\n"
67       "Architecture: %s\n"
68       "Description:  %s\n"
69       "Tags:         %s\n"
70       "URL:          %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->url,
80       pkg->size,
81       (pkg->installed ? "True" : "False")
82       );
83 }
84
85 int
86 main (int argc, char **argv)
87 {
88   opkg_t *opkg;
89   opkg_package_t *pkg;
90   int err;
91   
92   opkg = opkg_new ();
93
94   opkg_set_option (opkg, "offline_root", "/tmp/");
95
96   opkg_re_read_config_files (opkg);
97
98   err = opkg_update_package_lists (opkg, progress_callback, "Updating...");
99   printf ("\nopkg_update_package_lists returned %d (%s)\n", err, errors[err]);
100
101   opkg_list_packages (opkg, package_list_callback, NULL);
102   printf ("\n");
103
104   if (find_pkg)
105   {
106     printf ("Finding package \"%s\"\n", find_pkg->name);
107     pkg = opkg_find_package (opkg, find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->repository);
108     if (pkg)
109     {
110       print_package (pkg);
111       opkg_package_free (pkg);
112     }
113     else
114       printf ("Package \"%s\" not found!\n", find_pkg->name);
115     opkg_package_free (find_pkg);
116   }
117   else
118     printf ("No package available to test find_package.\n");
119
120   err = opkg_install_package (opkg, TEST_PACKAGE, progress_callback, "Installing...");
121   printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
122
123   err = opkg_upgrade_package (opkg, TEST_PACKAGE, progress_callback, "Upgrading...");
124   printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
125
126   err = opkg_remove_package (opkg, TEST_PACKAGE, progress_callback, "Removing...");
127   printf ("\nopkg_remove_package returned %d (%s)\n", err, errors[err]);
128
129   printf ("Listing upgradable packages...\n");
130   opkg_list_upgradable_packages (opkg, package_list_upgradable_callback, NULL);
131
132   err = opkg_upgrade_all (opkg, progress_callback, "Upgrading all...");
133   printf ("\nopkg_upgrade_all returned %d (%s)\n", err, errors[err]);
134
135   opkg_free (opkg);
136
137   return 0;
138 }