]> WPIA git - cassiopeia.git/blob - test/src/sql.cpp
upd: fix problem with tm-initializer
[cassiopeia.git] / test / src / sql.cpp
1 #include <boost/test/unit_test.hpp>
2 #include <memory.h>
3 #include <db/mysql.h>
4 #include <config.h>
5
6 #include <fstream>
7
8 extern std::string sqlHost, sqlUser, sqlPass, sqlDB;
9
10 BOOST_AUTO_TEST_SUITE( TestSQL )
11
12 BOOST_AUTO_TEST_CASE( testSQL ) {
13     std::ifstream conf( "config.txt" );
14
15     if( !conf ) {
16         BOOST_WARN_MESSAGE( 0, "Config file is missing. Exiting." );
17         return;
18     }
19
20     BOOST_REQUIRE( parseConfig( "config.txt" ) == 0 );
21     std::shared_ptr<MySQLJobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
22     BOOST_REQUIRE( jp->query( "TRUNCATE TABLE profiles" ).first == 0 );
23     BOOST_REQUIRE( jp->query( "TRUNCATE TABLE certs" ).first == 0 );
24     BOOST_REQUIRE( jp->query( "TRUNCATE TABLE certAvas" ).first == 0 );
25     BOOST_REQUIRE( jp->query( "TRUNCATE TABLE subjectAlternativeNames" ).first == 0 );
26     BOOST_REQUIRE( jp->query( "TRUNCATE TABLE jobs" ).first == 0 );
27     BOOST_REQUIRE( jp->query( "INSERT INTO profiles SET id='1', keyname='assured', keyUsage='', extendedKeyUsage='', name='assured'" ).first == 0 );
28     BOOST_REQUIRE( jp->query( "INSERT INTO jobs SET task='sign', targetId='1'" ).first == 0 );
29
30     std::shared_ptr<Job> job = jp->fetchJob();
31     BOOST_REQUIRE( job );
32     jp->failJob( job );
33     BOOST_REQUIRE_EQUAL( job->target, "1" );
34     BOOST_REQUIRE_EQUAL( job->task, "sign" );
35     job = jp->fetchJob();
36     BOOST_REQUIRE( job );
37     std::shared_ptr<TBSCertificate> cert = jp->fetchTBSCert( job );
38     BOOST_REQUIRE( !cert );
39     BOOST_REQUIRE( jp->query( "INSERT INTO certs SET csr_type='CSR', id='1', profile='1'" ).first == 0 );
40     BOOST_REQUIRE( jp->query( "INSERT INTO subjectAlternativeNames SET certId='1', contents='example.org', type='DNS'" ).first == 0 );
41     BOOST_REQUIRE( jp->query( "INSERT INTO certAvas SET certid='1', name='CN', value='example.org'" ).first == 0 );
42     cert = jp->fetchTBSCert( job );
43     BOOST_REQUIRE( cert );
44
45     std::shared_ptr<SignedCertificate> fcert( new SignedCertificate() );
46     fcert->certificate = "CERT";
47     fcert->serial = "1234";
48     fcert->crt_name = "crt.name.crt";
49     fcert->ca_name = "assured";
50     jp->writeBack( job, fcert );
51     jp->finishJob( job );
52     BOOST_REQUIRE( !jp->fetchJob() );
53     BOOST_REQUIRE( jp->query( "INSERT INTO jobs SET task='revoke', targetId='1'" ).first == 0 );
54     job = jp->fetchJob();
55     BOOST_REQUIRE_EQUAL( job->target, "1" );
56     BOOST_REQUIRE_EQUAL( job->task, "revoke" );
57     std::pair<std::string, std::string> revocationInfo = jp->getRevocationInfo( job );
58     BOOST_REQUIRE_EQUAL( revocationInfo.first, "1234" );
59     BOOST_REQUIRE_EQUAL( revocationInfo.second, "assured" );
60     jp->writeBackRevocation( job, "2000-01-01 01:01:01" );
61     jp->finishJob( job );
62 }
63
64 BOOST_AUTO_TEST_CASE( testSQLDisconnected ) {
65     std::ifstream conf( "config.txt" );
66
67     if( !conf ) {
68         BOOST_WARN_MESSAGE( 0, "Config file is missing. Exiting." );
69         return;
70     }
71
72     //if(1) return;
73     //BOOST_REQUIRE( parseConfig("config.txt") == 0 );
74     std::shared_ptr<MySQLJobProvider> jp( new MySQLJobProvider( sqlHost, sqlUser, sqlPass, sqlDB ) );
75     jp->disconnect();
76     jp->disconnect();
77     BOOST_REQUIRE( jp->query( "SELECT 1" ).first );
78     BOOST_REQUIRE_THROW( jp->escape_string( "uia" ), const char* );
79     BOOST_REQUIRE_THROW( jp->finishJob( std::shared_ptr<Job>() ), const char* );
80     BOOST_REQUIRE_THROW( jp->failJob( std::shared_ptr<Job>() ), const char* );
81 }
82
83 BOOST_AUTO_TEST_SUITE_END()