|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import stat |
| 4 | + |
| 5 | +from queue import Queue |
| 6 | + |
| 7 | +from bears.general.FileModeBear import FileModeBear |
| 8 | +from coalib.testing.LocalBearTestHelper import LocalBearTestHelper |
| 9 | +from coalib.results.Result import RESULT_SEVERITY, Result |
| 10 | +from coalib.settings.Section import Section |
| 11 | +from coalib.settings.Setting import Setting |
| 12 | + |
| 13 | + |
| 14 | +def get_testfile_path(file): |
| 15 | + return os.path.join(os.path.dirname(__file__), |
| 16 | + 'filemode_test_files', file) |
| 17 | + |
| 18 | + |
| 19 | +FILE_PATH = get_testfile_path('test_file.txt') |
| 20 | + |
| 21 | + |
| 22 | +class FileModeBearTest(LocalBearTestHelper): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.section = Section('') |
| 26 | + self.uut = FileModeBear(self.section, Queue()) |
| 27 | + |
| 28 | + def test_r_to_r_permissions(self): |
| 29 | + os.chmod(FILE_PATH, stat.S_IRUSR) |
| 30 | + self.section.append(Setting('filemode', 'r')) |
| 31 | + self.check_results( |
| 32 | + self.uut, |
| 33 | + [], |
| 34 | + [], |
| 35 | + filename=FILE_PATH, |
| 36 | + ) |
| 37 | + |
| 38 | + def test_w_to_w_permissions(self): |
| 39 | + os.chmod(FILE_PATH, stat.S_IWUSR) |
| 40 | + self.section.append(Setting('filemode', 'w')) |
| 41 | + self.check_results( |
| 42 | + self.uut, |
| 43 | + [], |
| 44 | + [], |
| 45 | + filename=FILE_PATH, |
| 46 | + ) |
| 47 | + |
| 48 | + def test_x_to_x_permissions(self): |
| 49 | + os.chmod(FILE_PATH, stat.S_IXUSR) |
| 50 | + if platform.system() != 'Windows': |
| 51 | + self.section.append(Setting('filemode', 'x')) |
| 52 | + self.check_results( |
| 53 | + self.uut, |
| 54 | + [], |
| 55 | + [], |
| 56 | + filename=FILE_PATH, |
| 57 | + ) |
| 58 | + |
| 59 | + def test_rw_to_rw_permissions(self): |
| 60 | + os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR) |
| 61 | + self.section.append(Setting('filemode', 'rw')) |
| 62 | + self.check_results( |
| 63 | + self.uut, |
| 64 | + [], |
| 65 | + [], |
| 66 | + filename=FILE_PATH, |
| 67 | + ) |
| 68 | + |
| 69 | + def test_wx_to_wx_permissions(self): |
| 70 | + os.chmod(FILE_PATH, stat.S_IWUSR | stat.S_IXUSR) |
| 71 | + if platform.system() != 'Windows': |
| 72 | + self.section.append(Setting('filemode', 'wx')) |
| 73 | + self.check_results( |
| 74 | + self.uut, |
| 75 | + [], |
| 76 | + [], |
| 77 | + filename=FILE_PATH, |
| 78 | + ) |
| 79 | + |
| 80 | + def test_rx_to_rx_permissions(self): |
| 81 | + os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IXUSR) |
| 82 | + if platform.system() != 'Windows': |
| 83 | + self.section.append(Setting('filemode', 'rx')) |
| 84 | + self.check_results( |
| 85 | + self.uut, |
| 86 | + [], |
| 87 | + [], |
| 88 | + filename=FILE_PATH, |
| 89 | + ) |
| 90 | + |
| 91 | + def test_rwx_to_rwx_permissions(self): |
| 92 | + os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) |
| 93 | + if platform.system() != 'Windows': |
| 94 | + self.section.append(Setting('filemode', 'rwx')) |
| 95 | + self.check_results( |
| 96 | + self.uut, |
| 97 | + [], |
| 98 | + [], |
| 99 | + filename=FILE_PATH, |
| 100 | + settings={'filemode': 'rwx'}) |
| 101 | + |
| 102 | + def test_r_to_rwx_permissions(self): |
| 103 | + os.chmod(FILE_PATH, stat.S_IRUSR) |
| 104 | + filemode = '-r--------' |
| 105 | + if platform.system() == 'Windows': |
| 106 | + filemode = '-r--r--r--' |
| 107 | + message = ('The file permissions are not adequate. The ' |
| 108 | + 'permissions are set to {}'.format(filemode)) |
| 109 | + self.section.append(Setting('filemode', 'rwx')) |
| 110 | + self.check_results( |
| 111 | + self.uut, |
| 112 | + [], |
| 113 | + [Result.from_values('FileModeBear', |
| 114 | + message, |
| 115 | + file=FILE_PATH, |
| 116 | + severity=RESULT_SEVERITY.INFO)], |
| 117 | + filename=FILE_PATH, |
| 118 | + settings={'filemode': 'rwx'}) |
| 119 | + |
| 120 | + def test_w_to_rwx_permissions(self): |
| 121 | + os.chmod(FILE_PATH, stat.S_IWUSR) |
| 122 | + filemode = '--w-------' |
| 123 | + if platform.system() == 'Windows': |
| 124 | + filemode = '-rw-rw-rw-' |
| 125 | + message = ('The file permissions are not adequate. The ' |
| 126 | + 'permissions are set to {}'.format(filemode)) |
| 127 | + self.section.append(Setting('filemode', 'rwx')) |
| 128 | + self.check_results( |
| 129 | + self.uut, |
| 130 | + [], |
| 131 | + [Result.from_values('FileModeBear', |
| 132 | + message, |
| 133 | + file=FILE_PATH, |
| 134 | + severity=RESULT_SEVERITY.INFO)], |
| 135 | + filename=FILE_PATH, |
| 136 | + ) |
| 137 | + |
| 138 | + def test_x_to_rwx_permissions(self): |
| 139 | + os.chmod(FILE_PATH, stat.S_IXUSR) |
| 140 | + filemode = '---x------' |
| 141 | + if platform.system() != 'Windows': |
| 142 | + message = ('The file permissions are not adequate. The ' |
| 143 | + 'permissions are set to {}'.format(filemode)) |
| 144 | + self.section.append(Setting('filemode', 'rwx')) |
| 145 | + self.check_results( |
| 146 | + self.uut, |
| 147 | + [], |
| 148 | + [Result.from_values('FileModeBear', |
| 149 | + message, |
| 150 | + file=FILE_PATH, |
| 151 | + severity=RESULT_SEVERITY.INFO)], |
| 152 | + filename=FILE_PATH, |
| 153 | + ) |
| 154 | + |
| 155 | + def test_rx_to_rwx_permissions(self): |
| 156 | + os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IXUSR) |
| 157 | + filemode = '-r-x------' |
| 158 | + if platform.system() != 'Windows': |
| 159 | + message = ('The file permissions are not adequate. The ' |
| 160 | + 'permissions are set to {}'.format(filemode)) |
| 161 | + self.section.append(Setting('filemode', 'rwx')) |
| 162 | + self.check_results( |
| 163 | + self.uut, |
| 164 | + [], |
| 165 | + [Result.from_values('FileModeBear', |
| 166 | + message, |
| 167 | + file=FILE_PATH, |
| 168 | + severity=RESULT_SEVERITY.INFO)], |
| 169 | + filename=FILE_PATH, |
| 170 | + ) |
| 171 | + |
| 172 | + def test_wx_to_rwx_permissions(self): |
| 173 | + os.chmod(FILE_PATH, stat.S_IWUSR | stat.S_IXUSR) |
| 174 | + filemode = '--wx------' |
| 175 | + if platform.system() != 'Windows': |
| 176 | + message = ('The file permissions are not adequate. The ' |
| 177 | + 'permissions are set to {}'.format(filemode)) |
| 178 | + self.section.append(Setting('filemode', 'rwx')) |
| 179 | + self.check_results( |
| 180 | + self.uut, |
| 181 | + [], |
| 182 | + [Result.from_values('FileModeBear', |
| 183 | + message, |
| 184 | + file=FILE_PATH, |
| 185 | + severity=RESULT_SEVERITY.INFO)], |
| 186 | + filename=FILE_PATH, |
| 187 | + ) |
| 188 | + |
| 189 | + def test_rw_to_rwx_permissions(self): |
| 190 | + os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR) |
| 191 | + filemode = '-rw-------' |
| 192 | + if platform.system() == 'Windows': |
| 193 | + filemode = '-rw-rw-rw-' |
| 194 | + message = ('The file permissions are not adequate. The ' |
| 195 | + 'permissions are set to {}'.format(filemode)) |
| 196 | + self.section.append(Setting('filemode', 'rwx')) |
| 197 | + self.check_results( |
| 198 | + self.uut, |
| 199 | + [], |
| 200 | + [Result.from_values('FileModeBear', |
| 201 | + message, |
| 202 | + file=FILE_PATH, |
| 203 | + severity=RESULT_SEVERITY.INFO)], |
| 204 | + filename=FILE_PATH, |
| 205 | + ) |
| 206 | + |
| 207 | + def test_invalid_char_in_filemode(self): |
| 208 | + self.section.append(Setting('filemode', 'rwm')) |
| 209 | + error_msg = ('ValueError: Unable to recognize ' |
| 210 | + 'character `m` in filemode `rwm`.') |
| 211 | + with self.assertRaisesRegex(AssertionError, error_msg): |
| 212 | + self.check_validity(self.uut, [], filename=FILE_PATH) |
0 commit comments