aboutsummaryrefslogtreecommitdiff
path: root/tools/tf_fuzz/utility/data_blocks.cpp
blob: 0bd0d79392fd50fcef8476fa6c9f9823b336fcbd (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
/*
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

/* These classes "cut down the clutter" by grouping together related data and
   associated methods (most importantly their constructors) used in template_
   line, psa_call, psa_asset (etc.). */

#include <string>
#include <vector>
#include <cstdint>

#include "class_forwards.hpp"

#include "boilerplate.hpp"
#include "gibberish.hpp"
#include "compute.hpp"
#include "string_ops.hpp"
#include "data_blocks.hpp"
#include "psa_asset.hpp"
#include "crypto_asset.hpp"
#include "find_or_create_asset.hpp"
#include "psa_call.hpp"
#include "template_line.hpp"
#include "tf_fuzz.hpp"



/**********************************************************************************
   Methods of class expect_info follow:
**********************************************************************************/

expect_info::expect_info (void)  // (default constructor)
{
    pf_nothing = false;  // by default, TF-Fuzz provides expected results
    pf_pass = pf_specified = false;
    pf_result_string.assign ("");  data.assign ("");
    data_var_specified = false;
    data_var.assign ("");  // name of expected-data variable
    data_specified = false;
    data.assign ("");
    pf_info_incomplete = true;
    n_exp_vars = -1;  // so the first reference is 0 (no suffix), then _1, _2, ...
}
expect_info::~expect_info (void)  // (destructor)
{}

void expect_info::set_pf_pass (void)
{
    pf_pass = true;
    pf_nothing = pf_specified = false;
    pf_result_string = "";
}

void expect_info::set_pf_nothing (void)
{
    pf_nothing = true;
    pf_pass = pf_specified = false;
    pf_result_string = "";
}

void expect_info::set_pf_error (string error)
{
    pf_specified = true;
    pf_result_string.assign (error);  // just default "guess," to be filled in
    pf_pass = pf_nothing = false;
}

/* The expected pass/fail results are not available from the parser until the call has
   already been created.  The flag, pf_info_incomplete, that indicates whether or not
   the "expects" information has been filled in.  If not, fill it in from the template,
   once that info has been parsed. */
void expect_info::copy_expect_to_call (psa_call *the_call)
{
    the_call->exp_data.pf_nothing = pf_nothing;
    the_call->exp_data.pf_pass = pf_pass;
    the_call->exp_data.pf_specified = pf_specified;
    the_call->exp_data.pf_result_string = pf_result_string;
    the_call->exp_data.pf_info_incomplete = false;
}

/**********************************************************************************
   End of methods of class expect_info.
**********************************************************************************/


/**********************************************************************************
   Class set_data_info methods regarding setting and getting asset-data values:
**********************************************************************************/

set_data_info::set_data_info (void)  // (default constructor)
{
    literal_data_not_file = true;  // currently, not using files as data sources
    string_specified = false;
    data.assign ("");
    random_data = false;
    file_specified = false;
    file_path.assign ("");
    n_set_vars = -1;  // so the first reference is 0 (no suffix), then _1, _2, ...
}
set_data_info::~set_data_info (void)  // (destructor)
{}

/* set() establishes:
   *  An asset's data value from a template line (e.g., set sst snort data "data
      value"), and
   *  *That* such a value was directly specified, as opposed to no data value having
      been specified, or a random data value being requested.
   Arguably, this method "has side effects," in that it not only sets a value, but
   also "takes notes" about where that value came from.
*/
void set_data_info::set (string set_val)
{
    literal_data_not_file = true;  // currently, not using files as data sources
    string_specified = true;
    data.assign (set_val);
}

/* set_calculated() establishes:
   *  An asset's data value as *not* taken from a template line, and
   *  *That* such a value was not directly specified in any template line, such as
      if a random data value being requested.
   Arguably, this method "has side effects," in that it not only sets a value, but
   also "takes notes" about where that value came from.
*/
void set_data_info::set_calculated (string set_val)
{
    literal_data_not_file = true;  // currently, not using files as data sources
    string_specified = false;
    data.assign (set_val);
}

/* randomize() establishes:
   *  An asset's data value as *not* taken from a template line, and
   *  *That* such a value was randomized.
   Arguably, this method "has side effects," in that it not only sets a value, but
   also "takes notes" about where that value came from.
*/
void set_data_info::randomize (void)
{
    gibberish gib;
    char gib_buff[4096];  // spew gibberish into here
    int rand_data_length = 0;

    string_specified = false;
    random_data = true;
    literal_data_not_file = true;
    rand_data_length = 40 + (rand() % 256);
        /* Note:  Multiple assets do get different random data */
    gib.sentence (gib_buff, gib_buff + rand_data_length - 1);
    data = gib_buff;
}

/* Getter for protected member, data.  Protected so that it can only be set by
   set() or set_calculated(), above, to establish not only its value but
   how it came about. */
string set_data_info::get (void)
{
    return data;
}

/* Currently, files as data sources aren't used, so this whole method is not "of
   use," but that might change at some point. */
bool set_data_info::set_file (string file_name)
{
    literal_data_not_file = true;
    string_specified = false;
    data.assign ("");
    file_specified = true;
    // Remove the ' ' quotes around the file name:
    file_name.erase (0, 1);
    file_name.erase (file_name.length()-1, 1);
    file_path = file_name;
    return true;
}

/**********************************************************************************
   End of methods of class set_data_info.
**********************************************************************************/


/**********************************************************************************
   Class asset_name_id_info methods regarding setting and getting asset-data values:
**********************************************************************************/

asset_name_id_info::asset_name_id_info (void)  // (default constructor)
{
    id_n_not_name = false;  // (arbitrary)
    id_n = 100 + (rand() % 10000);  // default to random ID# (e.g., SST UID)
    asset_name.assign ("");
    id_n_specified = name_specified = false;  // no ID info yet
    asset_name_vector.clear();
    asset_id_n_vector.clear();
    asset_type = psa_asset_type::unknown;
    the_asset = nullptr;
}
asset_name_id_info::~asset_name_id_info (void)
{
    asset_name_vector.clear();
    asset_id_n_vector.clear();
}

/* set_name() establishes:
   *  An asset's "human" name from a template line, and
   *  *That* that name was directly specified, as opposed to the asset being defined
      by ID only, or a random name being requested.
   Arguably, this method "has side effects," in that it not only sets a name, but
   also "takes notes" about where that name came from.
*/
void asset_name_id_info::set_name (string set_val)
{
    /* Use this to set the name as specified in the template file.  Call this only
       if the template file does indeed define a name. */
    name_specified = true;
    asset_name = set_val;
}

/* set_calc_name() establishes:
   *  An asset's "human" name *not* from a template line, and
   *  *That* that name was *not* directly specified in any template line.
   Arguably, this method "has side effects," in that it not only sets a name, but
   also "takes notes" about where that name came from.
*/
void asset_name_id_info::set_calc_name (string set_val)
{
    name_specified = false;
    asset_name = set_val;
}

// set_just_name() sets an asset's "human" name, without noting how that name came up.
void asset_name_id_info::set_just_name (string set_val)
{
    asset_name = set_val;
}

/* Getter for protected member, asset_name.  Protected so that it can only be set by
   set_name() or set_calc_name(), above, to establish not only its value but
   how it came about. */
string asset_name_id_info::get_name (void)
{
    return asset_name;
}

// Asset IDs can be set directly from a uint64_t or converted from a string:
void asset_name_id_info::set_id_n (string set_val)
{
    id_n = stol (set_val, 0, 0);
}
void asset_name_id_info::set_id_n (uint64_t set_val)
{
    id_n = set_val;
}

// Create ID-based name:
string asset_name_id_info::make_id_n_based_name (uint64_t id_n)
{
    string result;

    switch (asset_type) {
        case psa_asset_type::sst:
            result = "SST_ID_";
            break;
        case psa_asset_type::key:
            result = "Key_ID_";
            break;
        case psa_asset_type::policy:
            result = "Policy_ID_";
            break;
        default:
            cerr << "\nError:  Tool-internal:  Please report error "
                 << "#1223 to the TF-Fuzz developers." << endl;
            exit(1223);
    }
    result.append(to_string(id_n));
    return result;
}

/**********************************************************************************
   End of methods of class asset_name_id_info.
**********************************************************************************/