|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import io |
15 | 16 | import unittest |
16 | 17 |
|
17 | 18 | from unittest.mock import Mock |
18 | 19 |
|
19 | 20 | from cassandra import ProtocolVersion, UnsupportedOperation |
| 21 | +from cassandra.cqltypes import Int32Type, UTF8Type |
20 | 22 | from cassandra.protocol import ( |
21 | 23 | PrepareMessage, QueryMessage, ExecuteMessage, UnsupportedOperation, |
22 | 24 | _PAGING_OPTIONS_FLAG, _WITH_SERIAL_CONSISTENCY_FLAG, |
23 | 25 | _PAGE_SIZE_FLAG, _WITH_PAGING_STATE_FLAG, |
24 | | - BatchMessage |
| 26 | + BatchMessage, |
| 27 | + ResultMessage, RESULT_KIND_ROWS |
25 | 28 | ) |
26 | 29 | from cassandra.query import BatchType |
27 | | -from cassandra.marshal import uint32_unpack |
| 30 | +from cassandra.marshal import uint32_unpack, int32_pack |
28 | 31 | from cassandra.cluster import ContinuousPagingOptions |
29 | 32 | import pytest |
30 | 33 |
|
| 34 | +from cassandra.policies import ColDesc |
31 | 35 |
|
32 | 36 | class MessageTest(unittest.TestCase): |
33 | 37 |
|
@@ -189,3 +193,130 @@ def test_batch_message_with_keyspace(self): |
189 | 193 | (b'\x00\x03',), |
190 | 194 | (b'\x00\x00\x00\x80',), (b'\x00\x02',), (b'ks',)) |
191 | 195 | ) |
| 196 | + |
| 197 | +class ResultTest(unittest.TestCase): |
| 198 | + """ |
| 199 | + Tests to verify the optimization of column_encryption_policy checks |
| 200 | + in recv_results_rows. The optimization checks if the policy exists once |
| 201 | + per result message, avoiding the redundant 'column_encryption_policy and ...' |
| 202 | + check for every value. |
| 203 | + """ |
| 204 | + |
| 205 | + def _create_mock_result_metadata(self): |
| 206 | + """Create mock result metadata for testing""" |
| 207 | + return [ |
| 208 | + ('keyspace1', 'table1', 'col1', Int32Type), |
| 209 | + ('keyspace1', 'table1', 'col2', UTF8Type), |
| 210 | + ] |
| 211 | + |
| 212 | + def _create_mock_result_message(self): |
| 213 | + """Create a mock result message with data""" |
| 214 | + msg = ResultMessage(kind=RESULT_KIND_ROWS) |
| 215 | + msg.column_metadata = self._create_mock_result_metadata() |
| 216 | + msg.recv_results_metadata = Mock() |
| 217 | + msg.recv_row = Mock(side_effect=[ |
| 218 | + [int32_pack(42), b'hello'], |
| 219 | + [int32_pack(100), b'world'], |
| 220 | + ]) |
| 221 | + return msg |
| 222 | + |
| 223 | + def _create_mock_stream(self): |
| 224 | + """Create a mock stream for reading rows""" |
| 225 | + # Pack rowcount (2 rows) |
| 226 | + data = int32_pack(2) |
| 227 | + return io.BytesIO(data) |
| 228 | + |
| 229 | + def test_decode_without_encryption_policy(self): |
| 230 | + """ |
| 231 | + Test that decoding works correctly without column encryption policy. |
| 232 | + This should use the optimized simple path. |
| 233 | + """ |
| 234 | + msg = self._create_mock_result_message() |
| 235 | + f = self._create_mock_stream() |
| 236 | + |
| 237 | + msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, None) |
| 238 | + |
| 239 | + # Verify results |
| 240 | + self.assertEqual(len(msg.parsed_rows), 2) |
| 241 | + self.assertEqual(msg.parsed_rows[0][0], 42) |
| 242 | + self.assertEqual(msg.parsed_rows[0][1], 'hello') |
| 243 | + self.assertEqual(msg.parsed_rows[1][0], 100) |
| 244 | + self.assertEqual(msg.parsed_rows[1][1], 'world') |
| 245 | + |
| 246 | + def test_decode_with_encryption_policy_no_encrypted_columns(self): |
| 247 | + """ |
| 248 | + Test that decoding works with encryption policy when no columns are encrypted. |
| 249 | + """ |
| 250 | + msg = self._create_mock_result_message() |
| 251 | + f = self._create_mock_stream() |
| 252 | + |
| 253 | + # Create mock encryption policy that has no encrypted columns |
| 254 | + mock_policy = Mock() |
| 255 | + mock_policy.contains_column = Mock(return_value=False) |
| 256 | + |
| 257 | + msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) |
| 258 | + |
| 259 | + # Verify results |
| 260 | + self.assertEqual(len(msg.parsed_rows), 2) |
| 261 | + self.assertEqual(msg.parsed_rows[0][0], 42) |
| 262 | + self.assertEqual(msg.parsed_rows[0][1], 'hello') |
| 263 | + |
| 264 | + # Verify contains_column was called for each value (but policy existence check happens once) |
| 265 | + # Should be called 4 times (2 rows × 2 columns) |
| 266 | + self.assertEqual(mock_policy.contains_column.call_count, 4) |
| 267 | + |
| 268 | + def test_decode_with_encryption_policy_with_encrypted_column(self): |
| 269 | + """ |
| 270 | + Test that decoding works with encryption policy when one column is encrypted. |
| 271 | + """ |
| 272 | + msg = self._create_mock_result_message() |
| 273 | + f = self._create_mock_stream() |
| 274 | + |
| 275 | + # Create mock encryption policy where first column is encrypted |
| 276 | + mock_policy = Mock() |
| 277 | + def contains_column_side_effect(col_desc): |
| 278 | + return col_desc.col == 'col1' |
| 279 | + mock_policy.contains_column = Mock(side_effect=contains_column_side_effect) |
| 280 | + mock_policy.column_type = Mock(return_value=Int32Type) |
| 281 | + mock_policy.decrypt = Mock(side_effect=lambda col_desc, val: val) |
| 282 | + |
| 283 | + msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) |
| 284 | + |
| 285 | + # Verify results |
| 286 | + self.assertEqual(len(msg.parsed_rows), 2) |
| 287 | + self.assertEqual(msg.parsed_rows[0][0], 42) |
| 288 | + self.assertEqual(msg.parsed_rows[0][1], 'hello') |
| 289 | + |
| 290 | + # Verify contains_column was called for each value (but policy existence check happens once) |
| 291 | + # Should be called 4 times (2 rows × 2 columns) |
| 292 | + self.assertEqual(mock_policy.contains_column.call_count, 4) |
| 293 | + |
| 294 | + # Verify decrypt was called for each encrypted value (2 rows * 1 encrypted column) |
| 295 | + self.assertEqual(mock_policy.decrypt.call_count, 2) |
| 296 | + |
| 297 | + def test_optimization_efficiency(self): |
| 298 | + """ |
| 299 | + Verify that the optimization checks policy existence once per result message. |
| 300 | + The key optimization is checking 'if column_encryption_policy:' once, |
| 301 | + rather than 'column_encryption_policy and ...' for every value. |
| 302 | + """ |
| 303 | + msg = self._create_mock_result_message() |
| 304 | + |
| 305 | + # Create more rows to make the check pattern clear |
| 306 | + msg.recv_row = Mock(side_effect=[ |
| 307 | + [int32_pack(i), f'text{i}'.encode()] for i in range(100) |
| 308 | + ]) |
| 309 | + |
| 310 | + # Create mock stream with 100 rows |
| 311 | + f = io.BytesIO(int32_pack(100)) |
| 312 | + |
| 313 | + mock_policy = Mock() |
| 314 | + mock_policy.contains_column = Mock(return_value=False) |
| 315 | + |
| 316 | + msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) |
| 317 | + |
| 318 | + # With optimization: policy existence checked once, contains_column called per value |
| 319 | + # = 100 rows * 2 columns = 200 calls to contains_column |
| 320 | + # The key is we avoid checking 'column_encryption_policy and ...' 200 times |
| 321 | + self.assertEqual(mock_policy.contains_column.call_count, 200, |
| 322 | + "contains_column should be called for each value when policy exists") |
0 commit comments