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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#include <stdio.h>
#include <locale.h>
#include <glib.h>
#include <artifacts.h>
#include <context.h>
#include <utils.h>
#include "common.h"
typedef struct {
gchar *tmpdir;
RArtifactRepo *repo;
} ArtifactsFixture;
static void show_tree(const gchar *path)
{
g_autofree gchar *tree_cmd = g_strdup_printf("tree -a %s", path);
system(tree_cmd);
}
static void artifacts_fixture_set_up(ArtifactsFixture *fixture, gconstpointer user_data)
{
fixture->tmpdir = g_dir_make_tmp("rauc-artifacts-XXXXXX", NULL);
g_assert_nonnull(fixture->tmpdir);
g_assert(test_mkdir_relative(fixture->tmpdir, "repo", 0777) == 0);
fixture->repo = g_new0(RArtifactRepo, 1);
fixture->repo->name = g_intern_static_string("test-repo");
fixture->repo->description = g_strdup("desc");
fixture->repo->path = g_build_filename(fixture->tmpdir, "repo", NULL);
fixture->repo->type = g_strdup("files");
g_assert_true(r_artifact_repo_is_valid_type(fixture->repo->type));
}
static void artifacts_fixture_tear_down(ArtifactsFixture *fixture, gconstpointer user_data)
{
show_tree(fixture->tmpdir);
g_assert_true(rm_tree(fixture->tmpdir, NULL));
g_free(fixture->tmpdir);
g_clear_pointer(&fixture->repo, r_artifact_repo_free);
}
static RArtifact* create_random_artifact(const gchar *tmpdir, const gchar *name, gsize size, const guint32 seed)
{
g_message("creating %s", name);
g_autoptr(RArtifact) artifact = g_new0(RArtifact, 1);
artifact->name = g_intern_string(name);
g_autofree guint8 *content = random_bytes(size, seed);
artifact->checksum.digest = g_compute_checksum_for_data(G_CHECKSUM_SHA256, content, size);
artifact->checksum.size = size;
artifact->checksum.type = G_CHECKSUM_SHA256;
artifact->references = g_ptr_array_new();
g_autofree gchar *pathname = g_strdup_printf("%s/.artifact-%s-%s", tmpdir, name, artifact->checksum.digest);
if (!g_file_set_contents(pathname, (gchar *)content, size, NULL)) {
return NULL;
}
return g_steal_pointer(&artifact);
}
static void test_repo_type(ArtifactsFixture *fixture, gconstpointer user_data)
{
g_assert_true(r_artifact_repo_is_valid_type("files"));
g_assert_true(r_artifact_repo_is_valid_type("trees"));
g_assert_false(r_artifact_repo_is_valid_type("badtype"));
}
static void test_init(ArtifactsFixture *fixture, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
gboolean res = FALSE;
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_nonnull(fixture->repo->artifacts);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 0);
res = r_artifact_repo_commit(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
}
static void test_early_return(ArtifactsFixture *fixture, gconstpointer user_data)
{
r_artifact_free(NULL);
r_artifact_repo_free(NULL);
}
static void test_find(ArtifactsFixture *fixture, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
gboolean res = FALSE;
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
/* create artifact */
RArtifact *artifact = create_random_artifact(fixture->repo->path, "a1", 64, 21799804);
res = r_artifact_repo_insert(fixture->repo, artifact, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
g_assert_null(r_artifact_find(fixture->repo,
g_intern_static_string("missing"),
g_intern_static_string("foo")));
g_assert_null(r_artifact_find(fixture->repo,
g_intern_static_string("a1"),
g_intern_static_string("foo")));
g_assert_true(r_artifact_find(fixture->repo,
g_intern_static_string("a1"),
g_intern_static_string("6fb6a28f1b3d788150c8b02651575287ecb892312e4f077add0e84db55231d41")) == artifact);
}
static void test_create_load(ArtifactsFixture *fixture, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
gboolean res = FALSE;
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
/* create artifact */
RArtifact *artifact = create_random_artifact(fixture->repo->path, "a1", 64, 21799804);
res = r_artifact_repo_insert(fixture->repo, artifact, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
g_autofree gchar *a_filename = g_strdup_printf("%s/.artifact-a1-%s", fixture->repo->path, artifact->checksum.digest);
g_autofree gchar *l_filename = g_strdup_printf("%s/a1", fixture->repo->path);
g_assert_cmpstr(a_filename, ==, artifact->path);
g_assert_false(g_file_test(a_filename, G_FILE_TEST_IS_SYMLINK));
g_assert_true(g_file_test(a_filename, G_FILE_TEST_IS_REGULAR));
g_assert_false(g_file_test(l_filename, G_FILE_TEST_EXISTS));
/* add a link */
r_artifact_activate(artifact, "");
res = r_artifact_repo_commit(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
g_assert_true(g_file_test(l_filename, G_FILE_TEST_IS_SYMLINK));
g_assert_true(g_file_test(l_filename, G_FILE_TEST_IS_REGULAR));
/* check that duplicates are rejected */
RArtifact *artifact_dup = create_random_artifact(fixture->repo->path, "a1", 64, 21799804);
res = r_artifact_repo_insert(fixture->repo, artifact_dup, &error);
g_assert_error(error, R_ARTIFACTS_ERROR, R_ARTIFACTS_ERROR_DUPLICATE);
g_assert_false(res);
g_clear_error(&error);
g_clear_pointer(&artifact_dup, r_artifact_free);
/* remove the link again */
r_artifact_deactivate(artifact, "");
res = r_artifact_repo_commit(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
show_tree(fixture->tmpdir);
g_assert_false(g_file_test(l_filename, G_FILE_TEST_EXISTS));
/* check that we still know about the deactivated artifact */
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
show_tree(fixture->tmpdir);
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 0);
}
static void test_create_load_parent(ArtifactsFixture *fixture, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
gboolean res = FALSE;
fixture->repo->parent_class = g_intern_static_string("rootfs");
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
/* create artifact */
RArtifact *artifact = create_random_artifact(fixture->repo->path, "a1", 64, 21799804);
res = r_artifact_repo_insert(fixture->repo, artifact, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
g_autofree gchar *a_filename = g_strdup_printf("%s/.artifact-a1-%s", fixture->repo->path, artifact->checksum.digest);
g_autofree gchar *l_filename = g_strdup_printf("%s/rootfs.0/a1", fixture->repo->path);
g_assert_cmpstr(a_filename, ==, artifact->path);
g_assert_false(g_file_test(a_filename, G_FILE_TEST_IS_SYMLINK));
g_assert_true(g_file_test(a_filename, G_FILE_TEST_IS_REGULAR));
g_assert_false(g_file_test(l_filename, G_FILE_TEST_EXISTS));
/* add a link */
r_artifact_activate(artifact, "rootfs.0");
res = r_artifact_repo_commit(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_true(g_file_test(l_filename, G_FILE_TEST_IS_SYMLINK));
g_assert_true(g_file_test(l_filename, G_FILE_TEST_IS_REGULAR));
/* remove the link again */
r_artifact_deactivate(artifact, "rootfs.0");
show_tree(fixture->tmpdir);
res = r_artifact_repo_commit(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_false(g_file_test(l_filename, G_FILE_TEST_EXISTS));
/* check that we still know about the deactivated artifact */
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 1);
res = r_artifact_repo_prune(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
res = r_artifact_repo_prepare(fixture->repo, &error);
g_assert_no_error(error);
g_assert_true(res);
g_assert_cmpuint(g_hash_table_size(fixture->repo->artifacts), ==, 0);
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "C");
r_context_conf()->configpath = g_strdup("test/test.conf");
r_context();
g_test_init(&argc, &argv, NULL);
g_test_add("/artifacts/repo_type", ArtifactsFixture, NULL,
NULL, test_repo_type,
NULL);
g_test_add("/artifacts/init", ArtifactsFixture, NULL,
artifacts_fixture_set_up, test_init,
artifacts_fixture_tear_down);
g_test_add("/artifacts/early_return", ArtifactsFixture, NULL,
artifacts_fixture_set_up, test_early_return,
artifacts_fixture_tear_down);
g_test_add("/artifacts/find", ArtifactsFixture, NULL,
artifacts_fixture_set_up, test_find,
artifacts_fixture_tear_down);
g_test_add("/artifacts/create_load", ArtifactsFixture, NULL,
artifacts_fixture_set_up, test_create_load,
artifacts_fixture_tear_down);
g_test_add("/artifacts/create_load_parent", ArtifactsFixture, NULL,
artifacts_fixture_set_up, test_create_load_parent,
artifacts_fixture_tear_down);
return g_test_run();
}
|