summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorAndreas Weigel <andreaswe@securepoint.de>2017-02-20 11:24:18 +0100
committerChristian Beier <dontmind@freeshell.org>2017-05-14 20:38:23 +0200
commitf19d6ee225ff35eb54ca06927a921c98ff721adc (patch)
treee6c36654f7dbc2762b73c53c03ca3fa19e2ea5d7 /test
parenta2322e70069b30fb4f86248d6a6bce6d2a9c11e1 (diff)
downloadlibtdevnc-f19d6ee225ff35eb54ca06927a921c98ff721adc.tar.gz
libtdevnc-f19d6ee225ff35eb54ca06927a921c98ff721adc.zip
add ws_decode tests
modify automake to include ws_decode test add python frame generator for decode tests modify configure to only include ws_decode test if preconditions are fulfilled
Diffstat (limited to 'test')
-rwxr-xr-xtest/wsmaketestframe.py121
-rw-r--r--test/wstest.c195
2 files changed, 316 insertions, 0 deletions
diff --git a/test/wsmaketestframe.py b/test/wsmaketestframe.py
new file mode 100755
index 0000000..d0053a2
--- /dev/null
+++ b/test/wsmaketestframe.py
@@ -0,0 +1,121 @@
+#!/usr/bin/env python3
+# Copyright (C)2017 Andreas Weigel. All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# - Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+# - Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+import websockets
+import base64
+import errno
+
+def add_field(s, name, value, first=False):
+ deli = ",\n\t\t"
+ if first:
+ deli = "\t\t"
+ s += "{2}.{0}={1}".format(name, value, deli)
+ return s
+
+
+class Testframe():
+ def __init__(self, frame, descr, retbytes=[], modify_bytes={}, experrno=0, mask=True):
+ self.frame = frame
+ self.descr = descr
+ self.retbytes = retbytes
+ self.modify_bytes = modify_bytes
+ self.experrno = experrno
+ self.b64 = True if frame.opcode == 1 else False
+ self.mask = mask
+
+ def to_carray_initializer(self, buf):
+ values = []
+ for i in range(len(buf)):
+ values.append("0X{0:02X}".format(buf[i]))
+
+ if self.modify_bytes != {}:
+ for k in self.modify_bytes:
+ values[k] = "0X{0:02X}".format(self.modify_bytes[k])
+
+ return "{{{0}}}".format(", ".join(values))
+
+
+ def set_frame_buf(self, buf):
+ self.frame_carray = self.to_carray_initializer(buf)
+ self.framelen = len(buf)
+
+ def __str__(self):
+ #print("processing frame: {0}".format(self.descr))
+ the_frame = self.frame
+ if self.b64:
+ olddata = self.frame.data
+ newdata = base64.b64encode(self.frame.data)
+ #print("converting\n{0}\nto{1}\n".format(olddata, newdata))
+ the_frame = websockets.framing.Frame(self.frame.fin, self.frame.opcode, base64.b64encode(olddata))
+
+ websockets.framing.write_frame(the_frame, self.set_frame_buf, self.mask)
+ s = "\t{\n"
+ s = add_field(s, "frame", "{0}".format(self.frame_carray), True)
+ s = add_field(s, "expectedDecodeBuf", self.to_carray_initializer(self.frame.data))
+ s = add_field(s, "frame_len", self.framelen)
+ s = add_field(s, "raw_payload_len", len(self.frame.data))
+ s = add_field(s, "expected_errno", self.experrno)
+ s = add_field(s, "descr", "\"{0}\"".format(self.descr))
+ s = add_field(s, "ret_bytes", "{{{0}}}".format(", ".join(self.retbytes)))
+ s = add_field(s, "ret_bytes_len", len(self.retbytes))
+ s = add_field(s, "i", "0")
+ s = add_field(s, "simulate_sock_malfunction_at", "0")
+ s = add_field(s, "errno_val", "0")
+ s = add_field(s, "close_sock_at", "0")
+ s += "\n\t}"
+ return s
+
+### create test frames
+flist = []
+### standard text frames with different lengths
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray("Testit", encoding="utf-8")), "Short valid text frame", {}))
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray("Frame2 does contain much more text and even goes beyond the 126 byte len field. Frame2 does contain much more text and even goes beyond the 126 byte len field.", encoding="utf-8")),
+ "Mid-long valid text frame", {}))
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray([(x % 26) + 65 for x in range(100000)])), "100k text frame (ABC..YZABC..)", {}))
+
+### standard binary frames with different lengths
+flist.append(Testframe(websockets.framing.Frame(1, 2, bytearray("Testit", encoding="utf-8")), "Short valid binary frame", {}))
+flist.append(Testframe(websockets.framing.Frame(1, 2, bytearray("Frame2 does contain much more text and even goes beyond the 126 byte len field. Frame2 does contain much more text and even goes beyond the 126 byte len field.", encoding="utf-8")),
+ "Mid-long valid binary frame", {}))
+flist.append(Testframe(websockets.framing.Frame(1, 2, bytearray([(x % 26) + 65 for x in range(100000)])), "100k binary frame (ABC..YZABC..)", {}))
+
+### some conn reset frames, one with no close message, one with close message (the latter should cause an error)
+flist.append(Testframe(websockets.framing.Frame(1, 8, bytearray(list([0x03, 0xEB]))), "Close frame (Reason 1003)", {}, experrno=errno.ECONNRESET))
+flist.append(Testframe(websockets.framing.Frame(1, 8, bytearray(list([0x03, 0xEB])) + bytearray("I'm a close reason", encoding="utf-8")), "Close frame (Reason 1003) and msg", {}, experrno=errno.EIO))
+
+### invalid header values
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray("Testit", encoding="utf-8")), "Invalid frame: Wrong masking", {}, experrno=errno.EPROTO, mask=False))
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray("..Lore Ipsum", encoding="utf-8")), "Invalid frame: Length of < 126 with add. 16 bit len field", {}, experrno=errno.EPROTO, modify_bytes={ 1: 0xFE, 2: 0x00, 3: 0x0F}))
+flist.append(Testframe(websockets.framing.Frame(1, 1, bytearray("........Lore Ipsum", encoding="utf-8")), "Invalid frame: Length of < 126 with add. 64 bit len field", {}, experrno=errno.EPROTO, modify_bytes={ 1: 0xFF, 2: 0x00, 3: 0x00, 4: 0x00, 5: 0x00, 6: 0x80, 7: 0x40}))
+
+s = "struct ws_frame_test tests[] = {\n"
+for i in range(len(flist)):
+ s += flist[i].__str__()
+ if (i + 1 < len(flist)):
+ s += ","
+ s += "\n"
+s += "};\n"
+
+with open("wstestdata.in", "w") as cdatafile:
+ cdatafile.write(s)
diff --git a/test/wstest.c b/test/wstest.c
new file mode 100644
index 0000000..30324cb
--- /dev/null
+++ b/test/wstest.c
@@ -0,0 +1,195 @@
+/*
+ * Copyright (C)2017 Andreas Weigel. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <ws_decode.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifndef _WIN32
+
+#define TEST_BUF_SIZE B64LEN(131072) + WSHLENMAX
+#define RND_SEED 100
+#define WS_TMP_LOG "ws_tmp.log"
+
+enum {
+ OK,
+ FAIL_DATA,
+ FAIL_ERRNO,
+ FAIL_CLOSED,
+};
+
+const char *result_descr[] = {
+ "",
+ "Data buffers do not match",
+ "Wrong errno",
+ "Wrongly reported closed socket",
+ "Internal test error"
+};
+
+struct ws_frame_test {
+ char frame[TEST_BUF_SIZE];
+ char *pos;
+ char expectedDecodeBuf[TEST_BUF_SIZE];
+ uint64_t frame_len;
+ uint64_t raw_payload_len;
+ int expected_errno;
+ const char *descr;
+ int ret_bytes[16];
+ int ret_bytes_len;
+ int i;
+ int simulate_sock_malfunction_at;
+ int errno_val;
+ int close_sock_at;
+};
+
+char el_log[1000000];
+char *el_pos;
+
+static void logtest(const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ size_t left = el_log + sizeof(el_log) - el_pos;
+ size_t off = vsnprintf(el_pos, left, fmt, args);
+ el_pos += off;
+ va_end(args);
+}
+
+static int emu_read(void *ctx, char *dst, size_t len);
+
+static int emu_read(void *ctx, char *dst, size_t len)
+{
+ struct ws_frame_test *ft = (struct ws_frame_test *)ctx;
+ ssize_t nret;
+ int r;
+ ssize_t modu;
+
+ rfbLog("emu_read called with dst=%p and len=%lu\n", dst, len);
+ if (ft->simulate_sock_malfunction_at > 0 && ft->simulate_sock_malfunction_at == ft->i) {
+ rfbLog("simulating IO error with errno=%d\n", ft->errno_val);
+ errno = ft->errno_val;
+ return -1;
+ }
+
+ /* return something */
+ r = rand();
+ modu = (ft->frame + ft->frame_len) - ft->pos;
+ rfbLog("r=%d modu=%ld frame=%p pos=%p\n", r, modu, ft->frame, ft->pos);
+ nret = (r % modu) + 1;
+ nret = nret > len ? len : nret;
+
+ rfbLog("copy and return %ld bytes\n", nret);
+ memcpy(dst, ft->pos, nret);
+ ft->pos += nret;
+ rfbLog("leaving %s; pos=%p framebuf=%p nret=%ld\n", __func__, ft->pos, ft->frame, nret);
+ return nret;
+}
+
+static uint64_t run_test(struct ws_frame_test *ft, ws_ctx_t *ctx)
+{
+ uint64_t nleft = ft->raw_payload_len;
+ char dstbuf[ft->raw_payload_len];
+ char *dst = dstbuf;
+ ssize_t n;
+
+ ft->pos = ft->frame;
+
+ ctx->ctxInfo.ctxPtr = (void *)ft;
+
+ while (nleft > 0) {
+ rfbLog("calling ws_decode with dst=%p, len=%lu\n", dst, nleft);
+ n = ctx->decode(ctx, dst, nleft);
+ rfbLog("read n=%ld\n", n);
+ if (n == 0) {
+ if (ft->close_sock_at > 0) {
+ return OK;
+ } else {
+ return FAIL_CLOSED;
+ }
+ } else if (n < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ /* ok, just call again */
+ } else {
+ if (ft->expected_errno == errno) {
+ rfbLog("errno=%d as expected\n", errno);
+ return OK;
+ } else {
+ rfbLog("errno=%d != expected(%d)\n", errno, ft->expected_errno);
+ return FAIL_ERRNO;
+ }
+ }
+ } else {
+ nleft -= n;
+ dst += n;
+ rfbLog("read n=%ld from decode; dst=%p, nleft=%lu\n", n, dst, nleft);
+ }
+ }
+
+ if (memcmp(ft->expectedDecodeBuf, dstbuf, ft->raw_payload_len) != 0) {
+ ft->expectedDecodeBuf[ft->raw_payload_len] = '\0';
+ dstbuf[ft->raw_payload_len] = '\0';
+ rfbLog("decoded result not equal:\nexpected:\n%s\ngot\n%s\n\n", ft->expectedDecodeBuf, dstbuf);
+ return FAIL_DATA;
+ }
+
+ return OK;
+}
+
+#include "wstestdata.in"
+
+int main()
+{
+ ws_ctx_t ctx;
+ int retall= 0;
+ srand(RND_SEED);
+
+ for (int i = 0; i < ARRAYSIZE(tests); i++) {
+ int ret;
+
+ el_pos = el_log;
+ rfbLog = logtest;
+ rfbErr = logtest;
+
+ hybiDecodeCleanup(&ctx);
+ ctx.decode = webSocketsDecodeHybi;
+ ctx.version = WEBSOCKETS_VERSION_HYBI;
+
+ ctx.ctxInfo.readFunc = emu_read;
+
+ ret = run_test(&tests[i], &ctx);
+ printf("%s: \"%s\"\n", ret == 0 ? "PASS" : "FAIL", tests[i].descr);
+ if (ret != 0) {
+ *el_pos = '\0';
+ printf("%s", el_log);
+ retall = -1;
+ }
+ }
+ return retall;
+}
+
+#endif