test_opt_findNextHunk.cpp 4.8 KB

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