/*
  Copyright 2011-2021 David Robillard <d@drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#undef NDEBUG

#include "serd/serd.h"

#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

static void
test_write_bad_event(void)
{
  SerdWorld*    world     = serd_world_new();
  SerdEnv*      env       = serd_env_new(SERD_EMPTY_STRING());
  SerdBuffer    buffer    = {NULL, 0};
  SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);
  assert(writer);

  const SerdEvent event = {(SerdEventType)42};
  assert(serd_sink_write_event(serd_writer_sink(writer), &event) ==
         SERD_ERR_BAD_ARG);

  char* const out = serd_buffer_sink_finish(&buffer);

  assert(!strcmp(out, ""));
  serd_free(out);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);
  serd_world_free(world);
}

static void
test_write_bad_prefix(void)
{
  SerdWorld*    world     = serd_world_new();
  SerdNodes*    nodes     = serd_world_nodes(world);
  SerdEnv*      env       = serd_env_new(SERD_EMPTY_STRING());
  SerdBuffer    buffer    = {NULL, 0};
  SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);
  assert(writer);

  const SerdNode* name = serd_nodes_string(nodes, SERD_STRING("eg"));
  const SerdNode* uri  = serd_nodes_uri(nodes, SERD_STRING("rel"));

  assert(serd_sink_write_prefix(serd_writer_sink(writer), name, uri) ==
         SERD_ERR_BAD_ARG);

  char* const out = serd_buffer_sink_finish(&buffer);

  assert(!strcmp(out, ""));
  serd_free(out);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);
  serd_world_free(world);
}

static void
test_write_long_literal(void)
{
  SerdWorld*    world     = serd_world_new();
  SerdNodes*    nodes     = serd_world_nodes(world);
  SerdEnv*      env       = serd_env_new(SERD_EMPTY_STRING());
  SerdBuffer    buffer    = {NULL, 0};
  SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);
  assert(writer);

  const SerdNode* s =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));
  const SerdNode* p =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/p"));
  const SerdNode* o =
    serd_nodes_string(nodes, SERD_STRING("hello \"\"\"world\"\"\"!"));

  assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL));

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);

  char* out = serd_buffer_sink_finish(&buffer);

  static const char* const expected =
    "<http://example.org/s>\n"
    "\t<http://example.org/p> \"\"\"hello \"\"\\\"world\"\"\\\"!\"\"\" .\n";

  assert(!strcmp((char*)out, expected));
  serd_free(out);

  serd_world_free(world);
}

static size_t
null_sink(const void* const buf,
          const size_t      size,
          const size_t      nmemb,
          void* const       stream)
{
  (void)buf;
  (void)stream;

  return size * nmemb;
}

static void
test_writer_stack_overflow(void)
{
  SerdWorld*    world     = serd_world_new();
  SerdNodes*    nodes     = serd_world_nodes(world);
  SerdEnv*      env       = serd_env_new(SERD_EMPTY_STRING());
  SerdByteSink* byte_sink = serd_byte_sink_new_function(null_sink, NULL, 1u);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);

  const SerdSink* sink = serd_writer_sink(writer);

  const SerdNode* const s =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));

  const SerdNode* const p =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/p"));

  const SerdNode* o = serd_nodes_blank(nodes, SERD_STRING("blank"));

  SerdStatus st = serd_sink_write(sink, SERD_ANON_O, s, p, o, NULL);
  assert(!st);

  // Repeatedly write nested anonymous objects until the writer stack overflows
  for (unsigned i = 0u; i < 512u; ++i) {
    char buf[1024];
    snprintf(buf, sizeof(buf), "b%u", i);

    const SerdNode* next_o = serd_nodes_blank(nodes, SERD_STRING(buf));

    st = serd_sink_write(sink, SERD_ANON_O, o, p, next_o, NULL);

    o = next_o;

    if (st) {
      assert(st == SERD_ERR_OVERFLOW);
      break;
    }
  }

  assert(st == SERD_ERR_OVERFLOW);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);
  serd_world_free(world);
}

static void
test_strict_write(void)
{
  SerdWorld*  world = serd_world_new();
  SerdNodes*  nodes = serd_world_nodes(world);
  const char* path  = "serd_strict_write_test.ttl";
  FILE*       fd    = fopen(path, "wb");
  assert(fd);

  SerdEnv* env = serd_env_new(SERD_EMPTY_STRING());

  SerdByteSink* byte_sink =
    serd_byte_sink_new_function((SerdWriteFunc)fwrite, fd, 1);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0, env, byte_sink);
  assert(writer);

  const SerdSink*      sink      = serd_writer_sink(writer);
  const uint8_t        bad_str[] = {0xFF, 0x90, 'h', 'i', 0};
  const SerdStringView bad_view  = {(const char*)bad_str, 4};

  const SerdNode* s =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));

  const SerdNode* p =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));

  const SerdNode* bad_lit = serd_nodes_string(nodes, bad_view);
  const SerdNode* bad_uri = serd_nodes_uri(nodes, bad_view);

  assert(serd_sink_write(sink, 0, s, p, bad_lit, 0) == SERD_ERR_BAD_TEXT);
  assert(serd_sink_write(sink, 0, s, p, bad_uri, 0) == SERD_ERR_BAD_TEXT);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);
  fclose(fd);
  serd_world_free(world);
}

static size_t
faulty_sink(const void* const buf,
            const size_t      size,
            const size_t      nmemb,
            void* const       stream)
{
  (void)buf;
  (void)size;
  (void)nmemb;

  if (nmemb > 1) {
    errno = stream ? ERANGE : 0;
    return 0u;
  }

  return size * nmemb;
}

static void
test_write_error(void)
{
  SerdWorld* world = serd_world_new();
  SerdNodes* nodes = serd_world_nodes(world);
  SerdEnv*   env   = serd_env_new(SERD_EMPTY_STRING());

  const SerdNode* s =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));

  const SerdNode* p =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/p"));

  const SerdNode* o =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/o"));

  // Test with setting errno

  SerdByteSink* byte_sink = serd_byte_sink_new_function(faulty_sink, NULL, 1);

  SerdWriter* writer = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);
  assert(writer);

  SerdStatus st = serd_sink_write(serd_writer_sink(writer), 0u, s, p, o, NULL);
  assert(st == SERD_ERR_BAD_WRITE);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);

  // Test without setting errno
  byte_sink = serd_byte_sink_new_function(faulty_sink, world, 1);
  writer    = serd_writer_new(world, SERD_TURTLE, 0u, env, byte_sink);

  assert(writer);

  assert(serd_sink_write(serd_writer_sink(writer), 0u, s, p, o, NULL) ==
         SERD_ERR_BAD_WRITE);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);

  serd_env_free(env);
  serd_world_free(world);
}

static void
test_write_empty_syntax(void)
{
  SerdWorld* world = serd_world_new();
  SerdNodes* nodes = serd_world_nodes(world);
  SerdEnv*   env   = serd_env_new(SERD_EMPTY_STRING());

  const SerdNode* s =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/s"));

  const SerdNode* p =
    serd_nodes_uri(nodes, SERD_STRING("http://example.org/p"));

  const SerdNode* o = serd_nodes_curie(nodes, SERD_STRING("eg:o"));

  SerdBuffer    buffer    = {NULL, 0};
  SerdByteSink* byte_sink = serd_byte_sink_new_buffer(&buffer);

  SerdWriter* writer =
    serd_writer_new(world, SERD_SYNTAX_EMPTY, 0u, env, byte_sink);

  assert(writer);

  assert(!serd_sink_write(serd_writer_sink(writer), 0u, s, p, o, NULL));

  char* out = serd_buffer_sink_finish(&buffer);

  assert(strlen(out) == 0);
  serd_free(out);

  serd_writer_free(writer);
  serd_byte_sink_free(byte_sink);
  serd_env_free(env);
  serd_world_free(world);
}

int
main(void)
{
  test_write_bad_event();
  test_write_bad_prefix();
  test_write_long_literal();
  test_writer_stack_overflow();
  test_strict_write();
  test_write_error();
  test_write_empty_syntax();

  return 0;
}