Skip to content

Commit

Permalink
Test: add test for COPYRIGHT() function
Browse files Browse the repository at this point in the history
Adds a test to verify that use of the COPYRIGHT() function in bytecode
source correctly excludes the bytecode source from the compiled bytecode
signature.

I also tidied up the basic runtime tests to:
- put test files in the test temp directory rather than the current
directory.
- make the variable names more Pythonic.
- use pathlib.Path features rather than os.path.join().
  • Loading branch information
val-ms committed Jun 6, 2022
1 parent a43af8f commit 0381fdd
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 18 deletions.
30 changes: 12 additions & 18 deletions test/02_basic_runtime_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
# Copyright (C) 2021-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

"""
The tests in this file check that clambcc is able to compile the example
Expand Down Expand Up @@ -40,26 +40,20 @@ def tearDown(self):
def test_00_run_test(self):
self.step_name('Test that clamscan can run a specific signature.')

testPath = os.path.join(TC.path_source , 'test' , '02' , 'Sig.c')
testsig_src_file = self.path_source / 'test' / 'examples' / 'in' / 'lsig_simple2.c'
testsig_out_file = self.path_tmp / 'sigs' / 'lsig_simple2.cbc'
os.makedirs(testsig_out_file.parent, exist_ok=True)

SIGDIR = 'sigs'
os.mkdir(SIGDIR)
self.execute_command(f'{TC.clambcc} {testsig_src_file} -o {testsig_out_file} {TC.headers}')

self.execute_command(f'{TC.clambcc} {testPath} -o {SIGDIR} {TC.headers}')
test_sample_path = self.path_tmp / 'samples'
os.mkdir (test_sample_path)

SAMPLEDIR = 'samples'
os.mkdir (SAMPLEDIR)
test_string='CLAMAV-TEST-STRING-NOT-EICAR'
test_file = test_sample_path / 'testfile'
self.execute_command(f'echo {test_string} > {test_file}')

SIGSTRING='CLAMAV-TEST-STRING-NOT-EICAR'
outFile = os.path.join(SAMPLEDIR, 'file')
self.execute_command (f'echo {SIGSTRING} > {outFile}')

command = f'{self.clamscan} --bytecode-unsigned -d {SIGDIR} {SAMPLEDIR}'
output = self.execute_command (command)
command = f'{TC.clamscan} --bytecode-unsigned -d {testsig_out_file} {test_sample_path}'
output = self.execute_command(command)

self.verify_output(output.out, expected='Clamav-Unit-Test-Signature.02 FOUND')





73 changes: 73 additions & 0 deletions test/03_feature_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright (C) 2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

"""
The tests in this file are to verify behavior for assorted signature features.
"""

import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
import time
import unittest

import testcase


os_platform = platform.platform()
operating_system = os_platform.split('-')[0].lower()


class TC(testcase.TestCase):
@classmethod
def setUpClass(cls):
super(TC, cls).setUpClass()

@classmethod
def tearDownClass(cls):
super(TC, cls).tearDownClass()

def setUp(self):
super(TC, self).setUp()

def tearDown(self):
super(TC, self).tearDown()
self.verify_valgrind_log()

def test_00_no_copyright(self):
self.step_name('Test that without COPYRIGHT() function, source is included with compiled sig.')

testsig_src_file = self.path_source / 'test' / 'examples' / 'in' / 'lsig_simple2.c'
testsig_out_file = self.path_tmp / 'sigs' / 'lsig_simple2.cbc'
os.makedirs(testsig_out_file.parent, exist_ok=True)

self.execute_command(f'{self.clambcc} {testsig_src_file} -o {testsig_out_file} {self.headers}')

command = f'{self.clambc} --printsrc {testsig_out_file}'
output = self.execute_command(command)

self.verify_output(
output.out,
expected=r'VIRUSNAME_PREFIX\("Clamav-Unit-Test-Signature.02"\)',
unexpected='Cisco 2022'
)

def test_01_has_copyright(self):
self.step_name('Test that with COPYRIGHT() function, source is excluded from compiled sig.')

testsig_src_file = self.path_source / 'test' / 'examples' / 'in' / 'lsig_copyright.c'
testsig_out_file = self.path_tmp / 'sigs' / 'lsig_copyright.cbc'
os.makedirs(testsig_out_file.parent, exist_ok=True)

self.execute_command(f'{self.clambcc} {testsig_src_file} -o {testsig_out_file} {self.headers}')

command = f'{self.clambc} --printsrc {testsig_out_file}'
output = self.execute_command(command)

self.verify_output(
output.out,
expected='Cisco 2022',
unexpected=r'VIRUSNAME_PREFIX\("Clamav-Unit-Test-Signature.02"\)'
)
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ add_test(NAME clambcc-runtime-test COMMAND ${Python3_EXECUTABLE} -m;${Python3_TE
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TEST clambcc-runtime-test PROPERTY ENVIRONMENT ${ENVIRONMENT})

add_test(NAME clambcc-feature-test COMMAND ${Python3_EXECUTABLE} -m;${Python3_TEST_PACKAGE};03_feature_test.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set_property(TEST clambcc-feature-test PROPERTY ENVIRONMENT ${ENVIRONMENT})

if(WIN32)
#
# Prepare a test install, with all our DLL dependencies co-located with our EXEs and DLLs
Expand Down
29 changes: 29 additions & 0 deletions test/examples/in/lsig_copyright.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
COPYRIGHT("Cisco 2022")

VIRUSNAME_PREFIX("Clamav-Unit-Test-Signature.02")
VIRUSNAMES("")
TARGET(0)

FUNCTIONALITY_LEVEL_MIN(FUNC_LEVEL_096_4)

SIGNATURES_DECL_BEGIN
DECLARE_SIGNATURE(test_string)
SIGNATURES_DECL_END

SIGNATURES_DEF_BEGIN
/* matches "CLAMAV-TEST-STRING-NOT-EICAR" */
DEFINE_SIGNATURE(test_string, "0:434c414d41562d544553542d535452494e472d4e4f542d4549434152")
SIGNATURES_DEF_END

bool logical_trigger()
{
/***Will return true if signature matches ***/
return matches(Signatures.test_string);
}

/***bytecode function that executes if the logical signature matched ***/
int entrypoint(void)
{
foundVirus("");
return 0;
}
File renamed without changes.

0 comments on commit 0381fdd

Please sign in to comment.