2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/utils/nginx_parser"
|
2017-05-31 03:57:32 +00:00
|
|
|
|
|
|
|
describe NginxParser do
|
|
|
|
def parse(c)
|
|
|
|
NginxParser.new.parse(c)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsestr(c)
|
|
|
|
parse(c).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_file(f)
|
|
|
|
parse(File.read(f))
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an empty nginx file" do
|
|
|
|
_(parsestr("")).must_equal ""
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses a file with a comment" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("# some nice comment")).must_equal "# some nice comment"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses a simple assignment" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("assignment a;")).must_equal "[{:assignment=>{:identifier=>\"assignment\"@0, :args=>[{:value=>\"a\"@11}]}}]"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignment with a single quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse("include '/a/b/c/*.conf';")
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
|
|
|
result[0][:assignment][:args][0][:value].must_equal "/a/b/c/*.conf"
|
2018-05-03 13:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignment with a double quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse('include "/a/b/c/*.conf";')
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
|
|
|
result[0][:assignment][:args][0][:value].must_equal "/a/b/c/*.conf"
|
2018-05-03 13:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignemnt with single quote in a double quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse('include "/a/\'b/*.conf";')
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
|
|
|
result[0][:assignment][:args][0][:value].must_equal "/a/'b/*.conf"
|
2018-05-03 13:53:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignemnt with double quote in a single quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse("include '/a/\"b/*.conf';")
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
2018-05-03 13:53:20 +00:00
|
|
|
result[0][:assignment][:args][0][:value].must_equal "/a/\"b/*.conf"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignemnt with single quote in a single quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse("include '/a/\\\'b/*.conf';")
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
2018-05-03 13:53:20 +00:00
|
|
|
result[0][:assignment][:args][0][:value].must_equal "/a/\\\'b/*.conf"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an assignemnt with double quote in a double quoted value" do
|
2018-05-03 13:53:20 +00:00
|
|
|
result = parse('include "/a/\"b/*.conf";')
|
2019-06-11 22:24:35 +00:00
|
|
|
result[0][:assignment][:identifier].must_equal "include"
|
2018-05-03 13:53:20 +00:00
|
|
|
result[0][:assignment][:args][0][:value].must_equal '/a/\"b/*.conf'
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses an empty group" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("group {}")).must_equal "[{:section=>{:identifier=>\"group\"@0}, :args=>\"\", :expressions=>[]}]"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses a group with parameters" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("group a b {}")).must_equal "[{:section=>{:identifier=>\"group\"@0}, :args=>[{:value=>\"a\"@6}, {:value=>\"b\"@8}], :expressions=>[]}]"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses a group with a body" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("group {\na b;\n}")).must_equal "[{:section=>{:identifier=>\"group\"@0}, :args=>\"\", :expressions=>[{:assignment=>{:identifier=>\"a\"@8, :args=>[{:value=>\"b\"@10}]}}]}]"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses a group with arguments and a body" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("group c {\na b;\n}")).must_equal "[{:section=>{:identifier=>\"group\"@0}, :args=>[{:value=>\"c\"@6}], :expressions=>[{:assignment=>{:identifier=>\"a\"@10, :args=>[{:value=>\"b\"@12}]}}]}]"
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses nested groups" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parsestr("f {g {h {\n# comment\n}}}")).must_equal "[{:section=>{:identifier=>\"f\"@0}, :args=>\"\", :expressions=>[{:section=>{:identifier=>\"g\"@3}, :args=>\"\", :expressions=>[{:section=>{:identifier=>\"h\"@6}, :args=>\"\", :expressions=>[]}]}]}]"
|
|
|
|
end
|
2017-11-08 17:42:37 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "parses quoted identifiers for assignments" do
|
2017-11-08 17:42:37 +00:00
|
|
|
_(parsestr(%{"~^\/opcache-api" 1;})).must_equal "[{:assignment=>{:identifier=>\"~^/opcache-api\"@1, :args=>[{:value=>\"1\"@17}]}}]"
|
|
|
|
end
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe NginxTransform do
|
|
|
|
def parse(c)
|
|
|
|
NginxTransform.new.apply(NginxParser.new.parse(c))
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "transforms and empty file" do
|
|
|
|
_(parse("")).must_equal ""
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "transforms an assignment" do
|
|
|
|
_(parse("a b;")).must_equal [NginxTransform::Exp.new("a", ["b"])]
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "transforms an empty group" do
|
|
|
|
_(parse("group {}")).must_equal [NginxTransform::Group.new("group", "", [])]
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "transforms a simple group" do
|
|
|
|
_(parse("group a {\na b;\n}")).must_equal [NginxTransform::Group.new("group", ["a"], [NginxTransform::Exp.new("a", ["b"])])]
|
2017-05-31 03:57:32 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "transforms a deeply nested group" do
|
2017-05-31 03:57:32 +00:00
|
|
|
_(parse("f { g { h {\na b;\n}}}")).must_equal [
|
2019-06-11 22:24:35 +00:00
|
|
|
NginxTransform::Group.new("f", "", [
|
|
|
|
NginxTransform::Group.new("g", "", [
|
2019-07-09 00:20:30 +00:00
|
|
|
NginxTransform::Group.new("h", "", [NginxTransform::Exp.new("a", ["b"])]),
|
|
|
|
]),
|
|
|
|
]),
|
2017-05-31 03:57:32 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|