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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*
* Copyright © 2017 Endless Mobile, Inc.
*
* SPDX-License-Identifier: LGPL-2.0+
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors:
* - Philip Withnall <withnall@endlessm.com>
*/
#include "config.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <locale.h>
#include <string.h>
#include "ostree-autocleanups.h"
#include "ostree-repo-finder.h"
#include "ostree-repo-finder-avahi.h"
#include "ostree-repo-finder-avahi-private.h"
/* FIXME: Upstream this */
G_DEFINE_AUTOPTR_CLEANUP_FUNC (AvahiStringList, avahi_string_list_free)
/* Test the object constructor works at a basic level. */
static void
test_repo_finder_avahi_init (void)
{
g_autoptr(OstreeRepoFinderAvahi) finder = NULL;
g_autoptr(GMainContext) context = NULL;
/* Default main context. */
finder = ostree_repo_finder_avahi_new (NULL);
g_clear_object (&finder);
/* Explicit main context. */
context = g_main_context_new ();
finder = ostree_repo_finder_avahi_new (context);
g_clear_object (&finder);
}
/* Test parsing valid and invalid TXT records. */
static void
test_repo_finder_avahi_txt_records_parse (void)
{
struct
{
const guint8 *txt;
gsize txt_len;
const gchar *expected_key; /* (nullable) to indicate parse failure */
const guint8 *expected_value; /* (nullable) to allow for valueless keys */
gsize expected_value_len;
}
vectors[] =
{
{ (const guint8 *) "", 0, NULL, NULL, 0 },
{ (const guint8 *) "\x00", 1, NULL, NULL, 0 },
{ (const guint8 *) "\xff", 1, NULL, NULL, 0 },
{ (const guint8 *) "k\x00", 2, NULL, NULL, 0 },
{ (const guint8 *) "k\xff", 2, NULL, NULL, 0 },
{ (const guint8 *) "=", 1, NULL, NULL, 0 },
{ (const guint8 *) "=value", 6, NULL, NULL, 0 },
{ (const guint8 *) "k=v", 3, "k", (const guint8 *) "v", 1 },
{ (const guint8 *) "key=value", 9, "key", (const guint8 *) "value", 5 },
{ (const guint8 *) "k=v=", 4, "k", (const guint8 *) "v=", 2 },
{ (const guint8 *) "k=", 2, "k", (const guint8 *) "", 0 },
{ (const guint8 *) "k", 1, "k", NULL, 0 },
{ (const guint8 *) "k==", 3, "k", (const guint8 *) "=", 1 },
{ (const guint8 *) "k=\x00\x01\x02", 5, "k", (const guint8 *) "\x00\x01\x02", 3 },
};
gsize i;
for (i = 0; i < G_N_ELEMENTS (vectors); i++)
{
g_autoptr(AvahiStringList) string_list = NULL;
g_autoptr(GHashTable) attributes = NULL;
g_test_message ("Vector %" G_GSIZE_FORMAT, i);
string_list = avahi_string_list_add_arbitrary (NULL, vectors[i].txt, vectors[i].txt_len);
attributes = _ostree_txt_records_parse (string_list);
if (vectors[i].expected_key != NULL)
{
GBytes *value;
g_autoptr(GBytes) expected_value = NULL;
g_assert_true (g_hash_table_lookup_extended (attributes,
vectors[i].expected_key,
NULL,
(gpointer *) &value));
g_assert_cmpuint (g_hash_table_size (attributes), ==, 1);
if (vectors[i].expected_value != NULL)
{
g_assert_nonnull (value);
expected_value = g_bytes_new_static (vectors[i].expected_value, vectors[i].expected_value_len);
g_assert_true (g_bytes_equal (value, expected_value));
}
else
{
g_assert_null (value);
}
}
else
{
g_assert_cmpuint (g_hash_table_size (attributes), ==, 0);
}
}
}
/* Test that the first value for a set of duplicate records is returned.
* See RFC 6763, §6.4. */
static void
test_repo_finder_avahi_txt_records_duplicates (void)
{
g_autoptr(AvahiStringList) string_list = NULL;
g_autoptr(GHashTable) attributes = NULL;
GBytes *value;
g_autoptr(GBytes) expected_value = NULL;
/* Reverse the list before using it, as they are built in reverse order.
* (See the #AvahiStringList documentation.) */
string_list = avahi_string_list_new ("k=value1", "k=value2", "k=value3", NULL);
string_list = avahi_string_list_reverse (string_list);
attributes = _ostree_txt_records_parse (string_list);
g_assert_cmpuint (g_hash_table_size (attributes), ==, 1);
value = g_hash_table_lookup (attributes, "k");
g_assert_nonnull (value);
expected_value = g_bytes_new_static ("value1", strlen ("value1"));
g_assert_true (g_bytes_equal (value, expected_value));
}
/* Test that keys are parsed and looked up case insensitively.
* See RFC 6763, §6.4. */
static void
test_repo_finder_avahi_txt_records_case_sensitivity (void)
{
g_autoptr(AvahiStringList) string_list = NULL;
g_autoptr(GHashTable) attributes = NULL;
GBytes *value1, *value2;
g_autoptr(GBytes) expected_value1 = NULL, expected_value2 = NULL;
/* Reverse the list before using it, as they are built in reverse order.
* (See the #AvahiStringList documentation.) */
string_list = avahi_string_list_new ("k=value1",
"K=value2",
"KeY2=v",
NULL);
string_list = avahi_string_list_reverse (string_list);
attributes = _ostree_txt_records_parse (string_list);
g_assert_cmpuint (g_hash_table_size (attributes), ==, 2);
value1 = g_hash_table_lookup (attributes, "k");
g_assert_nonnull (value1);
expected_value1 = g_bytes_new_static ("value1", strlen ("value1"));
g_assert_true (g_bytes_equal (value1, expected_value1));
g_assert_null (g_hash_table_lookup (attributes, "K"));
value2 = g_hash_table_lookup (attributes, "key2");
g_assert_nonnull (value2);
expected_value2 = g_bytes_new_static ("v", 1);
g_assert_true (g_bytes_equal (value2, expected_value2));
g_assert_null (g_hash_table_lookup (attributes, "KeY2"));
}
/* Test that keys which have an empty value can be distinguished from those
* which have no value. See RFC 6763, §6.4. */
static void
test_repo_finder_avahi_txt_records_empty_and_missing (void)
{
g_autoptr(AvahiStringList) string_list = NULL;
g_autoptr(GHashTable) attributes = NULL;
GBytes *value1, *value2;
g_autoptr(GBytes) expected_value1 = NULL;
string_list = avahi_string_list_new ("empty=",
"missing",
NULL);
attributes = _ostree_txt_records_parse (string_list);
g_assert_cmpuint (g_hash_table_size (attributes), ==, 2);
value1 = g_hash_table_lookup (attributes, "empty");
g_assert_nonnull (value1);
expected_value1 = g_bytes_new_static ("", 0);
g_assert_true (g_bytes_equal (value1, expected_value1));
g_assert_true (g_hash_table_lookup_extended (attributes, "missing", NULL, (gpointer *) &value2));
g_assert_null (value2);
}
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/repo-finder-avahi/init", test_repo_finder_avahi_init);
g_test_add_func ("/repo-finder-avahi/txt-records/parse", test_repo_finder_avahi_txt_records_parse);
g_test_add_func ("/repo-finder-avahi/txt-records/duplicates", test_repo_finder_avahi_txt_records_duplicates);
g_test_add_func ("/repo-finder-avahi/txt-records/case-sensitivity", test_repo_finder_avahi_txt_records_case_sensitivity);
g_test_add_func ("/repo-finder-avahi/txt-records/empty-and-missing", test_repo_finder_avahi_txt_records_empty_and_missing);
/* FIXME: Add tests for service processing, probably by splitting the
* code in OstreeRepoFinderAvahi around found_services. */
return g_test_run();
}
|