<commit_before>#include "support.h"

#include "csync_update.c"

CSYNC *csync;

static void setup(void) {
  fail_if(system("mkdir -p /tmp/check_csync") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_set_config_dir(csync, "/tmp/check_csync") < 0, "Setup failed");
  fail_if(csync_init(csync) < 0, NULL);
}

static void setup_ftw(void) {
  fail_if(system("mkdir -p /tmp/check_csync") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp", "/tmp") < 0, "Setup failed");
  fail_if(csync_set_config_dir(csync, "/tmp/check_csync") < 0, "Setup failed");
  fail_if(csync_init(csync) < 0, NULL);
}

static void teardown(void) {
  fail_if(csync_destroy(csync) < 0, "Teardown failed");
  unsetenv("CSYNC_NOMEMORY");
}

static void teardown_rm(void) {
  teardown();
  fail_if(system("rm -rf /tmp/check_csync") < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync2") < 0, "Teardown failed");
}

static void stat_init(csync_vio_file_stat_t *fs) {
  time_t t;

  fail_if(fs == NULL, NULL);

  fs->name = c_strdup("file.txt");
  fs->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE;

  fs->type = CSYNC_VIO_FILE_TYPE_REGULAR;
  fs->mode = 0777;
  fs->inode = 619070;

  fs->device = 0;
  fs->size = 157459;
  fs->nlink = 1;

  fs->uid = 1000;
  fs->gid = 1000;

  fs->blksize = 312;
  fs->blkcount = 4096;

  fs->atime = fs->mtime = fs->ctime = time(&t);
}

static int failing_fn(CSYNC *ctx,
    const char *file,
    const csync_vio_file_stat_t *fs,
    enum csync_ftw_flags_e flag) {
  (void) ctx;
  (void) file;
  (void) fs;
  (void) flag;

  return -1;
}

START_TEST (check_csync_detect_update)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->mtime = 1217597845;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NEW, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_none)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->mtime = 1217597845;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NONE, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_eval)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_EVAL, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_rename)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();

  free(fs->name);
  fs->name = c_strdup("wurst.txt");
  stat_init(fs);

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/wurst.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to rename */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_RENAME, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_new)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->inode = 42;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NEW, "instruction is %s", csync_instruction_str(st->instruction));<commit_msg>Fix a memory leak in the rename test.<commit_after>#include "support.h"

#include "csync_update.c"

CSYNC *csync;

static void setup(void) {
  fail_if(system("mkdir -p /tmp/check_csync") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp/check_csync1", "/tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_set_config_dir(csync, "/tmp/check_csync") < 0, "Setup failed");
  fail_if(csync_init(csync) < 0, NULL);
}

static void setup_ftw(void) {
  fail_if(system("mkdir -p /tmp/check_csync") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync1") < 0, "Setup failed");
  fail_if(system("mkdir -p /tmp/check_csync2") < 0, "Setup failed");
  fail_if(csync_create(&csync, "/tmp", "/tmp") < 0, "Setup failed");
  fail_if(csync_set_config_dir(csync, "/tmp/check_csync") < 0, "Setup failed");
  fail_if(csync_init(csync) < 0, NULL);
}

static void teardown(void) {
  fail_if(csync_destroy(csync) < 0, "Teardown failed");
  unsetenv("CSYNC_NOMEMORY");
}

static void teardown_rm(void) {
  teardown();
  fail_if(system("rm -rf /tmp/check_csync") < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync1") < 0, "Teardown failed");
  fail_if(system("rm -rf /tmp/check_csync2") < 0, "Teardown failed");
}

static void stat_init(csync_vio_file_stat_t *fs) {
  time_t t;

  fail_if(fs == NULL, NULL);

  fs->name = c_strdup("file.txt");
  fs->fields = CSYNC_VIO_FILE_STAT_FIELDS_NONE;

  fs->type = CSYNC_VIO_FILE_TYPE_REGULAR;
  fs->mode = 0777;
  fs->inode = 619070;

  fs->device = 0;
  fs->size = 157459;
  fs->nlink = 1;

  fs->uid = 1000;
  fs->gid = 1000;

  fs->blksize = 312;
  fs->blkcount = 4096;

  fs->atime = fs->mtime = fs->ctime = time(&t);
}

static int failing_fn(CSYNC *ctx,
    const char *file,
    const csync_vio_file_stat_t *fs,
    enum csync_ftw_flags_e flag) {
  (void) ctx;
  (void) file;
  (void) fs;
  (void) flag;

  return -1;
}

START_TEST (check_csync_detect_update)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->mtime = 1217597845;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NEW, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_none)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->mtime = 1217597845;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NONE, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_eval)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_EVAL, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_rename)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();

  stat_init(fs);
  free(fs->name);
  fs->name = c_strdup("wurst.txt");

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/wurst.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to rename */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_RENAME, "instruction is %s", csync_instruction_str(st->instruction));

  /* set the instruction to UPDATED that it gets written to the statedb */
  st->instruction = CSYNC_INSTRUCTION_UPDATED;

  /* create a statedb */
  csync_set_status(csync, 0xFFFF);

  csync_vio_file_stat_destroy(fs);
}
END_TEST

START_TEST (check_csync_detect_update_db_new)
{
  csync_file_stat_t *st = NULL;
  csync_vio_file_stat_t *fs = NULL;

  fs = csync_vio_file_stat_new();
  stat_init(fs);
  fs->inode = 42;

  fail_unless(_csync_detect_update(csync, "/tmp/check_csync1/file.txt", fs, CSYNC_FTW_TYPE_FILE) == 0, NULL);

  /* the instruction should be set to new  */
  st = c_rbtree_node_data(csync->local.tree->root);
  fail_unless(st->instruction == CSYNC_INSTRUCTION_NEW, "instruction is %s", csync_instruction_str(st->instruction));
{'fraction_non_alphanumeric': 0.1388538475400973, 'fraction_numerical': 0.016939989187240944, 'mean_word_length': 4.856992084432718, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4109804', 'n_tokens_mistral': 4691, 'n_tokens_neox': 4441, 'n_words': 844}