body common control
{
  bundlesequence => { "init", "test", "check" };
  version => "1.0";
}

bundle common G {
  vars:
      "testdir"
        string => concat(
          getenv("TEMP", "65535"),
          "$(const.dirsep)TESTDIR.cfengine"
        );
}

bundle agent init
{
  vars:
      "files"
        slist => {
          "core/.gitignore",
          "core/.git/config",
          "core/libpromises/cf3parse.y",
          "core/libpromises/cf3lex.l",
          "core/libntech/.gitignore",
          "core/libntech/.git/config",
          "core/libntech/libutils/string.h",
          "core/libntech/libutils/string.c"
        };

  files:
      "$(G.testdir)/$(files)"
        create => "true";

  reports:
    DEBUG::
      "Created $(G.testdir)/$(files)";
}

bundle agent test
{
  meta:
      "description" -> { "CFE-3577" }
        string => "Test for expected results from policy function findfiles_up";

  vars:
      "t1"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".gitignore", "inf");
      "t2"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", "string.?");
      "t3"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".git/");
      "t4"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".git/", "1");
      "t5"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", ".git/config");
      "t6"
        data => findfiles_up("$(G.testdir)/core/libntech/libutils/", "*/cf?{lex,parse}.[ly]");
}

bundle agent check
{
  classes:
    windows::
      "c1"
        expression => and(
          strcmp("$(G.testdir)\\core\\libntech\\.gitignore", "$(test.t1[0])"),
          strcmp("$(G.testdir)\\core\\.gitignore", "$(test.t1[1])")
        );
      "c2"
        expression => and(
          strcmp("$(G.testdir)\\core\\libntech\\libutils\\string.c", "$(test.t2[0])"),
          strcmp("$(G.testdir)\\core\\libntech\\libutils\\string.h", "$(test.t2[1])")
        );
      "c3"
        expression => and(
          strcmp("$(G.testdir)\\core\\libntech\\.git\\", "$(test.t3[0])"),
          strcmp("$(G.testdir)\\core\\.git\\", "$(test.t3[1])")
        );
      "c4"
        expression => and(
          strcmp("$(G.testdir)\\core\\libntech\\.git\\", "$(test.t4[0])"),
          not(isvariable("test.t4[1]"))
        );
      "c5"
        expression => and(
          strcmp("$(G.testdir)\\core\\libntech\\.git\\config", "$(test.t5[0])"),
          strcmp("$(G.testdir)\\core\\.git\\config", "$(test.t5[1])")
        );
      "c6"
        expression => and(
          strcmp("$(G.testdir)\\core\\libpromises\\cf3lex.l", "$(test.t6[0])"),
          strcmp("$(G.testdir)\\core\\libpromises\\cf3parse.y", "$(test.t6[1])")
        );

    !windows::
      "c1"
        expression => and(
          strcmp("$(G.testdir)/core/libntech/.gitignore", "$(test.t1[0])"),
          strcmp("$(G.testdir)/core/.gitignore", "$(test.t1[1])")
        );
      "c2"
        expression => and(
          strcmp("$(G.testdir)/core/libntech/libutils/string.c", "$(test.t2[0])"),
          strcmp("$(G.testdir)/core/libntech/libutils/string.h", "$(test.t2[1])")
        );
      "c3"
        expression => and(
          strcmp("$(G.testdir)/core/libntech/.git/", "$(test.t3[0])"),
          strcmp("$(G.testdir)/core/.git/", "$(test.t3[1])")
        );
      "c4"
        expression => and(
          strcmp("$(G.testdir)/core/libntech/.git/", "$(test.t4[0])"),
          not(isvariable("test.t4[1]"))
        );
      "c5"
        expression => and(
          strcmp("$(G.testdir)/core/libntech/.git/config", "$(test.t5[0])"),
          strcmp("$(G.testdir)/core/.git/config", "$(test.t5[1])")
        );
      "c6"
        expression => and(
          strcmp("$(G.testdir)/core/libpromises/cf3lex.l", "$(test.t6[0])"),
          strcmp("$(G.testdir)/core/libpromises/cf3parse.y", "$(test.t6[1])")
        );

    any::
      "ok"
        expression => and("c1", "c2", "c3", "c4", "c5", "c6");

  reports:
    DEBUG.windows.!c1::
      "$(const.dollar)(test.t1[0]): Expected '$(G.testdir)\\core\\libntech\\.gitignore', found '$(test.t1[0])'";
      "$(const.dollar)(test.t1[1]): Expected '$(G.testdir)\\core\\.gitignore', found '$(test.t1[1])'";
    DEBUG.windows.!c2::
      "$(const.dollar)(test.t2[0]): Expected '$(G.testdir)\\core\\libntech\\libutils\\string.c', found '$(test.t2[0])'";
      "$(const.dollar)(test.t2[1]): Expected '$(G.testdir)\\core\\libntech\\libutils\\string.h', found '$(test.t2[1])'";
    DEBUG.windows.!c3::
      "$(const.dollar)(test.t3[0]): Expected '$(G.testdir)\\core\\libntech\\.git', found '$(test.t3[0])'";
      "$(const.dollar)(test.t3[1]): Expected '$(G.testdir)\\core\\.git', found '$(test.t3[1])'";
    DEBUG.windows.!c4::
      "$(const.dollar)(test.t4[0]): Expected '$(G.testdir)\\core\\libntech\\.git', found '$(test.t4[0])'";
      "$(const.dollar)(test.t4[1]): Should not exist, $(with). Expanded value: '$(test.t4[1])'"
        with => ifelse(isvariable("test.t4[1]"), "but does exist", "and does not exist");
    DEBUG.windows.!c5::
      "$(const.dollar)(test.t5[0]): Expected '$(G.testdir)\\core\\libntech\\.git\\config', found '$(test.t5[0])'";
      "$(const.dollar)(test.t5[1]): Expected '$(G.testdir)\\core\\.git\\config', found '$(test.t5[1])'";
    DEBUG.windows.!c6::
      "$(const.dollar)(test.t6[0]): Expected '$(G.testdir)\\core\\libpromises\\cf3lex.l', found '$(test.t6[0])'";
      "$(const.dollar)(test.t6[1]): Expected '$(G.testdir)\\core\\libpromises\\cf3parse.y', found '$(test.t6[1])'";

    DEBUG.!windows.!c1::
      "$(const.dollar)(test.t1[0]): Expected '$(G.testdir)/core/libntech/.gitignore', found '$(test.t1[0])'";
      "$(const.dollar)(test.t1[1]): Expected '$(G.testdir)/core/.gitignore', found '$(test.t1[1])'";
    DEBUG.!windows.!c2::
      "$(const.dollar)(test.t2[0]): Expected '$(G.testdir)/core/libntech/libutils/string.c', found '$(test.t2[0])'";
      "$(const.dollar)(test.t2[1]): Expected '$(G.testdir)/core/libntech/libutils/string.h', found '$(test.t2[1])'";
    DEBUG.!windows.!c3::
      "$(const.dollar)(test.t3[0]): Expected '$(G.testdir)/core/libntech/.git', found '$(test.t3[0])'";
      "$(const.dollar)(test.t3[1]): Expected '$(G.testdir)/core/.git', found '$(test.t3[1])'";
    DEBUG.!windows.!c4::
      "$(const.dollar)(test.t4[0]): Expected '$(G.testdir)/core/libntech/.git', found '$(test.t4[0])'";
      "$(const.dollar)(test.t4[1]): Should not exist, $(with). Expanded value: '$(test.t4[1])'"
        with => ifelse(isvariable("test.t4[1]"), "but does exist", "and does not exist");
    DEBUG.!windows.!c5::
      "$(const.dollar)(test.t5[0]): Expected '$(G.testdir)/core/libntech/.git/config', found '$(test.t5[0])'";
      "$(const.dollar)(test.t5[1]): Expected '$(G.testdir)/core/.git/config', found '$(test.t5[1])'";
    DEBUG.!windows.!c6::
      "$(const.dollar)(test.t6[0]): Expected '$(G.testdir)/core/libpromises/cf3lex.l', found '$(test.t6[0])'";
      "$(const.dollar)(test.t6[1]): Expected '$(G.testdir)/core/libpromises/cf3parse.y', found '$(test.t6[1])'";

    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
