From bcf6515329f4ce1062356a684ed1b6a9f05f5a0d Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 25 Feb 2016 15:35:45 -0500 Subject: [PATCH] Protect test runner generator against characters in strings that look like comments or functions --- auto/generate_test_runner.rb | 6 ++++-- test/expectdata/testsample_cmd.c | 4 ++++ test/expectdata/testsample_def.c | 4 ++++ test/expectdata/testsample_head1.c | 4 ++++ test/expectdata/testsample_head1.h | 11 +++++++---- test/expectdata/testsample_mock_head1.h | 10 ++++++---- test/expectdata/testsample_new1.c | 4 ++++ test/expectdata/testsample_new2.c | 4 ++++ test/expectdata/testsample_param.c | 4 ++++ test/expectdata/testsample_run1.c | 4 ++++ test/expectdata/testsample_run2.c | 4 ++++ test/expectdata/testsample_yaml.c | 4 ++++ test/testdata/testsample.c | 21 +++++++++++++++++++-- 13 files changed, 72 insertions(+), 12 deletions(-) diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index aeed0b2..3b030bf 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -91,7 +91,9 @@ class UnityTestRunnerGenerator def find_tests(source) tests_and_line_numbers = [] - source_scrubbed = source.gsub(/\/\/.*$/, '') # remove line comments + source_scrubbed = source.clone + source_scrubbed = source_scrubbed.gsub(/"[^"]*"/, '') # remove things in strings + source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line | (;|\{|\}) /x) # Match ;, {, and } as end of lines @@ -329,7 +331,7 @@ class UnityTestRunnerGenerator output.puts("#include #{inc.include?('<') ? inc : "\"#{inc.gsub('.h','')}.h\""}") end output.puts "\n" - tests.each do |test| + tests.each do |test| if ((test[:params].nil?) or (test[:params].empty?)) output.puts("void #{test[:test]}(void);") else diff --git a/test/expectdata/testsample_cmd.c b/test/expectdata/testsample_cmd.c index 3bd5c08..1c8090e 100644 --- a/test/expectdata/testsample_cmd.c +++ b/test/expectdata/testsample_cmd.c @@ -35,6 +35,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -52,6 +54,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/expectdata/testsample_def.c b/test/expectdata/testsample_def.c index 0c95c76..fe8f0c2 100644 --- a/test/expectdata/testsample_def.c +++ b/test/expectdata/testsample_def.c @@ -31,6 +31,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -48,6 +50,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/expectdata/testsample_head1.c b/test/expectdata/testsample_head1.c index feb823c..250b228 100644 --- a/test/expectdata/testsample_head1.c +++ b/test/expectdata/testsample_head1.c @@ -29,6 +29,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -46,6 +48,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/expectdata/testsample_head1.h b/test/expectdata/testsample_head1.h index 3cc3438..da6b7ab 100644 --- a/test/expectdata/testsample_head1.h +++ b/test/expectdata/testsample_head1.h @@ -1,12 +1,15 @@ /* AUTOGENERATED FILE. DO NOT EDIT. */ -#ifndef __B_U_I_L_D__T_E_S_T_S_A_M_P_L_E___H_E_A_D_1__H_ -#define __B_U_I_L_D__T_E_S_T_S_A_M_P_L_E___H_E_A_D_1__H_ +#ifndef _TESTSAMPLE_HEAD1_H +#define _TESTSAMPLE_HEAD1_H +#include "unity.h" #include "funky.h" #include "stanky.h" #include -void test_TheFirstThingToTest(); -void test_TheSecondThingToTest(); +void test_TheFirstThingToTest(void); +void test_TheSecondThingToTest(void); +void test_TheThirdThingToTest(void); +void test_TheFourthThingToTest(void); #endif diff --git a/test/expectdata/testsample_mock_head1.h b/test/expectdata/testsample_mock_head1.h index 2819414..30c509a 100644 --- a/test/expectdata/testsample_mock_head1.h +++ b/test/expectdata/testsample_mock_head1.h @@ -1,11 +1,13 @@ /* AUTOGENERATED FILE. DO NOT EDIT. */ -#ifndef __B_U_I_L_D__T_E_S_T_S_A_M_P_L_E___M_O_C_K___H_E_A_D_1__H_ -#define __B_U_I_L_D__T_E_S_T_S_A_M_P_L_E___M_O_C_K___H_E_A_D_1__H_ +#ifndef _TESTSAMPLE_MOCK_HEAD1_H +#define _TESTSAMPLE_MOCK_HEAD1_H +#include "unity.h" +#include "cmock.h" #include "funky.h" #include -void test_TheFirstThingToTest(); -void test_TheSecondThingToTest(); +void test_TheFirstThingToTest(void); +void test_TheSecondThingToTest(void); #endif diff --git a/test/expectdata/testsample_new1.c b/test/expectdata/testsample_new1.c index 6413006..48c3908 100644 --- a/test/expectdata/testsample_new1.c +++ b/test/expectdata/testsample_new1.c @@ -41,6 +41,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -58,6 +60,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/expectdata/testsample_new2.c b/test/expectdata/testsample_new2.c index 4ce6639..71ae68c 100644 --- a/test/expectdata/testsample_new2.c +++ b/test/expectdata/testsample_new2.c @@ -31,6 +31,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Suite Setup===== @@ -61,6 +63,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return suite_teardown(UnityEnd()); } diff --git a/test/expectdata/testsample_param.c b/test/expectdata/testsample_param.c index 950eeb3..29681de 100644 --- a/test/expectdata/testsample_param.c +++ b/test/expectdata/testsample_param.c @@ -32,6 +32,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -49,6 +51,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21, RUN_TEST_NO_ARGS); RUN_TEST(test_TheSecondThingToTest, 43, RUN_TEST_NO_ARGS); + RUN_TEST(test_TheThirdThingToTest, 53, RUN_TEST_NO_ARGS); + RUN_TEST(test_TheFourthThingToTest, 58, RUN_TEST_NO_ARGS); return (UnityEnd()); } diff --git a/test/expectdata/testsample_run1.c b/test/expectdata/testsample_run1.c index 6413006..48c3908 100644 --- a/test/expectdata/testsample_run1.c +++ b/test/expectdata/testsample_run1.c @@ -41,6 +41,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Test Reset Option===== @@ -58,6 +60,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/expectdata/testsample_run2.c b/test/expectdata/testsample_run2.c index 4ce6639..71ae68c 100644 --- a/test/expectdata/testsample_run2.c +++ b/test/expectdata/testsample_run2.c @@ -31,6 +31,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Suite Setup===== @@ -61,6 +63,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return suite_teardown(UnityEnd()); } diff --git a/test/expectdata/testsample_yaml.c b/test/expectdata/testsample_yaml.c index 2c91682..be35eba 100644 --- a/test/expectdata/testsample_yaml.c +++ b/test/expectdata/testsample_yaml.c @@ -38,6 +38,8 @@ extern void setUp(void); extern void tearDown(void); extern void test_TheFirstThingToTest(void); extern void test_TheSecondThingToTest(void); +extern void test_TheThirdThingToTest(void); +extern void test_TheFourthThingToTest(void); //=======Suite Setup===== @@ -62,6 +64,8 @@ int main(void) UnityBegin("testdata/testsample.c"); RUN_TEST(test_TheFirstThingToTest, 21); RUN_TEST(test_TheSecondThingToTest, 43); + RUN_TEST(test_TheThirdThingToTest, 53); + RUN_TEST(test_TheFourthThingToTest, 58); return (UnityEnd()); } diff --git a/test/testdata/testsample.c b/test/testdata/testsample.c index 4f30ec7..bd084ce 100644 --- a/test/testdata/testsample.c +++ b/test/testdata/testsample.c @@ -35,15 +35,32 @@ void test_ShouldBeIgnored(void) //void test_ShouldAlsoNotBeTested(void) //{ // Call_An_Expect(); -// +// // CallAFunction(); // test_CallAFunctionThatLooksLikeATest(); //} void test_TheSecondThingToTest(void) { + uint8_t* crazyString = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8081\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36\r\nPostman-Token: 768c7149-c3fb-f704-71a2-63918d9195b2\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\n\r\n"; + + Call_An_Expect(); + + CallAFunction(); + test_CallAFunctionThatLooksLikeATest(); +} + +void test_TheThirdThingToTest(void) +{ + CallAFunction(); +} + +void test_TheFourthThingToTest(void) +{ + uint8_t* anotherString = "GET / HTTP/1.1\r\nHost: 127.0.0.1:8081\r\nConnection: keep-alive\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36\r\nPostman-Token: 768c7149-c3fb-f704-71a2-63918d9195b2\r\nAccept: */*\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\n\r\n"; + Call_An_Expect(); - + CallAFunction(); test_CallAFunctionThatLooksLikeATest(); } -- GitLab