summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--.gitignore3
-rw-r--r--libvncserver/websockets.c22
-rw-r--r--libvncserver/ws_decode.c56
-rw-r--r--libvncserver/ws_decode.h4
-rwxr-xr-xtest/wsmaketestframe.py121
-rw-r--r--test/wstest.c195
6 files changed, 360 insertions, 41 deletions
diff --git a/.gitignore b/.gitignore
index fccd7af..a24f81a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -66,6 +66,9 @@ test/cargstest
test/copyrecttest
test/cursortest
test/encodingstest
+test/wstest
+test/wsmaketestframe.py
+test/wstestdata.in
/test/tjbench
/test/tjunittest
vncterm/LinuxVNC
diff --git a/libvncserver/websockets.c b/libvncserver/websockets.c
index 364225b..ab9cabb 100644
--- a/libvncserver/websockets.c
+++ b/libvncserver/websockets.c
@@ -100,8 +100,7 @@ void webSocketsGenMd5(char * target, char *key1, char *key2, char *key3);
static int webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst);
-static int ws_read(void *cl, char *buf, int len);
-static int ws_peek(void *cl, char *buf, int len);
+static int ws_read(void *cl, char *buf, size_t len);
static int
@@ -345,7 +344,6 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
wsctx->encode = webSocketsEncodeHybi;
wsctx->decode = webSocketsDecodeHybi;
wsctx->ctxInfo.readFunc = ws_read;
- wsctx->ctxInfo.peekFunc = ws_peek;
wsctx->base64 = base64;
hybiDecodeCleanup(wsctx);
cl->wsctx = (wsCtx *)wsctx;
@@ -403,7 +401,7 @@ webSocketsGenMd5(char * target, char *key1, char *key2, char *key3)
}
static int
-ws_read(void *ctxPtr, char *buf, int len)
+ws_read(void *ctxPtr, char *buf, size_t len)
{
int n;
rfbClientPtr cl = ctxPtr;
@@ -416,22 +414,6 @@ ws_read(void *ctxPtr, char *buf, int len)
}
static int
-ws_peek(void *ctxPtr, char *buf, int len)
-{
- int n;
- rfbClientPtr cl = ctxPtr;
- if (cl->sslctx) {
- n = rfbssl_peek(cl, buf, len);
- } else {
- while (-1 == (n = recv(cl->sock, buf, len, MSG_PEEK))) {
- if (errno != EAGAIN)
- break;
- }
- }
- return n;
-}
-
-static int
webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
{
int blen, ret = -1, sz = 0;
diff --git a/libvncserver/ws_decode.c b/libvncserver/ws_decode.c
index e74a33c..3bd17f4 100644
--- a/libvncserver/ws_decode.c
+++ b/libvncserver/ws_decode.c
@@ -1,11 +1,12 @@
#include "ws_decode.h"
-#include <syslog.h>
#include <string.h>
#include <errno.h>
#define WS_HYBI_MASK_LEN 4
-
+#define WS_HYBI_HEADER_LEN_SHORT 2 + WS_HYBI_MASK_LEN
+#define WS_HYBI_HEADER_LEN_EXTENDED 4 + WS_HYBI_MASK_LEN
+#define WS_HYBI_HEADER_LEN_LONG 10 + WS_HYBI_MASK_LEN
static int
hybiRemaining(ws_ctx_t *wsctx)
@@ -66,8 +67,12 @@ hybiReturnData(char *dst, int len, ws_ctx_t *wsctx, int *nWritten)
}
}
rfbLog("after copy: readPos=%p, readLen=%d\n", wsctx->readPos, wsctx->readlen);
- } else if (wsctx->hybiDecodeState == WS_HYBI_STATE_CLOSE_REASON_PENDING) {
- nextState = WS_HYBI_STATE_CLOSE_REASON_PENDING;
+ } else {
+ /* it may happen that we read some bytes but could not decode them,
+ * in that case, set errno to EAGAIN and return -1 */
+ nextState = wsctx->hybiDecodeState;
+ errno = EAGAIN;
+ *nWritten = -1;
}
return nextState;
}
@@ -98,7 +103,7 @@ hybiReadHeader(ws_ctx_t *wsctx, int *sockRet)
if (-1 == ret) {
/* save errno because rfbErr() will tamper it */
int olderrno = errno;
- rfbErr("%s: peek; %m\n", __func__);
+ rfbErr("%s: read; %s\n", __func__, strerror(errno));
errno = olderrno;
*sockRet = -1;
} else {
@@ -131,22 +136,22 @@ hybiReadHeader(ws_ctx_t *wsctx, int *sockRet)
* close the connection upon receiving a frame with the MASK bit set to 0.
**/
if (!(wsctx->header.data->b1 & 0x80)) {
- rfbErr("%s: got frame without mask ret=%d\n", __func__, ret);
- syslog(LOG_ERR, "%s: got frame without mask; ret=%d\n", __func__, ret);
- errno = EIO;
+ rfbErr("%s: got frame without mask; ret=%d\n", __func__, ret);
+ errno = EPROTO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
}
+
if (wsctx->header.payloadLen < 126 && wsctx->nReadRaw >= 6) {
- wsctx->header.headerLen = 2 + WS_HYBI_MASK_LEN;
+ wsctx->header.headerLen = WS_HYBI_HEADER_LEN_SHORT;
wsctx->header.mask = wsctx->header.data->u.m;
} else if (wsctx->header.payloadLen == 126 && 8 <= wsctx->nReadRaw) {
- wsctx->header.headerLen = 4 + WS_HYBI_MASK_LEN;
+ wsctx->header.headerLen = WS_HYBI_HEADER_LEN_EXTENDED;
wsctx->header.payloadLen = WS_NTOH16(wsctx->header.data->u.s16.l16);
wsctx->header.mask = wsctx->header.data->u.s16.m16;
} else if (wsctx->header.payloadLen == 127 && 14 <= wsctx->nReadRaw) {
- wsctx->header.headerLen = 10 + WS_HYBI_MASK_LEN;
+ wsctx->header.headerLen = WS_HYBI_HEADER_LEN_LONG;
wsctx->header.payloadLen = WS_NTOH64(wsctx->header.data->u.s64.l64);
wsctx->header.mask = wsctx->header.data->u.s64.m64;
} else {
@@ -157,6 +162,19 @@ hybiReadHeader(ws_ctx_t *wsctx, int *sockRet)
return WS_HYBI_STATE_HEADER_PENDING;
}
+ /* while RFC 6455 mandates that lengths MUST be encoded with the minimum
+ * number of bytes, it does not specify for the server how to react on
+ * 'wrongly' encoded frames --- this implementation rejects them*/
+ if ((wsctx->header.headerLen > WS_HYBI_HEADER_LEN_SHORT
+ && wsctx->header.payloadLen < 126)
+ || (wsctx->header.headerLen > WS_HYBI_HEADER_LEN_EXTENDED
+ && wsctx->header.payloadLen < 65536)) {
+ rfbErr("%s: invalid length field; headerLen=%d payloadLen=%llu\n", __func__, wsctx->header.headerLen, wsctx->header.payloadLen);
+ errno = EPROTO;
+ *sockRet = -1;
+ return WS_HYBI_STATE_ERR;
+ }
+
/* absolute length of frame */
wsctx->nToRead = wsctx->header.headerLen + wsctx->header.payloadLen;
@@ -238,7 +256,7 @@ hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
//if (-1 == (n = ws_read(cl, wsctx->writePos, nextRead))) {
if (-1 == (n = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, wsctx->writePos, nextRead))) {
int olderrno = errno;
- rfbErr("%s: read; %m", __func__);
+ rfbErr("%s: read; %s", __func__, strerror(errno));
errno = olderrno;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
@@ -260,6 +278,8 @@ hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
errno=EIO;
*sockRet = -1;
return WS_HYBI_STATE_ERR;
+ } else {
+ wsctx->hybiDecodeState = WS_HYBI_STATE_FRAME_COMPLETE;
}
}
@@ -294,7 +314,7 @@ hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
/* carry over remaining, non-multiple-of-four bytes */
wsctx->carrylen = toDecode - (i * 4);
if (wsctx->carrylen < 0 || wsctx->carrylen > ARRAYSIZE(wsctx->carryBuf)) {
- syslog(LOG_ERR, "%s: internal error, invalid carry over size: carrylen=%d, toDecode=%d, i=%d", __func__, wsctx->carrylen, toDecode, i);
+ rfbErr("%s: internal error, invalid carry over size: carrylen=%d, toDecode=%d, i=%d", __func__, wsctx->carrylen, toDecode, i);
*sockRet = -1;
errno = EIO;
return WS_HYBI_STATE_ERR;
@@ -310,7 +330,6 @@ hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
/* this data is not returned as payload data */
if (hybiWsFrameComplete(wsctx)) {
- rfbLog("got closure, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
rfbLog("got close cmd, reason %d\n", WS_NTOH16(((uint16_t *)data)[0]));
errno = ECONNRESET;
*sockRet = -1;
@@ -326,8 +345,7 @@ hybiReadAndDecode(ws_ctx_t *wsctx, char *dst, int len, int *sockRet)
data[toReturn] = '\0';
rfbLog("Initiate Base64 decoding in %p with max size %d and '\\0' at %p\n", data, bufsize, data + toReturn);
if (-1 == (wsctx->readlen = b64_pton((char *)data, data, bufsize))) {
- syslog(LOG_ERR, "Base64 decode error in %s; data=%p bufsize=%d", __func__, data, bufsize);
- rfbErr("%s: Base64 decode error; %m\n", __func__);
+ rfbErr("%s: Base64 decode error; %s\n", __func__, strerror(errno));
}
wsctx->writePos = hybiPayloadStart(wsctx);
break;
@@ -437,12 +455,14 @@ spor:
"writePos=%p "
"state=%d toRead=%d remaining=%d "
"nRead=%d carrylen=%d carryBuf=%p "
- "result=%d\n",
+ "result=%d "
+ "errno=%d\n",
__func__, len,
wsctx->readlen, wsctx->readPos,
wsctx->writePos,
wsctx->hybiDecodeState, wsctx->nToRead, hybiRemaining(wsctx),
wsctx->nReadRaw, wsctx->carrylen, wsctx->carryBuf,
- result);
+ result,
+ errno);
return result;
}
diff --git a/libvncserver/ws_decode.h b/libvncserver/ws_decode.h
index e75c4d1..fac3c68 100644
--- a/libvncserver/ws_decode.h
+++ b/libvncserver/ws_decode.h
@@ -50,13 +50,11 @@ typedef struct ws_ctx_s ws_ctx_t;
typedef int (*wsEncodeFunc)(rfbClientPtr cl, const char *src, int len, char **dst);
typedef int (*wsDecodeFunc)(ws_ctx_t *wsctx, char *dst, int len);
-typedef int (*wsReadFunc)(void *ctx, char *dst, int len);
-typedef int (*wsPeekFunc)(void *ctx, char *dst, int len);
+typedef int (*wsReadFunc)(void *ctx, char *dst, size_t len);
typedef struct ctxInfo_s{
void *ctxPtr;
wsReadFunc readFunc;
- wsPeekFunc peekFunc;
} ctxInfo_t;
enum {
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