|
26 | 26 | #include <brpc/server.h> |
27 | 27 | #include <brpc/redis_command.h> |
28 | 28 | #include <gtest/gtest.h> |
29 | | -#include <gflags/gflags.h> |
30 | 29 |
|
31 | 30 | namespace brpc { |
32 | 31 | DECLARE_int32(idle_timeout_second); |
33 | | -DECLARE_int32(redis_max_allocation_size); |
34 | 32 | } |
35 | 33 |
|
36 | 34 | int main(int argc, char* argv[]) { |
@@ -1352,97 +1350,4 @@ TEST_F(RedisTest, server_handle_pipeline) { |
1352 | 1350 | ASSERT_STREQ(response.reply(7).c_str(), "world"); |
1353 | 1351 | } |
1354 | 1352 |
|
1355 | | -TEST_F(RedisTest, memory_allocation_limits) { |
1356 | | - int32_t original_limit = brpc::FLAGS_redis_max_allocation_size; |
1357 | | - brpc::FLAGS_redis_max_allocation_size = 1024; |
1358 | | - |
1359 | | - butil::Arena arena; |
1360 | | - |
1361 | | - // Test redis_reply.cpp limits |
1362 | | - { |
1363 | | - // Test bulk string exceeding limit |
1364 | | - butil::IOBuf buf; |
1365 | | - std::string large_string = "*1\r\n$2000\r\n"; |
1366 | | - large_string.append(2000, 'a'); |
1367 | | - large_string.append("\r\n"); |
1368 | | - buf.append(large_string); |
1369 | | - |
1370 | | - brpc::RedisReply reply(&arena); |
1371 | | - brpc::ParseError err = reply.ConsumePartialIOBuf(buf); |
1372 | | - ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err); |
1373 | | - } |
1374 | | - |
1375 | | - { |
1376 | | - // Test array allocation exceeding limit |
1377 | | - butil::IOBuf buf; |
1378 | | - int32_t large_count = brpc::FLAGS_redis_max_allocation_size / sizeof(brpc::RedisReply) + 1; |
1379 | | - std::string large_array = "*" + std::to_string(large_count) + "\r\n"; |
1380 | | - buf.append(large_array); |
1381 | | - |
1382 | | - brpc::RedisReply reply(&arena); |
1383 | | - brpc::ParseError err = reply.ConsumePartialIOBuf(buf); |
1384 | | - ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err); |
1385 | | - } |
1386 | | - |
1387 | | - // Test redis_command.cpp limits |
1388 | | - { |
1389 | | - // Test command string exceeding limit |
1390 | | - brpc::RedisCommandParser parser; |
1391 | | - butil::IOBuf buf; |
1392 | | - std::string large_cmd = "*2\r\n$3\r\nget\r\n$2000\r\n"; |
1393 | | - large_cmd.append(2000, 'b'); |
1394 | | - large_cmd.append("\r\n"); |
1395 | | - buf.append(large_cmd); |
1396 | | - |
1397 | | - std::vector<butil::StringPiece> args; |
1398 | | - brpc::ParseError err = parser.Consume(buf, &args, &arena); |
1399 | | - ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err); |
1400 | | - } |
1401 | | - |
1402 | | - { |
1403 | | - // Test command array size exceeding limit |
1404 | | - brpc::RedisCommandParser parser; |
1405 | | - butil::IOBuf buf; |
1406 | | - int32_t large_array_size = brpc::FLAGS_redis_max_allocation_size / sizeof(butil::StringPiece) + 1; |
1407 | | - std::string large_array_cmd = "*" + std::to_string(large_array_size) + "\r\n"; |
1408 | | - buf.append(large_array_cmd); |
1409 | | - |
1410 | | - std::vector<butil::StringPiece> args; |
1411 | | - brpc::ParseError err = parser.Consume(buf, &args, &arena); |
1412 | | - ASSERT_EQ(brpc::PARSE_ERROR_ABSOLUTELY_WRONG, err); |
1413 | | - } |
1414 | | - |
1415 | | - // Test valid cases within limits |
1416 | | - { |
1417 | | - // Test small bulk string should work |
1418 | | - butil::IOBuf buf; |
1419 | | - std::string small_string = "*1\r\n$10\r\nhelloworld\r\n"; |
1420 | | - buf.append(small_string); |
1421 | | - |
1422 | | - brpc::RedisReply reply(&arena); |
1423 | | - brpc::ParseError err = reply.ConsumePartialIOBuf(buf); |
1424 | | - ASSERT_EQ(brpc::PARSE_OK, err); |
1425 | | - ASSERT_TRUE(reply.is_array()); |
1426 | | - ASSERT_EQ(1, (int)reply.size()); |
1427 | | - ASSERT_STREQ("helloworld", reply[0].c_str()); |
1428 | | - } |
1429 | | - |
1430 | | - { |
1431 | | - // Test small command should work |
1432 | | - brpc::RedisCommandParser parser; |
1433 | | - butil::IOBuf buf; |
1434 | | - std::string small_cmd = "*2\r\n$3\r\nget\r\n$5\r\nmykey\r\n"; |
1435 | | - buf.append(small_cmd); |
1436 | | - |
1437 | | - std::vector<butil::StringPiece> args; |
1438 | | - brpc::ParseError err = parser.Consume(buf, &args, &arena); |
1439 | | - ASSERT_EQ(brpc::PARSE_OK, err); |
1440 | | - ASSERT_EQ(2, (int)args.size()); |
1441 | | - ASSERT_EQ("get", args[0].as_string()); |
1442 | | - ASSERT_EQ("mykey", args[1].as_string()); |
1443 | | - } |
1444 | | - |
1445 | | - brpc::FLAGS_redis_max_allocation_size = original_limit; |
1446 | | -} |
1447 | | - |
1448 | 1353 | } //namespace |
0 commit comments