|
4 | 4 | #include "LuaEvaluator.h" |
5 | 5 |
|
6 | 6 | #include "Indicators.h" |
| 7 | +#include "Logging.h" |
7 | 8 | #include "MockContext.h" |
8 | 9 | #include "Result.h" |
9 | 10 |
|
@@ -598,6 +599,157 @@ TEST_F(LuaEvaluatorTest, Performance_MultipleEvaluations) |
598 | 599 | } |
599 | 600 | } |
600 | 601 |
|
| 602 | +// Test ce.log functions exist and can be called without error |
| 603 | +TEST_F(LuaEvaluatorTest, Log_Callable_AllLevels) |
| 604 | +{ |
| 605 | + LuaEvaluator evaluator; |
| 606 | + |
| 607 | + const std::string script = R"( |
| 608 | + ce.log.info("info message") |
| 609 | + ce.log.warning("warning message") |
| 610 | + ce.log.error("error message") |
| 611 | + ce.log.debug("debug message") |
| 612 | + return true |
| 613 | + )"; |
| 614 | + |
| 615 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 616 | + |
| 617 | + ASSERT_TRUE(result.HasValue()); |
| 618 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 619 | +} |
| 620 | + |
| 621 | +// Test that print is no longer available in the restricted Lua environment |
| 622 | +TEST_F(LuaEvaluatorTest, Security_PrintBlocked) |
| 623 | +{ |
| 624 | + LuaEvaluator evaluator; |
| 625 | + |
| 626 | + const std::string script = "if print then return 'print available' else return true end"; |
| 627 | + |
| 628 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 629 | + |
| 630 | + ASSERT_TRUE(result.HasValue()); |
| 631 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 632 | +} |
| 633 | + |
| 634 | +// Test that ce.log.info with no argument raises a Lua error |
| 635 | +TEST_F(LuaEvaluatorTest, Log_InvalidUsage_NoArgument) |
| 636 | +{ |
| 637 | + LuaEvaluator evaluator; |
| 638 | + |
| 639 | + const std::string script = R"( |
| 640 | + local ok, err = pcall(function() ce.log.info() end) |
| 641 | + if ok then |
| 642 | + return false, "expected error for missing argument" |
| 643 | + end |
| 644 | + return true, tostring(err) |
| 645 | + )"; |
| 646 | + |
| 647 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 648 | + |
| 649 | + ASSERT_TRUE(result.HasValue()); |
| 650 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 651 | +} |
| 652 | + |
| 653 | +// Test that ce.log.info with a non-string, non-coercible argument raises a Lua error |
| 654 | +TEST_F(LuaEvaluatorTest, Log_InvalidUsage_NonStringArgument) |
| 655 | +{ |
| 656 | + LuaEvaluator evaluator; |
| 657 | + |
| 658 | + // Boolean false is not coercible to a string in Lua |
| 659 | + const std::string script = R"( |
| 660 | + local ok, err = pcall(function() ce.log.info(false) end) |
| 661 | + if ok then |
| 662 | + return false, "expected error for non-string argument" |
| 663 | + end |
| 664 | + return true, tostring(err) |
| 665 | + )"; |
| 666 | + |
| 667 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 668 | + |
| 669 | + ASSERT_TRUE(result.HasValue()); |
| 670 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 671 | +} |
| 672 | + |
| 673 | +// Test that ce.log.info with more than one argument raises a Lua error |
| 674 | +TEST_F(LuaEvaluatorTest, Log_InvalidUsage_ExtraArguments) |
| 675 | +{ |
| 676 | + LuaEvaluator evaluator; |
| 677 | + |
| 678 | + const std::string script = R"( |
| 679 | + local ok, err = pcall(function() ce.log.info("msg", "extra") end) |
| 680 | + if ok then |
| 681 | + return false, "expected error for extra argument" |
| 682 | + end |
| 683 | + return true, tostring(err) |
| 684 | + )"; |
| 685 | + |
| 686 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 687 | + |
| 688 | + ASSERT_TRUE(result.HasValue()); |
| 689 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 690 | +} |
| 691 | + |
| 692 | +// Test that ce.log.info with an empty string raises a Lua error |
| 693 | +TEST_F(LuaEvaluatorTest, Log_InvalidUsage_EmptyString) |
| 694 | +{ |
| 695 | + LuaEvaluator evaluator; |
| 696 | + |
| 697 | + const std::string script = R"( |
| 698 | + local ok, err = pcall(function() ce.log.info("") end) |
| 699 | + if ok then |
| 700 | + return false, "expected error for empty message" |
| 701 | + end |
| 702 | + return true, tostring(err) |
| 703 | + )"; |
| 704 | + |
| 705 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 706 | + |
| 707 | + ASSERT_TRUE(result.HasValue()); |
| 708 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 709 | +} |
| 710 | + |
| 711 | +// E2E test: verify ce.log messages are written to the log file with the [Lua] prefix |
| 712 | +TEST_F(LuaEvaluatorTest, Log_E2E_WritesToLogFile) |
| 713 | +{ |
| 714 | + const std::string logPath = mContext.GetTempdirPath() + "/lua_test.log"; |
| 715 | + OsConfigLogHandle logHandle = OpenLog(logPath.c_str(), nullptr); |
| 716 | + ASSERT_NE(nullptr, logHandle); |
| 717 | + |
| 718 | + const LoggingLevel savedLevel = GetLoggingLevel(); |
| 719 | + const bool savedConsole = IsConsoleLoggingEnabled(); |
| 720 | + SetLoggingLevel(LoggingLevelDebug); |
| 721 | + SetConsoleLoggingEnabled(false); |
| 722 | + |
| 723 | + mContext.SetLogHandle(logHandle); |
| 724 | + |
| 725 | + LuaEvaluator evaluator; |
| 726 | + const std::string script = R"( |
| 727 | + ce.log.info("info message") |
| 728 | + ce.log.warning("warning message") |
| 729 | + ce.log.error("error message") |
| 730 | + ce.log.debug("debug message") |
| 731 | + return true |
| 732 | + )"; |
| 733 | + |
| 734 | + auto result = evaluator.Evaluate(script, mIndicators, mContext, Action::Audit); |
| 735 | + |
| 736 | + CloseLog(&logHandle); |
| 737 | + mContext.SetLogHandle(nullptr); |
| 738 | + SetLoggingLevel(savedLevel); |
| 739 | + SetConsoleLoggingEnabled(savedConsole); |
| 740 | + |
| 741 | + ASSERT_TRUE(result.HasValue()); |
| 742 | + EXPECT_EQ(result.Value(), Status::Compliant); |
| 743 | + |
| 744 | + std::ifstream logFile(logPath); |
| 745 | + const std::string contents((std::istreambuf_iterator<char>(logFile)), std::istreambuf_iterator<char>()); |
| 746 | + |
| 747 | + EXPECT_THAT(contents, ::testing::HasSubstr("[Lua] info message")); |
| 748 | + EXPECT_THAT(contents, ::testing::HasSubstr("[Lua] warning message")); |
| 749 | + EXPECT_THAT(contents, ::testing::HasSubstr("[Lua] error message")); |
| 750 | + EXPECT_THAT(contents, ::testing::HasSubstr("[Lua] debug message")); |
| 751 | +} |
| 752 | + |
601 | 753 | // Test non-copyable nature of LuaEvaluator |
602 | 754 | TEST_F(LuaEvaluatorTest, NonCopyable) |
603 | 755 | { |
|
0 commit comments