blob: dfedb61f729e9a0694c52e4f65ed2e3527e6f020 [file] [log] [blame]
Zane Shelley11b89942019-11-07 11:07:28 -06001#include "simulator.hpp"
2
Zane Shelley1be4c3c2020-04-17 15:55:07 -05003#include <fstream> // std::ifstream
4
5namespace libhei
6{
7
8//------------------------------------------------------------------------------
9
10// Paths are relative from the build/ directory
11const std::map<SimulatorData::ChipType, const char*>
12 SimulatorData::cv_chipPath = {
Zane Shelleyaadf3bf2020-04-30 21:25:29 -050013 {SAMPLE, "../test/simulator/sample_data/sample.cdb"},
Zane Shelley1be4c3c2020-04-17 15:55:07 -050014};
Zane Shelley11b89942019-11-07 11:07:28 -060015
16//------------------------------------------------------------------------------
17
18void SimulatorData::addChip(const Chip& i_chip)
19{
20 // First check if this entry already exists.
Zane Shelley1be4c3c2020-04-17 15:55:07 -050021 auto itr1 = std::find(iv_chipList.begin(), iv_chipList.end(), i_chip);
22 ASSERT_EQ(iv_chipList.end(), itr1);
Zane Shelley11b89942019-11-07 11:07:28 -060023
24 // Add the new entry.
25 iv_chipList.push_back(i_chip);
26
Zane Shelley1be4c3c2020-04-17 15:55:07 -050027 // 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 Shelley11b89942019-11-07 11:07:28 -060031
Zane Shelley1be4c3c2020-04-17 15:55:07 -050032 // 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 Shelley11b89942019-11-07 11:07:28 -060051 ReturnCode rc = initialize(buffer, sz_buffer);
52 ASSERT_TRUE(RC_SUCCESS == rc || RC_CHIP_DATA_INITIALIZED == rc);
Zane Shelley1be4c3c2020-04-17 15:55:07 -050053
54 // Clean up the buffer
55 delete[] buffer;
Zane Shelley11b89942019-11-07 11:07:28 -060056}
57
58//------------------------------------------------------------------------------
59
60void 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 Shelley1be4c3c2020-04-17 15:55:07 -050088
89} // end namespace libhei