LCOV - code coverage report
Current view: top level - tests - test_cJSON_deploy.c (source / functions) Hit Total Coverage
Test: deployctl-0.3.15.2.96a2d Code Coverage Lines: 115 115 100.0 %
Date: 2018-06-22 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             : test_cJSON_deploy.c
       3             :  Created by Danny Goossen, Gioxa Ltd on 26/3/17.
       4             : 
       5             :  MIT License
       6             : 
       7             :  Copyright (c) 2017 deployctl, Gioxa Ltd.
       8             : 
       9             :  Permission is hereby granted, free of charge, to any person obtaining a copy
      10             :  of this software and associated documentation files (the "Software"), to deal
      11             :  in the Software without restriction, including without limitation the rights
      12             :  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      13             :  copies of the Software, and to permit persons to whom the Software is
      14             :  furnished to do so, subject to the following conditions:
      15             : 
      16             :  The above copyright notice and this permission notice shall be included in all
      17             :  copies or substantial portions of the Software.
      18             : 
      19             :  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      20             :  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      21             :  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      22             :  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      23             :  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      24             :  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      25             :  SOFTWARE.
      26             : 
      27             :  */
      28             : 
      29             : 
      30             : 
      31             : //LCOV_EXCL_START
      32             : #include <check.h>
      33             : //LCOV_EXCL_STOP
      34             : 
      35             : #include "../src/deployd.h"
      36             : 
      37             : 
      38             : 
      39           3 : static void setup(void)
      40             : {
      41             : 
      42           3 : }
      43             : 
      44           3 : static void teardown(void)
      45             : {
      46             : 
      47           3 : }
      48             : 
      49             : 
      50           1 : START_TEST(check_env_cjson)
      51             : {
      52           1 :   struct trace_Struct * trace=NULL;
      53           1 :   init_dynamic_trace( &trace,"token","url",1);
      54             : 
      55           1 :   int result=0;
      56             :    // OK situation
      57           1 :    cJSON *environment = cJSON_CreateObject();
      58           1 :    ck_assert_ptr_ne(environment,NULL);
      59           1 :    cJSON_AddItemToObject(environment,"CI_PROJECT_PATH" , cJSON_CreateString("deployctl/test_deploy_release" ));
      60           1 :    cJSON_AddItemToObject(environment,"CI_PROJECT_PATH_SLUG" , cJSON_CreateString("deployctl-test-deploy-release" ));
      61           1 :    cJSON_AddItemToObject(environment,"DEPLOY_PROJECT_PATH_SLUG" , cJSON_CreateString("DEPLOY_PROJECT_PATH_SLUG" ));
      62           1 :    cJSON_AddItemToObject(environment,"GITLAB_PROJECT_PATH_SLUG" , cJSON_CreateString("GITLAB_PROJECT_PATH_SLUG" ));
      63           1 :    cJSON_AddItemToObject(environment,"blabla" , cJSON_CreateString("deployctl-test-deploy-release" ));
      64           1 :    char **newevpp=NULL;
      65             : 
      66           1 :    char * tt=NULL;
      67           1 :    result=get_key(environment, "CI_PROJECT_PATH", &tt);
      68           2 :    ck_assert_int_eq( result,0);
      69           2 :    ck_assert_str_eq(tt,"deployctl/test_deploy_release");
      70             : 
      71             :    // if no place to put the output, it's still ok
      72           1 :    result=get_key(environment, "CI_PROJECT_PATH", NULL);
      73           2 :    ck_assert_int_eq( result,0);
      74             : 
      75           1 :    result=parse_env(environment, &newevpp);
      76           2 :    ck_assert_int_eq( result,0);
      77             : 
      78           1 :    cJSON * parsed_environment=cJSON_Create_env_obj(newevpp);
      79             : 
      80           2 :    ck_assert_ptr_ne(parsed_environment,NULL);
      81             : 
      82             :    // should only get environment with DEPLOY_ / GITLAB_ / CI_
      83           1 :    tt=NULL;
      84           1 :    result=get_key(parsed_environment, "blabla", &tt);
      85           2 :    ck_assert_int_ne( result,0);
      86           2 :    ck_assert_ptr_eq(tt,NULL);
      87             : 
      88           1 :    result=get_key(parsed_environment, NULL, &tt);
      89           2 :    ck_assert_int_eq( result,-2);
      90           2 :    ck_assert_ptr_eq(tt,NULL);
      91             : 
      92             :    // free newevpp
      93           1 :    free_envp(&newevpp);
      94           2 :    ck_assert_ptr_eq(newevpp,NULL);
      95             : 
      96             :    // normal ok check no missing item
      97           2 :    result=check_presence(parsed_environment,
      98             :                   (char *[])
      99           1 :    {"CI_PROJECT_PATH_SLUG","CI_PROJECT_PATH",NULL}
     100             :                   , trace);
     101           2 :    ck_assert_int_eq( result,0);
     102             : 
     103             : 
     104             :    // missing item
     105           2 :    result=check_presence(parsed_environment,
     106             :                   (char *[])
     107           1 :    {"CI_PROJECT_PATH_SLUG","CI_PROJECT_PATH","mytest",NULL}
     108             :                   , trace);
     109           2 :    ck_assert_int_ne( result,0);
     110             :    // missing item and no output buffer
     111           1 :    struct trace_Struct * out_null=NULL;
     112           1 :    result=check_presence(parsed_environment,
     113             :                  (char *[])
     114           1 :    {"CI_PROJECT_PATH_SLUG","CI_PROJECT_PATH","mytest",NULL}
     115             :                  , out_null);
     116           2 :    ck_assert_int_ne( result,0);
     117             : 
     118             :    // have no environment
     119           1 :    result=check_presence(NULL,
     120             :                  (char *[])
     121           1 :    {"CI_PROJECT_PATH_SLUG","CI_PROJECT_PATH","mytest",NULL}
     122             :                  , out_null);
     123           2 :    ck_assert_int_eq( result,-1);
     124             : 
     125             :    // have no list
     126           1 :    result=check_presence(parsed_environment, NULL, out_null);
     127           2 :    ck_assert_int_ne( result,0);
     128             : 
     129             :    // have no buffer
     130           1 :    result=check_presence(parsed_environment,
     131             :                     (char *[])
     132           1 :      {"CI_PROJECT_PATH_SLUG","CI_PROJECT_PATH","mytest","onemore",NULL}
     133             :                     , NULL);
     134           2 :    ck_assert_int_eq( result,-1); // two missing
     135           1 :    cJSON_Delete(parsed_environment);
     136           1 :    cJSON_Delete(environment);
     137           1 :    free_dynamicbuf(&trace);
     138             : }
     139           1 : END_TEST
     140             : 
     141             : 
     142             : 
     143             : 
     144           1 : START_TEST(check_extra_cJson)
     145             : {
     146             :    // check strdup_n
     147             : 
     148             :    char todup[1024];
     149           1 :    sprintf(todup, "todupstring");
     150           1 :    char * dupped=NULL;
     151           1 :    dupped=cJSON_strdup_n((unsigned char *)todup, 5);
     152           1 :    ck_assert_ptr_ne(dupped,NULL);
     153             : 
     154           1 :    dupped=cJSON_strdup_n(NULL, 5);
     155           2 :    ck_assert_ptr_eq(dupped,NULL);
     156             : 
     157             :    // additem
     158             :    cJSON * obj;
     159           1 :    cJSON * test =cJSON_CreateObject();
     160           1 :    cJSON * item =cJSON_CreateObject();
     161           2 :    item->string =strdup("xxxx");
     162           1 :    item->valuestring=strdup("item2");
     163             : 
     164           1 :    cJSON_AddItemToObject(test,"ITEM1" , cJSON_CreateString("item1" ));
     165           1 :    cJSON_AddItemToObject_n(test, todup,5 , item);
     166           1 :    obj=cJSON_GetObjectItem(test,"todup");
     167           1 :    ck_assert_ptr_ne(obj,NULL);
     168           1 :    cJSON_AddItemToObject_n(test, todup,5 , cJSON_CreateString("todup"));
     169             : 
     170           1 :    cJSON_AddItemToObject_n(test, todup,6 , NULL);
     171             : 
     172           1 :    obj=cJSON_GetObjectItem(test,"todups");
     173           2 :    ck_assert_ptr_eq(obj,NULL);
     174           1 :    cJSON_Delete(test);
     175             : 
     176             :    // cJSON_AddItemToBeginArray
     177           1 :    cJSON * arr = cJSON_CreateArray();
     178           1 :    cJSON_AddItemToBeginArray(arr, cJSON_CreateString("item1" ));
     179           1 :    cJSON_AddItemToBeginArray(arr, cJSON_CreateString("before_item1" ));
     180             : 
     181             :    // check NULL item
     182           1 :    cJSON_AddItemToBeginArray(arr, NULL);
     183             : 
     184             :    // check if correct order
     185           1 :    dupped=cJSON_GetArrayItem(arr,0)->valuestring;
     186           2 :    ck_assert_str_eq(dupped,"before_item1");
     187           1 :    dupped=cJSON_GetArrayItem(arr,1)->valuestring;
     188           2 :    ck_assert_str_eq(dupped,"item1");
     189           1 :    cJSON_Delete(arr);
     190             : 
     191             :    // check if no array
     192           1 :    cJSON_AddItemToBeginArray(NULL, cJSON_CreateString("item1" ));
     193             : 
     194             : }
     195           1 : END_TEST
     196             : 
     197             : 
     198           1 : START_TEST(check_in_string_array)
     199             : {
     200           1 :    cJSON * list=NULL;
     201           1 :    ck_assert_int_eq(in_string_array(list, "notright"),-3);
     202           1 :    list=cJSON_CreateStringArray((const char *[]){"notright","test","right"}, 3);
     203           2 :    ck_assert_int_eq(in_string_array(list, NULL),0); // if search ==NULL, result if in
     204           2 :    ck_assert_int_eq(in_string_array(list, "notright"),0);
     205           2 :    ck_assert_int_eq(in_string_array(list, "right"),0);
     206           2 :    ck_assert_int_eq(in_string_array(list, "notinlist"),1);
     207           1 :    cJSON_Delete(list);
     208             : }
     209           1 : END_TEST
     210             : 
     211             : 
     212             : 
     213             : 
     214             : 
     215           4 : Suite * cJson_deploy_suite(void)
     216             : {
     217             :    Suite *s;
     218             :    TCase *tc_core;
     219             :    //TCase *tc_progress;
     220           4 :    s = suite_create("test_cJSON_deploy_suite");
     221             :    /* Core test case */
     222           4 :    tc_core = tcase_create("Core");
     223           4 :    tcase_add_checked_fixture(tc_core, setup, teardown);
     224             :    //tcase_add_unchecked_fixture(tc_core, setup, teardown);
     225           4 :    tcase_set_timeout(tc_core,15);
     226           4 :    tcase_add_test(tc_core, check_extra_cJson);
     227           4 :    tcase_add_test(tc_core, check_env_cjson);
     228           4 :    tcase_add_test(tc_core, check_in_string_array);
     229           4 :    suite_add_tcase(s, tc_core);
     230           4 :    return s;
     231             : }
     232             : 
     233           4 : int main(void)
     234             : {
     235             :    int number_failed;
     236             :    Suite *s;
     237             :    SRunner *sr;
     238           4 :    s = cJson_deploy_suite();
     239           4 :    sr = srunner_create(s);
     240           4 :    srunner_run_all(sr, CK_VERBOSE | CK_NOFORK);
     241           1 :    number_failed = srunner_ntests_failed(sr);
     242           1 :    srunner_free(sr);
     243           1 :    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
     244             : }

Generated by: LCOV version 1.10