Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions testing/unit/ntp/ntp_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module run all the NTP related unit tests"""
import unittest.mock
from ntp_module import NTPModule
import unittest
from scapy.all import rdpcap, NTP, wrpcap
Expand All @@ -26,9 +27,15 @@
OUTPUT_DIR = os.path.join(TEST_FILES_DIR,'output/')
REPORTS_DIR = os.path.join(TEST_FILES_DIR,'reports/')
CAPTURES_DIR = os.path.join(TEST_FILES_DIR,'captures/')
MODULE_FILES_DIR = 'modules/test/' + MODULE
TEMPLATE_SOURCE = os.path.join(MODULE_FILES_DIR, 'resources')
TEMPLATE_DIR = os.path.join(TEST_FILES_DIR, 'templates')
TEMPLATE_FILE = 'report_template.jinja2'
BASE_TEMPLATE_FILE = 'module_report_base.jinja2'
BASE_TEMPLATE_SOURCE = os.path.join('resources/report', BASE_TEMPLATE_FILE)

LOCAL_REPORT = os.path.join(REPORTS_DIR,'ntp_report_local.html')
LOCAL_REPORT_NO_NTP = os.path.join(REPORTS_DIR,'ntp_report_local_no_ntp.html')
LOCAL_REPORT = os.path.join(REPORTS_DIR,'ntp_report_local.jinja2')
LOCAL_REPORT_NO_NTP = os.path.join(REPORTS_DIR,'ntp_report_local_no_ntp.jinja2')

# Define the capture files to be used for the test
NTP_SERVER_CAPTURE_FILE = os.path.join(CAPTURES_DIR,'ntp.pcap')
Expand All @@ -44,6 +51,23 @@ def setUpClass(cls):
# Create the output directories and ignore errors if it already exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
os.environ['DEVICE_MAC'] = '38:d1:35:09:01:8e'
# Copy template files
if not os.path.isdir(TEMPLATE_DIR):
os.makedirs(TEMPLATE_DIR)
shutil.copy(
os.path.join(TEMPLATE_SOURCE, TEMPLATE_FILE),
os.path.join(TEMPLATE_DIR, TEMPLATE_FILE)
)
shutil.copy(
BASE_TEMPLATE_SOURCE,
os.path.join(TEMPLATE_DIR, BASE_TEMPLATE_FILE)
)

@classmethod
def tearDownClass(cls):
# Delete templates dir
if os.path.isdir(TEMPLATE_DIR):
shutil.rmtree(TEMPLATE_DIR)

# Test the module report generation
def ntp_module_report_test(self):
Expand All @@ -52,6 +76,8 @@ def ntp_module_report_test(self):
ntp_server_capture_file=NTP_SERVER_CAPTURE_FILE,
startup_capture_file=STARTUP_CAPTURE_FILE,
monitor_capture_file=MONITOR_CAPTURE_FILE)
setattr(ntp_module, '_report_template_folder', TEMPLATE_DIR)
setattr(ntp_module, '_base_template_file', BASE_TEMPLATE_FILE)

report_out_path = ntp_module.generate_module_report()

Expand All @@ -64,10 +90,9 @@ def ntp_module_report_test(self):
report_local = file.read()

# Copy the generated html report to a new file
new_report_name = 'ntp_local.html'
new_report_name = 'ntp_local.jinja2'
new_report_path = os.path.join(OUTPUT_DIR, new_report_name)
shutil.copy(report_out_path, new_report_path)

self.assertEqual(report_out, report_local)

# Test the module report generation if no DNS traffic
Expand Down Expand Up @@ -100,11 +125,14 @@ def ntp_module_report_no_ntp_test(self):
wrpcap(startup_cap_file, packets_startup)
wrpcap(monitor_cap_file, packets_monitor)

ntp_module = NTPModule(module='dns',
ntp_module = NTPModule(module=MODULE,
results_dir=OUTPUT_DIR,
ntp_server_capture_file=ntp_server_cap_file,
startup_capture_file=startup_cap_file,
monitor_capture_file=monitor_cap_file)

setattr(ntp_module, '_report_template_folder', TEMPLATE_DIR)
setattr(ntp_module, '_base_template_file', BASE_TEMPLATE_FILE)

report_out_path = ntp_module.generate_module_report()

Expand All @@ -117,7 +145,7 @@ def ntp_module_report_no_ntp_test(self):
report_local = file.read()

# Copy the generated html report to a new file
new_report_name = 'ntp_no_ntp.html'
new_report_name = 'ntp_no_ntp.jinja2'
new_report_path = os.path.join(OUTPUT_DIR, new_report_name)
shutil.copy(report_out_path, new_report_path)

Expand Down
115 changes: 0 additions & 115 deletions testing/unit/ntp/reports/ntp_report_local.html

This file was deleted.

179 changes: 179 additions & 0 deletions testing/unit/ntp/reports/ntp_report_local.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@

<div class="page">
<div class="header">
<div class="header-info">
{# Badge #}
<p class="header-info-badge">
{% if json_data['device']['test_pack'] == 'Device Qualification' %}
<img src="data:image/png;base64, {{ icon_qualification }}" alt="">
<span>Device Qualification</span>
{% else %}
<img src="data:image/png;base64, {{ icon_pilot }}" alt="">
<span>Pilot Assessment</span>
{% endif %}
</p>
<span style="font-weight:600">{{ title }}</span>
</div>
<span style="margin-top:0;max-width:700px;font-size:24px;font-weight:600;">
{{ device['manufacturer'] }}
{{ device['model']}}
</span>
<img src="data:image/png;base64, {{ logo }}" alt="Testrun" width="90" style="position: absolute;top: 40%; right: 0px;"></img>
</div>

<div class="module-page-content">

<h4 class="page-heading">NTP Module</h4>
<table class="module-summary">

<thead>
<tr>

<th>Requests to local NTP server</th>

<th>Requests to external NTP servers</th>

<th>Total NTP requests</th>

<th>Total NTP responses</th>

</tr>
</thead>
<tbody>
<tr>

<td>63</td>

<td>38</td>

<td>101</td>

<td>104</td>

</tr>
</tbody>
</table>


<table class="module-data" style="width:100%;">
<thead>
<tr>

<th>Source</th>

<th>Destination</th>

<th>Type</th>

<th>Version</th>

<th>Count</th>

<th>Sync Request Average</th>

</tr>
</thead>
<tbody>

<tr>
<td>10.10.10.15</td>
<td>216.239.35.12</td>
<td>Client</td>
<td>4</td>
<td>8</td>
<td>37.942 seconds</td>
</tr>

<tr>
<td>216.239.35.12</td>
<td>10.10.10.15</td>
<td>Server</td>
<td>4</td>
<td>8</td>
<td>N/A</td>
</tr>

<tr>
<td>10.10.10.15</td>
<td>216.239.35.4</td>
<td>Client</td>
<td>4</td>
<td>8</td>
<td>37.834 seconds</td>
</tr>

<tr>
<td>216.239.35.4</td>
<td>10.10.10.15</td>
<td>Server</td>
<td>4</td>
<td>8</td>
<td>N/A</td>
</tr>

<tr>
<td>10.10.10.15</td>
<td>216.239.35.8</td>
<td>Client</td>
<td>4</td>
<td>8</td>
<td>38.056 seconds</td>
</tr>

<tr>
<td>216.239.35.8</td>
<td>10.10.10.15</td>
<td>Server</td>
<td>4</td>
<td>8</td>
<td>N/A</td>
</tr>

<tr>
<td>10.10.10.15</td>
<td>216.239.35.0</td>
<td>Client</td>
<td>4</td>
<td>14</td>
<td>20.601 seconds</td>
</tr>

<tr>
<td>216.239.35.0</td>
<td>10.10.10.15</td>
<td>Server</td>
<td>4</td>
<td>17</td>
<td>N/A</td>
</tr>

<tr>
<td>10.10.10.15</td>
<td>10.10.10.5</td>
<td>Client</td>
<td>4</td>
<td>63</td>
<td>13.057 seconds</td>
</tr>

<tr>
<td>10.10.10.5</td>
<td>10.10.10.15</td>
<td>Server</td>
<td>4</td>
<td>63</td>
<td>N/A</td>
</tr>

</tbody>
</table>


</div>

<div class="footer">
<div class="footer-label">Testrun {{ version }}</div>
<div class="footer-label page-index" style="right: 0px"></div>
</div>
</div>
<div style="break-after:page"></div>
Loading
Loading