1 #include "config.h" 2 3 #include "ntp_stdlib.h" 4 5 #include "unity.h" 6 7 #ifndef VSNPRINTF_PERCENT_M 8 // format_errmsg() is normally private to msyslog.c 9 void format_errmsg (char *, size_t, const char *, int); 10 #endif 11 12 13 14 void test_msnprintf(void) { 15 #define FMT_PREFIX "msyslog.cpp ENOENT: " 16 char exp_buf[512]; 17 char act_buf[512]; 18 int exp_cnt; 19 int act_cnt; 20 21 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), FMT_PREFIX "%s", 22 strerror(ENOENT)); 23 errno = ENOENT; 24 act_cnt = msnprintf(act_buf, sizeof(act_buf), FMT_PREFIX "%m"); 25 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 26 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 27 } 28 29 void 30 test_msnprintfLiteralPercentm(void) 31 { 32 char exp_buf[32]; 33 char act_buf[32]; 34 int exp_cnt; 35 int act_cnt; 36 37 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%%m"); 38 errno = ENOENT; 39 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%%m"); 40 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 41 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 42 } 43 44 void 45 test_msnprintfBackslashLiteralPercentm(void) { 46 char exp_buf[32]; 47 char act_buf[32]; 48 int exp_cnt; 49 int act_cnt; 50 51 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%%m"); 52 errno = ENOENT; 53 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%%m"); 54 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 55 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 56 } 57 58 void 59 test_msnprintfBackslashPercent(void) { 60 char exp_buf[32]; 61 char act_buf[32]; 62 int exp_cnt; 63 int act_cnt; 64 65 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "\%s", 66 strerror(ENOENT)); 67 errno = ENOENT; 68 act_cnt = msnprintf(act_buf, sizeof(act_buf), "\%m"); 69 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 70 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 71 } 72 73 void 74 test_msnprintfHangingPercent(void) { 75 static char fmt[] = "percent then nul term then non-nul %\0oops!"; 76 char exp_buf[64]; 77 char act_buf[64]; 78 int exp_cnt; 79 int act_cnt; 80 81 ZERO(exp_buf); 82 ZERO(act_buf); 83 exp_cnt = snprintf(exp_buf, sizeof(exp_buf), "%s", fmt); 84 act_cnt = msnprintf(act_buf, sizeof(act_buf), "%s", fmt); 85 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 86 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 87 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf)); 88 } 89 90 void 91 test_format_errmsgHangingPercent(void) { 92 #ifndef VSNPRINTF_PERCENT_M 93 static char fmt[] = "percent then nul term then non-nul %\0oops!"; 94 char act_buf[64]; 95 96 ZERO(act_buf); 97 format_errmsg(act_buf, sizeof(act_buf), fmt, ENOENT); 98 TEST_ASSERT_EQUAL_STRING(fmt, act_buf); 99 TEST_ASSERT_EQUAL_STRING("", act_buf + 1 + strlen(act_buf)); 100 #else 101 TEST_IGNORE_MESSAGE("VSNPRINTF_PERCENT_M is defined") 102 #endif 103 } 104 105 void 106 test_msnprintfNullTarget(void) { 107 int exp_cnt; 108 int act_cnt; 109 110 exp_cnt = snprintf(NULL, 0, "%d", 123); 111 errno = ENOENT; 112 act_cnt = msnprintf(NULL, 0, "%d", 123); 113 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 114 } 115 116 void 117 test_msnprintfTruncate(void) { 118 char undist[] = "undisturbed"; 119 char exp_buf[512]; 120 char act_buf[512]; 121 int exp_cnt; 122 int act_cnt; 123 124 memcpy(exp_buf + 3, undist, sizeof(undist)); 125 memcpy(act_buf + 3, undist, sizeof(undist)); 126 exp_cnt = snprintf(exp_buf, 3, "%s", strerror(ENOENT)); 127 errno = ENOENT; 128 act_cnt = msnprintf(act_buf, 3, "%m"); 129 TEST_ASSERT_EQUAL('\0', exp_buf[2]); 130 TEST_ASSERT_EQUAL('\0', act_buf[2]); 131 TEST_ASSERT_TRUE(act_cnt > 0); 132 TEST_ASSERT_EQUAL(exp_cnt, act_cnt); 133 TEST_ASSERT_EQUAL_STRING(exp_buf, act_buf); 134 TEST_ASSERT_EQUAL_STRING(exp_buf + 3, undist); 135 TEST_ASSERT_EQUAL_STRING(act_buf + 3, undist); 136 } 137