opkg: add string versions of error codes to libopkg_test
[project/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[8] = {
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 };
17
18 void
19 progress_callback (opkg_t *opkg, const opkg_progress_data_t *progress, void *data)
20 {
21 printf ("\r%s %3d%%", (char*) data, progress->percentage);
22 fflush (stdout);
23 }
24
25 void
26 package_list_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
27 {
28 static install_count = 0;
29 static total_count = 0;
30
31 if (pkg->installed)
32 install_count++;
33
34 total_count++;
35
36 printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
37 fflush (stdout);
38
39 if (!find_pkg)
40 {
41 /* store the first package to print out later */
42 find_pkg = pkg;
43 }
44 else
45 opkg_package_free (pkg);
46 }
47
48 void
49 package_list_upgradable_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
50 {
51 printf ("%s - %s\n", pkg->name, pkg->version);
52 opkg_package_free (pkg);
53 }
54
55 void
56 print_package (opkg_package_t *pkg)
57 {
58 printf (
59 "Name: %s\n"
60 "Version: %s\n"
61 "Repository: %s\n"
62 "Architecture: %s\n"
63 "Description: %s\n"
64 "Tags: %s\n"
65 "URL: %s\n"
66 "Size: %d\n"
67 "Installed: %s\n",
68 pkg->name,
69 pkg->version,
70 pkg->repository,
71 pkg->architecture,
72 pkg->description,
73 pkg->tags,
74 pkg->url,
75 pkg->size,
76 (pkg->installed ? "True" : "False")
77 );
78 }
79
80 int
81 main (int argc, char **argv)
82 {
83 opkg_t *opkg;
84 opkg_package_t *pkg;
85 int err;
86
87 opkg = opkg_new ();
88
89 opkg_set_option (opkg, "offline_root", "/tmp/");
90
91 opkg_re_read_config_files (opkg);
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, "aspell", progress_callback, "Installing...");
116 printf ("\nopkg_install_package returned %d (%s)\n", err, errors[err]);
117
118 err = opkg_upgrade_package (opkg, "aspell", progress_callback, "Upgrading...");
119 printf ("\nopkg_upgrade_package returned %d (%s)\n", err, errors[err]);
120
121 err = opkg_remove_package (opkg, "aspell", 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 opkg_free (opkg);
131
132 return 0;
133 }