Skip to content

Commit 900f991

Browse files
committed
Add tests for signing rebase commits
1 parent 546e40c commit 900f991

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

tests/rebase/sign.c

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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 signature_cb_passthrough(char **signature, const char *commit_content, void *payload)
29+
{
30+
cl_assert_equal_p(NULL, *signature);
31+
cl_assert_equal_s(expected_commit_content, commit_content);
32+
cl_assert_equal_p(NULL, payload);
33+
return GIT_PASSTHROUGH;
34+
}
35+
36+
/* git checkout gravy ; git rebase --merge veal */
37+
void test_rebase_sign__passthrough_signature_cb(void)
38+
{
39+
git_rebase *rebase;
40+
git_reference *branch_ref, *upstream_ref;
41+
git_annotated_commit *branch_head, *upstream_head;
42+
git_rebase_operation *rebase_operation;
43+
git_oid commit_id, expected_id;
44+
git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
45+
git_commit *commit;
46+
int error;
47+
const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
48+
parent f87d14a4a236582a0278a916340a793714256864\n\
49+
author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
50+
committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n";
51+
52+
rebase_opts.signature_cb = signature_cb_passthrough;
53+
54+
cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
55+
cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
56+
57+
cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
58+
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
59+
60+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
61+
62+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
63+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
64+
65+
git_oid_fromstr(&expected_id, "129183968a65abd6c52da35bff43325001bfc630");
66+
cl_assert_equal_oid(&expected_id, &commit_id);
67+
68+
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
69+
cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
70+
71+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
72+
cl_assert_equal_i(GIT_ITEROVER, error);
73+
74+
git_reference_free(branch_ref);
75+
git_reference_free(upstream_ref);
76+
git_annotated_commit_free(branch_head);
77+
git_annotated_commit_free(upstream_head);
78+
git_commit_free(commit);
79+
git_rebase_free(rebase);
80+
}
81+
82+
int signature_cb_gpg(char **signature, const char *commit_content, void *payload)
83+
{
84+
const char *gpg_signature = "-----BEGIN PGP SIGNATURE-----\n\
85+
\n\
86+
iQIzBAEBCgAdFiEEgVlDEfSlmKn0fvGgK++h5T2/ctIFAlwZcrAACgkQK++h5T2/\n\
87+
ctIPVhAA42RyZhMdKl5Bm0KtQco2scsukIg2y7tjSwhti91zDu3HQgpusjjo0fQx\n\
88+
ZzB+OrmlvQ9CDcGpZ0THIzXD8GRJoDMPqdrvZVrBWkGcHvw7/YPA8skzsjkauJ8W\n\
89+
7lzF5LCuHSS6OUmPT/+5hEHPin5PB3zhfszyC+Q7aujnIuPJMrKiMnUa+w1HWifM\n\
90+
km49OOygQ9S6NQoVuEQede22+c76DlDL7yFghGoo1f0sKCE/9LW6SEnwI/bWv9eo\n\
91+
nom5vOPrvQeJiYCQk+2DyWo8RdSxINtY+G9bPE4RXm+6ZgcXECPm9TYDIWpL36fC\n\
92+
jvtGLs98woWFElOziBMp5Tb630GMcSI+q5ivHfJ3WS5NKLYLHBNK4iSFN0/dgAnB\n\
93+
dj6GcKXKWnIBWn6ZM4o40pcM5KSRUUCLtA0ZmjJH4c4zx3X5fUxd+enwkf3e9VZO\n\
94+
fNKC/+xfq6NfoPUPK9+UnchHpJaJw7RG5tZS+sWCz2xpQ1y3/o49xImNyM3wnpvB\n\
95+
cRAZabqIHpZa9/DIUkELOtCzln6niqkjRgg3M/YCCNznwV+0RNgz87VtyTPerdef\n\
96+
xrqn0+ROMF6ebVqIs6PPtuPkxnAJu7TMKXVB5rFnAewS24e6cIGFzeIA7810py3l\n\
97+
cttVRsdOoego+fiy08eFE+aJIeYiINRGhqOBTsuqG4jIdpdKxPE=\n\
98+
=KbsY\n\
99+
-----END PGP SIGNATURE-----";
100+
101+
cl_assert_equal_s(expected_commit_content, commit_content);
102+
cl_assert_equal_p(NULL, payload);
103+
104+
*signature = strdup(gpg_signature);
105+
return GIT_OK;
106+
}
107+
108+
int signature_field_cb_passthrough(char **signature_field, void *payload)
109+
{
110+
cl_assert_equal_p(NULL, *signature_field);
111+
cl_assert_equal_p(NULL, payload);
112+
return GIT_PASSTHROUGH;
113+
}
114+
115+
/* git checkout gravy ; git rebase --merge veal */
116+
void test_gpg_signature(bool use_passthrough)
117+
{
118+
git_rebase *rebase;
119+
git_reference *branch_ref, *upstream_ref;
120+
git_annotated_commit *branch_head, *upstream_head;
121+
git_rebase_operation *rebase_operation;
122+
git_oid commit_id, expected_id;
123+
git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
124+
git_commit *commit;
125+
int error;
126+
const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
127+
parent f87d14a4a236582a0278a916340a793714256864\n\
128+
author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
129+
committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n\
130+
gpgsig -----BEGIN PGP SIGNATURE-----\n\
131+
\n\
132+
iQIzBAEBCgAdFiEEgVlDEfSlmKn0fvGgK++h5T2/ctIFAlwZcrAACgkQK++h5T2/\n\
133+
ctIPVhAA42RyZhMdKl5Bm0KtQco2scsukIg2y7tjSwhti91zDu3HQgpusjjo0fQx\n\
134+
ZzB+OrmlvQ9CDcGpZ0THIzXD8GRJoDMPqdrvZVrBWkGcHvw7/YPA8skzsjkauJ8W\n\
135+
7lzF5LCuHSS6OUmPT/+5hEHPin5PB3zhfszyC+Q7aujnIuPJMrKiMnUa+w1HWifM\n\
136+
km49OOygQ9S6NQoVuEQede22+c76DlDL7yFghGoo1f0sKCE/9LW6SEnwI/bWv9eo\n\
137+
nom5vOPrvQeJiYCQk+2DyWo8RdSxINtY+G9bPE4RXm+6ZgcXECPm9TYDIWpL36fC\n\
138+
jvtGLs98woWFElOziBMp5Tb630GMcSI+q5ivHfJ3WS5NKLYLHBNK4iSFN0/dgAnB\n\
139+
dj6GcKXKWnIBWn6ZM4o40pcM5KSRUUCLtA0ZmjJH4c4zx3X5fUxd+enwkf3e9VZO\n\
140+
fNKC/+xfq6NfoPUPK9+UnchHpJaJw7RG5tZS+sWCz2xpQ1y3/o49xImNyM3wnpvB\n\
141+
cRAZabqIHpZa9/DIUkELOtCzln6niqkjRgg3M/YCCNznwV+0RNgz87VtyTPerdef\n\
142+
xrqn0+ROMF6ebVqIs6PPtuPkxnAJu7TMKXVB5rFnAewS24e6cIGFzeIA7810py3l\n\
143+
cttVRsdOoego+fiy08eFE+aJIeYiINRGhqOBTsuqG4jIdpdKxPE=\n\
144+
=KbsY\n\
145+
-----END PGP SIGNATURE-----\n";
146+
147+
rebase_opts.signature_cb = signature_cb_gpg;
148+
if (use_passthrough)
149+
rebase_opts.signature_field_cb = signature_field_cb_passthrough;
150+
151+
cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
152+
cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
153+
154+
cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
155+
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
156+
157+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
158+
159+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
160+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
161+
162+
git_oid_fromstr(&expected_id, "bf78348e45c8286f52b760f1db15cb6da030f2ef");
163+
cl_assert_equal_oid(&expected_id, &commit_id);
164+
165+
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
166+
cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
167+
168+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
169+
cl_assert_equal_i(GIT_ITEROVER, error);
170+
171+
git_reference_free(branch_ref);
172+
git_reference_free(upstream_ref);
173+
git_annotated_commit_free(branch_head);
174+
git_annotated_commit_free(upstream_head);
175+
git_commit_free(commit);
176+
git_rebase_free(rebase);
177+
}
178+
179+
/* git checkout gravy ; git rebase --merge veal */
180+
void test_rebase_sign__gpg_with_no_field_cb(void)
181+
{
182+
test_gpg_signature(false);
183+
}
184+
185+
/* git checkout gravy ; git rebase --merge veal */
186+
void test_rebase_sign__gpg_with_passthrough_field_cb(void)
187+
{
188+
test_gpg_signature(true);
189+
}
190+
191+
int signature_cb_magic_field(char **signature, const char *commit_content, void *payload)
192+
{
193+
cl_assert_equal_s(expected_commit_content, commit_content);
194+
cl_assert_equal_p(NULL, payload);
195+
*signature = strdup("magic word: pretty please");
196+
return GIT_OK;
197+
}
198+
199+
int signature_field_cb_magic_field(char **signature_field, void *payload)
200+
{
201+
cl_assert_equal_p(NULL, payload);
202+
*signature_field = strdup("magicsig");
203+
return GIT_OK;
204+
}
205+
206+
/* git checkout gravy ; git rebase --merge veal */
207+
void test_rebase_sign__custom_signature_field(void)
208+
{
209+
git_rebase *rebase;
210+
git_reference *branch_ref, *upstream_ref;
211+
git_annotated_commit *branch_head, *upstream_head;
212+
git_rebase_operation *rebase_operation;
213+
git_oid commit_id, expected_id;
214+
git_rebase_options rebase_opts = GIT_REBASE_OPTIONS_INIT;
215+
git_commit *commit;
216+
int error;
217+
const char *expected_commit_raw_header = "tree cd99b26250099fc38d30bfaed7797a7275ed3366\n\
218+
parent f87d14a4a236582a0278a916340a793714256864\n\
219+
author Edward Thomson <ethomson@edwardthomson.com> 1405625055 -0400\n\
220+
committer Rebaser <rebaser@rebaser.rb> 1405694510 +0000\n\
221+
magicsig magic word: pretty please\n";
222+
223+
rebase_opts.signature_cb = signature_cb_magic_field;
224+
rebase_opts.signature_field_cb = signature_field_cb_magic_field;
225+
226+
cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/gravy"));
227+
cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/veal"));
228+
229+
cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
230+
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
231+
232+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, &rebase_opts));
233+
234+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
235+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL));
236+
237+
git_oid_fromstr(&expected_id, "f46a4a8d26ae411b02aa61b7d69576627f4a1e1c");
238+
cl_assert_equal_oid(&expected_id, &commit_id);
239+
240+
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
241+
cl_assert_equal_s(expected_commit_raw_header, git_commit_raw_header(commit));
242+
243+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
244+
cl_assert_equal_i(GIT_ITEROVER, error);
245+
246+
git_reference_free(branch_ref);
247+
git_reference_free(upstream_ref);
248+
git_annotated_commit_free(branch_head);
249+
git_annotated_commit_free(upstream_head);
250+
git_commit_free(commit);
251+
git_rebase_free(rebase);
252+
}

0 commit comments

Comments
 (0)