File: test_iutil.cpp

package info (click to toggle)
libdnf 0.75.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,468 kB
  • sloc: cpp: 48,297; xml: 1,638; python: 1,537; ansic: 1,223; sql: 227; sh: 54; makefile: 39
file content (217 lines) | stat: -rw-r--r-- 6,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (C) 2012-2013 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */


#include "libdnf/hy-util.h"
#include "libdnf/hy-iutil.h"
#include "libdnf/hy-iutil-private.hpp"

#include "fixtures.h"
#include "test_suites.h"

#include <solv/pool.h>
#include <solv/repo.h>
#include <solv/repo_write.h>

#include <stdlib.h>
#include <unistd.h>
#include <glib.h>

static void
build_test_file(const char *filename)
{
    FILE *fp = fopen(filename, "w+");
    fail_if(fp == NULL);
    fail_unless(fwrite("empty", 5, 1, fp) == 1);
    fclose(fp);
}

START_TEST(test_abspath)
{
    char *abs = abspath("/cus/tard");
    ck_assert_str_eq(abs, "/cus/tard");
    g_free(abs);

    abs = abspath("cus/tard");
    ck_assert_int_eq(abs[0], '/');
    g_free(abs);
}
END_TEST

START_TEST(test_checksum)
{
    /* create a new file, edit it a bit */
    char *new_file = solv_dupjoin(test_globals.tmpdir,
                                  "/test_checksum", NULL);
    build_test_file(new_file);

    unsigned char cs1[CHKSUM_BYTES];
    unsigned char cs2[CHKSUM_BYTES];
    unsigned char cs1_sum[CHKSUM_BYTES];
    unsigned char cs2_sum[CHKSUM_BYTES];
    bzero(cs1, CHKSUM_BYTES);
    bzero(cs2, CHKSUM_BYTES);
    bzero(cs1_sum, CHKSUM_BYTES);
    bzero(cs2_sum, CHKSUM_BYTES);
    fail_if(checksum_cmp(cs1, cs2)); // tests checksum_cmp

    /* take the first checksums */
    FILE *fp;
    fail_if((fp = fopen(new_file, "r")) == NULL);
    fail_if(checksum_fp(cs1, fp));
    fail_if(checksum_stat(cs1_sum, fp));
    fclose(fp);
    /* the taken checksum are not zeros anymore */
    fail_if(checksum_cmp(cs1, cs2) == 0);
    fail_if(checksum_cmp(cs1_sum, cs2_sum) == 0);
    fp = NULL;

    /* append something */
    fail_if((fp = fopen(new_file, "a")) == NULL);
    fail_unless(fwrite("X", 1, 1, fp) == 1);
    fclose(fp);
    fp = NULL;

    /* take the second checksums */
    fail_if((fp = fopen(new_file, "r")) == NULL);
    fail_if(checksum_stat(cs2, fp));
    fail_if(checksum_stat(cs2_sum, fp));
    fclose(fp);
    fail_unless(checksum_cmp(cs1, cs2));
    fail_unless(checksum_cmp(cs1_sum, cs2_sum));

    g_free(new_file);
}
END_TEST

START_TEST(test_dnf_solvfile_userdata)
{
    char *new_file = solv_dupjoin(test_globals.tmpdir,
                                  "/test_dnf_solvfile_userdata", NULL);
    build_test_file(new_file);

    unsigned char cs_computed[CHKSUM_BYTES];
    FILE *fp = fopen(new_file, "r+");
    checksum_fp(cs_computed, fp);

    SolvUserdata solv_userdata;
    fail_if(solv_userdata_fill(&solv_userdata, cs_computed, NULL));

    Pool *pool = pool_create();
    Repo *repo = repo_create(pool, "test_repo");
    Repowriter *writer = repowriter_create(repo);
    repowriter_set_userdata(writer, &solv_userdata, solv_userdata_size);
    fail_if(repowriter_write(writer, fp));
    repowriter_free(writer);
    fclose(fp);

    fail_if((fp = fopen(new_file, "r")) == NULL);
    std::unique_ptr<SolvUserdata, decltype(solv_free)*> dnf_solvfile = solv_userdata_read(fp);
    fail_unless(dnf_solvfile);
    fail_unless(solv_userdata_verify(dnf_solvfile.get(), cs_computed));
    fclose(fp);

    g_free(new_file);
    repo_free(repo, 0);
    pool_free(pool);
}
END_TEST

START_TEST(test_mkcachedir)
{
    const char *workdir = test_globals.tmpdir;
    char * dir;
    fail_if(asprintf(&dir, "%s%s", workdir, "/mkcachedir/sub/deep") == -1);
    fail_if(mkcachedir(dir));
    fail_if(access(dir, R_OK|X_OK|W_OK));
    free(dir);

    fail_if(asprintf(&dir, "%s%s", workdir, "/mkcachedir/sub2/wd-XXXXXX") == -1);
    fail_if(mkcachedir(dir));
    fail_if(g_str_has_suffix(dir, "XXXXXX")); /* mkcache dir changed the Xs */
    fail_if(access(dir, R_OK|X_OK|W_OK));

    /* test the globbing capability of mkcachedir */
    char *dir2;
    fail_if(asprintf(&dir2, "%s%s", workdir, "/mkcachedir/sub2/wd-XXXXXX") == -1);
    fail_if(mkcachedir(dir2));
    fail_if(strcmp(dir, dir2)); /* mkcachedir should have arrived at the same name */

    free(dir2);
    free(dir);
}
END_TEST

START_TEST(test_version_split)
{
    Pool *pool = pool_create();

    /*
     * On "foreign" systems, the disttype will
     * not default to RPM. Set this explicitly.
     */
    fail_if(-1 == pool_setdisttype(pool, DISTTYPE_RPM));

    /*
     * On "foreign" systems, the implicitobsoleteusescolors
     * flag is turned off by default.
     * This leads to unexpected results when working with
     * Fedora, Mageia or CentOS repositories, so enable it
     * forcefully.
     */
    pool_set_flag(pool, POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS, 1);

    char evr[] = "1:5.9.3-8";
    char *epoch, *version, *release;

    pool_split_evr(pool, evr, &epoch, &version, &release);
    ck_assert_str_eq(epoch, "1");
    ck_assert_str_eq(version, "5.9.3");
    ck_assert_str_eq(release, "8");

    char evr2[] = "8.0-9";
    pool_split_evr(pool, evr2, &epoch, &version, &release);
    fail_unless(epoch == NULL);
    ck_assert_str_eq(version, "8.0");
    ck_assert_str_eq(release, "9");

    char evr3[] = "1.4";
    pool_split_evr(pool, evr3, &epoch, &version, &release);
    fail_unless(epoch == NULL);
    ck_assert_str_eq(version, "1.4");
    fail_unless(release == NULL);

    pool_free(pool);
}
END_TEST

Suite *
iutil_suite(void)
{
    Suite *s = suite_create("iutil");
    TCase *tc = tcase_create("Main");
    tcase_add_test(tc, test_abspath);
    tcase_add_test(tc, test_checksum);
    tcase_add_test(tc, test_dnf_solvfile_userdata);
    tcase_add_test(tc, test_mkcachedir);
    tcase_add_test(tc, test_version_split);
    suite_add_tcase(s, tc);
    return s;
}