-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.c
2265 lines (1892 loc) · 63.2 KB
/
frame.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------
| Linux based APIs Auto Test Framework
| ----------------------------------------------------------
| QA test frame which managing tests and control cases running.
|
| File Name: frame.c
| Author: Liu, Yang
| Created date: 11/03/2011
| Version: v_0_1
| Last update: 03/20/2012
| Updated by: liuyang
+----------------------------------------------------------*/
/*----------------------------------------------------------
| Include definition below this line
+----------------------------------------------------------*/
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gprintf.h>
#include "frame.h"
/*----------------------------------------------------------
| Macro definition below this line
+----------------------------------------------------------*/
#define MAX_NAME_LEN 64
/*----------------------------------------------------------
| Type definition below this line
+----------------------------------------------------------*/
/*Case control running structs*/
typedef struct TestResult
{
struct TestModule *pTestModule; /*Pointer to test module*/
struct TestSuite *pTestSuite; /*Pointer to test suite*/
struct TestCase *pTestCase; /*Pointer to test case*/
guint uiTimesOfPass; /*Number of running times*/
guint uiTimesOfFail; /*Number of Fail times*/
guint uiTimesOfCrash; /*Number of Crash times*/
guint uiTimesOfNoRan; /*Number of No Run times*/
gint iSubCaseWeight; /*The number used to randly select*/
QaError ExceptionCode; /*Exception code*/
struct TestResult *pPrev; /*Pointer to previous test result record in list*/
struct TestResult *pNext; /*Pointer to next test result record in list*/
}TestResult;
typedef enum
{
STATUS_STOPPED = 1,
STATUS_RUNNING,
STATUS_PAUSED
}CaseRunningStatus;
typedef struct FrameStatus
{
struct TestResult *pCurrentResult; /*pointer to current running test case's result*/
struct TestResult *pTestResult; /*pointer to first test result record*/
gint iNumOfRemainingCases; /*The number of remaining cases need to run*/
gint iNumOfRandomSteps; /*The number of random stress steps*/
gint iSumOfCaseWeight; /*The sum of cases' weight*/
CaseRunningStatus iStatus; /*Stopped, running, or Pause*/
RunningMode iRMode; /*The running mode of current testing*/
}FrameStatus;
typedef struct PipeData
{
gchar module_name[MAX_NAME_LEN+1];
gchar suite_name[MAX_NAME_LEN+1];
gchar case_name[MAX_NAME_LEN+1];
CaseResult iResult;
QaError iFrameError;
}PipeData;
typedef void (*case_driver)(gint);
/*----------------------------------------------------------
| Global vars definition below this line
+----------------------------------------------------------*/
static TestRegistry *g_pTestRegistry;
static FrameStatus *g_pFrameStatus;
static guint g_uiCaseIntervalTime = 2;
static pid_t case_driver_pid;
static gint pipe_fd[2];
/*----------------------------------------------------------
| Functions prototype below this line
+----------------------------------------------------------*/
TestCase *qa_create_test_case(const gchar * pCaseName,TestFunc pTestFunc);
TestCase *qa_find_case_in_suite(TestSuite * pTestSuite,const gchar * pCaseName,QaError * ErrorCode);
void insert_test_case(TestRegistry * pTestRegistry,TestSuite * pTestSuite,TestCase * pTestCase);
void cleanup_test_case(TestCase * pTestCase);
TestSuite *qa_create_test_suite(const gchar * pSuiteName,InitFunc pInitFunc,CleanupFunc pCleanupFunc);
TestSuite *qa_find_suite(TestModule * pTestModule,const gchar * pSuiteName,QaError * ErrorCode);
void insert_test_suite(TestRegistry * pTestRegistry,TestModule * pTestModule,TestSuite * pTestSuite);
void cleanup_test_suite(TestSuite * pTestSuite);
TestModule *qa_create_test_module(const gchar * pName);
TestModule *qa_find_module(TestRegistry * pTestRegistry,const gchar * pName,QaError * ErrorCode);
void insert_test_module(TestRegistry * pTestRegistry,TestModule * pTestModule);
void cleanup_test_module(TestModule * pTestModule);
void cleanup_test_registry(TestRegistry * pTestRegistry);
void qa_destroy_existing_registry(TestRegistry * * ppTestRegistry);
void qa_clear_previous_results(void);
void qa_frame_cleanup_registry(void);
TestRegistry *qa_create_new_registry(void);
QaError qa_frame_initialize_test_registry(void);
guint qa_frame_get_total_modules(void);
guint qa_frame_get_total_suites(void);
guint qa_frame_get_total_cases(void);
QaError push_case_to_result_list(TestModule * pTestModule,TestSuite * pTestSuite,TestCase * pTestCase,FrameStatus * pFrameStatus);
QaError push_suite_to_result_list(TestModule * pTestModule,TestSuite * pTestSuite,FrameStatus * pFrameStatus, int priority);
QaError push_module_to_result_list(TestModule * pTestModule,FrameStatus * pFrameStatus, int priority);
QaError push_case_to_result_list_by_ids(char *case_list, int priority);
void send_test_result(gchar * module_name,gchar * suite_name,gchar * case_name,CaseResult result,QaError ErrorCode,gint write_pipe);
gint read_test_result(PipeData * data,gint read_pipe);
TestResult *find_test_result(gchar * module_name,gchar * suite_name,gchar * case_name);
QaError do_test_suite_init(TestSuite * pTestSuite);
CaseResult run_single_case(TestResult * pTestResult,QaError * error);
void do_test_suite_cleanup(TestSuite * pTestSuite);
QaError qa_frame_print_out_test_result(void);
FrameStatus* qa_create_new_frame_status(void);
void sequence_goto_next_suite(FrameStatus * pFrameStatus,gint write_pipe);
void sequence_case_driver(gint write_pipe);
pid_t fork_child_process(case_driver driver_f);
void signal_handler(int sig);
QaError qa_do_sequence_testing(void);
void random_goto_next_case(FrameStatus * pFrameStatus);
void random_case_driver(gint write_pipe);
QaError qa_do_random_testing(void);
void set_test_result(TestResult * pTestResult,CaseResult result,QaError ErrorCode);
/*----------------------------------------------------------
| Functions definition below this line
+----------------------------------------------------------*/
TestCase* qa_create_test_case(const gchar *pCaseName, TestFunc pTestFunc)
{
TestCase *pRetCase = NULL;
gint len;
pRetCase = (TestCase *)g_slice_alloc0(sizeof(TestCase));
if(pRetCase != NULL){
len = strlen(pCaseName);
if(len>MAX_NAME_LEN)
len = MAX_NAME_LEN;
pRetCase->pName = (gchar *)g_slice_alloc0(len + 1);
if(pRetCase->pName != NULL){
strncpy(pRetCase->pName, pCaseName, len);
pRetCase->pTestFunc = pTestFunc;
pRetCase->iCaseWight = CASE_WEIGHT_MEDIUM;
pRetCase->pPrev = NULL;
pRetCase->pNext = NULL;
pRetCase->uiID = 0;
}else{
g_slice_free1(sizeof(TestCase), pRetCase);
pRetCase = NULL;
}
}
return pRetCase;
}
TestCase* qa_find_case_in_suite(TestSuite *pTestSuite, const gchar *pCaseName, QaError *ErrorCode)
{
TestCase *pCurCase = NULL;
*ErrorCode = FRAME_NO_CASE;
pCurCase = pTestSuite->pTestCase;
while(pCurCase != NULL){
if(strcmp(pCurCase->pName, pCaseName)==0){
*ErrorCode = FRAME_SUCCESS;
break;
}
pCurCase = pCurCase->pNext;
}
return pCurCase;
}
TestCase* qa_frame_get_case_by_name(const gchar *pCaseName, QaError *ErrorCode)
{
TestModule *pModule=NULL;
TestSuite *pSuite=NULL;
TestCase *pCase=NULL;
*ErrorCode = FRAME_NO_CASE;
if(g_pTestRegistry==NULL){
*ErrorCode = FRAME_NO_REGISTRY;
return NULL;
}else if((pModule=g_pTestRegistry->pTestModule)==NULL){
*ErrorCode = FRAME_NO_MODULE;
return NULL;
}else if((pSuite=pModule->pTestSuite)==NULL){
*ErrorCode = FRAME_NO_SUITE;
return NULL;
}else if((pCase=pSuite->pTestCase)==NULL){
*ErrorCode = FRAME_NO_CASE;
return NULL;
}
while(pModule!=NULL){
while(pSuite!=NULL){
while(pCase!=NULL){
if(g_strcmp0(pCase->pName, pCaseName)==0){
*ErrorCode = FRAME_SUCCESS;
return pCase;
}
pCase = pCase->pNext;
}
pSuite = pSuite->pNext;
}
pModule = pModule->pNext;
}
return NULL;
}
void insert_test_case(TestRegistry *pTestRegistry, TestSuite *pTestSuite, TestCase *pTestCase)
{
TestCase *pCurCase = NULL;
pCurCase = pTestSuite->pTestCase;
pTestCase->pNext = NULL;
pTestRegistry->uiNumberOfTotalTestCases++;
pTestSuite->uiNumberOfTest++;
pTestCase->uiID = pTestSuite->uiNumberOfTest;
if(pCurCase == NULL){ /*first case*/
pTestSuite->pTestCase = pTestCase;
pTestCase->pPrev = NULL;
}else{
while(pCurCase->pNext != NULL)
pCurCase = pCurCase->pNext;
pCurCase->pNext = pTestCase;
pTestCase->pPrev = pCurCase;
}
}
void cleanup_test_case(TestCase *pTestCase)
{
if(pTestCase->pName != NULL)
g_slice_free1(strlen(pTestCase->pName)+1, pTestCase->pName);
pTestCase = NULL;
}
TestCase* qa_frame_add_test_case(TestSuite *pTestSuite, const gchar *pCaseName, TestFunc pTestFunc, QaError *ErrorCode)
{
TestCase *pRetCase=NULL;
if(pTestSuite == NULL)
*ErrorCode = FRAME_NO_SUITE;
else if(pCaseName == NULL)
*ErrorCode = FRAME_NO_CASE_NAME;
else if(pTestFunc == NULL)
*ErrorCode = FRAME_NO_CASE_FUNC;
else if(qa_find_case_in_suite(pTestSuite, pCaseName, ErrorCode) != NULL)
*ErrorCode = FRAME_DUPLICATE_CASE;
else{
pRetCase = qa_create_test_case(pCaseName, pTestFunc);
if(pRetCase == NULL)
*ErrorCode = FRAME_NO_MEMORY;
else{
insert_test_case(g_pTestRegistry, pTestSuite, pRetCase);
*ErrorCode = FRAME_SUCCESS;
}
}
return pRetCase;
}
TestSuite* qa_create_test_suite(const gchar *pSuiteName, InitFunc pInitFunc, CleanupFunc pCleanupFunc)
{
TestSuite *pRetSuite;
gint len;
pRetSuite = (TestSuite *)g_slice_alloc0(sizeof(TestSuite));
if(pRetSuite != NULL){
len = strlen(pSuiteName);
if(len>MAX_NAME_LEN)
len = MAX_NAME_LEN;
pRetSuite->pName = (gchar *)g_slice_alloc0(len+1);
if(pRetSuite->pName != NULL){
strncpy(pRetSuite->pName, pSuiteName, len);
pRetSuite->pInitFunc = pInitFunc;
pRetSuite->pCleanupFunc = pCleanupFunc;
pRetSuite->pTestCase = NULL;
pRetSuite->uiNumberOfTest = 0;
pRetSuite->pNext = NULL;
pRetSuite->pPrev = NULL;
pRetSuite->uiID = 0;
}else{
g_slice_free1(sizeof(TestSuite), pRetSuite);
pRetSuite = NULL;
}
}
return pRetSuite;
}
TestSuite* qa_find_suite(TestModule *pTestModule, const gchar *pSuiteName, QaError *ErrorCode)
{
TestSuite *pCurSuite = NULL;
*ErrorCode = FRAME_NO_SUITE;
pCurSuite = pTestModule->pTestSuite;
while(pCurSuite != NULL){
if(strcmp(pCurSuite->pName, pSuiteName) == 0){
*ErrorCode = FRAME_SUCCESS;
break;
}
pCurSuite = pCurSuite->pNext;
}
return pCurSuite;
}
TestSuite* qa_frame_get_suite_by_name(TestModule *pTestModule, const gchar *pSuiteName, QaError *ErrorCode)
{
return qa_find_suite(pTestModule, pSuiteName, ErrorCode);
}
void insert_test_suite(TestRegistry *pTestRegistry, TestModule *pTestModule, TestSuite *pTestSuite)
{
TestSuite *pCurSuite=NULL;
pCurSuite = pTestModule->pTestSuite;
pTestSuite->pNext = NULL;
pTestModule->uiNumberOfSuite++;
pTestRegistry->uiNumberOfTotalTestSuites++;
pTestSuite->uiID = pTestModule->uiNumberOfSuite;
if(pCurSuite == NULL){ /*first suite*/
pTestModule->pTestSuite = pTestSuite;
pTestSuite->pPrev = NULL;
}else{
while(pCurSuite->pNext != NULL)
pCurSuite = pCurSuite->pNext;
pCurSuite->pNext = pTestSuite;
pTestSuite->pPrev = pCurSuite;
}
}
void cleanup_test_suite(TestSuite *pTestSuite)
{
TestCase *pCurCase;
TestCase *pNextCase;
pCurCase = pTestSuite->pTestCase;
while(pCurCase != NULL){
pNextCase = pCurCase->pNext;
cleanup_test_case(pCurCase);
g_slice_free1(sizeof(TestCase), pCurCase);
pCurCase = pNextCase;
}
if(pTestSuite->pName != NULL)
g_slice_free1(strlen(pTestSuite->pName)+1, pTestSuite->pName);
pTestSuite->pName = NULL;
pTestSuite->pTestCase = NULL;
pTestSuite->uiNumberOfTest = 0;
pTestSuite->uiID = 0;
}
TestSuite* qa_frame_add_test_suite(TestModule *pModule, const gchar* pSuiteName, InitFunc pInitFunc, CleanupFunc pCleanupFunc, QaError *ErrorCode)
{
TestSuite *pRetSuite = NULL;
if(pModule == NULL)
*ErrorCode = FRAME_NO_MODULE;
else if(pSuiteName == NULL)
*ErrorCode = FRAME_NO_SUITE_NAME;
else if(g_pTestRegistry == NULL)
*ErrorCode = FRAME_NO_REGISTRY;
else if(qa_find_suite(pModule, pSuiteName, ErrorCode) != NULL)
*ErrorCode = FRAME_DUPLICATE_SUITE;
else{
pRetSuite = qa_create_test_suite(pSuiteName, pInitFunc, pCleanupFunc);
if(pRetSuite == NULL)
*ErrorCode = FRAME_NO_MEMORY;
else{
insert_test_suite(g_pTestRegistry, pModule, pRetSuite);
*ErrorCode = FRAME_SUCCESS;
}
}
return pRetSuite;
}
TestModule *qa_create_test_module(const gchar* pName)
{
TestModule *pTestModule;
gint len;
pTestModule = (TestModule *)g_slice_alloc0(sizeof(TestModule));
if(pTestModule != NULL){
len = strlen(pName);
if(len>MAX_NAME_LEN)
len = MAX_NAME_LEN;
pTestModule->pName = (gchar *)g_slice_alloc0(len+1);
if(pTestModule->pName != NULL){
strncpy(pTestModule->pName, pName, len);
pTestModule->pTestSuite = NULL;
pTestModule->uiNumberOfSuite = 0;
pTestModule->pPrev = NULL;
pTestModule->pNext = NULL;
pTestModule->uiID = 0;
}else{
g_slice_free1(sizeof(TestModule), pTestModule);
pTestModule = NULL;
}
}
return pTestModule;
}
TestModule* qa_find_module(TestRegistry *pTestRegistry, const gchar *pName, QaError *ErrorCode)
{
TestModule *pCurModule = NULL;
*ErrorCode = FRAME_NO_MODULE;
pCurModule = pTestRegistry->pTestModule;
while(pCurModule != NULL){
if(strcmp(pCurModule->pName, pName) == 0){
*ErrorCode = FRAME_SUCCESS;
break;
}
pCurModule = pCurModule->pNext;
}
return pCurModule;
}
TestModule* qa_frame_get_module_by_name(const gchar *pName, QaError *ErrorCode)
{
return qa_find_module(g_pTestRegistry, pName, ErrorCode);
}
TestModule* qa_frame_get_first_module(QaError *ErrorCode)
{
if(g_pTestRegistry == NULL){
*ErrorCode = FRAME_NO_REGISTRY;
return NULL;
}else if(g_pTestRegistry->pTestModule == NULL){
*ErrorCode = FRAME_NO_MODULE;
return NULL;
}else{
*ErrorCode = FRAME_SUCCESS;
return g_pTestRegistry->pTestModule;
}
}
void insert_test_module(TestRegistry *pTestRegistry, TestModule *pTestModule)
{
TestModule *pCurModule = NULL;
pCurModule = pTestRegistry->pTestModule;
pTestModule->pNext = NULL;
pTestRegistry->uiNumberOfTotalTestModules++;
pTestModule->uiID = pTestRegistry->uiNumberOfTotalTestModules;
if(pCurModule == NULL){/*The first module*/
pTestRegistry->pTestModule = pTestModule;
pTestModule->pPrev = NULL;
}else{
while(pCurModule->pNext != NULL){
pCurModule = pCurModule->pNext;
}
pCurModule->pNext = pTestModule;
pTestModule->pPrev = pCurModule;
}
}
void cleanup_test_module(TestModule *pTestModule)
{
TestSuite *pCurSuite;
TestSuite *pNextSuite;
pCurSuite = pTestModule->pTestSuite;
while(pCurSuite != NULL){
pNextSuite = pCurSuite->pNext;
cleanup_test_suite(pCurSuite);
g_slice_free1(sizeof(TestSuite), pCurSuite);
pCurSuite = pNextSuite;
}
if(pTestModule->pName != NULL)
g_slice_free1(strlen(pTestModule->pName)+1, pTestModule->pName);
pTestModule->pName = NULL;
pTestModule->pTestSuite = NULL;
pTestModule->uiNumberOfSuite = 0;
pTestModule->uiID = 0;
}
TestModule* qa_frame_add_test_module(const gchar* pName, QaError *ErrorCode)
{
TestModule *retModule = NULL;
if(pName == NULL){
*ErrorCode = FRAME_NO_MODULE_NAME;
}else if(g_pTestRegistry == NULL){
*ErrorCode = FRAME_NO_REGISTRY;
}else if(qa_find_module(g_pTestRegistry, pName, ErrorCode) != NULL){
*ErrorCode = FRAME_DUPLICATE_MODULE;
}else{
retModule = qa_create_test_module(pName);
if(retModule == NULL)
*ErrorCode = FRAME_NO_MEMORY;
else{
insert_test_module(g_pTestRegistry, retModule);
*ErrorCode = FRAME_SUCCESS;
}
}
return retModule;
}
void cleanup_test_registry(TestRegistry *pTestRegistry)
{
TestModule *pCurModule;
TestModule *pNextModule;
pCurModule = pTestRegistry->pTestModule;
while(pCurModule != NULL){
pNextModule = pCurModule->pNext;
cleanup_test_module(pCurModule);
g_slice_free1(sizeof(TestModule), pCurModule);
pCurModule = pNextModule;
}
pTestRegistry->pTestModule = NULL;
pTestRegistry->uiNumberOfTotalTestCases = 0;
pTestRegistry->uiNumberOfTotalTestModules = 0;
pTestRegistry->uiNumberOfTotalTestSuites = 0;
}
void qa_destroy_existing_registry(TestRegistry **ppTestRegistry)
{
if(NULL != *ppTestRegistry){
cleanup_test_registry(*ppTestRegistry);
}
g_slice_free1(sizeof(TestRegistry), *ppTestRegistry);
*ppTestRegistry = NULL;
}
void qa_clear_previous_results(void)
{
TestResult *pCurResult, *pNextResult;
if(g_pFrameStatus != NULL){
g_pFrameStatus->pCurrentResult= NULL;
g_pFrameStatus->iNumOfRemainingCases = 0;
g_pFrameStatus->iStatus= STATUS_STOPPED;
g_pFrameStatus->iNumOfRandomSteps = 0;
g_pFrameStatus->iRMode = RUNNING_SEQUENCE;
g_pFrameStatus->iSumOfCaseWeight = 0;
pCurResult = g_pFrameStatus->pTestResult;
while(pCurResult != NULL){
pNextResult = pCurResult->pNext;
g_slice_free1(sizeof(TestResult), pCurResult);
pCurResult = pNextResult;
}
g_pFrameStatus->pTestResult = NULL;
}
}
void qa_frame_cleanup_registry(void)
{
qa_destroy_existing_registry(&g_pTestRegistry);
qa_clear_previous_results();
if(g_pFrameStatus!=NULL){
g_slice_free1(sizeof(FrameStatus), g_pFrameStatus);
g_pFrameStatus = NULL;
}
}
TestRegistry* qa_create_new_registry(void)
{
TestRegistry *pTestRegistry = (TestRegistry *)g_slice_alloc0(sizeof(TestRegistry));
if(NULL != pTestRegistry){
pTestRegistry->pTestModule = NULL;
pTestRegistry->uiNumberOfTotalTestCases = 0;
pTestRegistry->uiNumberOfTotalTestModules = 0;
pTestRegistry->uiNumberOfTotalTestSuites = 0;
}
return pTestRegistry;
}
QaError qa_frame_initialize_test_registry(void)
{
QaError result = FRAME_SUCCESS;
if (NULL != g_pTestRegistry) {
qa_frame_cleanup_registry();
}
g_pTestRegistry = qa_create_new_registry();
if (NULL == g_pTestRegistry) {
result = FRAME_NO_MEMORY;
}
return result;
}
guint qa_frame_get_total_modules(void)
{
if(g_pTestRegistry != NULL)
return g_pTestRegistry->uiNumberOfTotalTestModules;
else
return 0;
}
guint qa_frame_get_total_suites(void)
{
if(g_pTestRegistry != NULL)
return g_pTestRegistry->uiNumberOfTotalTestSuites;
else
return 0;
}
guint qa_frame_get_total_cases(void)
{
if(g_pTestRegistry != NULL)
return g_pTestRegistry->uiNumberOfTotalTestCases;
else
return 0;
}
const gchar *qa_frame_get_error_str(QaError ErrorCode)
{
static const gchar *qa_frame_error_desc[]=
{
"FRAME_SUCCESS",
"FRAME_FAILURE",
"FRAME_NO_MEMORY",
"FRAME_NO_SUITE_NAME",
"FRAME_NO_REGISTRY",
"FRAME_DUPLICATE_MODULE",
"FRAME_NO_MODULE",
"FRAME_NO_SUITE_NAME",
"FRAME_NO_SUITE",
"FRAME_DUPLICATE_SUITE",
"FRAME_NO_CASE_NAME",
"FRAME_NO_CASE",
"FRAME_DUPLICATE_CASE",
"FRAME_NO_CASE_FUNC",
"FRAME_INIT_SUITE_FAIL",
"FRAME_NO_FRAME_STATUS",
"FRAME_TIMER_CREATE_ERROR",
"FRAME_TIMER_START_ERROR",
"FRAME_NO_VERIF_FUNC",
"FRAME_NO_RESULT_RECORD",
"FRAME_ALREADY_START",
"Undefined Error",
};
gint iMaxIndex = (gint)(sizeof(qa_frame_error_desc)/sizeof(gchar *)-1);
gint iErrorIndex = (gint)(ErrorCode * (-1));
if(iErrorIndex<0)
return qa_frame_error_desc[0];
else if(iErrorIndex>iMaxIndex)
return qa_frame_error_desc[iMaxIndex];
else
return qa_frame_error_desc[iErrorIndex];
}
QaError push_case_to_result_list(TestModule *pTestModule, TestSuite *pTestSuite, TestCase *pTestCase, FrameStatus*pFrameStatus)
{
QaError RetVal = FRAME_SUCCESS;
TestResult *pCurResult = NULL;
TestResult *pNewResult = NULL;
QA_PRINT(3, ("push_case_to_result_list\n"));
if(pTestSuite == NULL)
RetVal = FRAME_NO_SUITE;
else if(pTestCase == NULL)
RetVal = FRAME_NO_CASE;
else if(pTestModule == NULL)
RetVal = FRAME_NO_MODULE;
else if(pFrameStatus == NULL)
RetVal = FRAME_NO_FRAME_STATUS;
else{
pNewResult = (TestResult *)g_slice_alloc0(sizeof(TestResult));
if(pNewResult == NULL)
RetVal = FRAME_NO_MEMORY;
else{
pNewResult->pTestModule = pTestModule;
pNewResult->pTestSuite = pTestSuite;
pNewResult->pTestCase = pTestCase;
pNewResult->uiTimesOfPass = 0;
pNewResult->uiTimesOfFail = 0;
pNewResult->uiTimesOfNoRan = 0;
pNewResult->uiTimesOfCrash = 0;
pNewResult->ExceptionCode = FRAME_SUCCESS;
pNewResult->pNext = NULL;
pFrameStatus->iSumOfCaseWeight += pTestCase->iCaseWight;
pNewResult->iSubCaseWeight = pFrameStatus->iSumOfCaseWeight;
pCurResult = pFrameStatus->pTestResult;
QA_PRINT(3, ("Insert test result to list. current_test=%s\n", pNewResult->pTestCase->pName));
if(pCurResult == NULL){ /*first one*/
pFrameStatus->pTestResult = pNewResult;
pNewResult->pPrev = NULL;
}else{
while(pCurResult->pNext != NULL)
pCurResult = pCurResult->pNext;
pCurResult->pNext = pNewResult;
pNewResult->pPrev = pCurResult;
}
pFrameStatus->iNumOfRemainingCases++;
}
}
return RetVal;
}
QaError push_suite_to_result_list(TestModule *pTestModule, TestSuite *pTestSuite, FrameStatus*pFrameStatus, int priority)
{
QaError RetVal = FRAME_SUCCESS;
TestCase *pCurCase;
QA_PRINT(3, ("push_suite_to_result_list\n"));
if(pTestSuite == NULL)
RetVal = FRAME_NO_SUITE;
else if(pFrameStatus == NULL)
RetVal = FRAME_NO_FRAME_STATUS;
else{
pCurCase = pTestSuite->pTestCase;
while(pCurCase != NULL){
if((pCurCase->iCaseWight)&priority){
RetVal = push_case_to_result_list(pTestModule, pTestSuite, pCurCase, pFrameStatus);
if(RetVal != FRAME_SUCCESS)
printf("---Add test case '%s' to running list faile. ErrorCode='%s'.\n", pCurCase->pName, qa_frame_get_error_str(RetVal));
else
printf("---Add test case '%s' to running list Success.\n", pCurCase->pName);
}else
printf("***%s's priority=%d, ignored.\n", pCurCase->pName, pCurCase->iCaseWight);
pCurCase = pCurCase->pNext;
}
}
return RetVal;
}
QaError push_module_to_result_list(TestModule *pTestModule, FrameStatus*pFrameStatus, int priority)
{
QaError RetVal = FRAME_SUCCESS;
TestSuite *pCurSuite;
QA_PRINT(3, ("push_module_to_result_list\n"));
if(pTestModule == NULL)
RetVal = FRAME_NO_MODULE;
else if(pFrameStatus == NULL)
RetVal = FRAME_NO_FRAME_STATUS;
else{
pCurSuite = pTestModule->pTestSuite;
while(pCurSuite!=NULL){
RetVal = push_suite_to_result_list(pTestModule, pCurSuite, pFrameStatus, priority);
if(RetVal != FRAME_SUCCESS)
printf("-Add test suite '%s' to running list faile. ErrorCode='%s'.\n", pCurSuite->pName, qa_frame_get_error_str(RetVal));
else
printf("-Add test suite '%s' to running list Success.\n", pCurSuite->pName);
pCurSuite = pCurSuite->pNext;
}
}
return RetVal;
}
QaError push_case_to_result_list_by_ids(char *case_list, int priority)
{
QaError RetVal = FRAME_SUCCESS;
char **case_array=NULL;
char **id_array=NULL;
int max_array_length=10;
TestModule *pModule=NULL;
TestSuite *pSuite=NULL;
TestCase *pCase=NULL;
int i;
QA_PRINT(3, ("push_case_to_result_list_by_ids\n"));
QA_PRINT(3, ("The case list: %s\n", case_list));
/*1.1.1,2,3.2,4.3.1*/
if(case_list != NULL){
case_array = g_strsplit(case_list, ",", max_array_length);
assert(case_array!=NULL);
QA_PRINT(3, ("case array size is %d, The first case id is %s\n", g_strv_length(case_array), case_array[0]));
}
for(i=0; i<g_strv_length(case_array); i++){
QA_PRINT(3, ("Parse '%s'.\n", case_array[i]));
if(case_array[i]!=NULL && g_ascii_isdigit(case_array[i][0])){
id_array = g_strsplit(case_array[i], ".", 3);
assert(id_array!=NULL&&id_array[0]!=NULL);
pModule = qa_frame_find_module_by_ID((int)(g_ascii_strtod(id_array[0],NULL)), &RetVal);
assert(pModule!=NULL);
if(id_array[1]!=NULL){
pSuite = qa_frame_find_suite_by_ID(pModule, (int)(g_ascii_strtod(id_array[1],NULL)), &RetVal);
assert(pSuite!=NULL);
if(id_array[2]!=NULL){
pCase = qa_frame_find_case_by_ID(pSuite, (int)(g_ascii_strtod(id_array[2],NULL)), &RetVal);
assert(pCase!=NULL);
}
}
if(pCase!=NULL){
if(pCase->iCaseWight&priority){
RetVal = push_case_to_result_list(pModule, pSuite, pCase, g_pFrameStatus);
if(RetVal != FRAME_SUCCESS)
printf("---Add test case '%s' to running list faile. ErrorCode='%s'.\n", pCase->pName, qa_frame_get_error_str(RetVal));
else
printf("---Add test case '%s' to running list Success.\n", pCase->pName);
}else
printf("***%s's priority=%d. ignored.\n", pCase->pName, pCase->iCaseWight);
}
else if(pSuite!=NULL)
push_suite_to_result_list(pModule, pSuite, g_pFrameStatus, priority);
else
push_module_to_result_list(pModule, g_pFrameStatus, priority);
pModule=NULL;
pSuite=NULL;
pCase=NULL;
g_strfreev(id_array);
}
}
g_strfreev(case_array);
return RetVal;
}
void send_test_result(gchar *module_name, gchar *suite_name, gchar *case_name,
CaseResult result, QaError ErrorCode, gint write_pipe)
{
PipeData data;
QA_PRINT(3, ("send_test_result(%s,%s,%s,%d,%d,%d)\n",
module_name, suite_name, case_name, result, ErrorCode, write_pipe));
strcpy(data.module_name, module_name);
strcpy(data.suite_name, suite_name);
strcpy(data.case_name, case_name);
data.iResult = result;
data.iFrameError = ErrorCode;
lockf(write_pipe, 1, 0);
write(write_pipe, &data, sizeof(PipeData));
lockf(write_pipe, 0, 0);
}
gint read_test_result(PipeData *data, gint read_pipe)
{
gint len=0;
len = read(read_pipe,data,sizeof(PipeData));
QA_PRINT(3, ("read from pipe return %d.\n", len));
if(len==sizeof(PipeData)){
QA_PRINT(3, ("The data: module_name:%s, suite_name:%s, case_name:%s, iResult:%d, iFrameError:%d\n",
data->module_name,data->suite_name,data->case_name, data->iResult, data->iFrameError));
return TRUE;
}else if(len==0){
QA_PRINT(3, ("Read from pipe return 0.\n"));
return FALSE;
}else{
printf("FATAL FAIL - Read from pipe return %d.\n", len);
exit(1);
}
}
void set_test_result(TestResult *pTestResult, CaseResult result, QaError ErrorCode)
{
if(pTestResult == NULL){
printf("Fatal error [%s:%d]: The pTestResult is null when set test result.\n", __FILE__, __LINE__);
return;
}
QA_PRINT(3, ("set_test_result(%s, %d, %d)\n",
pTestResult->pTestCase->pName,
result,
ErrorCode));
switch(result)
{
case RET_PASS:
pTestResult->uiTimesOfPass++;
break;
case RET_FAIL:
pTestResult->uiTimesOfFail++;
pTestResult->ExceptionCode = ErrorCode;
break;
case RET_NO_RUN:
pTestResult->uiTimesOfNoRan++;
pTestResult->ExceptionCode = ErrorCode;
break;
default:
pTestResult->uiTimesOfCrash++;
pTestResult->ExceptionCode = ErrorCode;
break;
}
}
TestResult* find_test_result(gchar *module_name, gchar *suite_name, gchar *case_name)
{
TestResult *pCurresult;
QA_PRINT(3, ("find_test_result().\n"));
pCurresult = g_pFrameStatus->pTestResult;
while(pCurresult != NULL){
if(strcmp(pCurresult->pTestCase->pName, case_name)==0
&&strcmp(pCurresult->pTestSuite->pName, suite_name)==0
&&strcmp(pCurresult->pTestModule->pName, module_name)==0){
QA_PRINT(3, ("Test result item is founded. case_name:%s.\n", pCurresult->pTestCase->pName));
return pCurresult;
}
pCurresult = pCurresult->pNext;
}
return NULL;
}
QaError do_test_suite_init(TestSuite *pTestSuite)
{
QaError RetVal = FRAME_FAILURE;
CaseResult result;
if(pTestSuite==NULL)
RetVal = FRAME_NO_SUITE;
else if(pTestSuite->pInitFunc == NULL)
RetVal = FRAME_SUCCESS;
else{
result = (*pTestSuite->pInitFunc)();
if(result != RET_PASS)
RetVal = FRAME_FAILURE;
else
RetVal = FRAME_SUCCESS;
}
return RetVal;
}
CaseResult run_single_case(TestResult *pTestResult, QaError *error)
{
TestCase *pCase;
CaseResult result;
gchar time_str[256];
time_t now;
pCase = pTestResult->pTestCase;
if(pCase->pTestFunc!=NULL){
now = time(NULL);
qa_time_to_gmt_str(time_str, &now);
printf("\n++++++++ Run case(%s-%s-%s) begin (current time:%s)+++++++\n",
pTestResult->pTestModule->pName, pTestResult->pTestSuite->pName, pCase->pName, time_str);