Hello!

I have made some characterizing tests of copy_nword in order to change
it and what I have found is the following:

      * The return value is NULL on error OR on last argument so it is
        impossible to distinguish those cases. 
      * Quoting is handled somewhat confusingly - an input of "\"The
        \"red" will give two tokens, "The" and "red" but an input of
        "The\"red\"" will give just one token, "The\"red\"", of course a
        less evil input of "\"The\" red" will give two tokens, "The" and
        "red" so it seems as if start of string is checked only on
        string start. 
      * Unterminated quotes are not reported, the leading quote is just
        stripped away. 
      * Overlong tokens are silently truncated. 
      * A trailing escape character is silently ignored.

I think all of the above points a little peculiar - are they all
intentional?
Should all of this be added to the documentation of copy_nword?

/MF

(Attached is my characterizing test program. I would like to commit them
but I am not sure where to put them, neither the files nor in the make
hierarchy. Should they be in testing? Should they run from make test? If
not, what should they run from? make check?)
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/config_api.h>

#include <stddef.h>
#include <stdio.h>
#include <string.h>

int failed = 0;

#define ASSERT(name, cond, desc)				\
  do {								\
    if ((cond)) ; else {					\
      failed = 1;						\
      printf("%s:%d - " #cond " failed\n", (name), __LINE__);	\
      (void)printf desc ;					\
    }								\
  } while(0)

static void t1(void)
{
  char input[] = "\"The red rose\"";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Quoted", strcmp(output, "The red rose") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Quoted", run == NULL,);
}

static void t2(void)
{
  char input[] = "\\\"The red rose\\\"";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Unquoted", strcmp(output, "\"The") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Unquoted", run == input + 6,("  run = input + %d\n", run - input));
  run = copy_nword(run, output, sizeof(output));
  ASSERT("Unquoted", strcmp(output, "red") == 0, (" output = >%s<\n", output));
  ASSERT("Unquoted", run == input + 10,("  run = input + %d\n", run - input));
  run = copy_nword(run, output, sizeof(output));
  ASSERT("Unquoted", strcmp(output, "rose\"") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Unquoted", run == NULL,);
}

static void t3(void)
{
  char input[] = "\"The";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Unterminated \\\"", strcmp(output, "The") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Unterminated \\\"", run == NULL,);
}

static void t4(void)
{
  char input[] = "\'The";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Unterminated \\'", strcmp(output, "The") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Unterminated \\'", run == NULL,);
}

static void t5(void)
{
  char output[10] = "";
  char* run = NULL;
  run = copy_nword(run, output, sizeof(output));
  ASSERT("Extract from NULL", run == NULL,);
}

static void t6(void)
{
  char input[] = "The red rose";
  char* output = NULL;
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Extract to NULL", run == NULL,);
}

static void t7(void)
{
  char input[] = "\"Very long token that overflows the buffer\" foo";
  char output[10] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Long token", strcmp(output, "Very long") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Long token", run == input + 44,
	 ("  run = input + %d\n", run - input));
}

static void t8(void)
{
  char input[] = "The\\\0red rose";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Quoted end of string", strcmp(output, "The\\") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Quoted end of string", run == NULL,);
}

static void t9(void)
{
  char input[] = "";
  char output[sizeof(input) + 1] = "X";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Empty string", strcmp(output, "") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Empty string", run == NULL,);
}

static void t10(void)
{
  char input[] = "    \t   ";
  char output[sizeof(input)] = "X";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Whitespace string", strcmp(output, "") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Whitespace string", run == NULL,);
}

static void t11(void)
{
  char input[] = "\"The\"red rose";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Quote, no whitespace after", strcmp(output, "The") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Quote, no whitespace after", run == input + 5,
	 ("  run = input + %d\n", run - input));
}

static void t12(void)
{
  char input[] = "The\"red\" rose";
  char output[sizeof(input)] = "";
  char* run = copy_nword(input, output, sizeof(output));
  ASSERT("Quote, no whitespace before", strcmp(output, "The\"red\"") == 0,
	 ("  output = >%s<\n", output));
  ASSERT("Quote, no whitespace before", run == input + 9,
	 ("  run = input + %d\n", run - input));
}

int main(void)
{
  t1();
  t2();
  t3();
  t4();
  t5();
  t6();
  t7();
  t8();
  t9();
  t10();
  t11();
  t12();
  return (failed != 0);
}
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Net-snmp-coders mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to