Gabriele Contini
2020-01-11 1d1082fe695366da3498f528b953203a90fe7385
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
#define BOOST_TEST_MODULE test_volid
 
#include <boost/test/unit_test.hpp>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <boost/filesystem.hpp>
#include <licensecc_properties.h>
#include <licensecc_properties_test.h>
 
#include <licensecc/licensecc.h>
#include "../../src/library/ini/SimpleIni.h"
#include "../../src/library/pc-identifiers.h"
#include "../../src/library/os/os.h"
#include "generate-license.h"
 
namespace fs = boost::filesystem;
using namespace license;
using namespace std;
 
namespace license {
namespace test {
 
BOOST_AUTO_TEST_CASE(default_volid_lic_file) {
    PcSignature identifier_out;
 
    const LCC_API_IDENTIFICATION_STRATEGY strategy = LCC_API_IDENTIFICATION_STRATEGY::STRATEGY_ETHERNET;
    BOOST_TEST_CHECKPOINT("Before generate");
    const FUNCTION_RETURN generate_ok = generate_user_pc_signature(identifier_out, strategy);
    BOOST_TEST_CHECKPOINT("After generate signature");
    BOOST_ASSERT(generate_ok == FUNCTION_RETURN::FUNC_RET_OK);
    cout << "Identifier:" << identifier_out << endl;
    vector<string> extraArgs;
    extraArgs.push_back("-s");
    extraArgs.push_back(identifier_out);
    BOOST_TEST_CHECKPOINT("Before generate license");
    const string licLocation = generate_license("volid_license", extraArgs);
 
    LicenseInfo license;
    LicenseLocation location = {LICENSE_PATH};
    std::copy(licLocation.begin(), licLocation.end(), location.licenseData);
    const LCC_EVENT_TYPE result = acquire_license(nullptr, &location, &license);
    BOOST_CHECK_EQUAL(result, LICENSE_OK);
    BOOST_CHECK_EQUAL(license.has_expiry, false);
    BOOST_CHECK_EQUAL(license.linked_to_pc, true);
}
 
static void generate_reference_file(const string &idfileLocation, LCC_API_IDENTIFICATION_STRATEGY strategies[],
                                    int num_strategies) {
    ofstream idfile(idfileLocation);
    PcSignature identifier_out;
    for (int i = 0; i < num_strategies; i++) {
        FUNCTION_RETURN generate_ok = generate_user_pc_signature(identifier_out, strategies[i]);
        if (generate_ok != FUNC_RET_OK) {
            BOOST_ERROR("Generating identifier for strategy " << strategies[i] << " failed with: " << generate_ok);
            idfile << "0000-0000-0000-0000" << endl;
            BOOST_ASSERT(generate_ok == FUNC_RET_OK);
        } else
            idfile << identifier_out << endl;
    }
    idfile.close();
}
 
BOOST_AUTO_TEST_CASE(generated_identifiers_stability) {
    const string idfileLocation(PROJECT_TEST_TEMP_DIR "/identifiers_file");
    std::vector<LCC_API_IDENTIFICATION_STRATEGY> strategies;
    size_t disk_num;
    getDiskInfos(NULL, &disk_num);
    if (disk_num > 0) {
        strategies = {STRATEGY_DEFAULT, STRATEGY_DISK_NUM, STRATEGY_DISK_LABEL};
    } else {
        BOOST_TEST_CHECKPOINT("if no disk default strategy fails see #49");
        // strategies = { DEFAULT };
        strategies = {};
    }
    size_t adapters;
    getAdapterInfos(nullptr, &adapters);
    if (adapters > 0) {
        strategies.push_back(STRATEGY_ETHERNET);
    }
 
    size_t num_strategies = strategies.size();
    if (num_strategies == 0) {
        // see issue #49 can't use default
        return;
    }
    std::ifstream test_idfile_exist(idfileLocation);
    if (!test_idfile_exist.good()) {
        generate_reference_file(idfileLocation, strategies.data(), strategies.size());
    } else {
        std::istream_iterator<string> start(test_idfile_exist), end;
        std::vector<string> reference_signatures(start, end);
        test_idfile_exist.close();
        if (reference_signatures.size() != num_strategies ||
            std::find(reference_signatures.begin(), reference_signatures.end(), "0000-0000-0000-0000") !=
                reference_signatures.end())
            generate_reference_file(idfileLocation, strategies.data(), num_strategies);
    }
    std::ifstream is(idfileLocation);
    std::istream_iterator<string> start(is), end;
    std::vector<string> reference_signatures(start, end);
    BOOST_TEST_CHECKPOINT("Generating current signatures and comparing with past");
    for (int i = 0; i < num_strategies; i++) {
        PcSignature generated_identifier;
        FUNCTION_RETURN generate_ok = generate_user_pc_signature(generated_identifier, strategies[i]);
        BOOST_ASSERT(generate_ok == FUNCTION_RETURN::FUNC_RET_OK);
        if (generate_ok != FUNC_RET_OK) {
            BOOST_ERROR("Generating identifier for strategy " << strategies[i] << " failed with: " << generate_ok);
            continue;
        }
        if (reference_signatures[i] != generated_identifier) {
            string message = string("pc signature compare fail: strategy: ") +
                             to_string(static_cast<long long>(strategies[i])) + " generated: [" + generated_identifier +
                             "] reference: [" + reference_signatures[i] + "]";
            BOOST_ERROR(message);
        }
    }
    BOOST_TEST_CHECKPOINT("Verifying signatures");
    for (int j = 0; j < 100; j++) {
        for (unsigned int i = 0; i < reference_signatures.size(); i++) {
            if (reference_signatures[i] == "0000-0000-0000-0000") continue;
            PcSignature pcsig;
            strncpy(pcsig, reference_signatures[i].c_str(), sizeof(PcSignature) - 1);
            LCC_EVENT_TYPE val_result = validate_pc_signature(pcsig);
            BOOST_TEST_CHECKPOINT("Verifying signature: ");
            BOOST_CHECK_EQUAL(val_result, LICENSE_OK);
        }
    }
}
 
}  // namespace test
}  // namespace license