test_system.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* test_mean.c: Implementation of a testable component.
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <limits.h>
  8. #include "unity.h"
  9. #include "platform_console.h"
  10. #include "platform_esp32.h"
  11. #include "platform_config.h"
  12. #include "string.h"
  13. struct arg_lit *arglit;
  14. struct arg_int *argint;
  15. struct arg_str *argstr;
  16. struct arg_end *end;
  17. extern int is_output_gpio(struct arg_int * gpio, FILE * f, int * gpio_out, bool mandatory);
  18. extern void initialize_console();
  19. extern esp_err_t run_command(char * line);
  20. static char *buf = NULL;
  21. static char * s_tmp_line_buf=NULL;
  22. static size_t buf_size = 0;
  23. static FILE * f;
  24. static size_t argc=1;
  25. static char ** argv=NULL;
  26. static bool config_initialized=false;
  27. void init_console(){
  28. if(config_initialized) return;
  29. initialize_console();
  30. config_initialized=true;
  31. }
  32. /****************************************************************************************
  33. *
  34. */
  35. void open_mem_stream_file(){
  36. f = open_memstream(&buf, &buf_size);
  37. }
  38. /****************************************************************************************
  39. *
  40. */
  41. void close_flush_all(void * argtable, int count,bool print){
  42. fflush (f);
  43. if(print){
  44. printf("%s", buf);
  45. }
  46. fclose(f);
  47. free(buf);
  48. arg_freetable(argtable,count);
  49. free(argv);
  50. }
  51. /****************************************************************************************
  52. *
  53. */
  54. int alloc_split_command_line(char * cmdline){
  55. argv = (char **) calloc(22, sizeof(char *));
  56. if(!s_tmp_line_buf){
  57. s_tmp_line_buf= calloc(strlen(cmdline), 1);
  58. }
  59. strlcpy(s_tmp_line_buf, cmdline, 22);
  60. argc = esp_console_split_argv(s_tmp_line_buf, argv,22);
  61. return 0;
  62. }
  63. /****************************************************************************************
  64. *
  65. */
  66. int alloc_split_parse_command_line(char * cmdline, void ** args){
  67. alloc_split_command_line(cmdline);
  68. return arg_parse(argc, argv,args);
  69. }
  70. /****************************************************************************************
  71. *
  72. */
  73. TEST_CASE("Invalid GPIO detected", "[config][ui]")
  74. {
  75. char * cmdline = "test -i 55\n";
  76. void *argtable[] = {
  77. argint = arg_int1("i","int","<gpio>","GPIO number"),
  78. end = arg_end(6)
  79. };
  80. open_mem_stream_file();
  81. alloc_split_parse_command_line(cmdline, &argtable);
  82. int out_val = 0;
  83. TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Invalid GPIO not detected");
  84. TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
  85. fflush (f);
  86. TEST_ASSERT_EQUAL_STRING_MESSAGE("Invalid int gpio: [55] is not a GPIO\n",buf,"Invalid GPIO message wrong");
  87. close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
  88. }
  89. /****************************************************************************************
  90. *
  91. */
  92. TEST_CASE("Input Only GPIO detected", "[config][ui]")
  93. {
  94. char * cmdline = "test -i 35\n";
  95. void *argtable[] = {
  96. argint = arg_int1("i","int","<gpio>","GPIO number"),
  97. end = arg_end(6)
  98. };
  99. open_mem_stream_file();
  100. alloc_split_parse_command_line(cmdline, &argtable);
  101. int out_val = 0;
  102. TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Input only GPIO not detected");
  103. TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
  104. fflush (f);
  105. TEST_ASSERT_EQUAL_STRING_MESSAGE("Invalid int gpio: [35] has input capabilities only\n",buf,"Missing GPIO message wrong");
  106. close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
  107. }
  108. /****************************************************************************************
  109. *
  110. */
  111. TEST_CASE("Valid GPIO Processed", "[config][ui]")
  112. {
  113. char * cmdline = "test -i 33\n";
  114. void *argtable[] = {
  115. argint = arg_int1("i","int","<gpio>","GPIO number"),
  116. end = arg_end(6)
  117. };
  118. open_mem_stream_file();
  119. alloc_split_parse_command_line(cmdline, &argtable);
  120. int out_val = 0;
  121. TEST_ASSERT_EQUAL_INT_MESSAGE(0,is_output_gpio(argtable[0], f, &out_val, true),"Valid GPIO not recognized");
  122. TEST_ASSERT_EQUAL_INT_MESSAGE(33,out_val,"GPIO Should be set to 33");
  123. fflush (f);
  124. TEST_ASSERT_EQUAL_STRING_MESSAGE("",buf,"Valid GPIO shouldn't produce a message");
  125. close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
  126. }
  127. /****************************************************************************************
  128. *
  129. */
  130. TEST_CASE("Missing mandatory GPIO detected", "[config][ui]")
  131. {
  132. char * cmdline = "test \n";
  133. void *argtable[] = {
  134. argint = arg_int1("i","int","<gpio>","GPIO number"),
  135. end = arg_end(6)
  136. };
  137. open_mem_stream_file();
  138. alloc_split_parse_command_line(cmdline, &argtable);
  139. int out_val = 0;
  140. TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Missing GPIO not detected");
  141. fflush (f);
  142. TEST_ASSERT_EQUAL_STRING_MESSAGE("Missing: int\n",buf,"Missing GPIO parameter message wrong");
  143. TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
  144. close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
  145. }
  146. /****************************************************************************************
  147. *
  148. */
  149. TEST_CASE("Missing mandatory parameter detected", "[config][ui]")
  150. {
  151. char * cmdline = "test \n";
  152. void *argtable[] = {
  153. argint = arg_int1("i","int","<gpio>","GPIO number"),
  154. end = arg_end(6)
  155. };
  156. open_mem_stream_file();
  157. alloc_split_parse_command_line(cmdline, &argtable);
  158. int out_val = 0;
  159. TEST_ASSERT_EQUAL_INT_MESSAGE(1,is_output_gpio(argtable[0], f, &out_val, true),"Missing parameter not detected");
  160. fflush (f);
  161. TEST_ASSERT_EQUAL_STRING_MESSAGE("Missing: int\n",buf,"Missing parameter message wrong");
  162. TEST_ASSERT_EQUAL_INT_MESSAGE(-1,out_val,"GPIO Should be set to -1");
  163. close_flush_all(argtable,sizeof(argtable)/sizeof(argtable[0]),false);
  164. }
  165. /****************************************************************************************
  166. *
  167. */
  168. TEST_CASE("dac config command", "[config_cmd]")
  169. {
  170. config_set_value(NVS_TYPE_STR, "dac_config", "");
  171. esp_err_t err=run_command("cfg-hw-dac\n");
  172. char * nvs_value = config_alloc_get_str("dac_config", NULL,NULL);
  173. TEST_ASSERT_NOT_NULL(nvs_value);
  174. TEST_ASSERT_EQUAL_MESSAGE(ESP_OK,err,"Running command failed");
  175. free(nvs_value);
  176. }