<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2014 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "config.h"

#include <mutex>
#include <condition_variable>
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits.h>
#include <cJSON.h>
#include <platform/dirutils.h>
#include <stddef.h>

#include "memcached/extension.h"
#include "memcached/extension_loggers.h"
#include "memcached/audit_interface.h"
#include "auditd_audit_events.h"

#ifdef WIN32
#include <direct.h>
#define sleep(a) Sleep(a * 1000)
#define mkdir(a, b) _mkdir(a)
#endif

class Configuration {
public:
    Configuration(const std::string &fname, const std::string &descr)
            : filename(fname),
              descriptor_path(descr),
              rotationSize(200),
              rotationInterval(10),
              logPath("test"),
              enabled(true)
    {
        writeFile();
    }

    void setRotationSize(size_t rotationSize) {
        Configuration::rotationSize = rotationSize;
        writeFile();
    }

    void setRotationInterval(size_t rotationInterval) {
        Configuration::rotationInterval = rotationInterval;
        writeFile();
    }

    void setLogPath(std::string &logPath) {
        Configuration::logPath = logPath;

        if (CouchbaseDirectoryUtilities::isDirectory(logPath)) {
            if (!CouchbaseDirectoryUtilities::rmrf(logPath)) {
                std::cerr << "Failed to remove test directory" << std::endl;
                exit(EXIT_FAILURE);
            }
        }

        if (!CouchbaseDirectoryUtilities::mkdirp(logPath)) {
            std::cerr << "error, unable to create directory" << std::endl;
            exit(EXIT_FAILURE);
        }
        writeFile();
    }

    void setEnabled(bool enabled) {
        Configuration::enabled = enabled;
        writeFile();
    }


    const std::string &getFilename() const {
        return filename;
    }

private:
    void writeFile(void) {
        cJSON *config_json = cJSON_CreateObject();
        cJSON_AddNumberToObject(config_json, "version", 1);
        if (enabled) {
            cJSON_AddTrueToObject(config_json, "auditd_enabled");
        } else {
            cJSON_AddFalseToObject(config_json, "auditd_enabled");
        }

        cJSON_AddNumberToObject(config_json, "rotate_size", (double)rotationSize);
        cJSON_AddNumberToObject(config_json, "rotate_interval", (double)rotationInterval);
        cJSON_AddStringToObject(config_json, "log_path", logPath.c_str());
        cJSON_AddStringToObject(config_json, "descriptors_path", descriptor_path.c_str());
        cJSON *disabled_arr = cJSON_CreateArray();
        cJSON_AddItemToObject(config_json, "disabled", disabled_arr);
        cJSON *sync_arr = cJSON_CreateArray();
        cJSON_AddItemToObject(config_json, "sync", sync_arr);
        char *data = cJSON_Print(config_json);
        FILE *fp = fopen(filename.c_str(), "wb");
        fprintf(fp, "%s\n", data);
        fclose(fp);
        cJSON_Free(data);
        cJSON_Delete(config_json);
    }

    std::string filename;
    std::string descriptor_path;
    size_t rotationSize;
    size_t rotationInterval;
    std::string logPath;
    bool enabled;
};

static std::mutex mutex;
static std::condition_variable cond;
static bool ready = false;

extern "C" {
static void notify_io_complete(const void *cookie, ENGINE_ERROR_CODE status) {
    std::lock_guard<std::mutex> lock(mutex);
    ready = true;
    cond.notify_one();
}
}

size_t expectedEventProcessed;
size_t auditEventProcessed;

void audit_processed_listener(void) {
    std::lock_guard<std::mutex> lock(mutex);
    cond.notify_one();
    auditEventProcessed++;
}

static void waitForProcessingEvents(void) {
    // we have to wait
    std::unique_lock<std::mutex> lock(mutex);<commit_msg>Fix ThreadSanziter warnings in auditd_tests<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2014 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "config.h"

#include <atomic>
#include <mutex>
#include <condition_variable>
#include <iostream>
#include <fstream>
#include <sstream>
#include <limits.h>
#include <cJSON.h>
#include <platform/dirutils.h>
#include <stddef.h>

#include "memcached/extension.h"
#include "memcached/extension_loggers.h"
#include "memcached/audit_interface.h"
#include "auditd_audit_events.h"

#ifdef WIN32
#include <direct.h>
#define sleep(a) Sleep(a * 1000)
#define mkdir(a, b) _mkdir(a)
#endif

class Configuration {
public:
    Configuration(const std::string &fname, const std::string &descr)
            : filename(fname),
              descriptor_path(descr),
              rotationSize(200),
              rotationInterval(10),
              logPath("test"),
              enabled(true)
    {
        writeFile();
    }

    void setRotationSize(size_t rotationSize) {
        Configuration::rotationSize = rotationSize;
        writeFile();
    }

    void setRotationInterval(size_t rotationInterval) {
        Configuration::rotationInterval = rotationInterval;
        writeFile();
    }

    void setLogPath(std::string &logPath) {
        Configuration::logPath = logPath;

        if (CouchbaseDirectoryUtilities::isDirectory(logPath)) {
            if (!CouchbaseDirectoryUtilities::rmrf(logPath)) {
                std::cerr << "Failed to remove test directory" << std::endl;
                exit(EXIT_FAILURE);
            }
        }

        if (!CouchbaseDirectoryUtilities::mkdirp(logPath)) {
            std::cerr << "error, unable to create directory" << std::endl;
            exit(EXIT_FAILURE);
        }
        writeFile();
    }

    void setEnabled(bool enabled) {
        Configuration::enabled = enabled;
        writeFile();
    }


    const std::string &getFilename() const {
        return filename;
    }

private:
    void writeFile(void) {
        cJSON *config_json = cJSON_CreateObject();
        cJSON_AddNumberToObject(config_json, "version", 1);
        if (enabled) {
            cJSON_AddTrueToObject(config_json, "auditd_enabled");
        } else {
            cJSON_AddFalseToObject(config_json, "auditd_enabled");
        }

        cJSON_AddNumberToObject(config_json, "rotate_size", (double)rotationSize);
        cJSON_AddNumberToObject(config_json, "rotate_interval", (double)rotationInterval);
        cJSON_AddStringToObject(config_json, "log_path", logPath.c_str());
        cJSON_AddStringToObject(config_json, "descriptors_path", descriptor_path.c_str());
        cJSON *disabled_arr = cJSON_CreateArray();
        cJSON_AddItemToObject(config_json, "disabled", disabled_arr);
        cJSON *sync_arr = cJSON_CreateArray();
        cJSON_AddItemToObject(config_json, "sync", sync_arr);
        char *data = cJSON_Print(config_json);
        FILE *fp = fopen(filename.c_str(), "wb");
        fprintf(fp, "%s\n", data);
        fclose(fp);
        cJSON_Free(data);
        cJSON_Delete(config_json);
    }

    std::string filename;
    std::string descriptor_path;
    size_t rotationSize;
    size_t rotationInterval;
    std::string logPath;
    bool enabled;
};

static std::mutex mutex;
static std::condition_variable cond;
static bool ready = false;

extern "C" {
static void notify_io_complete(const void *cookie, ENGINE_ERROR_CODE status) {
    std::lock_guard<std::mutex> lock(mutex);
    ready = true;
    cond.notify_one();
}
}

std::atomic<size_t> expectedEventProcessed;
std::atomic<size_t> auditEventProcessed;

void audit_processed_listener(void) {
    std::lock_guard<std::mutex> lock(mutex);
    cond.notify_one();
    auditEventProcessed++;
}

static void waitForProcessingEvents(void) {
    // we have to wait
    std::unique_lock<std::mutex> lock(mutex);
{'fraction_non_alphanumeric': 0.11450123845980635, 'fraction_numerical': 0.004953839225399685, 'mean_word_length': 3.215946843853821, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4480866', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2660, 'n_words': 621}