File: cm-utils.c

package info (click to toggle)
chatty 0.8.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,180 kB
  • sloc: ansic: 57,069; sql: 5,122; xml: 158; cpp: 39; makefile: 26; sh: 11; lisp: 8; javascript: 6
file content (209 lines) | stat: -rw-r--r-- 5,135 bytes parent folder | download | duplicates (3)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* cm-utils.c
 *
 * Copyright 2022 Purism SPC
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#undef NDEBUG
#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS
#undef G_LOG_DOMAIN

#include "cm-utils-private.h"

static JsonObject *
get_json_object_for_file (const char *dir,
                          const char *file_name)
{
  g_autoptr(JsonParser) parser = NULL;
  g_autoptr(GError) error = NULL;
  g_autofree char *path = NULL;

  path = g_build_filename (dir, file_name, NULL);
  parser = json_parser_new ();
  json_parser_load_from_file (parser, path, &error);
  g_assert_no_error (error);

  return json_node_dup_object (json_parser_get_root (parser));
}

static void
test_utils_canonical (void)
{
  g_autoptr(GError) error = NULL;
  g_autoptr(GDir) dir = NULL;
  g_autofree char *path = NULL;
  const char *name;

  path = g_test_build_filename (G_TEST_DIST, "cm-utils", NULL);
  dir = g_dir_open (path, 0, &error);
  g_assert_no_error (error);

  while ((name = g_dir_read_name (dir)) != NULL) {
    g_autofree char *expected_file = NULL;
    g_autofree char *expected_name = NULL;
    g_autofree char *expected_json = NULL;
    g_autoptr(JsonObject) object = NULL;
    g_autoptr(GString) json_str = NULL;

    if (!g_str_has_suffix (name, ".json"))
      continue;

    expected_name = g_strconcat (name, ".expected", NULL);
    expected_file = g_build_filename (path, expected_name, NULL);
    g_file_get_contents (expected_file, &expected_json, NULL, &error);
    g_assert_no_error (error);

    object = get_json_object_for_file (path, name);
    json_str = cm_utils_json_get_canonical (object, NULL);
    g_assert_cmpstr (json_str->str, ==, expected_json);
  }
}

static void
test_utils_valid_user_name (void)
{
  struct Data
  {
    const char *user_name;
    gboolean valid;
  } data[] = {
     {NULL, FALSE},
     {"", FALSE},
     {"@:.", FALSE},
     {"@bob:", FALSE},
     {"@:example.org", FALSE},
     {"abc", FALSE},
     {"good@bad:com", FALSE},
     {"@a:example.org", TRUE},
     {"@alice:example.org", TRUE},
     {"@alice:example.org@alice:example.org", FALSE},
     {"@alice:sub.example.org", TRUE},
     {"@bob:localhost", TRUE},
  };

  for (guint i = 0; i < G_N_ELEMENTS (data); i++)
    {
      const char *user_name = data[i].user_name;
      gboolean valid = data[i].valid;

      if (valid)
        g_assert_true (cm_utils_user_name_valid (user_name));
      else
        g_assert_false (cm_utils_user_name_valid (user_name));
    }
}

static void
test_utils_valid_email (void)
{
  struct Data
  {
    const char *email;
    gboolean valid;
  } data[] = {
    {"", FALSE},
    {"@:.", FALSE},
    {"@bob:", FALSE},
    {"@:example.org", FALSE},
    {"abc", FALSE},
    {"good@bad:com", FALSE},
    {"@a:example.org", FALSE},
    {"@alice:example.org", FALSE},
    {"test@user.com", TRUE},
    {"test@user.comtest@user.com", FALSE},
    {"തറ@home.com", TRUE},
  };

  for (guint i = 0; i < G_N_ELEMENTS (data); i++)
    {
      const char *email = data[i].email;
      gboolean valid = data[i].valid;

      if (valid)
        g_assert_true (cm_utils_user_name_is_email (email));
      else
        g_assert_false (cm_utils_user_name_is_email (email));
    }
}

static void
test_utils_valid_phone (void)
{
  struct Data
  {
    const char *phone;
    gboolean valid;
  } data[] = {
     {"", FALSE},
     {"123", FALSE},
     {"+9123", FALSE},
     {"+91223344", FALSE},
     {"+91123456789", TRUE},
     {"+13123456789", TRUE},
     {"+13123456789002211443", FALSE},
  };

  for (guint i = 0; i < G_N_ELEMENTS (data); i++)
    {
      const char *phone = data[i].phone;
      gboolean valid = data[i].valid;

      if (valid)
        g_assert_true (cm_utils_mobile_is_valid (phone));
      else
        g_assert_false (cm_utils_mobile_is_valid (phone));
    }
}

static void
test_utils_valid_home_server (void)
{
  struct Data
  {
    const char *uri;
    gboolean valid;
  } data[] = {
    {"", FALSE},
    {"http://", FALSE},
    {"ftp://example.com", FALSE},
    {"http://example.com", TRUE},
    {"https://example.com", TRUE},
    {"http://example.com/", TRUE},
    {"http://example.com.", FALSE},
    {"http://localhost:8008", TRUE},
    {"http://localhost:8008/path", FALSE},
  };

  for (guint i = 0; i < G_N_ELEMENTS (data); i++)
    {
      const char *uri = data[i].uri;
      gboolean valid = data[i].valid;

      if (valid)
        g_assert_true (cm_utils_home_server_valid (uri));
      else
        g_assert_false (cm_utils_home_server_valid (uri));
    }
}

int
main (int   argc,
      char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/cm-utils/canonical", test_utils_canonical);
  g_test_add_func ("/cm-utils/valid-user-name", test_utils_valid_user_name);
  g_test_add_func ("/cm-utils/valid-email", test_utils_valid_email);
  g_test_add_func ("/cm-utils/valid-phone", test_utils_valid_phone);
  g_test_add_func ("/cm-utils/valid-home-server", test_utils_valid_home_server);

  return g_test_run ();
}