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