[vlc-commits] sdp: initial test cases for the SDP parser

Rémi Denis-Courmont git at videolan.org
Fri Apr 10 17:54:54 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Apr 10 12:54:46 2020 +0300| [565ec5ff3fbcd1ac1ea2c27da844c0cd9a06ed5c] | committer: Rémi Denis-Courmont

sdp: initial test cases for the SDP parser

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=565ec5ff3fbcd1ac1ea2c27da844c0cd9a06ed5c
---

 modules/access/rtp/Makefile.am |   4 ++
 modules/access/rtp/sdp.c       |   2 +-
 modules/access/rtp/sdp_test.c  | 153 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 158 insertions(+), 1 deletion(-)

diff --git a/modules/access/rtp/Makefile.am b/modules/access/rtp/Makefile.am
index 2011fdff0f..3d82e9232c 100644
--- a/modules/access/rtp/Makefile.am
+++ b/modules/access/rtp/Makefile.am
@@ -10,6 +10,10 @@ librtp_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/access/rtp
 librtp_plugin_la_CFLAGS = $(AM_CFLAGS)
 librtp_plugin_la_LIBADD = $(SOCKET_LIBS)
 
+sdp_parse_test_SOURCES = access/rtp/sdp_test.c access/rtp/sdp.c
+check_PROGRAMS += sdp_parse_test
+TESTS += sdp_parse_test
+
 # Secure RTP library
 libvlc_srtp_la_SOURCES = access/rtp/srtp.c access/rtp/srtp.h
 libvlc_srtp_la_CPPFLAGS = -I$(srcdir)/access/rtp
diff --git a/modules/access/rtp/sdp.c b/modules/access/rtp/sdp.c
index 1774ef973e..3e311531c1 100644
--- a/modules/access/rtp/sdp.c
+++ b/modules/access/rtp/sdp.c
@@ -75,7 +75,7 @@ bad:
     c->addr[addrlen] = '\0';
 
     if (len >= 7 && memcmp(net_type, "IN ", 3) == 0) {
-        int offset, val;
+        int offset, val = -1;
 
         if (memcmp(addr_type, "IP4 ", 4) == 0) {
             /* IPv4 */
diff --git a/modules/access/rtp/sdp_test.c b/modules/access/rtp/sdp_test.c
new file mode 100644
index 0000000000..7852a1b5ed
--- /dev/null
+++ b/modules/access/rtp/sdp_test.c
@@ -0,0 +1,153 @@
+/**
+ * @file sdp_test.c
+ */
+/*****************************************************************************
+ * Copyright © 2020 Rémi Denis-Courmont
+ *
+ * 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.1
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+ ****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#undef NDEBUG
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+#include "sdp.h"
+#include <vlc_common.h>
+
+static void test_sdp_valid(const char *str)
+{
+    size_t len;
+
+    for (len = 0; str[len] != '\0'; len++) {
+        struct vlc_sdp *sdp = vlc_sdp_parse(str, len);
+
+        if (sdp != NULL)
+            vlc_sdp_free(sdp);
+    }
+
+    struct vlc_sdp *sdp = vlc_sdp_parse(str, len);
+
+    assert(sdp != NULL);
+    vlc_sdp_free(sdp);
+}
+
+static void test_sdp_invalid(const char *base, size_t len)
+{
+    bool ok = false;
+
+    for (size_t i = 0; i <= len; i++) {
+        struct vlc_sdp *sdp = vlc_sdp_parse(base, i);
+
+        if (sdp != NULL)
+            vlc_sdp_free(sdp);
+
+        ok = sdp != NULL;
+    }
+
+    assert(!ok);
+}
+
+int main(void)
+{
+    static const char example[] =
+        "v=0\r\n"
+        "o=- 1 1 IN IP4 192.0.2.1\r\n"
+        "s=Title here\r\n"
+        "i=Description here\r\n"
+        "u=https://www.example.com/\r\n"
+        "e=postmaster at example.com (Postmaster)\r\n"
+        "p=+1 800-555-0100\r\n"
+        "c=IN IP4 239.255.0.1/127\r\n"
+        "b=AS:100\r\n"
+        "t=3155673600 3155673600\r\n"
+        "r=604800 3600 0 86400\r\n"
+        "k=prompt\r\n"
+        "a=recvonly\r\n"
+        "m=text 5004 RTP/AVP 96\r\n"
+        "i=Media title here\r\n"
+        "c=IN IP4 239.255.0.2/127\r\n"
+        "c=IN IP6 ff03::1/2\r\n"
+        "b=AS:100\r\n"
+        "k=prompt\r\n"
+        "a=rtpmap:96 t140/1000\r\n";
+
+    test_sdp_valid(example);
+
+    struct vlc_sdp *sdp = vlc_sdp_parse(example, strlen(example));
+    assert(sdp != NULL);
+    assert(vlc_sdp_attr_present(sdp, "recvonly"));
+    assert(!vlc_sdp_attr_present(sdp, "sendrecv"));
+    assert(vlc_sdp_attr_value(sdp, "recvonly") == NULL);
+
+    const struct vlc_sdp_media *m = sdp->media;
+    assert(m != NULL);
+    assert(m->next == NULL);
+    assert(!strcmp(m->type, "text"));
+    assert(m->port == 5004);
+    assert(m->port_count == 1);
+    assert(!strcmp(m->proto, "RTP/AVP"));
+    assert(!strcmp(m->format, "96"));
+    assert(vlc_sdp_media_attr_present(m, "rtpmap"));
+    assert(!strcmp(vlc_sdp_media_attr_value(m, "rtpmap"), "96 t140/1000"));
+
+    const struct vlc_sdp_conn *c = vlc_sdp_media_conn(m);
+    assert(c != NULL);
+    assert(c->family == 4);
+    assert(!strcmp(c->addr, "239.255.0.2"));
+    assert(c->ttl == 127);
+    assert(c->addr_count == 1);
+    c = c->next;
+    assert(c != NULL);
+    assert(c->family == 6);
+    assert(!strcmp(c->addr, "ff03::1"));
+    assert(c->ttl == 255);
+    assert(c->addr_count == 2);
+
+    vlc_sdp_free(sdp);
+
+    char smallest[] =
+        "v=0\n"
+        "o=- 0 0 x y z\n"
+        "s=\n"
+        "t=0 0\n";
+
+    test_sdp_valid(smallest);
+
+    smallest[10] = 0;
+    test_sdp_invalid(smallest, sizeof (smallest) - 1);
+    smallest[10] = '\r';
+    test_sdp_invalid(smallest, sizeof (smallest) - 1);
+
+    static const char *const bad[] = {
+        "\r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=\r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=text \r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=text 0 \r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=text 0/1 \r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=video x udp mpeg\r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nm=video 0 udp \r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nc=IN\r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nc=IN IP4\r\n",
+        "v=0\r\no=- 1 1 x y z\r\ns=-\r\nc=IN IP4 example.com x\r\n",
+    };
+
+    for (size_t i = 0; i < ARRAY_SIZE(bad); i++)
+        test_sdp_invalid(bad[i], strlen(bad[i]));
+    return 0;
+}



More information about the vlc-commits mailing list