test_opt_findNextHunk.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_opt_findNextHunk.c
  10. * @brief unittest for shellmatta opt_findNextHunk
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. #include "src/shellmatta_opt.c"
  15. #include <string.h>
  16. TEST_CASE( "shellmatta_opt findNextHunk easy" ) {
  17. shellmatta_retCode_t ret = SHELLMATTA_OK;
  18. shellmatta_instance_t inst;
  19. char *dummyData = (char*) "This is Sparta";
  20. char buffer[1024u];
  21. memcpy(buffer, dummyData, strlen(dummyData));
  22. inst.buffer = buffer;
  23. inst.bufferSize = sizeof(buffer);
  24. inst.inputCount = 14;
  25. inst.optionParser.nextOffset = 4u;
  26. ret = findNextHunk(&inst);
  27. CHECK( ret == SHELLMATTA_OK );
  28. CHECK( inst.optionParser.offset == 5u);
  29. CHECK( inst.optionParser.len == 2u);
  30. ret = findNextHunk(&inst);
  31. CHECK( ret == SHELLMATTA_OK );
  32. CHECK( inst.optionParser.offset == 8u);
  33. CHECK( inst.optionParser.len == 6u);
  34. ret = findNextHunk(&inst);
  35. REQUIRE( ret == SHELLMATTA_ERROR );
  36. }
  37. TEST_CASE( "shellmatta_opt findNextHunk quotation 1" ) {
  38. shellmatta_retCode_t ret = SHELLMATTA_OK;
  39. shellmatta_instance_t inst;
  40. char *dummyData = (char*) "This is Sparta \"argument with spaces\"";
  41. char buffer[1024u];
  42. memcpy(buffer, dummyData, strlen(dummyData));
  43. inst.buffer = buffer;
  44. inst.bufferSize = sizeof(buffer);
  45. inst.inputCount = strlen(dummyData);
  46. inst.optionParser.nextOffset = 4u;
  47. ret = findNextHunk(&inst);
  48. CHECK( ret == SHELLMATTA_OK );
  49. CHECK( inst.optionParser.offset == 5u);
  50. CHECK( inst.optionParser.len == 2u);
  51. ret = findNextHunk(&inst);
  52. CHECK( ret == SHELLMATTA_OK );
  53. CHECK( inst.optionParser.offset == 8u);
  54. CHECK( inst.optionParser.len == 6u);
  55. ret = findNextHunk(&inst);
  56. CHECK( ret == SHELLMATTA_OK );
  57. CHECK( inst.optionParser.offset == 15u);
  58. CHECK( inst.optionParser.len == 20u);
  59. CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with spaces", 20));
  60. ret = findNextHunk(&inst);
  61. REQUIRE( ret == SHELLMATTA_ERROR );
  62. }
  63. TEST_CASE( "shellmatta_opt findNextHunk quotation 2" ) {
  64. shellmatta_retCode_t ret = SHELLMATTA_OK;
  65. shellmatta_instance_t inst;
  66. char *dummyData = (char*) "This is Sparta 'argument with spaces'";
  67. char buffer[1024u];
  68. memcpy(buffer, dummyData, strlen(dummyData));
  69. inst.buffer = buffer;
  70. inst.bufferSize = sizeof(buffer);
  71. inst.inputCount = strlen(dummyData);
  72. inst.optionParser.nextOffset = 4u;
  73. ret = findNextHunk(&inst);
  74. CHECK( ret == SHELLMATTA_OK );
  75. CHECK( inst.optionParser.offset == 5u);
  76. CHECK( inst.optionParser.len == 2u);
  77. ret = findNextHunk(&inst);
  78. CHECK( ret == SHELLMATTA_OK );
  79. CHECK( inst.optionParser.offset == 8u);
  80. CHECK( inst.optionParser.len == 6u);
  81. ret = findNextHunk(&inst);
  82. CHECK( ret == SHELLMATTA_OK );
  83. CHECK( inst.optionParser.offset == 15u);
  84. CHECK( inst.optionParser.len == 20u);
  85. CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with spaces", 20));
  86. ret = findNextHunk(&inst);
  87. REQUIRE( ret == SHELLMATTA_ERROR );
  88. }
  89. TEST_CASE( "shellmatta_opt findNextHunk quotation escaped" ) {
  90. shellmatta_retCode_t ret = SHELLMATTA_OK;
  91. shellmatta_instance_t inst;
  92. char *dummyData = (char*) "This is Sparta \"argument with \\\"spaces\"";
  93. char buffer[1024u];
  94. memcpy(buffer, dummyData, strlen(dummyData));
  95. inst.buffer = buffer;
  96. inst.bufferSize = sizeof(buffer);
  97. inst.inputCount = strlen(dummyData);
  98. inst.optionParser.nextOffset = 4u;
  99. ret = findNextHunk(&inst);
  100. CHECK( ret == SHELLMATTA_OK );
  101. CHECK( inst.optionParser.offset == 5u);
  102. CHECK( inst.optionParser.len == 2u);
  103. ret = findNextHunk(&inst);
  104. CHECK( ret == SHELLMATTA_OK );
  105. CHECK( inst.optionParser.offset == 8u);
  106. CHECK( inst.optionParser.len == 6u);
  107. ret = findNextHunk(&inst);
  108. CHECK( ret == SHELLMATTA_OK );
  109. CHECK( inst.optionParser.offset == 15u);
  110. CHECK( inst.optionParser.len == 21u);
  111. CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with \"spaces", 21));
  112. ret = findNextHunk(&inst);
  113. REQUIRE( ret == SHELLMATTA_ERROR );
  114. }
  115. TEST_CASE( "shellmatta_opt findNextHunk quotation missing closing quotation" ) {
  116. shellmatta_retCode_t ret = SHELLMATTA_OK;
  117. shellmatta_instance_t inst;
  118. char *dummyData = (char*) "This is Sparta \"argument with \\\"spaces";
  119. char buffer[1024u];
  120. memcpy(buffer, dummyData, strlen(dummyData));
  121. inst.buffer = buffer;
  122. inst.bufferSize = sizeof(buffer);
  123. inst.inputCount = strlen(dummyData);
  124. inst.optionParser.nextOffset = 4u;
  125. ret = findNextHunk(&inst);
  126. CHECK( ret == SHELLMATTA_OK );
  127. CHECK( inst.optionParser.offset == 5u);
  128. CHECK( inst.optionParser.len == 2u);
  129. ret = findNextHunk(&inst);
  130. CHECK( ret == SHELLMATTA_OK );
  131. CHECK( inst.optionParser.offset == 8u);
  132. CHECK( inst.optionParser.len == 6u);
  133. ret = findNextHunk(&inst);
  134. REQUIRE( ret == SHELLMATTA_ERROR );
  135. }