Blame src/microhttpd/test_str_token.c

Packit 875988
/*
Packit 875988
  This file is part of libmicrohttpd
Packit 875988
  Copyright (C) 2017 Karlson2k (Evgeny Grin)
Packit 875988
Packit 875988
  This test tool is free software; you can redistribute it and/or
Packit 875988
  modify it under the terms of the GNU General Public License as
Packit 875988
  published by the Free Software Foundation; either version 2, or
Packit 875988
  (at your option) any later version.
Packit 875988
Packit 875988
  This test tool is distributed in the hope that it will be useful,
Packit 875988
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 875988
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 875988
  Lesser General Public License for more details.
Packit 875988
Packit 875988
  You should have received a copy of the GNU Lesser General Public
Packit 875988
  License along with this library; if not, write to the Free Software
Packit 875988
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 875988
*/
Packit 875988
Packit 875988
/**
Packit 875988
 * @file microhttpd/test_str_token.c
Packit 875988
 * @brief  Unit tests for some mhd_str functions
Packit 875988
 * @author Karlson2k (Evgeny Grin)
Packit 875988
 */
Packit 875988
Packit 875988
#include "mhd_options.h"
Packit 875988
#include <stdio.h>
Packit 875988
#include "mhd_str.h"
Packit 875988
Packit 875988
Packit 875988
static int
Packit 875988
expect_found_n(const char *str, const char *token, size_t token_len)
Packit 875988
{
Packit 875988
  if (!MHD_str_has_token_caseless_(str, token, token_len))
Packit 875988
    {
Packit 875988
      fprintf(stderr, "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return false\n",
Packit 875988
              str, token, (unsigned long) token_len);
Packit 875988
      return 1;
Packit 875988
    }
Packit 875988
  return 0;
Packit 875988
}
Packit 875988
Packit 875988
#define expect_found(s,t) expect_found_n((s),(t),MHD_STATICSTR_LEN_(t))
Packit 875988
Packit 875988
static int
Packit 875988
expect_not_found_n(const char *str, const char *token, size_t token_len)
Packit 875988
{
Packit 875988
  if (MHD_str_has_token_caseless_(str, token, token_len))
Packit 875988
    {
Packit 875988
      fprintf(stderr, "MHD_str_has_token_caseless_() FAILED:\n\tMHD_str_has_token_caseless_(%s, %s, %lu) return true\n",
Packit 875988
              str, token, (unsigned long) token_len);
Packit 875988
      return 1;
Packit 875988
    }
Packit 875988
  return 0;
Packit 875988
}
Packit 875988
Packit 875988
#define expect_not_found(s,t) expect_not_found_n((s),(t),MHD_STATICSTR_LEN_(t))
Packit 875988
Packit 875988
int check_match(void)
Packit 875988
{
Packit 875988
  int errcount = 0;
Packit 875988
  errcount += expect_found("string", "string");
Packit 875988
  errcount += expect_found("String", "string");
Packit 875988
  errcount += expect_found("string", "String");
Packit 875988
  errcount += expect_found("strinG", "String");
Packit 875988
  errcount += expect_found("\t strinG", "String");
Packit 875988
  errcount += expect_found("strinG\t ", "String");
Packit 875988
  errcount += expect_found(" \t tOkEn  ", "toKEN");
Packit 875988
  errcount += expect_found("not token\t,  tOkEn  ", "toKEN");
Packit 875988
  errcount += expect_found("not token,\t  tOkEn, more token", "toKEN");
Packit 875988
  errcount += expect_found("not token,\t  tOkEn\t, more token", "toKEN");
Packit 875988
  errcount += expect_found(",,,,,,test,,,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,\t,test,,,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,,test, ,,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test,,,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test not,test,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test not,,test,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test not ,test,,", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test", "TESt");
Packit 875988
  errcount += expect_found(",,,,,, test      ", "TESt");
Packit 875988
  errcount += expect_found("no test,,,,,, test      ", "TESt");
Packit 875988
  return errcount;
Packit 875988
}
Packit 875988
Packit 875988
int check_not_match(void)
Packit 875988
{
Packit 875988
  int errcount = 0;
Packit 875988
  errcount += expect_not_found("strin", "string");
Packit 875988
  errcount += expect_not_found("Stringer", "string");
Packit 875988
  errcount += expect_not_found("sstring", "String");
Packit 875988
  errcount += expect_not_found("string", "Strin");
Packit 875988
  errcount += expect_not_found("\t( strinG", "String");
Packit 875988
  errcount += expect_not_found(")strinG\t ", "String");
Packit 875988
  errcount += expect_not_found(" \t tOkEn t ", "toKEN");
Packit 875988
  errcount += expect_not_found("not token\t,  tOkEner  ", "toKEN");
Packit 875988
  errcount += expect_not_found("not token,\t  tOkEns, more token", "toKEN");
Packit 875988
  errcount += expect_not_found("not token,\t  tOkEns\t, more token", "toKEN");
Packit 875988
  errcount += expect_not_found(",,,,,,testing,,,,", "TESt");
Packit 875988
  errcount += expect_not_found(",,,,,\t,test,,,,", "TESting");
Packit 875988
  errcount += expect_not_found("tests,,,,,,quest, ,,,", "TESt");
Packit 875988
  errcount += expect_not_found(",,,,,, testы,,,,", "TESt");
Packit 875988
  errcount += expect_not_found(",,,,,, test not,хtest,,", "TESt");
Packit 875988
  errcount += expect_not_found("testing,,,,,, test not,,test2,,", "TESt");
Packit 875988
  errcount += expect_not_found(",testi,,,,, test not ,test,,", "TESting");
Packit 875988
  errcount += expect_not_found(",,,,,,2 test", "TESt");
Packit 875988
  errcount += expect_not_found(",,,,,,test test      ", "test");
Packit 875988
  errcount += expect_not_found("no test,,,,,, test      test", "test");
Packit 875988
  return errcount;
Packit 875988
}
Packit 875988
Packit 875988
int main(int argc, char * argv[])
Packit 875988
{
Packit 875988
  int errcount = 0;
Packit 875988
  (void)argc; (void)argv; /* Unused. Silent compiler warning. */
Packit 875988
  errcount += check_match();
Packit 875988
  errcount += check_not_match();
Packit 875988
  return errcount == 0 ? 0 : 1;
Packit 875988
}