Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 1 | #include "simulator.hpp" |
| 2 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 3 | #include <fstream> // std::ifstream |
| 4 | |
| 5 | namespace libhei |
| 6 | { |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | |
| 10 | // Paths are relative from the build/ directory |
| 11 | const std::map<SimulatorData::ChipType, const char*> |
| 12 | SimulatorData::cv_chipPath = { |
Zane Shelley | aadf3bf | 2020-04-30 21:25:29 -0500 | [diff] [blame^] | 13 | {SAMPLE, "../test/simulator/sample_data/sample.cdb"}, |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 14 | }; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 15 | |
| 16 | //------------------------------------------------------------------------------ |
| 17 | |
| 18 | void SimulatorData::addChip(const Chip& i_chip) |
| 19 | { |
| 20 | // First check if this entry already exists. |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 21 | auto itr1 = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip); |
| 22 | ASSERT_EQ(iv_chipList.end(), itr1); |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 23 | |
| 24 | // Add the new entry. |
| 25 | iv_chipList.push_back(i_chip); |
| 26 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 27 | // Look for the file path |
| 28 | auto itr2 = cv_chipPath.find(static_cast<ChipType>(i_chip.getType())); |
| 29 | ASSERT_NE(cv_chipPath.end(), itr2); |
| 30 | const char* path = itr2->second; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 31 | |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 32 | // Open the Chip Data File |
| 33 | std::ifstream cdf{path, std::ifstream::binary}; |
| 34 | ASSERT_TRUE(cdf.good()); |
| 35 | |
| 36 | // Get the length of file |
| 37 | cdf.seekg(0, cdf.end); |
| 38 | size_t sz_buffer = cdf.tellg(); |
| 39 | cdf.seekg(0, cdf.beg); |
| 40 | |
| 41 | // Allocate memory |
| 42 | char* buffer = new char[sz_buffer]; |
| 43 | |
| 44 | // Read data as a block |
| 45 | cdf.read(buffer, sz_buffer); |
| 46 | |
| 47 | // Close the Chip Data File |
| 48 | cdf.close(); |
| 49 | |
| 50 | // Initilize the chip with this Chip Data File. |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 51 | ReturnCode rc = initialize(buffer, sz_buffer); |
| 52 | ASSERT_TRUE(RC_SUCCESS == rc || RC_CHIP_DATA_INITIALIZED == rc); |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 53 | |
| 54 | // Clean up the buffer |
| 55 | delete[] buffer; |
Zane Shelley | 11b8994 | 2019-11-07 11:07:28 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | //------------------------------------------------------------------------------ |
| 59 | |
| 60 | void SimulatorData::endIteration() |
| 61 | { |
| 62 | // Start by calling libhei::isolate(). |
| 63 | IsolationData isoData{}; |
| 64 | ReturnCode rc = isolate(iv_chipList, isoData); |
| 65 | |
| 66 | // It is possible that even in a failure scenario the information in the |
| 67 | // returned IsolationData would be useful. |
| 68 | // TODO: Figure out where to put the data. |
| 69 | |
| 70 | // Verify if isolation completed successfully. |
| 71 | ASSERT_TRUE(RC_SUCCESS == rc); |
| 72 | |
| 73 | // Get the list of signatures found in isolation. |
| 74 | std::vector<Signature> givenSigList = isoData.getSignatureList(); |
| 75 | |
| 76 | // Verify the expected list and given list are the same. |
| 77 | ASSERT_EQ(iv_expSigList.size(), givenSigList.size()); |
| 78 | |
| 79 | std::sort(iv_expSigList.begin(), iv_expSigList.end()); |
| 80 | std::sort(givenSigList.begin(), givenSigList.end()); |
| 81 | |
| 82 | ASSERT_TRUE(std::equal(givenSigList.begin(), givenSigList.end(), |
| 83 | iv_expSigList.begin())); |
| 84 | |
| 85 | // The iteration is complete so we can flush the data. |
| 86 | flushIterationData(); |
| 87 | } |
Zane Shelley | 1be4c3c | 2020-04-17 15:55:07 -0500 | [diff] [blame] | 88 | |
| 89 | } // end namespace libhei |