Gary Morrison | ced8c6f | 2020-02-27 19:35:59 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* These classes "cut down the clutter" by grouping together related data and |
| 9 | associated methods (most importantly their constructors) used in template_ |
| 10 | line, psa_call, psa_asset (etc.). */ |
| 11 | |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | #include <cstdint> |
| 15 | |
| 16 | #include "class_forwards.hpp" |
| 17 | |
| 18 | #include "boilerplate.hpp" |
| 19 | #include "gibberish.hpp" |
| 20 | #include "compute.hpp" |
| 21 | #include "string_ops.hpp" |
| 22 | #include "data_blocks.hpp" |
| 23 | #include "psa_asset.hpp" |
| 24 | #include "crypto_asset.hpp" |
| 25 | #include "find_or_create_asset.hpp" |
| 26 | #include "psa_call.hpp" |
| 27 | #include "template_line.hpp" |
| 28 | #include "tf_fuzz.hpp" |
| 29 | |
| 30 | |
| 31 | |
| 32 | /********************************************************************************** |
| 33 | Methods of class expect_info follow: |
| 34 | **********************************************************************************/ |
| 35 | |
| 36 | expect_info::expect_info (void) // (default constructor) |
| 37 | { |
| 38 | pf_nothing = false; // by default, TF-Fuzz provides expected results |
| 39 | pf_pass = pf_specified = false; |
| 40 | pf_result_string.assign (""); data.assign (""); |
| 41 | data_var_specified = false; |
| 42 | data_var.assign (""); // name of expected-data variable |
| 43 | data_specified = false; |
| 44 | data.assign (""); |
| 45 | pf_info_incomplete = true; |
| 46 | } |
| 47 | expect_info::~expect_info (void) // (destructor) |
| 48 | {} |
| 49 | |
| 50 | void expect_info::set_pf_pass (void) |
| 51 | { |
| 52 | pf_pass = true; |
| 53 | pf_nothing = pf_specified = false; |
| 54 | pf_result_string = ""; |
| 55 | } |
| 56 | |
| 57 | void expect_info::set_pf_nothing (void) |
| 58 | { |
| 59 | pf_nothing = true; |
| 60 | pf_pass = pf_specified = false; |
| 61 | pf_result_string = ""; |
| 62 | } |
| 63 | |
| 64 | void expect_info::set_pf_error (string error) |
| 65 | { |
| 66 | pf_specified = true; |
| 67 | pf_result_string.assign (error); // just default "guess," to be filled in |
| 68 | pf_pass = pf_nothing = false; |
| 69 | } |
| 70 | |
| 71 | /* What the call expects is not available from the parser until the call has already |
| 72 | been created. The flag, pf_info_incomplete, that indicates whether or not the |
| 73 | "expects" information has been filled in If not, fill it in from the template, |
| 74 | once that info has been parsed. */ |
| 75 | void expect_info::copy_expect_to_call (psa_call *the_call) |
| 76 | { |
| 77 | the_call->expect.pf_nothing = pf_nothing; |
| 78 | the_call->expect.pf_pass = pf_pass; |
| 79 | the_call->expect.pf_specified = pf_specified; |
| 80 | the_call->expect.pf_result_string = pf_result_string; |
| 81 | the_call->expect.pf_info_incomplete = false; |
| 82 | } |
| 83 | |
| 84 | /********************************************************************************** |
| 85 | End of methods of class expect_info. |
| 86 | **********************************************************************************/ |
| 87 | |
| 88 | |
| 89 | /********************************************************************************** |
| 90 | Class set_data_info methods regarding setting and getting asset-data values: |
| 91 | **********************************************************************************/ |
| 92 | |
| 93 | set_data_info::set_data_info (void) // (default constructor) |
| 94 | { |
| 95 | literal_data_not_file = true; // currently, not using files as data sources |
| 96 | string_specified = false; |
| 97 | data.assign (""); |
| 98 | random_data = false; |
| 99 | file_specified = false; |
| 100 | file_path.assign (""); |
| 101 | } |
| 102 | set_data_info::~set_data_info (void) // (destructor) |
| 103 | {} |
| 104 | |
| 105 | /* set() establishes: |
| 106 | * An asset's data value from a template line (e.g., set sst snort data "data |
| 107 | value"), and |
| 108 | * *That* such a value was directly specified, as opposed to no data value having |
| 109 | been specified, or a random data value being requested. |
| 110 | Arguably, this method "has side effects," in that it not only sets a value, but |
| 111 | also "takes notes" about where that value came from. |
| 112 | */ |
| 113 | void set_data_info::set (string set_val) |
| 114 | { |
| 115 | literal_data_not_file = true; // currently, not using files as data sources |
| 116 | string_specified = true; |
| 117 | data.assign (set_val); |
| 118 | } |
| 119 | |
| 120 | /* set_calculated() establishes: |
| 121 | * An asset's data value as *not* taken from a template line, and |
| 122 | * *That* such a value was not directly specified in any template line, such as |
| 123 | if a random data value being requested. |
| 124 | Arguably, this method "has side effects," in that it not only sets a value, but |
| 125 | also "takes notes" about where that value came from. |
| 126 | */ |
| 127 | void set_data_info::set_calculated (string set_val) |
| 128 | { |
| 129 | literal_data_not_file = true; // currently, not using files as data sources |
| 130 | string_specified = false; |
| 131 | data.assign (set_val); |
| 132 | } |
| 133 | |
| 134 | /* Getter for protected member, data. Protected so that it can only be set by |
| 135 | set() or set_calculated(), above, to establish not only its value but |
| 136 | how it came about. */ |
| 137 | string set_data_info::get (void) |
| 138 | { |
| 139 | return data; |
| 140 | } |
| 141 | |
| 142 | /* Currently, files as data sources aren't used, so this whole method is not "of |
| 143 | use," but that might change at some point. */ |
| 144 | bool set_data_info::set_file (string file_name) |
| 145 | { |
| 146 | literal_data_not_file = true; |
| 147 | string_specified = false; |
| 148 | data.assign (""); |
| 149 | file_specified = true; |
| 150 | // Remove the ' ' quotes around the file name: |
| 151 | file_name.erase (0, 1); |
| 152 | file_name.erase (file_name.length()-1, 1); |
| 153 | file_path = file_name; |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /********************************************************************************** |
| 158 | End of methods of class set_data_info. |
| 159 | **********************************************************************************/ |
| 160 | |
| 161 | |
| 162 | /********************************************************************************** |
| 163 | Class asset_name_id_info methods regarding setting and getting asset-data values: |
| 164 | **********************************************************************************/ |
| 165 | |
| 166 | asset_name_id_info::asset_name_id_info (void) // (default constructor) |
| 167 | { |
| 168 | id_n_not_name = false; // (arbitrary) |
| 169 | id_n = 100 + (rand() % 10000); // default to random ID# (e.g., SST UID) |
| 170 | asset_name.assign (""); |
| 171 | id_n_specified = name_specified = false; // no ID info yet |
| 172 | asset_name_vector.clear(); |
| 173 | asset_id_n_vector.clear(); |
| 174 | } |
| 175 | asset_name_id_info::~asset_name_id_info (void) |
| 176 | { |
| 177 | asset_name_vector.clear(); |
| 178 | asset_id_n_vector.clear(); |
| 179 | } |
| 180 | |
| 181 | /* set_name() establishes: |
| 182 | * An asset's "human" name from a template line, and |
| 183 | * *That* that name was directly specified, as opposed to the asset being defined |
| 184 | by ID only, or a random name being requested. |
| 185 | Arguably, this method "has side effects," in that it not only sets a name, but |
| 186 | also "takes notes" about where that name came from. |
| 187 | */ |
| 188 | void asset_name_id_info::set_name (string set_val) |
| 189 | { |
| 190 | /* Use this to set the name as specified in the template file. Call this only |
| 191 | if the template file does indeed define a name. */ |
| 192 | name_specified = true; |
| 193 | asset_name = set_val; |
| 194 | } |
| 195 | |
| 196 | /* set_calc_name() establishes: |
| 197 | * An asset's "human" name *not* from a template line, and |
| 198 | * *That* that name was *not* directly specified in any template line. |
| 199 | Arguably, this method "has side effects," in that it not only sets a name, but |
| 200 | also "takes notes" about where that name came from. |
| 201 | */ |
| 202 | void asset_name_id_info::set_calc_name (string set_val) |
| 203 | { |
| 204 | name_specified = false; |
| 205 | asset_name = set_val; |
| 206 | } |
| 207 | |
| 208 | // set_just_name() sets an asset's "human" name, without noting how that name came up. |
| 209 | void asset_name_id_info::set_just_name (string set_val) |
| 210 | { |
| 211 | asset_name = set_val; |
| 212 | } |
| 213 | |
| 214 | /* Getter for protected member, asset_name. Protected so that it can only be set by |
| 215 | set_name() or set_calc_name(), above, to establish not only its value but |
| 216 | how it came about. */ |
| 217 | string asset_name_id_info::get_name (void) |
| 218 | { |
| 219 | return asset_name; |
| 220 | } |
| 221 | |
| 222 | // Asset IDs can be set directly from a uint64_t or converted from a string: |
| 223 | void asset_name_id_info::set_id_n (string set_val) |
| 224 | { |
| 225 | id_n = stol (set_val, 0, 0); |
| 226 | } |
| 227 | void asset_name_id_info::set_id_n (uint64_t set_val) |
| 228 | { |
| 229 | id_n = set_val; |
| 230 | } |
| 231 | |
| 232 | // Create ID-based name: |
| 233 | string asset_name_id_info::make_id_n_based_name (uint64_t id_n, string &name) |
| 234 | { |
| 235 | string result = "SST_ID_"; |
| 236 | result.append(to_string(id_n)); |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | /********************************************************************************** |
| 241 | End of methods of class asset_name_id_info. |
| 242 | **********************************************************************************/ |