Hash :
6e6049be
Author :
Date :
2019-07-23T14:43:11
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
/*
* Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/queue.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <util.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <unistd.h>
#include "got_error.h"
#include "got_object.h"
#include "got_reference.h"
#include "got_repository.h"
#include "got_diff.h"
#include "got_opentemp.h"
#include "got_privsep.h"
#include "got_path.h"
#ifndef nitems
#define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
#endif
#define GOT_REPO_PATH "../../../"
static int verbose;
void
test_printf(char *fmt, ...)
{
va_list ap;
if (!verbose)
return;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
static const struct got_error *
print_commit_object(struct got_object_id *, struct got_repository *);
static const struct got_error *
print_parent_commits(struct got_commit_object *commit,
struct got_repository *repo)
{
const struct got_object_id_queue *parent_ids;
struct got_object_qid *qid;
const struct got_error *err = NULL;
parent_ids = got_object_commit_get_parent_ids(commit);
SIMPLEQ_FOREACH(qid, parent_ids, entry) {
err = print_commit_object(qid->id, repo);
if (err)
break;
}
return err;
}
static const struct got_error *
print_tree_object(struct got_object_id *id, char *parent,
struct got_repository *repo)
{
struct got_tree_object *tree;
const struct got_tree_entries *entries;
struct got_tree_entry *te;
const struct got_error *err;
err = got_object_open_as_tree(&tree, repo, id);
if (err != NULL)
return err;
entries = got_object_tree_get_entries(tree);
SIMPLEQ_FOREACH(te, &entries->head, entry) {
char *next_parent;
char *hex;
err = got_object_id_str(&hex, te->id);
if (err)
break;
if (!S_ISDIR(te->mode)) {
test_printf("%s %s/%s\n", hex, parent, te->name);
free(hex);
continue;
}
test_printf("%s %s/%s\n", hex, parent, te->name);
free(hex);
if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
err = got_error_from_errno("asprintf");
break;
}
err = print_tree_object(te->id, next_parent, repo);
free(next_parent);
if (err)
break;
}
got_object_tree_close(tree);
return err;
}
static const struct got_error *
print_commit_object(struct got_object_id *id, struct got_repository *repo)
{
struct got_commit_object *commit;
const struct got_object_id_queue *parent_ids;
struct got_object_qid *qid;
char *buf;
const struct got_error *err;
int obj_type;
err = got_object_open_as_commit(&commit, repo, id);
if (err)
return err;
err = got_object_id_str(&buf, id);
if (err) {
got_object_commit_close(commit);
return err;
}
test_printf("tree: %s\n", buf);
free(buf);
test_printf("parent%s: ",
(got_object_commit_get_nparents(commit) == 1) ? "" : "s");
parent_ids = got_object_commit_get_parent_ids(commit);
SIMPLEQ_FOREACH(qid, parent_ids, entry) {
err = got_object_id_str(&buf, qid->id);
if (err) {
got_object_commit_close(commit);
return err;
}
test_printf("%s\n", buf);
free(buf);
}
test_printf("author: %s\n", got_object_commit_get_author(commit));
test_printf("committer: %s\n", got_object_commit_get_committer(commit));
test_printf("log: %s\n", got_object_commit_get_logmsg(commit));
err = got_object_get_type(&obj_type, repo,
got_object_commit_get_tree_id(commit));
if (err != NULL) {
got_object_commit_close(commit);
return err;
}
if (obj_type == GOT_OBJ_TYPE_TREE)
test_printf("\n");
err = print_parent_commits(commit, repo);
got_object_commit_close(commit);
return err;
}
static int
repo_read_log(const char *repo_path)
{
const struct got_error *err;
struct got_repository *repo;
struct got_reference *head_ref;
struct got_object_id *id;
char *buf;
err = got_repo_open(&repo, repo_path);
if (err != NULL || repo == NULL)
return 0;
err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
if (err != NULL || head_ref == NULL)
return 0;
err = got_ref_resolve(&id, repo, head_ref);
if (err != NULL || head_ref == NULL)
return 0;
err = got_object_id_str(&buf, id);
if (err != NULL)
return 0;
test_printf("HEAD is at %s\n", buf);
free(buf);
err = print_commit_object(id, repo);
if (err)
return 0;
free(id);
got_ref_close(head_ref);
got_repo_close(repo);
return 1;
}
static int
repo_read_tree(const char *repo_path)
{
const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
const struct got_error *err;
struct got_repository *repo;
struct got_object_id *id;
err = got_repo_open(&repo, repo_path);
if (err != NULL || repo == NULL)
return 0;
err = got_object_resolve_id_str(&id, repo, tree_sha1);
if (err != NULL)
return 0;
print_tree_object(id, "", repo);
test_printf("\n");
got_repo_close(repo);
return (err == NULL);
}
static int
repo_read_blob(const char *repo_path)
{
const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
const struct got_error *err;
struct got_repository *repo;
struct got_object_id *id;
struct got_blob_object *blob;
int i;
size_t len;
err = got_repo_open(&repo, repo_path);
if (err != NULL || repo == NULL)
return 0;
err = got_object_resolve_id_str(&id, repo, blob_sha1);
if (err != NULL)
return 0;
err = got_object_open_as_blob(&blob, repo, id, 64);
if (err != NULL)
return 0;
test_printf("\n");
do {
const uint8_t *buf = got_object_blob_get_read_buf(blob);
err = got_object_blob_read_block(&len, blob);
if (err)
break;
for (i = 0; i < len; i++)
test_printf("%c", buf[i]);
} while (len != 0);
test_printf("\n");
got_object_blob_close(blob);
got_repo_close(repo);
return (err == NULL);
}
static int
repo_diff_blob(const char *repo_path)
{
const char *blob1_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
const struct got_error *err;
struct got_repository *repo;
struct got_object_id *id1, *id2;
struct got_blob_object *blob1;
struct got_blob_object *blob2;
FILE *outfile;
int i;
char *line;
size_t len;
const char delim[3] = {'\0', '\0', '\0'};
const char *expected_output[] = {
"blob - 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
"blob + de7eb21b21c7823a753261aadf7cba35c9580fbf",
"--- 141f5fdc96126c1f4195558560a3c915e3d9b4c3",
"+++ de7eb21b21c7823a753261aadf7cba35c9580fbf",
"@@ -1,10 +1,10 @@",
" .PATH:${.CURDIR}/../../lib",
" ",
" PROG = repository_test",
"-SRCS = path.c repository.c error.c refs.c repository_test.c",
"+SRCS = path.c repository.c error.c refs.c object.c sha1.c repository_test.c",
" ",
" CPPFLAGS = -I${.CURDIR}/../../include",
"-LDADD = -lutil",
"+LDADD = -lutil -lz",
" ",
" NOMAN = yes"
};
err = got_repo_open(&repo, repo_path);
if (err != NULL || repo == NULL)
return 0;
err = got_object_resolve_id_str(&id1, repo, blob1_sha1);
if (err != NULL)
return 0;
err = got_object_resolve_id_str(&id2, repo, blob2_sha1);
if (err != NULL)
return 0;
err = got_object_open_as_blob(&blob1, repo, id1, 512);
if (err != NULL)
return 0;
err = got_object_open_as_blob(&blob2, repo, id2, 512);
if (err != NULL)
return 0;
test_printf("\n");
outfile = got_opentemp();
if (outfile == NULL)
return 0;
got_diff_blob(blob1, blob2, NULL, NULL, 3, outfile);
rewind(outfile);
i = 0;
while ((line = fparseln(outfile, &len, NULL, delim, 0)) != NULL) {
test_printf(line);
test_printf("\n");
if (i < nitems(expected_output) &&
strcmp(line, expected_output[i]) != 0) {
test_printf("diff output mismatch; expected: '%s'\n",
expected_output[i]);
return 0;
}
i++;
}
if (fclose(outfile) != 0 && err == NULL)
err = got_error_from_errno("fclose");
test_printf("\n");
if (i != nitems(expected_output) + 1) {
test_printf("number of lines expected: %d; actual: %d\n",
nitems(expected_output), i - 1);
return 0;
}
got_object_blob_close(blob1);
got_object_blob_close(blob2);
got_repo_close(repo);
return (err == NULL);
}
static int
repo_diff_tree(const char *repo_path)
{
const char *tree1_sha1 = "1efc41caf761a0a1f119d0c5121eedcb2e7a88c3";
const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
const struct got_error *err;
struct got_repository *repo;
struct got_object_id *id1;
struct got_object_id *id2;
struct got_tree_object *tree1;
struct got_tree_object *tree2;
FILE *outfile;
struct got_diff_blob_output_unidiff_arg arg;
err = got_repo_open(&repo, repo_path);
if (err != NULL || repo == NULL)
return 0;
err = got_object_resolve_id_str(&id1, repo, tree1_sha1);
if (err != NULL)
return 0;
err = got_object_resolve_id_str(&id2, repo, tree2_sha1);
if (err != NULL)
return 0;
err = got_object_open_as_tree(&tree1, repo, id1);
if (err != NULL)
return 0;
err = got_object_open_as_tree(&tree2, repo, id2);
if (err != NULL)
return 0;
if (!verbose) {
outfile = fopen("/dev/null", "w+");
if (outfile == NULL)
return 0;
} else
outfile = stdout;
test_printf("\n");
arg.diff_context = 3;
arg.outfile = outfile;
got_diff_tree(tree1, tree2, "", "", repo,
got_diff_blob_output_unidiff, &arg);
test_printf("\n");
got_object_tree_close(tree1);
got_object_tree_close(tree2);
got_repo_close(repo);
return (err == NULL);
}
#define RUN_TEST(expr, name) \
{ test_ok = (expr); \
printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
failure = (failure || !test_ok); }
void
usage(void)
{
fprintf(stderr, "usage: repository_test [-v] [REPO_PATH]\n");
}
static const struct got_error *
apply_unveil(const char *repo_path)
{
const struct got_error *error;
if (repo_path) {
if (unveil(repo_path, "r") != 0)
return got_error_from_errno2("unveil", repo_path);
}
if (unveil("/tmp", "rwc") != 0)
return got_error_from_errno2("unveil", "/tmp");
if (unveil("/dev/null", "rwc") != 0)
return got_error_from_errno2("unveil", "/dev/null");
error = got_privsep_unveil_exec_helpers();
if (error != NULL)
return error;
if (unveil(NULL, NULL) != 0)
return got_error_from_errno("unveil");
return NULL;
}
int
main(int argc, char *argv[])
{
int test_ok = 0, failure = 0;
char *repo_path;
int ch;
const struct got_error *error;
#ifndef PROFILE
if (pledge("stdio rpath wpath cpath proc exec sendfd unveil", NULL)
== -1)
err(1, "pledge");
#endif
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
verbose = 1;
break;
default:
usage();
return 1;
}
}
argc -= optind;
argv += optind;
switch (argc) {
case 0:
repo_path = realpath(GOT_REPO_PATH, NULL);
break;
case 1:
repo_path = realpath(argv[0], NULL);
break;
default:
usage();
return 1;
}
if (repo_path == NULL) {
fprintf(stderr, "realpath: %s\n", strerror(errno));
return 1;
}
error = apply_unveil(repo_path);
if (error) {
fprintf(stderr, "unveil: %s\n", error->msg);
free(repo_path);
return 1;
}
RUN_TEST(repo_read_tree(repo_path), "read_tree");
RUN_TEST(repo_read_log(repo_path), "read_log");
RUN_TEST(repo_read_blob(repo_path), "read_blob");
RUN_TEST(repo_diff_blob(repo_path), "diff_blob");
RUN_TEST(repo_diff_tree(repo_path), "diff_tree");
free(repo_path);
return failure ? 1 : 0;
}