Beispiel #1
0
    def test_license_from_copying_hash_no_license_show(self):
        """
        Test license_from_copying_hash with invalid hash and no license_show
        set
        """
        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        open_name = 'tarball.open'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            content = copyingf.read()

        bkup_hash = license.config.license_hashes[license.tarball.get_sha1sum(
            'tests/COPYING_TEST')]
        # remove the hash from license_hashes
        del (license.config.license_hashes[license.tarball.get_sha1sum(
            'tests/COPYING_TEST')])
        license.config.license_show = "license.show.url"
        m_open = mock_open(read_data=content)
        with patch(open_name, m_open, create=True):
            license.license_from_copying_hash('copying.txt', '')

        # restore the hash
        license.config.license_hashes[license.tarball.get_sha1sum(
            'tests/COPYING_TEST')] = bkup_hash
        self.assertEquals(license.licenses, [])
Beispiel #2
0
 def test_license_from_copying_hash(self):
     """
     Test license_from_copying_hash with valid license file
     """
     conf = config.Config()
     conf.setup_patterns()
     license.license_from_copying_hash('tests/COPYING_TEST', '', conf, '')
     self.assertIn('GPL-3.0', license.licenses)
Beispiel #3
0
    def test_license_from_copying_hash_license_server(self):
        """
        Test license_from_copying_hash with license server. This is heavily
        mocked.
        """
        class MockBytesIO(BytesIO):
            """
            Mock class for BytesIO to set returnvalue of BytesIO.getvalue()
            """
            def getvalue(_):
                return 'GPL-3.0'.encode('utf-8')

        # set the mocks
        download.BytesIO = MockBytesIO

        class MockCurl():
            URL = None
            WRITEDATA = None
            POSTFIELDS = None
            FOLLOWLOCATION = 0
            FAILONERROR = False
            CONNECTTIMEOUT = 0
            TIMEOUT = 0
            LOW_SPEED_LIMIT = 0
            LOW_SPEED_TIME = 0

            def setopt(_, __, ___):
                pass

            def perform(_):
                pass

            def close(_):
                pass

            def getinfo(_, __):
                return 200

        # set the mock curl
        download.pycurl.Curl = MockCurl

        conf = config.Config()
        conf.license_fetch = 'license.server.url'

        # let's check that the proper thing is being printed as well
        out = StringIO()
        with redirect_stdout(out):
            license.license_from_copying_hash('tests/COPYING_TEST', '', conf,
                                              '')

        self.assertIn('GPL-3.0', license.licenses)
        self.assertIn('License     :  GPL-3.0  (server)', out.getvalue())

        # unset the manual mock
        download.BytesIO = BytesIO

        # unset the manual mock
        download.pycurl.Curl = pycurl.Curl
Beispiel #4
0
    def test_license_from_copying_hash_no_license_show(self):
        """
        Test license_from_copying_hash with invalid hash and no license_show
        set
        """
        conf = config.Config()
        conf.setup_patterns()
        # remove the hash from license_hashes
        del (conf.license_hashes[license.get_sha1sum('tests/COPYING_TEST')])
        conf.license_show = "license.show.url"
        license.license_from_copying_hash('tests/COPYING_TEST', '', conf, '')

        self.assertEquals(license.licenses, [])
Beispiel #5
0
    def test_license_from_copying_hash_bad_license(self):
        """
        Test license_from_copying_hash with invalid license file
        """
        content = util.get_contents("tests/COPYING_TEST").replace(
            b"GNU", b"SNU")
        m_open = MagicMock()
        m_open.__str__.return_value = content

        with patch('license.get_contents', m_open, create=True):
            license.license_from_copying_hash('copying.txt', '')

        self.assertEquals(license.licenses, [])
Beispiel #6
0
    def test_license_from_copying_hash_license_server_excep(self):
        """
        Test license_from_copying_hash with license server when pycurl raises
        an exception.
        """
        class MockCurl():
            URL = None
            WRITEDATA = None
            POSTFIELDS = None
            FOLLOWLOCATION = 0
            FAILONERROR = False
            CONNECTTIMEOUT = 0
            TIMEOUT = 0
            LOW_SPEED_LIMIT = 0
            LOW_SPEED_TIME = 0

            def setopt(_, __, ___):
                pass

            def perform(_):
                raise pycurl.error('Test Exception')

            def close(_):
                pass

        # set the mock curl
        download.pycurl.Curl = MockCurl

        license.config.license_fetch = 'license.server.url'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            content = copyingf.read()

        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        # Also patch the open in license.py
        m_open = mock_open(read_data=content)
        with patch('tarball.open', m_open, create=True):
            with patch('license.open', m_open, create=True):
                # let's check that the proper thing is being printed as well
                out = StringIO()
                with redirect_stdout(out):
                    with self.assertRaises(SystemExit):
                        license.license_from_copying_hash('copying.txt', '')

        self.assertIn('Unable to fetch license.server.url: Test Exception',
                      out.getvalue())

        # unset the manual mock
        download.pycurl.Curl = pycurl.Curl
Beispiel #7
0
    def test_license_from_copying_hash(self):
        """
        Test license_from_copying_hash with valid license file
        """
        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        open_name = 'tarball.open'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            content = copyingf.read()

        m_open = mock_open(read_data=content)
        with patch(open_name, m_open, create=True):
            license.license_from_copying_hash('copying.txt')

        self.assertIn('GPL-3.0', license.licenses)
Beispiel #8
0
    def test_license_from_copying_hash_bad_license(self):
        """
        Test license_from_copying_hash with invalid license file
        """
        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        open_name = 'tarball.open'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            # note the replace corrupting the file contents
            content = copyingf.read().replace(b"GNU", b"SNU")

        m_open = mock_open(read_data=content)
        with patch(open_name, m_open, create=True):
            license.license_from_copying_hash('copying.txt')

        self.assertEquals(license.licenses, [])
Beispiel #9
0
    def test_license_from_copying_hash_no_license_show(self):
        """
        Test license_from_copying_hash with invalid hash and no license_show
        set
        """
        bkup_hash = license.config.license_hashes[license.get_sha1sum(
            'tests/COPYING_TEST')]
        # remove the hash from license_hashes
        del (license.config.license_hashes[license.get_sha1sum(
            'tests/COPYING_TEST')])
        license.config.license_show = "license.show.url"
        license.license_from_copying_hash('tests/COPYING_TEST', '')

        # restore the hash
        license.config.license_hashes[license.get_sha1sum(
            'tests/COPYING_TEST')] = bkup_hash
        self.assertEquals(license.licenses, [])
Beispiel #10
0
    def test_license_from_copying_hash_license_server_excep(self):
        """
        Test license_from_copying_hash with license server when pycurl raises
        an exception.
        """
        class MockCurl():
            URL = None
            WRITEDATA = None
            POSTFIELDS = None
            FOLLOWLOCATION = 0
            FAILONERROR = False
            CONNECTTIMEOUT = 0
            TIMEOUT = 0
            LOW_SPEED_LIMIT = 0
            LOW_SPEED_TIME = 0

            def setopt(_, __, ___):
                pass

            def perform(_):
                raise pycurl.error('Test Exception')

            def close(_):
                pass

        # set the mock curl
        download.pycurl.Curl = MockCurl

        conf = config.Config()
        conf.license_fetch = 'license.server.url'

        # let's check that the proper thing is being printed as well
        out = StringIO()
        with redirect_stdout(out):
            with self.assertRaises(SystemExit):
                license.license_from_copying_hash('tests/COPYING_TEST', '',
                                                  conf, '')

        self.assertIn('Unable to fetch license.server.url: Test Exception',
                      out.getvalue())

        # unset the manual mock
        download.pycurl.Curl = pycurl.Curl
Beispiel #11
0
    def test_license_from_copying_hash_license_server(self, mock_pycurl_curl):
        """
        Test license_from_copying_hash with license server. This is heavily
        mocked.
        """
        class MockBytesIO(BytesIO):
            """
            Mock class for BytesIO to set returnvalue of BytesIO.getvalue()
            """
            def getvalue(_):
                return 'GPL-3.0'.encode('utf-8')

        # set the mocks
        mock_pycurl_curl.return_value = MagicMock()
        license.BytesIO = MockBytesIO

        license.config.license_fetch = 'license.server.url'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            # note the replace corrupting the file contents
            content = copyingf.read()

        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        # Also patch the open in license.py
        m_open = mock_open(read_data=content)
        with patch('tarball.open', m_open, create=True):
            with patch('license.open', m_open, create=True):
                # let's check that the proper thing is being printed as well
                out = StringIO()
                with redirect_stdout(out):
                    license.license_from_copying_hash('copying.txt')

        self.assertIn('GPL-3.0', license.licenses)
        self.assertIn('License     :  GPL-3.0  (server)', out.getvalue())

        # unset the manual mock
        license.BytesIO = BytesIO
Beispiel #12
0
    def test_license_from_copying_hash_license_server(self):
        """
        Test license_from_copying_hash with license server. This is heavily
        mocked.
        """
        class MockBytesIO(BytesIO):
            """
            Mock class for BytesIO to set returnvalue of BytesIO.getvalue()
            """
            def getvalue(_):
                return 'GPL-3.0'.encode('utf-8')

        # set the mocks
        download.BytesIO = MockBytesIO

        class MockCurl():
            URL = None
            WRITEDATA = None
            POSTFIELDS = None
            FOLLOWLOCATION = 0
            FAILONERROR = False
            CONNECTTIMEOUT = 0
            TIMEOUT = 0
            LOW_SPEED_LIMIT = 0
            LOW_SPEED_TIME = 0

            def setopt(_, __, ___):
                pass

            def perform(_):
                pass

            def close(_):
                pass

            def getinfo(_, __):
                return 200

        # set the mock curl
        download.pycurl.Curl = MockCurl

        license.config.license_fetch = 'license.server.url'
        with open('tests/COPYING_TEST', 'rb') as copyingf:
            content = copyingf.read()

        # Calls out to tarball.get_sha1sum to get the hash of the license
        # we might as well test that is returning what it should because it
        # doesn't call any external resources, it just calculates the hash.
        # Also patch the open in license.py
        m_open = mock_open(read_data=content)
        with patch('tarball.open', m_open, create=True):
            with patch('license.open', m_open, create=True):
                # let's check that the proper thing is being printed as well
                out = StringIO()
                with redirect_stdout(out):
                    license.license_from_copying_hash('copying.txt', '')

        self.assertIn('GPL-3.0', license.licenses)
        self.assertIn('License     :  GPL-3.0  (server)', out.getvalue())

        # unset the manual mock
        download.BytesIO = BytesIO

        # unset the manual mock
        download.pycurl.Curl = pycurl.Curl
Beispiel #13
0
 def test_license_from_copying_hash(self):
     """
     Test license_from_copying_hash with valid license file
     """
     license.license_from_copying_hash('tests/COPYING_TEST', '')
     self.assertIn('GPL-3.0', license.licenses)