test_utils_insertChars.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "test/framework/catch.hpp"
  2. #include "src/shellmatta_utils.c"
  3. #include <string.h>
  4. static uint32_t write_callCnt = 0u;
  5. static char write_data[20];
  6. static uint32_t write_idx;
  7. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  8. {
  9. write_callCnt ++;
  10. strncpy(&write_data[write_idx], data, length);
  11. write_idx += length;
  12. return SHELLMATTA_OK;
  13. }
  14. TEST_CASE( "shellmatta_insertChars normal call" ) {
  15. shellmatta_instance_t inst;
  16. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  17. memset(&inst, 0, sizeof(inst));
  18. inst.buffer = buffer;
  19. inst.bufferSize = 20;
  20. inst.cursor = 8;
  21. inst.inputCount = 10;
  22. inst.echoEnabled = true;
  23. inst.write = writeFct;
  24. write_callCnt = 0u;
  25. memset(write_data, 0, sizeof(write_data));
  26. write_idx = 0u;
  27. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
  28. CHECK( inst.cursor == 12);
  29. CHECK( inst.inputCount == 14);
  30. CHECK( write_callCnt == 5u );
  31. CHECK( strncmp("blks\x1b[K\x1b[sij\x1b[u", write_data, 15u) == 0);
  32. REQUIRE( strncmp("abcdefghblksij\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  33. }
  34. TEST_CASE( "shellmatta_insertChars overwrite" ) {
  35. shellmatta_instance_t inst;
  36. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  37. memset(&inst, 0, sizeof(inst));
  38. inst.buffer = buffer;
  39. inst.bufferSize = 20;
  40. inst.cursor = 8;
  41. inst.inputCount = 10;
  42. inst.echoEnabled = true;
  43. inst.mode = SHELLMATTA_MODE_OVERWRITE;
  44. inst.write = writeFct;
  45. write_callCnt = 0u;
  46. memset(write_data, 0, sizeof(write_data));
  47. write_idx = 0u;
  48. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
  49. CHECK( inst.cursor == 12);
  50. CHECK( inst.inputCount == 12);
  51. CHECK( write_callCnt == 1u );
  52. CHECK( strncmp("blks", write_data, 5u) == 0);
  53. REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  54. }
  55. TEST_CASE( "shellmatta_insertChars append" ) {
  56. shellmatta_instance_t inst;
  57. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  58. memset(&inst, 0, sizeof(inst));
  59. inst.buffer = buffer;
  60. inst.bufferSize = 20;
  61. inst.cursor = 10;
  62. inst.inputCount = 10;
  63. inst.echoEnabled = true;
  64. inst.mode = SHELLMATTA_MODE_INSERT;
  65. inst.write = writeFct;
  66. write_callCnt = 0u;
  67. memset(write_data, 0, sizeof(write_data));
  68. write_idx = 0u;
  69. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u, false);
  70. CHECK( inst.cursor == 14);
  71. CHECK( inst.inputCount == 14);
  72. CHECK( write_callCnt == 1u );
  73. CHECK( strncmp("blks", write_data, 5u) == 0);
  74. REQUIRE( strncmp("abcdefghijblks\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  75. }
  76. TEST_CASE( "shellmatta_insertChars 0 length" ) {
  77. shellmatta_instance_t inst;
  78. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  79. memset(&inst, 0, sizeof(inst));
  80. inst.buffer = buffer;
  81. inst.bufferSize = 20;
  82. inst.cursor = 8;
  83. inst.inputCount = 10;
  84. inst.echoEnabled = true;
  85. inst.write = writeFct;
  86. write_callCnt = 0u;
  87. memset(write_data, 0, sizeof(write_data));
  88. write_idx = 0u;
  89. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u, false);
  90. CHECK( inst.cursor == 8u );
  91. CHECK( inst.inputCount == 10u );
  92. CHECK( write_callCnt == 0u );
  93. CHECK( memcmp("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", write_data, sizeof(write_data) ) == 0u );
  94. REQUIRE( memcmp("abcdefghij\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  95. }
  96. TEST_CASE( "shellmatta_insertChars buffer full" ) {
  97. shellmatta_instance_t inst;
  98. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  99. memset(&inst, 0, sizeof(inst));
  100. inst.buffer = buffer;
  101. inst.bufferSize = 20;
  102. inst.cursor = 8;
  103. inst.inputCount = 10;
  104. inst.echoEnabled = true;
  105. inst.write = writeFct;
  106. write_callCnt = 0u;
  107. memset(write_data, 0, sizeof(write_data));
  108. write_idx = 0u;
  109. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 10u, false);
  110. CHECK( inst.cursor == 18u );
  111. CHECK( inst.inputCount == 20u );
  112. CHECK( write_callCnt == 5u );
  113. CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
  114. REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
  115. }
  116. TEST_CASE( "shellmatta_insertChars buffer overflow by 1" ) {
  117. shellmatta_instance_t inst;
  118. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  119. memset(&inst, 0, sizeof(inst));
  120. inst.buffer = buffer;
  121. inst.bufferSize = 20;
  122. inst.cursor = 8;
  123. inst.inputCount = 10;
  124. inst.echoEnabled = true;
  125. inst.write = writeFct;
  126. write_callCnt = 0u;
  127. memset(write_data, 0, sizeof(write_data));
  128. write_idx = 0u;
  129. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 11u, false);
  130. CHECK( inst.cursor == 18u );
  131. CHECK( inst.inputCount == 20u );
  132. CHECK( write_callCnt == 5u );
  133. CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
  134. REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
  135. }