File: davix_test_lib.cpp

package info (click to toggle)
davix 0.8.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,184 kB
  • sloc: ansic: 164,612; cpp: 38,741; python: 17,726; perl: 14,124; sh: 13,458; xml: 3,567; makefile: 1,959; javascript: 885; pascal: 570; lisp: 7
file content (131 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download | duplicates (3)
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

#include "davix_test_lib.h"

#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <sstream>


using namespace Davix;


int mycred_auth_callback_x509(void* userdata, const SessionInfo & info, X509Credential * cred, DavixError** err){
    (void) info;
    Davix::DavixError* tmp_err= NULL;

    std::string path((char*) userdata);
    int ret = cred->loadFromFileP12( path, "", &tmp_err);

    if(ret != 0){
        fprintf(stderr, " FATAL authentication Error : %s", tmp_err->getErrMsg().c_str());
        DavixError::propagateError(err, tmp_err);
    }
    return ret;
}




char* generate_random_uri(const char* uri_dir, const char* prefix, char* buff, size_t s_buff){
    snprintf(buff, s_buff, "%s/%s_%d%ld%ld",uri_dir, prefix, (int)getpid() ,(long) time(NULL), (long) rand());
    return buff;
}

Davix::Uri generate_random_uri(const Davix::Uri & url_directory, const std::string & prefix){
    std::ostringstream ss;
    ss << url_directory << "/" << prefix << static_cast<int>(getpid()) << static_cast<long>(time(NULL)) << static_cast<long>(rand());
    return Davix::Uri(ss.str());
}

void configure_grid_env(char * cert_path, RequestParams&  p){

    if(strcmp(cert_path, "proxy") == 0){ // VOMS PROXY MODE
        X509Credential x;
        std::string proxy_path;
        char* p_str;
        if ( (p_str = getenv("X509_USER_PROXY") ) != NULL ){
            proxy_path = p_str;
        }else{
            std::ostringstream ss;
            ss << "/tmp/x509up_u" << getuid();
            proxy_path = ss.str();
        }
        x.loadFromFilePEM(proxy_path,proxy_path,"", NULL);
        p.setClientCertX509(x);
    } else{
        char login_passwd[strlen(cert_path)+1];
        char* pstr;
        strcpy(login_passwd, cert_path);
        pstr = strchr(login_passwd, ':');
        if( pstr != NULL){
            *pstr= '\0';
            pstr++;
            p.setClientLoginPassword(std::string(login_passwd), std::string(pstr));
        }
        p.setClientCertCallbackX509(&mycred_auth_callback_x509, cert_path);
    }

    // add standard grid certificate for wlcg test
    p.addCertificateAuthorityPath("/etc/grid-security/certificates/");
    // add wrong cert path for testing purpose
    p.addCertificateAuthorityPath("/Iamnotexisting");
    p.addCertificateAuthorityPath("");
    p.addCertificateAuthorityPath("/etc/group"); // add a file



}


char * generate_random_string_content(size_t size){
    char * res = (char*) malloc(size * sizeof(char));
    size_t i =0;
    while(i < size){
        res[i]= (char) (((rand()%2)?65:97)+(rand()%26));
        i++;
    }
    return res;
}


/*
DAVIX_C_DECL_BEGIN


void configure_grid_env_c(char * cert_path, davix_params_t  params){


    davix_error_t tmp_err=NULL;
    char login_passwd[strlen(cert_path)+1];
    char* pstr;

    strcpy(login_passwd, cert_path);
    pstr = strchr(login_passwd, ':');
    if( pstr != NULL){
        *pstr= '\0';
        pstr++;
        davix_params_set_login_passwd(params, login_passwd, pstr);
    }else{
        davix_x509_cert_t cred = davix_x509_cert_new();
        davix_x509_cert_load_from_p12(cred, cert_path, "", &tmp_err);
        if(tmp_err){
            std::cerr << " failure when load cert : " << davix_error_msg(tmp_err) << std::endl;
            exit(-1);
        }
        davix_params_set_client_cert_X509(params, cred);
        davix_x509_cert_free(cred);

    }

    davix_params_set_ssl_check(params, false, &tmp_err);
}

DAVIX_C_DECL_END
*/