#!/usr/bin/env python3

#   Copyright 2014-2018 PUNCH Cyber Analytics Group
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import os
import logging
import unittest

from stoq import RequestMeta, Stoq, Payload
from stoq.data_classes import WorkerResponse
from stoq.exceptions import StoqPluginException


class TestCore(unittest.TestCase):
    def setUp(self) -> None:
        self.plugin_name = 'javaclass'
        self.plugin_dir = os.path.join(os.getcwd(), self.plugin_name)
        self.data_dir = os.path.join(os.getcwd(), 'tests', 'data')
        logging.disable(logging.CRITICAL)

    def tearDown(self) -> None:
        logging.disable(logging.NOTSET)

    def test_scan(self) -> None:
        s = Stoq(plugin_dir_list=[self.plugin_dir])
        plugin = s.load_plugin(self.plugin_name)
        with open(f'{self.data_dir}/TestJavaClass.class', 'rb') as f:
            payload = Payload(f.read())
        request_meta = RequestMeta(archive_payloads=False)
        response = plugin.scan(payload, request_meta)
        self.assertIsInstance(response, WorkerResponse)
        self.assertIn('TestJavaClass', response.results['provided'])
        self.assertGreaterEqual(len(response.results['provided']), 4)
        self.assertGreaterEqual(len(response.results['required']), 2)
        self.assertGreaterEqual(len(response.results['constants']), 10)

    def test_scan_invalid_payload(self) -> None:
        s = Stoq(plugin_dir_list=[self.plugin_dir])
        plugin = s.load_plugin(self.plugin_name)
        payload = Payload(b'definitely not a javaclass payload')
        request_meta = RequestMeta(archive_payloads=False)
        with self.assertRaises(StoqPluginException):
            response = plugin.scan(payload, request_meta)

@@ -676,16 +676,42 @@
 ittest%0A%0A
+from pathlib import Path%0A%0A
 from sto
@@ -961,29 +961,32 @@
 elf.
-plugin
+base
 _dir = 
+Path(
 os.path.
 join
@@ -985,43 +985,34 @@
 ath.
-join(os.getcwd(), self.plugin_name)
+realpath(__file__)).parent
 %0A   
@@ -1049,78 +1049,108 @@
 oin(
-os.getcwd(), 'tests', 'data')%0A        logging.disable(logging.CRITICAL
+self.base_dir, 'data')%0A        self.plugin_dir = os.path.join(self.base_dir.parent, self.plugin_name
 )%0A%0A 
@@ -1192,39 +1192,12 @@
     
-logging.disable(logging.NOTSET)
+pass
 %0A%0A  

Fix path for javaclass tests