summaryrefslogtreecommitdiffstats
path: root/certmanager/lib/tests/test.data
blob: 5a63ff09179478ba9336bb58fed36c23ca12953c (plain)
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
/*
    tests/test_uiserver.cpp

    This file is part of Kleopatra, the KDE keymanager
    Copyright (c) 2007 Klarälvdalens Datakonsult AB

    Kleopatra is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kleopatra is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

//
// Usage: test_uiserver <socket> --verify-detached <signed data> <signature>
//
#ifndef _ASSUAN_ONLY_GPG_ERRORS
#define _ASSUAN_ONLY_GPG_ERRORS
#endif
#include "../uiserver/kleo-assuan.h"
#include <gpg-error.h>

#include <QtCore>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace Kleo;

static std::vector<int> inFDs, outFDs, msgFDs;
static std::vector<std::string> inFiles, outFiles, msgFiles;
static std::map<std::string,std::string> inquireData;

static std::string hexencode( const std::string & in ) {
    std::string result;
    result.reserve( 3 * in.size() );

    static const char hex[] = "0123456789ABCDEF";

    for ( std::string::const_iterator it = in.begin(), end = in.end() ; it != end ; ++it )
        switch ( const unsigned char ch = *it ) {
        default:
            if ( ch >= '!' && ch <= '~' || ch > 0xA0 ) {
                result += ch;
                break;
            }
            // else fall through
        case ' ':
            result += '+';
            break;
        case '"':
        case '#':
        case '$':
        case '%':
        case '\'':
        case '+':
        case '=':
            result += '%';
            result += hex[ (ch & 0xF0) >> 4 ];
            result += hex[ (ch & 0x0F)      ];
            break;
        }
    
    return result;
}

static void usage( const std::string & msg=std::string() ) {
    std::cerr << msg << std::endl <<
        "\n"
        "Usage: test_uiserver <socket> [<io>] [<options>] [<inquire>] command [<args>]\n"
        "where:\n"
        "      <io>: [--input[-fd] <file>] [--output[-fd] <file>] [--message[-fd] <file>]\n"
        " <options>: *[--option name=value]\n"
        " <inquire>: [--inquire keyword=<file>]\n";
    exit( 1 );
}

static int data( void * void_ctx, const void * buffer, size_t len ) {
    (void)void_ctx; (void)buffer; (void)len;
    return 0; // ### implement me
}

static int status( void * void_ctx, const char * line ) {
    (void)void_ctx; (void)line;
    return 0;
}

static int inquire( void * void_ctx, const char * keyword ) {
    assuan_context_t ctx = (assuan_context_t)void_ctx;
    assert( ctx );
    const std::map<std::string,std::string>::const_iterator it = inquireData.find( keyword );
    if ( it == inquireData.end() )
        return gpg_error( GPG_ERR_UNKNOWN_COMMAND );

    if ( !it->second.empty() && it->second[0] == '@' )
        return gpg_error( GPG_ERR_NOT_IMPLEMENTED );

    if ( const gpg_error_t err = assuan_send_data( ctx, it->second.c_str(), it->second.size() ) ) {
        tqDebug( "assuan_write_data: %s", gpg_strerror( err ) );
        return err;
    }

    return 0;
}

int main( int argc, char * argv[] ) {

    assuan_set_assuan_err_source( GPG_ERR_SOURCE_DEFAULT );

    if ( argc < 3 )
        usage(); // need socket and command, at least

    const char * socket = argv[1];

    std::vector<const char*> options;

    std::string command;
    for ( int optind = 2 ; optind < argc ; ++optind ) {
        const char * const arg = argv[optind];
        if ( qstrcmp( arg, "--input-fd" ) == 0 ) {
            int inFD;
            if ( (inFD = open( argv[++optind], O_RDONLY )) == -1 ) {
                perror( "--input-fd open()" );
                return 1;
            }
            inFDs.push_back( inFD );
        } else if ( qstrcmp( arg, "--output-fd" ) == 0 ) {
            int outFD;
            if ( (outFD = open( argv[++optind], O_WRONLY|O_CREAT )) ==  -1 ) {
                perror( "--output-fd open()" );
                return 1;
            }
            outFDs.push_back( outFD );
        } else if ( qstrcmp( arg, "--message-fd" ) == 0 ) {
            int msgFD;
            if ( (msgFD = open( argv[++optind], O_RDONLY )) ==  -1 ) {
                perror( "--message-fd open()" );
                return 1;
            }
            msgFDs.push_back( msgFD );
        } else if ( qstrcmp( arg, "--input" ) == 0 ) {
            const std::string file = argv[++optind];
            inFiles.push_back( file );
        } else if ( qstrcmp( arg, "--output" ) == 0 ) {
            const std::string file = argv[++optind];
            outFiles.push_back( file );
        } else if ( qstrcmp( arg, "--message" ) == 0 ) {
            const std::string file = argv[++optind];
            msgFiles.push_back( file );
        } else if ( qstrcmp( arg, "--option" ) == 0 ) {
            options.push_back( argv[++optind] );
        } else if ( qstrcmp( arg, "--inquire" ) == 0 ) {
            const std::string inqval = argv[++optind];
            const size_t pos = inqval.find( '=' );
            // ### implement indirection with "@file"...
            inquireData[inqval.substr( 0, pos )] = inqval.substr( pos+1 );
        } else {
            while ( optind < argc ) {
                if ( !command.empty() )
                    command += ' ';
                command += argv[optind++];
            }
        }
    }
    if ( command.empty() )
        usage( "Command expected, but only options found" );

    assuan_context_t ctx = 0;

    if ( const gpg_error_t err = assuan_socket_connect_ext( &ctx, socket, -1, 1 ) ) {
        tqDebug( "%s", assuan_exception( err, "assuan_socket_connect_ext" ).what() );
        return 1;
    }

    assuan_set_log_stream( ctx, stderr );

    for ( std::vector<int>::const_iterator it = inFDs.begin(), end = inFDs.end() ; it != end ; ++it ) {
        if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) {
            tqDebug( "%s", assuan_exception( err, "assuan_sendfd( inFD )" ).what() );
            return 1;
        }

        if ( const gpg_error_t err = assuan_transact( ctx, "INPUT FD", 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, "INPUT FD" ).what() );
            return 1;
        }
    }

    
    for ( std::vector<std::string>::const_iterator it = inFiles.begin(), end = inFiles.end() ; it != end ; ++it ) {
        char buffer[1024];
        sprintf( buffer, "INPUT FILE=%s", hexencode( *it ).c_str() );

        if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, buffer ).what() );
            return 1;
        }
    }

    
    for ( std::vector<int>::const_iterator it = msgFDs.begin(), end = msgFDs.end() ; it != end ; ++it ) {
        if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) {
            tqDebug( "%s", assuan_exception( err, "assuan_sendfd( msgFD )" ).what() );
            return 1;
        }

        if ( const gpg_error_t err = assuan_transact( ctx, "MESSAGE FD", 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, "MESSAGE FD" ).what() );
            return 1;
        }
    }

    
    for ( std::vector<std::string>::const_iterator it = msgFiles.begin(), end = msgFiles.end() ; it != end ; ++it ) {
        char buffer[1024];
        sprintf( buffer, "MESSAGE FILE=%s", hexencode( *it ).c_str() );

        if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, buffer ).what() );
            return 1;
        }
    }

    
    for ( std::vector<int>::const_iterator it = outFDs.begin(), end = outFDs.end() ; it != end ; ++it ) {
        if ( const gpg_error_t err = assuan_sendfd( ctx, *it ) ) {
            tqDebug( "%s", assuan_exception( err, "assuan_sendfd( outFD )" ).what() );
            return 1;
        }

        if ( const gpg_error_t err = assuan_transact( ctx, "OUTPUT FD", 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, "OUTPUT FD" ).what() );
            return 1;
        }
    }


    for ( std::vector<std::string>::const_iterator it = outFiles.begin(), end = outFiles.end() ; it != end ; ++it ) {
        char buffer[1024];
        sprintf( buffer, "OUTPUT FILE=%s", hexencode( *it ).c_str() );

        if ( const gpg_error_t err = assuan_transact( ctx, buffer, 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, buffer ).what() );
            return 1;
        }
    }

    
    
    Q_FOREACH( const char * opt, options ) {
        std::string line = "OPTION ";
        line += opt;
        if ( const gpg_error_t err = assuan_transact( ctx, line.c_str(), 0, 0, 0, 0, 0, 0 ) ) {
            tqDebug( "%s", assuan_exception( err, line ).what() );
            return 1;
        }
    }

    if ( const gpg_error_t err = assuan_transact( ctx, command.c_str(), data, ctx, inquire, ctx, status, ctx ) ) {
        tqDebug( "%s", assuan_exception( err, command ).what() );
        return 1;
    }

    assuan_disconnect( ctx );
    
    return 0;
}