a10ddf5
From 7586bc7e5006fd7df55199283de4766b2775f60f Mon Sep 17 00:00:00 2001
af78c9a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
af78c9a
Date: Sun, 18 Jun 2017 15:53:15 -0400
a2b328a
Subject: [PATCH] test-resolved-packet: add a simple test for our allocation
a2b328a
 functions
af78c9a
a10ddf5
(cherry picked from commit 751ca3f1de316ca79b60001334dbdf54077e1d01)
af78c9a
---
af78c9a
 .gitignore                         |  1 +
af78c9a
 Makefile.am                        | 14 ++++++++++++
af78c9a
 src/resolve/test-resolved-packet.c | 45 ++++++++++++++++++++++++++++++++++++++
af78c9a
 3 files changed, 60 insertions(+)
af78c9a
 create mode 100644 src/resolve/test-resolved-packet.c
af78c9a
af78c9a
diff --git a/.gitignore b/.gitignore
a10ddf5
index 01cb6e7db7..25b976a0e3 100644
af78c9a
--- a/.gitignore
af78c9a
+++ b/.gitignore
a10ddf5
@@ -269,6 +269,7 @@
af78c9a
 /test-replace-var
af78c9a
 /test-resolve
af78c9a
 /test-resolve-tables
af78c9a
+/test-resolved-packet
af78c9a
 /test-ring
af78c9a
 /test-rlimit-util
af78c9a
 /test-sched-prio
af78c9a
diff --git a/Makefile.am b/Makefile.am
a10ddf5
index a767a5aa0d..e97a66e0fa 100644
af78c9a
--- a/Makefile.am
af78c9a
+++ b/Makefile.am
a10ddf5
@@ -5663,6 +5663,7 @@ dist_zshcompletion_data += \
af78c9a
 tests += \
af78c9a
 	test-dns-packet \
af78c9a
 	test-resolve-tables \
af78c9a
+	test-resolved-packet \
af78c9a
 	test-dnssec
af78c9a
 
af78c9a
 manual_tests += \
a10ddf5
@@ -5684,6 +5685,19 @@ test_resolve_tables_LDADD = \
af78c9a
 	$(GCRYPT_LIBS) \
af78c9a
 	-lm
af78c9a
 
af78c9a
+test_resolved_packet_SOURCES = \
af78c9a
+	src/resolve/test-resolved-packet.c \
af78c9a
+	$(basic_dns_sources)
af78c9a
+
af78c9a
+test_resolved_packet_CFLAGS = \
af78c9a
+	$(AM_CFLAGS) \
af78c9a
+	$(GCRYPT_CFLAGS)
af78c9a
+
af78c9a
+test_resolved_packet_LDADD = \
af78c9a
+	libsystemd-shared.la \
af78c9a
+	$(GCRYPT_LIBS) \
af78c9a
+	-lm
af78c9a
+
af78c9a
 test_dns_packet_SOURCES = \
af78c9a
 	src/resolve/test-dns-packet.c \
af78c9a
 	$(basic_dns_sources)
af78c9a
diff --git a/src/resolve/test-resolved-packet.c b/src/resolve/test-resolved-packet.c
af78c9a
new file mode 100644
af78c9a
index 0000000000..8b7da1408d
af78c9a
--- /dev/null
af78c9a
+++ b/src/resolve/test-resolved-packet.c
af78c9a
@@ -0,0 +1,45 @@
af78c9a
+/***
af78c9a
+  This file is part of systemd
af78c9a
+
af78c9a
+  Copyright 2017 Zbigniew Jędrzejewski-Szmek
af78c9a
+
af78c9a
+  systemd is free software; you can redistribute it and/or modify it
af78c9a
+  under the terms of the GNU Lesser General Public License as published by
af78c9a
+  the Free Software Foundation; either version 2.1 of the License, or
af78c9a
+  (at your option) any later version.
af78c9a
+
af78c9a
+  systemd is distributed in the hope that it will be useful, but
af78c9a
+  WITHOUT ANY WARRANTY; without even the implied warranty of
af78c9a
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af78c9a
+  Lesser General Public License for more details.
af78c9a
+
af78c9a
+  You should have received a copy of the GNU Lesser General Public License
af78c9a
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
af78c9a
+***/
af78c9a
+
af78c9a
+#include "log.h"
af78c9a
+#include "resolved-dns-packet.h"
af78c9a
+
af78c9a
+static void test_dns_packet_new(void) {
af78c9a
+        size_t i;
af78c9a
+
af78c9a
+        for (i = 0; i < DNS_PACKET_SIZE_MAX + 2; i++) {
af78c9a
+                _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
af78c9a
+
af78c9a
+                assert_se(dns_packet_new(&p, DNS_PROTOCOL_DNS, i) == 0);
af78c9a
+
af78c9a
+                log_debug("dns_packet_new: %zu → %zu", i, p->allocated);
af78c9a
+                assert_se(p->allocated >= MIN(DNS_PACKET_SIZE_MAX, i));
af78c9a
+        }
af78c9a
+}
af78c9a
+
af78c9a
+int main(int argc, char **argv) {
af78c9a
+
af78c9a
+        log_set_max_level(LOG_DEBUG);
af78c9a
+        log_parse_environment();
af78c9a
+        log_open();
af78c9a
+
af78c9a
+        test_dns_packet_new();
af78c9a
+
af78c9a
+        return 0;
af78c9a
+}