File: testencode.c

package info (click to toggle)
gconf 3.2.6-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,976 kB
  • ctags: 3,656
  • sloc: ansic: 39,702; sh: 11,189; python: 1,073; makefile: 448; xml: 332
file content (182 lines) | stat: -rw-r--r-- 4,269 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* GConf
 * Copyright (C) 1999, 2000 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <gconf/gconf.h>
#include <gconf/gconf-internals.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <locale.h>

static void
check(gboolean condition, const gchar* fmt, ...)
{
  va_list args;
  gchar* description;
  
  va_start (args, fmt);
  description = g_strdup_vprintf(fmt, args);
  va_end (args);
  
  if (condition)
    {
      printf(".");
      fflush(stdout);
    }
  else
    {
      fprintf(stderr, "\n*** FAILED: %s\n", description);
      exit(1);
    }
  
  g_free(description);
}

static const gchar*
keys[] = {
  "/testing/foo/tar",
  "/testing/foo/bar",
  "/testing/quad",
  "/testing/blah",
  "/testing/q/a/b/c/z/w/x/y/z",
  "/testing/foo/baz",
  "/testing/oops/bloo",
  "/testing/oops/snoo",
  "/testing/oops/kwoo",
  "/testing/foo/quaz",
  NULL
};

static const gchar*
quote_success_tests[] = {
  "sd;kjapoirgupoi4ut349u3940 u09ujt l;k234jtop ub0[t9\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" \"\"\" \"\" \\\\\\d \\sdf \\d \\\\\f \\\\\\f\\\\g a|\"\\|'|'|'|\"|\"|\"|\"|\"|\"|\"|\"|\"|\"|'|'|\"|\"|\"|\"|\"|\"\\""\\\"|\\||||||||\\\\\\\\\\||\"|\"|\"|\"|\"\"\"\"\"\"\"\"\"\\||||||||||||\"\"\"\"\"\"|\"|\"\\\"|\"|\"|\\\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"||\"\"\"\"\"\"\"\"|||\\\\\\\\\\\\\\\\\\||\\\\\"\"\"||\\\\|\"|\"",
  "\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"",
  "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",
  "''''''''''''''''''''''''''''''''''",
  ",,,,,,,,,,,,,,,,,,,,,,,,,",
  ",\"\\dfkljs;kldjg",
  "",
  " ",
  "\"",
  "\\",
  "\\\\",
  "\"\"",
  NULL
};

static const gchar*
unquote_shouldfail_tests[] = {
  "blah blah blah blah \"", /* no start quote */
  "\"blah blah blah blah", /* no end quote */
  NULL
};

static gint ints[] = { -1, -2, -3, 0, 1, 2, 3, 4000, 0xfffff, -0xfffff, G_MININT, G_MAXINT, 0, 0, 57, 83, 95 };
static const guint n_ints = sizeof(ints)/sizeof(ints[0]);

static int
null_safe_strcmp(const char* lhs, const char* rhs)
{
  if (lhs == NULL && rhs == NULL)
    return 0;
  else if (lhs == NULL)
    return 1;
  else if (rhs == NULL)
    return -1;
  else
    return strcmp(lhs, rhs);
}

static void
print_parallel(const gchar* r, const gchar* l)
{
  while (*r && *l)
    {
      if (*r != *l)
        printf("[%c %c]", *r, *l);
      else
        printf("(%c %c)", *r, *l);

      ++r;
      ++l;
    }

  if (*r)
    printf("[... %s]", r);

  if (*l)
    printf("[... %s]", l);
}

static void
check_quoting(void)
{
  const gchar** testp;

  testp = quote_success_tests;

  while (*testp)
    {
      gchar* quoted;
      gchar* unquoted;
      const gchar* end;
      GError* error = NULL;
      
      quoted = gconf_quote_string(*testp);

      unquoted = gconf_unquote_string(quoted, &end, &error);

      /*       print_parallel(*testp, unquoted); */

      check (error == NULL,
             "error parsing quoted string: %s",
             error ? error->message : "<NULL>");
      
      check (strcmp(*testp, unquoted) == 0,
             "Original: `%s'\nUnquoted: `%s'\nNot equal?",
             *testp, unquoted);

      check (*end == '\0',
             "Entire string not unquoted?\nOriginal: `%s'\nQuoted: `%s'\nUnquoted: `%s'\nLeftover: `%s\n",
             *testp, quoted, unquoted, end);
      
      g_free(quoted);
      g_free(unquoted);

      ++testp;
    }

}

int 
main (int argc, char** argv)
{
  GConfEngine* conf;
  GError* err = NULL;
  
  printf("\nChecking string quoting:");
  
  check_quoting();

  printf("\n\n");
  
  return 0;
}