HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Find directories containing jar files and call other program to scan them for vulnerable versions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
containingscanjarversionsprogramcallfilesforvulnerablefind

Problem

I wrote a Python script to collect a list of directories containing third party libraries and submit them all to dependency-check, a program that compares library versions to known-vulnerable third party libraries. This code works correctly and I'm after feedback about Python programming, especially my testing methods.

Here are the tests:

```
from unittest import TestCase
import unittest
import run_check
import platform
import os

# requirements:
# platform independent
# start from base directory and drill down looking for jar files
# use base path given to create app_name
# launch 'dependency-check' with three arguments: app_name, path, output file dir
# test that expected HTML files are generated

class TestDirectoryCheck(TestCase):
def test_app_name(self):
if platform.system() == "Windows":
test_path = "count5\\some\\more"
test_path2 = "count5\\some\\maybe\\"
test_path3 = "C:\\myuser\\mydocs\\mylibs\\"
self.assertEqual("count5-some-more", run_check.get_app_name(test_path))
self.assertEqual("count5-some-maybe", run_check.get_app_name(test_path2))
self.assertEqual("C-myuser-mydocs-mylibs", run_check.get_app_name(test_path3))
else:
test_path = "/srv/tomcata/webapps/myapp"
other_path = "/count3/maybe/maybemore"
self.assertEqual("srv-tomcata-webapps-myapp", run_check.get_app_name(test_path))
self.assertEqual("count3-maybe-maybemore", run_check.get_app_name(other_path))

def test_path_list(self):
if platform.system() == "Windows":
base_path = "C:\\Users\\manamana\\Downloads\\badjar\\count3"
app_list = ["C:\\Users\\manamana\\Downloads\\badjar\\count3\\some",
"C:\\Users\\manamana\\Downloads\\badjar\\count3\\someothers",
"C:\\Users\\manamana\\Downloads\\badjar\\count3\\someothers\\more"
]

else:
base_path = "/home/mcg

Solution

-
Ideally you should not have if conditions in your tests. You should always know exactly what it is that you want to test. test_app_name has a check on the platform. You should be able to mock it out and test the functionality under a 'windows' and a 'non-windows' environment. I'd split it out into different tests.

-
remove_old_files walks through a dir on your machine. Mock it out or include it in the repo for your code. Ideally mock it out if you don't really care about particulars of the files.

-
You have a lot of hardcoded paths in your code. This will not work on any other machine. You'll need some way of fixing this.

On a larger note:

You have the correct picture of a unit test, but you seem to try adn cover all cases under a single test. You need to test out all the methods, but you can split it out into multiple tests:

eg:
Let's pick get_extension. There are 2 code paths in that method. So lets write 2 methods for it.

1. test_get_extension_windows
2. test_get_extension_non_windows


That should cover all cases for that method and we know exactly what gets executed in there. No need for conditions in our code. You can go on the same path for the other tests too.

Code Snippets

1. test_get_extension_windows
2. test_get_extension_non_windows

Context

StackExchange Code Review Q#75384, answer score: 8

Revisions (0)

No revisions yet.