]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/index/add.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / index / add.c
CommitLineData
6ddf533a
ET
1#include "clar_libgit2.h"
2
3static git_repository *g_repo = NULL;
4static git_index *g_index = NULL;
5
6static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92";
7static const char *valid_tree_id = "181037049a54a1eb5fab404658a3a250b44335d7";
8static const char *valid_commit_id = "763d71aadf09a7951596c9746c024e7eece7c7af";
9static const char *invalid_id = "1234567890123456789012345678901234567890";
10
11void test_index_add__initialize(void)
12{
13 g_repo = cl_git_sandbox_init("testrepo");
14 cl_git_pass(git_repository_index(&g_index, g_repo));
15}
16
17void test_index_add__cleanup(void)
18{
19 git_index_free(g_index);
20 cl_git_sandbox_cleanup();
21 g_repo = NULL;
22
f2dddf52 23 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
6ddf533a
ET
24}
25
26static void test_add_entry(
27 bool should_succeed, const char *idstr, git_filemode_t mode)
28{
29 git_index_entry entry = {{0}};
30
31 cl_git_pass(git_oid_fromstr(&entry.id, idstr));
32
33 entry.path = mode == GIT_FILEMODE_TREE ? "test_folder" : "test_file";
34 entry.mode = mode;
35
36 if (should_succeed)
37 cl_git_pass(git_index_add(g_index, &entry));
38 else
39 cl_git_fail(git_index_add(g_index, &entry));
40}
41
42void test_index_add__invalid_entries_succeeds_by_default(void)
43{
44 /*
f2dddf52 45 * Ensure that there is validation on object ids by default
6ddf533a
ET
46 */
47
48 /* ensure that we can add some actually good entries */
49 test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB);
50 test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE);
51 test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
52
53 /* test that we fail to add some invalid (missing) blobs and trees */
f2dddf52
ET
54 test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB);
55 test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
56 test_add_entry(false, invalid_id, GIT_FILEMODE_LINK);
6ddf533a
ET
57
58 /* test that we validate the types of objects */
f2dddf52
ET
59 test_add_entry(false, valid_commit_id, GIT_FILEMODE_BLOB);
60 test_add_entry(false, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
61 test_add_entry(false, valid_commit_id, GIT_FILEMODE_LINK);
6ddf533a
ET
62
63 /*
f2dddf52 64 * Ensure that there we can disable validation
6ddf533a
ET
65 */
66
f2dddf52 67 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
6ddf533a
ET
68
69 /* ensure that we can add some actually good entries */
70 test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB);
71 test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE);
72 test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
73
f2dddf52
ET
74 /* test that we can now add some invalid (missing) blobs and trees */
75 test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB);
76 test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
77 test_add_entry(true, invalid_id, GIT_FILEMODE_LINK);
6ddf533a 78
f2dddf52
ET
79 /* test that we do not validate the types of objects */
80 test_add_entry(true, valid_commit_id, GIT_FILEMODE_BLOB);
81 test_add_entry(true, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
82 test_add_entry(true, valid_commit_id, GIT_FILEMODE_LINK);
6ddf533a
ET
83}
84