| 1 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 2 | |
| 3 | #include "unit-test.h" |
| 4 | #include "trailer.h" |
| 5 | |
| 6 | struct contents { |
| 7 | const char *raw; |
| 8 | const char *key; |
| 9 | const char *val; |
| 10 | }; |
| 11 | |
| 12 | static void t_trailer_iterator(const char *msg, size_t num_expected, |
| 13 | struct contents *contents) |
| 14 | { |
| 15 | struct trailer_iterator iter; |
| 16 | size_t i = 0; |
| 17 | |
| 18 | trailer_iterator_init(&iter, msg); |
| 19 | while (trailer_iterator_advance(&iter)) { |
| 20 | if (num_expected) { |
| 21 | cl_assert_equal_s(iter.raw, contents[i].raw); |
| 22 | cl_assert_equal_s(iter.key.buf, contents[i].key); |
| 23 | cl_assert_equal_s(iter.val.buf, contents[i].val); |
| 24 | } |
| 25 | i++; |
| 26 | } |
| 27 | trailer_iterator_release(&iter); |
| 28 | |
| 29 | cl_assert_equal_i(i, num_expected); |
| 30 | } |
| 31 | |
| 32 | void test_trailer__empty_input(void) |
| 33 | { |
| 34 | struct contents expected_contents[] = { 0 }; |
| 35 | t_trailer_iterator("", 0, expected_contents); |
| 36 | } |
| 37 | |
| 38 | void test_trailer__no_newline_start(void) |
| 39 | { |
| 40 | struct contents expected_contents[] = { 0 }; |
| 41 | |
| 42 | t_trailer_iterator("Fixes: x\n" |
| 43 | "Acked-by: x\n" |
| 44 | "Reviewed-by: x\n", |
| 45 | 0, |
| 46 | expected_contents); |
| 47 | } |
| 48 | |
| 49 | void test_trailer__newline_start(void) |
| 50 | { |
| 51 | struct contents expected_contents[] = { |
| 52 | { |
| 53 | .raw = "Fixes: x\n", |
| 54 | .key = "Fixes", |
| 55 | .val = "x", |
| 56 | }, |
| 57 | { |
| 58 | .raw = "Acked-by: x\n", |
| 59 | .key = "Acked-by", |
| 60 | .val = "x", |
| 61 | }, |
| 62 | { |
| 63 | .raw = "Reviewed-by: x\n", |
| 64 | .key = "Reviewed-by", |
| 65 | .val = "x", |
| 66 | }, |
| 67 | { |
| 68 | 0 |
| 69 | }, |
| 70 | }; |
| 71 | |
| 72 | t_trailer_iterator("\n" |
| 73 | "Fixes: x\n" |
| 74 | "Acked-by: x\n" |
| 75 | "Reviewed-by: x\n", |
| 76 | 3, |
| 77 | expected_contents); |
| 78 | } |
| 79 | |
| 80 | void test_trailer__no_body_text(void) |
| 81 | { |
| 82 | struct contents expected_contents[] = { |
| 83 | |
| 84 | { |
| 85 | .raw = "Fixes: x\n", |
| 86 | .key = "Fixes", |
| 87 | .val = "x", |
| 88 | }, |
| 89 | { |
| 90 | .raw = "Acked-by: x\n", |
| 91 | .key = "Acked-by", |
| 92 | .val = "x", |
| 93 | }, |
| 94 | { |
| 95 | .raw = "Reviewed-by: x\n", |
| 96 | .key = "Reviewed-by", |
| 97 | .val = "x", |
| 98 | }, |
| 99 | { |
| 100 | 0 |
| 101 | }, |
| 102 | }; |
| 103 | |
| 104 | t_trailer_iterator("subject: foo bar\n" |
| 105 | "\n" |
| 106 | "Fixes: x\n" |
| 107 | "Acked-by: x\n" |
| 108 | "Reviewed-by: x\n", |
| 109 | 3, |
| 110 | expected_contents); |
| 111 | } |
| 112 | |
| 113 | void test_trailer__body_text_no_divider(void) |
| 114 | { |
| 115 | struct contents expected_contents[] = { |
| 116 | { |
| 117 | .raw = "Fixes: x\n", |
| 118 | .key = "Fixes", |
| 119 | .val = "x", |
| 120 | }, |
| 121 | { |
| 122 | .raw = "Acked-by: x\n", |
| 123 | .key = "Acked-by", |
| 124 | .val = "x", |
| 125 | }, |
| 126 | { |
| 127 | .raw = "Reviewed-by: x\n", |
| 128 | .key = "Reviewed-by", |
| 129 | .val = "x", |
| 130 | }, |
| 131 | { |
| 132 | .raw = "Signed-off-by: x\n", |
| 133 | .key = "Signed-off-by", |
| 134 | .val = "x", |
| 135 | }, |
| 136 | { |
| 137 | 0 |
| 138 | }, |
| 139 | }; |
| 140 | |
| 141 | t_trailer_iterator("my subject\n" |
| 142 | "\n" |
| 143 | "my body which is long\n" |
| 144 | "and contains some special\n" |
| 145 | "chars like : = ? !\n" |
| 146 | "hello\n" |
| 147 | "\n" |
| 148 | "Fixes: x\n" |
| 149 | "Acked-by: x\n" |
| 150 | "Reviewed-by: x\n" |
| 151 | "Signed-off-by: x\n", |
| 152 | 4, |
| 153 | expected_contents); |
| 154 | } |
| 155 | |
| 156 | void test_trailer__body_no_divider_2nd_block(void) |
| 157 | { |
| 158 | struct contents expected_contents[] = { |
| 159 | { |
| 160 | .raw = "Helped-by: x\n", |
| 161 | .key = "Helped-by", |
| 162 | .val = "x", |
| 163 | }, |
| 164 | { |
| 165 | .raw = "Signed-off-by: x\n", |
| 166 | .key = "Signed-off-by", |
| 167 | .val = "x", |
| 168 | }, |
| 169 | { |
| 170 | 0 |
| 171 | }, |
| 172 | }; |
| 173 | |
| 174 | t_trailer_iterator("my subject\n" |
| 175 | "\n" |
| 176 | "my body which is long\n" |
| 177 | "and contains some special\n" |
| 178 | "chars like : = ? !\n" |
| 179 | "hello\n" |
| 180 | "\n" |
| 181 | "Fixes: x\n" |
| 182 | "Acked-by: x\n" |
| 183 | "Reviewed-by: x\n" |
| 184 | "Signed-off-by: x\n" |
| 185 | "\n" |
| 186 | /* |
| 187 | * Because this is the last trailer block, it takes |
| 188 | * precedence over the first one encountered above. |
| 189 | */ |
| 190 | "Helped-by: x\n" |
| 191 | "Signed-off-by: x\n", |
| 192 | 2, |
| 193 | expected_contents); |
| 194 | } |
| 195 | |
| 196 | void test_trailer__body_and_divider(void) |
| 197 | { |
| 198 | struct contents expected_contents[] = { |
| 199 | { |
| 200 | .raw = "Signed-off-by: x\n", |
| 201 | .key = "Signed-off-by", |
| 202 | .val = "x", |
| 203 | }, |
| 204 | { |
| 205 | 0 |
| 206 | }, |
| 207 | }; |
| 208 | |
| 209 | t_trailer_iterator("my subject\n" |
| 210 | "\n" |
| 211 | "my body which is long\n" |
| 212 | "and contains some special\n" |
| 213 | "chars like : = ? !\n" |
| 214 | "hello\n" |
| 215 | "\n" |
| 216 | "---\n" |
| 217 | "\n" |
| 218 | /* |
| 219 | * This trailer still counts because the iterator |
| 220 | * always ignores the divider. |
| 221 | */ |
| 222 | "Signed-off-by: x\n", |
| 223 | 1, |
| 224 | expected_contents); |
| 225 | } |
| 226 | |
| 227 | void test_trailer__non_trailer_in_block(void) |
| 228 | { |
| 229 | struct contents expected_contents[] = { |
| 230 | { |
| 231 | .raw = "not a trailer line\n", |
| 232 | .key = "not a trailer line", |
| 233 | .val = "", |
| 234 | }, |
| 235 | { |
| 236 | .raw = "not a trailer line\n", |
| 237 | .key = "not a trailer line", |
| 238 | .val = "", |
| 239 | }, |
| 240 | { |
| 241 | .raw = "not a trailer line\n", |
| 242 | .key = "not a trailer line", |
| 243 | .val = "", |
| 244 | }, |
| 245 | { |
| 246 | .raw = "Signed-off-by: x\n", |
| 247 | .key = "Signed-off-by", |
| 248 | .val = "x", |
| 249 | }, |
| 250 | { |
| 251 | 0 |
| 252 | }, |
| 253 | }; |
| 254 | |
| 255 | t_trailer_iterator("subject: foo bar\n" |
| 256 | "\n" |
| 257 | /* |
| 258 | * Even though this trailer block has a non-trailer line |
| 259 | * in it, it's still a valid trailer block because it's |
| 260 | * at least 25% trailers and is Git-generated (see |
| 261 | * git_generated_prefixes[] in trailer.c). |
| 262 | */ |
| 263 | "not a trailer line\n" |
| 264 | "not a trailer line\n" |
| 265 | "not a trailer line\n" |
| 266 | "Signed-off-by: x\n", |
| 267 | /* |
| 268 | * Even though there is only really 1 real "trailer" |
| 269 | * (Signed-off-by), we still have 4 trailer objects |
| 270 | * because we still want to iterate through the entire |
| 271 | * block. |
| 272 | */ |
| 273 | 4, |
| 274 | expected_contents); |
| 275 | } |
| 276 | |
| 277 | void test_trailer__too_many_non_trailers(void) |
| 278 | { |
| 279 | struct contents expected_contents[] = { 0 }; |
| 280 | |
| 281 | t_trailer_iterator("subject: foo bar\n" |
| 282 | "\n" |
| 283 | /* |
| 284 | * This block has only 20% trailers, so it's below the |
| 285 | * 25% threshold. |
| 286 | */ |
| 287 | "not a trailer line\n" |
| 288 | "not a trailer line\n" |
| 289 | "not a trailer line\n" |
| 290 | "not a trailer line\n" |
| 291 | "Signed-off-by: x\n", |
| 292 | 0, |
| 293 | expected_contents); |
| 294 | } |
| 295 | |
| 296 | void test_trailer__one_non_trailer_no_git_trailers(void) |
| 297 | { |
| 298 | struct contents expected_contents[] = { 0 }; |
| 299 | |
| 300 | t_trailer_iterator("subject: foo bar\n" |
| 301 | "\n" |
| 302 | /* |
| 303 | * This block has only 1 non-trailer out of 10 (IOW, 90% |
| 304 | * trailers) but is not considered a trailer block |
| 305 | * because the 25% threshold only applies to cases where |
| 306 | * there was a Git-generated trailer. |
| 307 | */ |
| 308 | "Reviewed-by: x\n" |
| 309 | "Reviewed-by: x\n" |
| 310 | "Reviewed-by: x\n" |
| 311 | "Helped-by: x\n" |
| 312 | "Helped-by: x\n" |
| 313 | "Helped-by: x\n" |
| 314 | "Acked-by: x\n" |
| 315 | "Acked-by: x\n" |
| 316 | "Acked-by: x\n" |
| 317 | "not a trailer line\n", |
| 318 | 0, |
| 319 | expected_contents); |
| 320 | } |