| 94 | } |
| 95 | |
| 96 | static void test_strncpy(void **state) |
| 97 | { |
| 98 | int i; |
| 99 | int n1 = 2, n2 = 8; |
| 100 | char src[] = "Hello"; |
| 101 | char dst[sizeof(src) + 5]; |
| 102 | size_t src_len = __builtin_strlen(src); |
| 103 | size_t dst_len = sizeof(dst); |
| 104 | |
| 105 | /* n1 case */ |
| 106 | |
| 107 | /* Needed for ensuring that characters behind the limit |
| 108 | are not overwritten */ |
| 109 | memset(dst, 'x', dst_len); |
| 110 | |
| 111 | strncpy(dst, src, n1); |
| 112 | |
| 113 | assert_int_equal(0, memcmp(dst, src, n1)); |
| 114 | |
| 115 | for (i = n1; i < dst_len; i++) |
| 116 | assert_true(dst[i] == 'x'); |
| 117 | |
| 118 | /* n2 case: */ |
| 119 | |
| 120 | memset(dst, 'x', dst_len); |
| 121 | |
| 122 | strncpy(dst, src, n2); |
| 123 | |
| 124 | assert_int_equal(0, memcmp(dst, src, src_len)); |
| 125 | |
| 126 | for (i = src_len; i < n2; i++) |
| 127 | assert_true(dst[i] == '\0'); |
| 128 | |
| 129 | for (i = n2; i < dst_len; i++) |
| 130 | assert_true(dst[i] == 'x'); |
| 131 | } |
| 132 | |
| 133 | static void test_strcpy(void **state) |
| 134 | { |