| 1 | #include <cgreen/constraint.h> |
| 2 | #include <cgreen/message_formatting.h> |
| 3 | #include <cgreen/string_comparison.h> |
| 4 | #include <inttypes.h> |
| 5 | #ifndef __cplusplus |
| 6 | #include <stdbool.h> |
| 7 | #endif |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #ifdef __ANDROID__ |
| 13 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 14 | #endif // #ifdef __ANDROID__ |
| 15 | |
| 16 | #ifdef _MSC_VER |
| 17 | #include "wincompat.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "constraint_internal.h" |
| 21 | |
| 22 | |
| 23 | // Handling of percent signs |
| 24 | static const char *next_percent_sign(const char *s) { |
| 25 | return strchr(s, '%'); |
| 26 | } |
| 27 | |
| 28 | static size_t count_percent_signs(char const *s) |
| 29 | { |
| 30 | size_t count = 0; |
| 31 | char const *p = next_percent_sign(s); |
| 32 | while (p != NULL) { |
| 33 | count++; |
| 34 | p = next_percent_sign(p+1); |
| 35 | } |
| 36 | return count; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | static char *copy_while_doubling_percent_signs(char *string, char const *original) |
| 41 | { |
| 42 | char *destination = string; |
| 43 | const char *source = original; |
| 44 | const char *next = next_percent_sign(source); |
| 45 | for (; next != NULL; next = next_percent_sign(next+1)) { |
| 46 | size_t len = next - source + 1; |
| 47 | memcpy(destination, source, len); |
| 48 | destination += len; |
| 49 | *destination++ = '%'; |
| 50 | source = next+1; |
| 51 | } |
| 52 | strcpy(destination, source); |
| 53 | |
| 54 | return string; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static char *double_all_percent_signs_in(const char *original) |
| 59 | { |
| 60 | size_t percent_count = count_percent_signs(original); |
| 61 | char *new_string = (char *)malloc(strlen(original) + percent_count + 1); |
| 62 | if (new_string == NULL) { |
| 63 | return NULL; |
| 64 | } |
| 65 | copy_while_doubling_percent_signs(new_string, original); |
| 66 | return new_string; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static int find_index_of_difference(void *expected, void *actual, size_t size_to_compare) { |
| 71 | char *expectedp = expected; |
| 72 | char *actualp = actual; |
| 73 | |
| 74 | while (size_to_compare--) { |
| 75 | if (*expectedp++ != *actualp++) { |
| 76 | return (int)((void *)actualp - actual)-1; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | static bool actual_value_not_necessary_for(Constraint *constraint, const char *actual_string, const char *actual_value_string) { |
| 84 | (void)constraint; // UNUSED! |
| 85 | return strings_are_equal(actual_string, actual_value_string) || |
| 86 | strings_are_equal(actual_string, "true") || |
| 87 | strings_are_equal(actual_string, "false"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | bool parameters_are_not_valid_for(Constraint *constraint, intptr_t actual) { |
| 92 | char *message = validation_failure_message_for(constraint, actual); |
| 93 | |
| 94 | bool not_valid = (strlen(message) > 0); |
| 95 | |
| 96 | free(message); |
| 97 | |
| 98 | return not_valid; |
| 99 | } |
| 100 | |
| 101 | char *validation_failure_message_for(Constraint *constraint, intptr_t actual) { |
| 102 | const char *name_has_incorrect_size_message = |
| 103 | "Wanted to compare contents with [%s],\n" |
| 104 | "\t\tbut [%ld] was given for the comparison size."; |
| 105 | const char *null_used_for_compare_message = |
| 106 | "Wanted to compare contents with [%s],\n" |
| 107 | "\t\tbut NULL was used for the pointer we wanted to compare to.\n" |
| 108 | "\t\tIf you want to explicitly check for null, use the is_null constraint instead."; |
| 109 | const char *null_used_for_actual_message = |
| 110 | "Wanted to compare contents of [%s] but it had a value of NULL.\n" |
| 111 | "\t\tIf you want to explicitly check for null, use the is_null constraint instead."; |
| 112 | |
| 113 | |
| 114 | |
| 115 | size_t message_size = strlen(name_has_incorrect_size_message) + |
| 116 | strlen(null_used_for_compare_message) + |
| 117 | strlen(null_used_for_actual_message) + |
| 118 | 512; // just in case |
| 119 | |
| 120 | char *message = (char *)malloc(message_size); |
| 121 | memset(message, 0, message_size); |
| 122 | |
| 123 | // (void)function; // UNUSED! |
| 124 | // if (function != NULL && strlen(function) > 0) { |
| 125 | // snprintf(message, message_size - 1, "\tIn mocked function [%s]:\n", function); |
| 126 | // } else { |
| 127 | // snprintf(message, message_size - 1, "\tIn assertion:\n"); |
| 128 | // } |
| 129 | |
| 130 | if (is_content_comparing(constraint)) { |
| 131 | const char *as_string; |
| 132 | if (constraint->parameter_name != NULL) { |
| 133 | as_string = constraint->parameter_name; |
| 134 | } else { |
| 135 | as_string = constraint->expected_value_name; |
| 136 | } |
| 137 | |
| 138 | if ((long signed)constraint->size_of_expected_value <= 0) { |
| 139 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 140 | name_has_incorrect_size_message, |
| 141 | as_string, |
| 142 | (long signed)constraint->size_of_expected_value); |
| 143 | |
| 144 | return message; |
| 145 | } |
| 146 | |
| 147 | if ((void *)actual == NULL) { |
| 148 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 149 | null_used_for_compare_message, |
| 150 | as_string); |
| 151 | |
| 152 | return message; |
| 153 | } |
| 154 | |
| 155 | if (constraint->expected_value.value.pointer_value == NULL) { |
| 156 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 157 | null_used_for_actual_message, |
| 158 | as_string); |
| 159 | |
| 160 | return message; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return message; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static bool is_equal_to_string_constraint(Constraint *constraint) { |
| 169 | return strstr(constraint->name, "not ") == NULL || strstr(constraint->name, "equal ") == NULL; |
| 170 | } |
| 171 | |
| 172 | /* Formats for printing actual and expected values */ |
| 173 | static const char actual_value_string_format[] = "\n\t\tactual value:\t\t\t[\"%s\"]"; |
| 174 | static const char expected_value_string_format[] = "[%s]"; |
| 175 | static const char constraint_as_string_format[] = "Expected [%s] to [%s]"; |
| 176 | static const char at_offset[] = "\n\t\tat offset:\t\t\t[%d]"; |
| 177 | static const char expected_content[] = "\n\t\t\tactual value:\t\t[0x%02x]\n\t\t\texpected value:\t\t[0x%02x]"; |
| 178 | |
| 179 | |
| 180 | static void format_actual_string_value(intptr_t actual_value, char *message, size_t message_size) { |
| 181 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 182 | actual_value_string_format, |
| 183 | (const char *)actual_value); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void format_expected_string_value(Constraint *constraint, char *message, size_t message_size) { |
| 188 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 189 | constraint->expected_value_message, |
| 190 | constraint->expected_value.value.string_value); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | char *failure_message_for(Constraint *constraint, const char *actual_string, intptr_t actual_value) { |
| 195 | char actual_int_value_string[32]; |
| 196 | const char *actual_value_as_string; |
| 197 | char *message; |
| 198 | size_t message_size = strlen(constraint_as_string_format) + |
| 199 | strlen(expected_value_string_format) + |
| 200 | strlen(actual_value_string_format) + |
| 201 | strlen(at_offset) + |
| 202 | strlen(constraint->actual_value_message) + |
| 203 | strlen(constraint->expected_value_message) + |
| 204 | strlen(constraint->expected_value_name) + |
| 205 | strlen(constraint->name) + |
| 206 | strlen(actual_string) + |
| 207 | 512; // just in case |
| 208 | |
| 209 | snprintf(actual_int_value_string, sizeof(actual_int_value_string) - 1, "%" PRIdPTR, actual_value); |
| 210 | |
| 211 | if (values_are_strings_in(constraint)) { |
| 212 | message_size += strlen(constraint->expected_value.value.string_value); |
| 213 | if (actual_value != (intptr_t)NULL) { |
| 214 | message_size += strlen((char *)actual_value); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | message = (char *)malloc(message_size); |
| 219 | |
| 220 | /* if the actual value expression contains '%' we want it to survive the final expansion with |
| 221 | arguments that happens in assert_true() */ |
| 222 | actual_value_as_string = double_all_percent_signs_in(actual_string); |
| 223 | |
| 224 | /* expand the constraint with the actual value in string format... */ |
| 225 | snprintf(message, message_size - 1, |
| 226 | constraint_as_string_format, |
| 227 | actual_value_as_string, |
| 228 | constraint->name); |
| 229 | |
| 230 | free((void*)actual_value_as_string); |
| 231 | |
| 232 | if (no_expected_value_in(constraint)) { |
| 233 | return message; |
| 234 | } else |
| 235 | strcat(message, " "); |
| 236 | |
| 237 | /* expand the expected value string for all assertions that have one... */ |
| 238 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 239 | expected_value_string_format, |
| 240 | constraint->expected_value_name); |
| 241 | |
| 242 | if (actual_value_not_necessary_for(constraint, actual_string, actual_int_value_string)) { |
| 243 | /* when the actual string and the actual value are the same, don't print both of them */ |
| 244 | /* also, don't print "0" for false and "1" for true */ |
| 245 | /* also, don't print expected/actual for contents constraints since that is useless */ |
| 246 | return message; |
| 247 | } |
| 248 | |
| 249 | /* for string constraints, print out the strings encountered and not their pointer values */ |
| 250 | if (values_are_strings_in(constraint)) { |
| 251 | format_actual_string_value(actual_value, message, message_size); |
| 252 | if (is_equal_to_string_constraint(constraint)) { |
| 253 | strcat(message, "\n"); |
| 254 | format_expected_string_value(constraint, message, message_size); |
| 255 | } |
| 256 | /* The final string may have percent characters, so, since it is |
| 257 | later used in a (v)printf, we have to double them |
| 258 | */ |
| 259 | if (next_percent_sign(message) != NULL) { |
| 260 | char *message_with_doubled_percent_signs = double_all_percent_signs_in(message); |
| 261 | free(message); |
| 262 | message = message_with_doubled_percent_signs; |
| 263 | } |
| 264 | return message; |
| 265 | } |
| 266 | |
| 267 | /* show difference for contents as a position */ |
| 268 | if (is_content_comparing(constraint)) { |
| 269 | int difference_index = find_index_of_difference(constraint->expected_value.value.pointer_value, |
| 270 | (void *)actual_value, |
| 271 | constraint->size_of_expected_value); |
| 272 | if (difference_index != -1) { |
| 273 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 274 | at_offset, |
| 275 | difference_index); |
| 276 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 277 | expected_content, |
| 278 | ((char *)actual_value)[difference_index], |
| 279 | ((char *)constraint->expected_value.value.pointer_value)[difference_index]); |
| 280 | } |
| 281 | return message; |
| 282 | } |
| 283 | |
| 284 | /* add the actual value */ |
| 285 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 286 | constraint->actual_value_message, |
| 287 | actual_value); |
| 288 | |
| 289 | /* add the expected value */ |
| 290 | if (strstr(constraint->name, "not ") == NULL) { |
| 291 | strcat(message, "\n"); |
| 292 | snprintf(message + strlen(message), message_size - strlen(message) - 1, |
| 293 | constraint->expected_value_message, |
| 294 | constraint->expected_value.value.string_value); |
| 295 | } |
| 296 | |
| 297 | return message; |
| 298 | } |