-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathtest_datapickler.py
125 lines (91 loc) · 3.55 KB
/
test_datapickler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2014-2023 Greenbone AG
#
# SPDX-License-Identifier: AGPL-3.0-or-later
import pickle
from pathlib import Path
from hashlib import sha256
from unittest import TestCase
from unittest.mock import patch
from ospd.errors import OspdCommandError
from ospd.datapickler import DataPickler
from .helper import assert_called_once
class DataPecklerTestCase(TestCase):
def test_store_data(self):
data = {'foo', 'bar'}
filename = 'scan_info_1'
pickled_data = pickle.dumps(data)
tmp_hash = sha256()
tmp_hash.update(pickled_data)
data_pickler = DataPickler('/tmp')
ret = data_pickler.store_data(filename, data)
self.assertEqual(ret, tmp_hash.hexdigest())
data_pickler.remove_file(filename)
def test_store_data_failed(self):
data = {'foo', 'bar'}
filename = 'scan_info_1'
data_pickler = DataPickler('/root')
self.assertRaises(
OspdCommandError, data_pickler.store_data, filename, data
)
def test_store_data_check_permission(self):
OWNER_ONLY_RW_PERMISSION = '0o100600' # pylint: disable=invalid-name
data = {'foo', 'bar'}
filename = 'scan_info_1'
data_pickler = DataPickler('/tmp')
data_pickler.store_data(filename, data)
file_path = (
Path(data_pickler._storage_path) # pylint: disable=protected-access
/ filename
)
self.assertEqual(
oct(file_path.stat().st_mode), OWNER_ONLY_RW_PERMISSION
)
data_pickler.remove_file(filename)
def test_load_data(self):
data_pickler = DataPickler('/tmp')
data = {'foo', 'bar'}
filename = 'scan_info_1'
pickled_data = pickle.dumps(data)
tmp_hash = sha256()
tmp_hash.update(pickled_data)
pickled_data_hash = tmp_hash.hexdigest()
ret = data_pickler.store_data(filename, data)
self.assertEqual(ret, pickled_data_hash)
original_data = data_pickler.load_data(filename, pickled_data_hash)
self.assertIsNotNone(original_data)
self.assertIn('foo', original_data)
@patch("ospd.datapickler.logger")
def test_remove_file_failed(self, mock_logger):
filename = 'inenxistent_file'
data_pickler = DataPickler('/root')
data_pickler.remove_file(filename)
assert_called_once(mock_logger.error)
@patch("ospd.datapickler.logger")
def test_load_data_no_file(self, mock_logger):
filename = 'scan_info_1'
data_pickler = DataPickler('/tmp')
data_loaded = data_pickler.load_data(filename, "1234")
assert_called_once(mock_logger.error)
self.assertIsNone(data_loaded)
data_pickler.remove_file(filename)
def test_load_data_corrupted(self):
data_pickler = DataPickler('/tmp')
data = {'foo', 'bar'}
filename = 'scan_info_1'
pickled_data = pickle.dumps(data)
tmp_hash = sha256()
tmp_hash.update(pickled_data)
pickled_data_hash = tmp_hash.hexdigest()
ret = data_pickler.store_data(filename, data)
self.assertEqual(ret, pickled_data_hash)
# courrupt data
file_to_corrupt = (
Path(data_pickler._storage_path) # pylint: disable=protected-access
/ filename
)
with file_to_corrupt.open('ab') as f:
f.write(b'bar2')
original_data = data_pickler.load_data(filename, pickled_data_hash)
self.assertIsNone(original_data)
data_pickler.remove_file(filename)