test_utils_insertChars.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_utils_insertChars.c
  10. * @brief unittest for shellmatta utils_insertChars
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. #include "src/shellmatta_utils.c"
  15. #include <string.h>
  16. static uint32_t write_callCnt = 0u;
  17. static char write_data[20];
  18. static uint32_t write_idx;
  19. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  20. {
  21. write_callCnt ++;
  22. strncpy(&write_data[write_idx], data, length);
  23. write_idx += length;
  24. return SHELLMATTA_OK;
  25. }
  26. TEST_CASE( "shellmatta_insertChars normal call" ) {
  27. shellmatta_instance_t inst;
  28. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  29. memset(&inst, 0, sizeof(inst));
  30. inst.buffer = buffer;
  31. inst.bufferSize = 20;
  32. inst.cursor = 8;
  33. inst.inputCount = 10;
  34. inst.echoEnabled = true;
  35. inst.write = writeFct;
  36. write_callCnt = 0u;
  37. memset(write_data, 0, sizeof(write_data));
  38. write_idx = 0u;
  39. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  40. CHECK( inst.cursor == 12);
  41. CHECK( inst.inputCount == 14);
  42. CHECK( write_callCnt == 5u );
  43. CHECK( strncmp("blks\x1b[K\x1b[sij\x1b[u", write_data, 15u) == 0);
  44. REQUIRE( strncmp("abcdefghblksij\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  45. }
  46. TEST_CASE( "shellmatta_insertChars overwrite" ) {
  47. shellmatta_instance_t inst;
  48. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  49. memset(&inst, 0, sizeof(inst));
  50. inst.buffer = buffer;
  51. inst.bufferSize = 20;
  52. inst.cursor = 8;
  53. inst.inputCount = 10;
  54. inst.echoEnabled = true;
  55. inst.mode = SHELLMATTA_MODE_OVERWRITE;
  56. inst.write = writeFct;
  57. write_callCnt = 0u;
  58. memset(write_data, 0, sizeof(write_data));
  59. write_idx = 0u;
  60. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  61. CHECK( inst.cursor == 12);
  62. CHECK( inst.inputCount == 12);
  63. CHECK( write_callCnt == 1u );
  64. CHECK( strncmp("blks", write_data, 5u) == 0);
  65. REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  66. }
  67. TEST_CASE( "shellmatta_insertChars append" ) {
  68. shellmatta_instance_t inst;
  69. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  70. memset(&inst, 0, sizeof(inst));
  71. inst.buffer = buffer;
  72. inst.bufferSize = 20;
  73. inst.cursor = 10;
  74. inst.inputCount = 10;
  75. inst.echoEnabled = true;
  76. inst.mode = SHELLMATTA_MODE_INSERT;
  77. inst.write = writeFct;
  78. write_callCnt = 0u;
  79. memset(write_data, 0, sizeof(write_data));
  80. write_idx = 0u;
  81. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
  82. CHECK( inst.cursor == 14);
  83. CHECK( inst.inputCount == 14);
  84. CHECK( write_callCnt == 1u );
  85. CHECK( strncmp("blks", write_data, 5u) == 0);
  86. REQUIRE( strncmp("abcdefghijblks\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  87. }
  88. TEST_CASE( "shellmatta_insertChars 0 length" ) {
  89. shellmatta_instance_t inst;
  90. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  91. memset(&inst, 0, sizeof(inst));
  92. inst.buffer = buffer;
  93. inst.bufferSize = 20;
  94. inst.cursor = 8;
  95. inst.inputCount = 10;
  96. inst.echoEnabled = true;
  97. inst.write = writeFct;
  98. write_callCnt = 0u;
  99. memset(write_data, 0, sizeof(write_data));
  100. write_idx = 0u;
  101. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u);
  102. CHECK( inst.cursor == 8u );
  103. CHECK( inst.inputCount == 10u );
  104. CHECK( write_callCnt == 0u );
  105. 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 );
  106. REQUIRE( memcmp("abcdefghij\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
  107. }
  108. TEST_CASE( "shellmatta_insertChars buffer full" ) {
  109. shellmatta_instance_t inst;
  110. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  111. memset(&inst, 0, sizeof(inst));
  112. inst.buffer = buffer;
  113. inst.bufferSize = 20;
  114. inst.cursor = 8;
  115. inst.inputCount = 10;
  116. inst.echoEnabled = true;
  117. inst.write = writeFct;
  118. write_callCnt = 0u;
  119. memset(write_data, 0, sizeof(write_data));
  120. write_idx = 0u;
  121. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 10u);
  122. CHECK( inst.cursor == 18u );
  123. CHECK( inst.inputCount == 20u );
  124. CHECK( write_callCnt == 5u );
  125. CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
  126. REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
  127. }
  128. TEST_CASE( "shellmatta_insertChars buffer overflow by 1" ) {
  129. shellmatta_instance_t inst;
  130. char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
  131. memset(&inst, 0, sizeof(inst));
  132. inst.buffer = buffer;
  133. inst.bufferSize = 20;
  134. inst.cursor = 8;
  135. inst.inputCount = 10;
  136. inst.echoEnabled = true;
  137. inst.write = writeFct;
  138. write_callCnt = 0u;
  139. memset(write_data, 0, sizeof(write_data));
  140. write_idx = 0u;
  141. utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 11u);
  142. CHECK( inst.cursor == 18u );
  143. CHECK( inst.inputCount == 20u );
  144. CHECK( write_callCnt == 5u );
  145. CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
  146. REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
  147. }