File: tests.h

package info (click to toggle)
yapet 0.6-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,012 kB
  • ctags: 2,913
  • sloc: ansic: 13,661; cpp: 11,384; sh: 4,814; makefile: 847; yacc: 291; sed: 16
file content (130 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download | duplicates (2)
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
// -*- c++ -*-
//
// $Id: tests.h 2834 2009-08-31 18:15:47Z rafi $
//
// This file holds defines common to all tests
#include <stdio.h>
#include <iostream>
#include <stdexcept>

#include <partdec.h>
#include <structs.h>
#include <key.h>
#include <crypt.h>

#ifndef ROUNDS
# define ROUNDS 5000
#endif

#define FN "encryptiontest.gps"

#ifndef NAME
# define NAME "Test name %d"
#endif

#ifndef HOST
# define HOST "Test host %d"
#endif

#ifndef UNAME
# define UNAME "Test username %d"
#endif

#ifndef PW
# define PW "Test password %d"
#endif

#ifndef COMMENT
# define COMMENT "Test comment %d"
#endif

inline
void progress() {
    static int n = 0;

    // I change the progress indicator to somewhat less verbose, since the
    // above indicator looks terrible in build logs of automated builds, e.g.:
    // https://buildd.debian.org/pkg.cgi?pkg=yapet
    if ( (n % 100) == 0 && ! ( (n % 1000) == 0) ) {
        std::cout << ".";
        std::cout.flush();
    }

    if ( (n % 1000) == 0 && n != 0) {
        std::cout << "#";
        std::cout.flush();
    }

    n++;
}

inline
void print_record (YAPET::PartDec& pd, const YAPET::Key& key) {
    YAPET::Crypt crypt (key);
    std::cout << "PartDec Name:\t" << pd.getName() << std::endl;
    const YAPET::BDBuffer& enc_rec = pd.getEncRecord();
    YAPET::Record<YAPET::PasswordRecord>* ptr_dec_rec = crypt.decrypt<YAPET::PasswordRecord> (enc_rec);
    YAPET::PasswordRecord* ptr_pw = *ptr_dec_rec;
    std::cout << "\tName:\t" << ptr_pw->name << std::endl;
    std::cout << "\tHost:\t" << ptr_pw->host << std::endl;
    std::cout << "\tUname:\t" << ptr_pw->username << std::endl;
    std::cout << "\tPW:\t" << ptr_pw->password << std::endl;
    std::cout << "\tCMT:\t" << ptr_pw->comment << std::endl;
    std::cout << std::endl;
    delete ptr_dec_rec;
}

inline
void check_record (YAPET::PartDec& pd, const YAPET::Key& key, int rec_no) {
    progress();
    YAPET::Crypt crypt (key);
    char _name[YAPET::NAME_SIZE];
    char _host[YAPET::HOST_SIZE];
    char _uname[YAPET::USERNAME_SIZE];
    char _pw[YAPET::PASSWORD_SIZE];
    char _cmt[YAPET::COMMENT_SIZE];
    snprintf (_name, YAPET::NAME_SIZE, NAME, rec_no);
    snprintf (_host, YAPET::HOST_SIZE, HOST, rec_no);
    snprintf (_uname, YAPET::USERNAME_SIZE, UNAME, rec_no);
    snprintf (_pw, YAPET::PASSWORD_SIZE, PW, rec_no);
    snprintf (_cmt, YAPET::COMMENT_SIZE, COMMENT, rec_no);

    if (strncmp (_name, (char*) pd.getName(), YAPET::NAME_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("Name does not match");
    }

    const YAPET::BDBuffer& enc_rec = pd.getEncRecord();

    YAPET::Record<YAPET::PasswordRecord>* ptr_dec_rec = crypt.decrypt<YAPET::PasswordRecord> (enc_rec);

    YAPET::PasswordRecord* ptr_pw = *ptr_dec_rec;

    if (strncmp (_name, (char*) ptr_pw->name, YAPET::NAME_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("Name does not match");
    }

    if (strncmp (_host, (char*) ptr_pw->host, YAPET::HOST_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("Host does not match");
    }

    if (strncmp (_uname, (char*) ptr_pw->username, YAPET::USERNAME_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("username does not match");
    }

    if (strncmp (_pw, (char*) ptr_pw->password, YAPET::PASSWORD_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("password does not match");
    }

    if (strncmp (_cmt, (char*) ptr_pw->comment, YAPET::COMMENT_SIZE) != 0) {
        print_record (pd, key);
        throw std::logic_error ("comment does not match");
    }

    delete ptr_dec_rec;
    /*    std::cout << "\r"; */
}