]> git.proxmox.com Git - libgit2.git/blob - tests/rebase/sign.c
fa47766618813122738d02db8feaa35de49d864e
[libgit2.git] / tests / rebase / sign.c
1 #include "clar_libgit2.h"
2 #include "git2/rebase.h"
3
4 static git_repository *repo;
5 static git_signature *signature;
6
7 /* Fixture setup and teardown */
8 void test_rebase_sign__initialize(void)
9 {
10 repo = cl_git_sandbox_init("rebase");
11 cl_git_pass(git_signature_new(&signature, "Rebaser",
12 "rebaser@rebaser.rb", 1405694510, 0));
13 }
14
15 void test_rebase_sign__cleanup(void)
16 {
17 git_signature_free(signature);
18 cl_git_sandbox_cleanup();
19 }
20
21 static const char *expected_commit_content = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
22 parent f87d14a4a236582a0278a916340a793714256864\n\
23 author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
24 committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n\
25 \n\
26 Modification 3 to gravy\n";
27
28 int signing_cb_passthrough(
29 git_buf *signature,
30 git_buf *signature_field,
31 const char *commit_content,
32 void *payload)
33 {
34 cl_assert_equal_b(false, git_buf_is_allocated(signature));
35 cl_assert_equal_b(false, git_buf_is_allocated(signature_field));
36 cl_assert_equal_s(expected_commit_content, commit_content);
37 cl_assert_equal_p(NULL, payload);
38 return GIT_PASSTHROUGH;
39 }
40
41 /* git checkout gravy ; git rebase --merge veal */
42 void test_rebase_sign__passthrough_signing_cb(void)
43 {
44 git_rebase *rebase;
45 git_reference *branch_ref, *upstream_ref;
46 git_annotated_commit *branch_head, *upstream_head;
47 git_rebase_operation *rebase_operation;
48 git_oid commit_id, expected_id;
49 git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
50 git_commit *commit;
51 const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
52 parent f87d14a4a236582a0278a916340a793714256864\n\
53 author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
54 committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n";
55
56 rebase_opts.signing_cb = signing_cb_passthrough;
57
58 cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
59 cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
60
61 cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
62 cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
63
64 cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
65
66 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
67 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
68
69 git_oid_fromstr(&expected_id, "129183968a65abd6c52da35bff43325001bfc630");
70 cl_assert_equal_oid(&expected_id, &commit_id);
71
72 cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
73 cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
74
75 cl_git_fail_with(GIT_ITEROVER, git_rebase_next(&rebase_operation, rebase));
76
77 git_reference_free(branch_ref);
78 git_reference_free(upstream_ref);
79 git_annotated_commit_free(branch_head);
80 git_annotated_commit_free(upstream_head);
81 git_commit_free(commit);
82 git_rebase_free(rebase);
83 }
84
85 int signing_cb_gpg(
86 git_buf *signature,
87 git_buf *signature_field,
88 const char *commit_content,
89 void *payload)
90 {
91 const char *gpg_signature = "-----BEGIN PGP SIGNATURE-----\n\
92 \n\
93 iQIzBAEBCgAdFiEEgVlDEfSlmKn0fvGgK++h5T2/ctIFAlwZcrAACgkQK++h5T2/\n\
94 ctIPVhAA42RyZhMdKl5Bm0KtQco2scsukIg2y7tjSwhti91zDu3HQgpusjjo0fQx\n\
95 ZzB+OrmlvQ9CDcGpZ0THIzXD8GRJoDMPqdrvZVrBWkGcHvw7/YPA8skzsjkauJ8W\n\
96 7lzF5LCuHSS6OUmPT/+5hEHPin5PB3zhfszyC+Q7aujnIuPJMrKiMnUa+w1HWifM\n\
97 km49OOygQ9S6NQoVuEQede22+c76DlDL7yFghGoo1f0sKCE/9LW6SEnwI/bWv9eo\n\
98 nom5vOPrvQeJiYCQk+2DyWo8RdSxINtY+G9bPE4RXm+6ZgcXECPm9TYDIWpL36fC\n\
99 jvtGLs98woWFElOziBMp5Tb630GMcSI+q5ivHfJ3WS5NKLYLHBNK4iSFN0/dgAnB\n\
100 dj6GcKXKWnIBWn6ZM4o40pcM5KSRUUCLtA0ZmjJH4c4zx3X5fUxd+enwkf3e9VZO\n\
101 fNKC/+xfq6NfoPUPK9+UnchHpJaJw7RG5tZS+sWCz2xpQ1y3/o49xImNyM3wnpvB\n\
102 cRAZabqIHpZa9/DIUkELOtCzln6niqkjRgg3M/YCCNznwV+0RNgz87VtyTPerdef\n\
103 xrqn0+ROMF6ebVqIs6PPtuPkxnAJu7TMKXVB5rFnAewS24e6cIGFzeIA7810py3l\n\
104 cttVRsdOoego+fiy08eFE+aJIeYiINRGhqOBTsuqG4jIdpdKxPE=\n\
105 =KbsY\n\
106 -----END PGP SIGNATURE-----";
107
108 cl_assert_equal_b(false, git_buf_is_allocated(signature));
109 cl_assert_equal_b(false, git_buf_is_allocated(signature_field));
110 cl_assert_equal_s(expected_commit_content, commit_content);
111 cl_assert_equal_p(NULL, payload);
112
113 cl_git_pass(git_buf_set(signature, gpg_signature, strlen(gpg_signature) + 1));
114 return GIT_OK;
115 }
116
117 /* git checkout gravy ; git rebase --merge veal */
118 void test_rebase_sign__gpg_with_no_field(void)
119 {
120 git_rebase *rebase;
121 git_reference *branch_ref, *upstream_ref;
122 git_annotated_commit *branch_head, *upstream_head;
123 git_rebase_operation *rebase_operation;
124 git_oid commit_id, expected_id;
125 git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
126 git_commit *commit;
127 const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
128 parent f87d14a4a236582a0278a916340a793714256864\n\
129 author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
130 committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n\
131 gpgsig -----BEGIN PGP SIGNATURE-----\n\
132 \n\
133 iQIzBAEBCgAdFiEEgVlDEfSlmKn0fvGgK++h5T2/ctIFAlwZcrAACgkQK++h5T2/\n\
134 ctIPVhAA42RyZhMdKl5Bm0KtQco2scsukIg2y7tjSwhti91zDu3HQgpusjjo0fQx\n\
135 ZzB+OrmlvQ9CDcGpZ0THIzXD8GRJoDMPqdrvZVrBWkGcHvw7/YPA8skzsjkauJ8W\n\
136 7lzF5LCuHSS6OUmPT/+5hEHPin5PB3zhfszyC+Q7aujnIuPJMrKiMnUa+w1HWifM\n\
137 km49OOygQ9S6NQoVuEQede22+c76DlDL7yFghGoo1f0sKCE/9LW6SEnwI/bWv9eo\n\
138 nom5vOPrvQeJiYCQk+2DyWo8RdSxINtY+G9bPE4RXm+6ZgcXECPm9TYDIWpL36fC\n\
139 jvtGLs98woWFElOziBMp5Tb630GMcSI+q5ivHfJ3WS5NKLYLHBNK4iSFN0/dgAnB\n\
140 dj6GcKXKWnIBWn6ZM4o40pcM5KSRUUCLtA0ZmjJH4c4zx3X5fUxd+enwkf3e9VZO\n\
141 fNKC/+xfq6NfoPUPK9+UnchHpJaJw7RG5tZS+sWCz2xpQ1y3/o49xImNyM3wnpvB\n\
142 cRAZabqIHpZa9/DIUkELOtCzln6niqkjRgg3M/YCCNznwV+0RNgz87VtyTPerdef\n\
143 xrqn0+ROMF6ebVqIs6PPtuPkxnAJu7TMKXVB5rFnAewS24e6cIGFzeIA7810py3l\n\
144 cttVRsdOoego+fiy08eFE+aJIeYiINRGhqOBTsuqG4jIdpdKxPE=\n\
145 =KbsY\n\
146 -----END PGP SIGNATURE-----\n";
147
148 rebase_opts.signing_cb = signing_cb_gpg;
149
150 cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
151 cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
152
153 cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
154 cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
155
156 cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
157
158 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
159 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
160
161 git_oid_fromstr(&expected_id, "bf78348e45c8286f52b760f1db15cb6da030f2ef");
162 cl_assert_equal_oid(&expected_id, &commit_id);
163
164 cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
165 cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
166
167 cl_git_fail_with(GIT_ITEROVER, git_rebase_next(&rebase_operation, rebase));
168
169 git_reference_free(branch_ref);
170 git_reference_free(upstream_ref);
171 git_annotated_commit_free(branch_head);
172 git_annotated_commit_free(upstream_head);
173 git_commit_free(commit);
174 git_rebase_free(rebase);
175 }
176
177
178 int signing_cb_magic_field(
179 git_buf *signature,
180 git_buf *signature_field,
181 const char *commit_content,
182 void *payload)
183 {
184 const char *signature_content = "magic word: pretty please";
185 const char *signature_field_content = "magicsig";
186
187 cl_assert_equal_b(false, git_buf_is_allocated(signature));
188 cl_assert_equal_b(false, git_buf_is_allocated(signature_field));
189 cl_assert_equal_s(expected_commit_content, commit_content);
190 cl_assert_equal_p(NULL, payload);
191
192 cl_git_pass(git_buf_set(signature, signature_content,
193 strlen(signature_content) + 1));
194 cl_git_pass(git_buf_set(signature_field, signature_field_content,
195 strlen(signature_field_content) + 1));
196
197 return GIT_OK;
198 }
199
200 /* git checkout gravy ; git rebase --merge veal */
201 void test_rebase_sign__custom_signature_field(void)
202 {
203 git_rebase *rebase;
204 git_reference *branch_ref, *upstream_ref;
205 git_annotated_commit *branch_head, *upstream_head;
206 git_rebase_operation *rebase_operation;
207 git_oid commit_id, expected_id;
208 git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
209 git_commit *commit;
210 const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
211 parent f87d14a4a236582a0278a916340a793714256864\n\
212 author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
213 committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n\
214 magicsig magic word: pretty please\n";
215
216 rebase_opts.signing_cb = signing_cb_magic_field;
217
218 cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
219 cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
220
221 cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
222 cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
223
224 cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
225
226 cl_git_pass(git_rebase_next(&rebase_operation, rebase));
227 cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
228
229 git_oid_fromstr(&expected_id, "f46a4a8d26ae411b02aa61b7d69576627f4a1e1c");
230 cl_assert_equal_oid(&expected_id, &commit_id);
231
232 cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
233 cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
234
235 cl_git_fail_with(GIT_ITEROVER, git_rebase_next(&rebase_operation, rebase));
236
237 git_reference_free(branch_ref);
238 git_reference_free(upstream_ref);
239 git_annotated_commit_free(branch_head);
240 git_annotated_commit_free(upstream_head);
241 git_commit_free(commit);
242 git_rebase_free(rebase);
243 }