annotate spec/util_dbuffer_spec.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parents bf6d2f9fad4d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local dbuffer = require "util.dbuffer";
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 describe("util.dbuffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("#new", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 it("has a constructor", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 assert.Function(dbuffer.new);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 it("can be created", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 assert.truthy(dbuffer.new());
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
9 assert.truthy(dbuffer.new(1));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
10 assert.truthy(dbuffer.new(1024));
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("won't create an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 assert.falsy(dbuffer.new(0));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 it("won't create a negatively sized buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 assert.falsy(dbuffer.new(-1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 describe(":write", function ()
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
20 local b = dbuffer.new(10, 3);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 assert.truthy(b:write("hi"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
24 it("fails when the buffer is full", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
25 local ret = b:write(" there world, this is a long piece of data");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
26 assert.is_falsy(ret);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
27 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
28 it("works when max_chunks is reached", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
29 -- Chunks are an optimization, dbuffer should collapse chunks when needed
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
30 for _ = 1, 8 do
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
31 assert.truthy(b:write("!"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
32 end
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
33 assert.falsy(b:write("!")); -- Length reached
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
34 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 describe(":read", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 it("supports optional bytes parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 -- should return the frontmost chunk
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 assert.truthy(b:write("hello"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.truthy(b:write(" "));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 assert.truthy(b:write("world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 assert.equal("h", b:read(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 assert.equal("ello", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 assert.equal(" ", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 assert.equal("world", b:read());
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
50 it("fails when there is not enough data in the buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
51 local b = dbuffer.new(12);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
52 b:write("hello");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
53 b:write(" ");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
54 b:write("world");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
55 assert.is_falsy(b:read(12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
56 assert.is_falsy(b:read(13));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
57 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
11636
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
60 describe(":read_until", function ()
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
61 it("works", function ()
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
62 local b = dbuffer.new();
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
63 b:write("hello\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
64 b:write("world");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
65 b:write("\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
66 b:write("\n\n");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
67 b:write("stuff");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
68 b:write("more\nand more");
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
69
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
70 assert.equal(nil, b:read_until("."));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
71 assert.equal(nil, b:read_until("%"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
72 assert.equal("hello\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
73 assert.equal("world\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
74 assert.equal("\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
75 assert.equal("\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
76 assert.equal("stu", b:read(3));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
77 assert.equal("ffmore\n", b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
78 assert.equal(nil, b:read_until("\n"));
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
79 assert.equal("and more", b:read_chunk());
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
80 end);
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
81 end);
11e0a0a08da3 util.dbuffer: Add read_until() method
Matthew Wild <mwild1@gmail.com>
parents: 11158
diff changeset
82
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 describe(":discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 assert.truthy(b:discard(6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 assert.equal(5, b:length());
11156
a8ef69f7fc35 util.dbuffer: Expose length as :len() method, like strings
Kim Alvefur <zash@zash.se>
parents: 11105
diff changeset
89 assert.equal(5, b:len());
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 assert.equal("world", b:read(5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
92 it("works across chunks", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
93 assert.truthy(b:write("hello"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
94 assert.truthy(b:write(" "));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
95 assert.truthy(b:write("world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
96 assert.truthy(b:discard(3));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
97 assert.equal(8, b:length());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
98 assert.truthy(b:discard(3));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
99 assert.equal(5, b:length());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
100 assert.equal("world", b:read(5));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
101 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
102 it("can discard the entire buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
103 assert.equal(b:len(), 0);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
104 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
105 assert.truthy(b:discard(11));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
106 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
107 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
108 assert.truthy(b:discard(12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
109 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
110 assert.truthy(b:write("hello world"));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
111 assert.truthy(b:discard(128));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
112 assert.equal(0, b:len());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
113 end);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
114 it("works on an empty buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
115 assert.truthy(dbuffer.new():discard());
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
116 assert.truthy(dbuffer.new():discard(0));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
117 assert.truthy(dbuffer.new():discard(1));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
118 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 describe(":collapse()", function ()
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
122 it("works", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
123 local b = dbuffer.new();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
124 b:write("hello");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
125 b:write(" ");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
126 b:write("world");
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
127 b:collapse(6);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
128 local ret, bytes = b:read_chunk();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
129 assert.equal("hello ", ret);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
130 assert.equal(6, bytes);
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
131 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 b:collapse();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 describe(":sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 -- Helper function to compare buffer:sub() with string:sub()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 local s = "hello world";
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 local function test_sub(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 local string_result, buffer_result = s:sub(x, y), b:sub(x, y);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 assert.equals(string_result, buffer_result, ("buffer:sub(%d, %s) does not match string:sub()"):format(x, y and ("%d"):format(y) or "nil"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 it("works", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 assert.equals("hello", b:sub(1, 5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 it("works after discard", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 local b = dbuffer.new(256);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 assert.truthy(b:write("foobar"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 assert.equals("foobar", b:sub(1, 6));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 assert.truthy(b:discard(3)); -- consume "foo"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 assert.equals("bar", b:sub(1, 3));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 it("supports optional end parameter", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 assert.truthy(b:write("hello world"));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 assert.equals("hello world", b:sub(1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 assert.equals("world", b:sub(-5));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 it("is equivalent to string:sub", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 test_sub(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175 end);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
176
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
177 it("works on an empty buffer", function ()
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
178 local b = dbuffer.new();
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
179 assert.equal("", b:sub(1, 12));
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
180 end);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 describe(":byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184 -- Helper function to compare buffer:byte() with string:byte()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local s = "hello world"
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local function test_byte(b, x, y)
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local string_result, buffer_result = {s:byte(x, y)}, {b:byte(x, y)};
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
188 assert.same(
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
189 string_result,
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
190 buffer_result,
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
191 ("buffer:byte(%s, %s) does not match string:byte()"):format(x and ("%d"):format(x) or "nil", y and ("%d"):format(y) or "nil")
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
192 );
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 it("is equivalent to string:byte", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 local b = dbuffer.new(11);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 assert.truthy(b:write(s));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 test_byte(b, 1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 test_byte(b, 3);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 test_byte(b, -1);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 test_byte(b, -3);
12764
bf6d2f9fad4d util.dbuffer: Add a bunch of missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 11637
diff changeset
202 test_byte(b, nil, 5);
11105
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 for i = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 for j = -13, 13 do
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 test_byte(b, i, j);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207 end
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 it("works with characters > 127", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212 b:write(string.char(0, 140));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
213 local r = { b:byte(1, 2) };
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
214 assert.same({ 0, 140 }, r);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
215 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
216
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
217 it("works on an empty buffer", function ()
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 local b = dbuffer.new();
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 assert.equal("", b:sub(1,1));
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
221 end);
3c0940f3cf74 util.dbuffer: Simplify test case
Kim Alvefur <zash@zash.se>
parents:
diff changeset
222 end);