Comparison

spec/util_format_spec.lua @ 12039:e0a8c5b1ab4f

util.format: Ensure metatable __tostring results are also sanitized
author Kim Alvefur <zash@zash.se>
date Mon, 13 Dec 2021 16:34:55 +0100
parent 12036:2ce06f788093
child 12220:25b853e64d83
comparison
equal deleted inserted replaced
12038:4e54334febc1 12039:e0a8c5b1ab4f
778 778
779 describe("table", function () 779 describe("table", function ()
780 describe("to %c", function () 780 describe("to %c", function ()
781 it("works", function () 781 it("works", function ()
782 assert.matches("[table: 0[xX]%x+]", format("%c", { })) 782 assert.matches("[table: 0[xX]%x+]", format("%c", { }))
783 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%c", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
783 end); 784 end);
784 end); 785 end);
785 786
786 describe("to %d", function () 787 describe("to %d", function ()
787 it("works", function () 788 it("works", function ()
788 assert.matches("[table: 0[xX]%x+]", format("%d", { })) 789 assert.matches("[table: 0[xX]%x+]", format("%d", { }))
790 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%d", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
789 end); 791 end);
790 end); 792 end);
791 793
792 describe("to %i", function () 794 describe("to %i", function ()
793 it("works", function () 795 it("works", function ()
794 assert.matches("[table: 0[xX]%x+]", format("%i", { })) 796 assert.matches("[table: 0[xX]%x+]", format("%i", { }))
797 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%i", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
795 end); 798 end);
796 end); 799 end);
797 800
798 describe("to %o", function () 801 describe("to %o", function ()
799 it("works", function () 802 it("works", function ()
800 assert.matches("[table: 0[xX]%x+]", format("%o", { })) 803 assert.matches("[table: 0[xX]%x+]", format("%o", { }))
804 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%o", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
801 end); 805 end);
802 end); 806 end);
803 807
804 describe("to %u", function () 808 describe("to %u", function ()
805 it("works", function () 809 it("works", function ()
806 assert.matches("[table: 0[xX]%x+]", format("%u", { })) 810 assert.matches("[table: 0[xX]%x+]", format("%u", { }))
811 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%u", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
807 end); 812 end);
808 end); 813 end);
809 814
810 describe("to %x", function () 815 describe("to %x", function ()
811 it("works", function () 816 it("works", function ()
812 assert.matches("[table: 0[xX]%x+]", format("%x", { })) 817 assert.matches("[table: 0[xX]%x+]", format("%x", { }))
818 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%x", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
813 end); 819 end);
814 end); 820 end);
815 821
816 describe("to %X", function () 822 describe("to %X", function ()
817 it("works", function () 823 it("works", function ()
818 assert.matches("[table: 0[xX]%x+]", format("%X", { })) 824 assert.matches("[table: 0[xX]%x+]", format("%X", { }))
825 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%X", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
819 end); 826 end);
820 end); 827 end);
821 828
822 describe("to %a", function () 829 describe("to %a", function ()
823 it("works", function () 830 it("works", function ()
824 assert.matches("[table: 0[xX]%x+]", format("%a", { })) 831 assert.matches("[table: 0[xX]%x+]", format("%a", { }))
832 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%a", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
825 end); 833 end);
826 end); 834 end);
827 835
828 describe("to %A", function () 836 describe("to %A", function ()
829 it("works", function () 837 it("works", function ()
830 assert.matches("[table: 0[xX]%x+]", format("%A", { })) 838 assert.matches("[table: 0[xX]%x+]", format("%A", { }))
839 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%A", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
831 end); 840 end);
832 end); 841 end);
833 842
834 describe("to %e", function () 843 describe("to %e", function ()
835 it("works", function () 844 it("works", function ()
836 assert.matches("[table: 0[xX]%x+]", format("%e", { })) 845 assert.matches("[table: 0[xX]%x+]", format("%e", { }))
846 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%e", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
837 end); 847 end);
838 end); 848 end);
839 849
840 describe("to %E", function () 850 describe("to %E", function ()
841 it("works", function () 851 it("works", function ()
842 assert.matches("[table: 0[xX]%x+]", format("%E", { })) 852 assert.matches("[table: 0[xX]%x+]", format("%E", { }))
853 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%E", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
843 end); 854 end);
844 end); 855 end);
845 856
846 describe("to %f", function () 857 describe("to %f", function ()
847 it("works", function () 858 it("works", function ()
848 assert.matches("[table: 0[xX]%x+]", format("%f", { })) 859 assert.matches("[table: 0[xX]%x+]", format("%f", { }))
860 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%f", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
849 end); 861 end);
850 end); 862 end);
851 863
852 describe("to %g", function () 864 describe("to %g", function ()
853 it("works", function () 865 it("works", function ()
854 assert.matches("[table: 0[xX]%x+]", format("%g", { })) 866 assert.matches("[table: 0[xX]%x+]", format("%g", { }))
867 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%g", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
855 end); 868 end);
856 end); 869 end);
857 870
858 describe("to %G", function () 871 describe("to %G", function ()
859 it("works", function () 872 it("works", function ()
860 assert.matches("[table: 0[xX]%x+]", format("%G", { })) 873 assert.matches("[table: 0[xX]%x+]", format("%G", { }))
874 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%G", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
861 end); 875 end);
862 end); 876 end);
863 877
864 describe("to %q", function () 878 describe("to %q", function ()
865 it("works", function () 879 it("works", function ()
866 assert.matches("{ }", format("%q", { })) 880 assert.matches("{ }", format("%q", { }))
881 assert.equal("{ }", format("%q", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
867 end); 882 end);
868 end); 883 end);
869 884
870 describe("to %s", function () 885 describe("to %s", function ()
871 it("works", function () 886 it("works", function ()
872 assert.matches("table: 0[xX]%x+", format("%s", { })) 887 assert.matches("table: 0[xX]%x+", format("%s", { }))
888 assert.equal("foo \226\144\129\226\144\130\226\144\131 bar", format("%s", setmetatable({},{__tostring=function ()return "foo \1\2\3 bar"end})))
873 end); 889 end);
874 end); 890 end);
875 891
876 end); 892 end);
877 893