/******************************************************************************
*
* Copyright (C) 2004-2017  Brian Berlin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/interface/util.h"

static int failure = 0;

static void check_result ( int condition, const char *name )
{
    printf ( "%s: %s\n", condition ? "PASS" : "FAIL", name );
    if ( !condition )
        failure = 1;
}

static void check_uri ( const char *path, const char *expected,
        const char *name )
{
    char *uri = util_create_file_uri_alloc ( path );

    check_result ( uri && !strcmp(uri, expected), name );
    if ( uri && strcmp(uri, expected) )
        printf ( "  got:      %s\n  expected: %s\n", uri, expected );
    free ( uri );
}

int main ( void )
{
    check_uri ( "/tmp/kat5200-user-guide.pdf",
            "file:///tmp/kat5200-user-guide.pdf",
            "plain POSIX path" );

    check_uri ( "/tmp/a b#c%?&+;.pdf",
            "file:///tmp/a%20b%23c%25%3F%26%2B%3B.pdf",
            "reserved characters are encoded" );

    check_uri ( "/tmp/J" "\xC3" "\xB6" "rg/manual.pdf",
            "file:///tmp/J%C3%B6rg/manual.pdf",
            "UTF-8 bytes are encoded" );

    check_uri ( "C:\\Users\\Test User\\manual.pdf",
            "file:///C:/Users/Test%20User/manual.pdf",
            "Windows drive path" );

    check_uri ( "\\\\server\\share\\kat5200 manual.pdf",
            "file://server/share/kat5200%20manual.pdf",
            "Windows UNC path" );

    check_uri ( "//server/share/kat5200 manual.pdf",
            "file://server/share/kat5200%20manual.pdf",
            "slash UNC path" );

    check_result ( util_create_file_uri_alloc("relative/manual.pdf") == NULL,
            "relative path rejected" );
    check_result ( util_create_file_uri_alloc("") == NULL,
            "empty path rejected" );
    check_result ( util_create_file_uri_alloc(NULL) == NULL,
            "NULL path rejected" );

    return failure ? 1 : 0;
}
