Mercurial > prosody-hg
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 |
| rev | line source |
|---|---|
| 11105 | 1 local dbuffer = require "util.dbuffer"; |
| 2 describe("util.dbuffer", function () | |
| 3 describe("#new", function () | |
| 4 it("has a constructor", function () | |
| 5 assert.Function(dbuffer.new); | |
| 6 end); | |
| 7 it("can be created", function () | |
| 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 | 11 end); |
| 12 it("won't create an empty buffer", function () | |
| 13 assert.falsy(dbuffer.new(0)); | |
| 14 end); | |
| 15 it("won't create a negatively sized buffer", function () | |
| 16 assert.falsy(dbuffer.new(-1)); | |
| 17 end); | |
| 18 end); | |
| 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 | 21 it("works", function () |
| 22 assert.truthy(b:write("hi")); | |
| 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 | 35 end); |
| 36 | |
| 37 describe(":read", function () | |
| 38 it("supports optional bytes parameter", function () | |
| 39 -- should return the frontmost chunk | |
| 40 local b = dbuffer.new(); | |
| 41 assert.truthy(b:write("hello")); | |
| 42 assert.truthy(b:write(" ")); | |
| 43 assert.truthy(b:write("world")); | |
| 44 assert.equal("h", b:read(1)); | |
| 45 | |
| 46 assert.equal("ello", b:read()); | |
| 47 assert.equal(" ", b:read()); | |
| 48 assert.equal("world", b:read()); | |
| 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 | 58 end); |
| 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 | 83 describe(":discard", function () |
| 84 local b = dbuffer.new(); | |
| 85 it("works", function () | |
| 86 assert.truthy(b:write("hello world")); | |
| 87 assert.truthy(b:discard(6)); | |
| 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 | 90 assert.equal("world", b:read(5)); |
| 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 | 119 end); |
| 120 | |
| 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 | 132 it("works on an empty buffer", function () |
| 133 local b = dbuffer.new(); | |
| 134 b:collapse(); | |
| 135 end); | |
| 136 end); | |
| 137 | |
| 138 describe(":sub", function () | |
| 139 -- Helper function to compare buffer:sub() with string:sub() | |
| 140 local s = "hello world"; | |
| 141 local function test_sub(b, x, y) | |
| 142 local string_result, buffer_result = s:sub(x, y), b:sub(x, y); | |
| 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")); | |
| 144 end | |
| 145 | |
| 146 it("works", function () | |
| 147 local b = dbuffer.new(); | |
| 148 assert.truthy(b:write("hello world")); | |
| 149 assert.equals("hello", b:sub(1, 5)); | |
| 150 end); | |
| 151 | |
| 152 it("works after discard", function () | |
| 153 local b = dbuffer.new(256); | |
| 154 assert.truthy(b:write("foobar")); | |
| 155 assert.equals("foobar", b:sub(1, 6)); | |
| 156 assert.truthy(b:discard(3)); -- consume "foo" | |
| 157 assert.equals("bar", b:sub(1, 3)); | |
| 158 end); | |
| 159 | |
| 160 it("supports optional end parameter", function () | |
| 161 local b = dbuffer.new(); | |
| 162 assert.truthy(b:write("hello world")); | |
| 163 assert.equals("hello world", b:sub(1)); | |
| 164 assert.equals("world", b:sub(-5)); | |
| 165 end); | |
| 166 | |
| 167 it("is equivalent to string:sub", function () | |
| 168 local b = dbuffer.new(11); | |
| 169 assert.truthy(b:write(s)); | |
| 170 for i = -13, 13 do | |
| 171 for j = -13, 13 do | |
| 172 test_sub(b, i, j); | |
| 173 end | |
| 174 end | |
| 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 | 181 end); |
| 182 | |
| 183 describe(":byte", function () | |
| 184 -- Helper function to compare buffer:byte() with string:byte() | |
| 185 local s = "hello world" | |
| 186 local function test_byte(b, x, y) | |
| 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 | 193 end |
| 194 | |
| 195 it("is equivalent to string:byte", function () | |
| 196 local b = dbuffer.new(11); | |
| 197 assert.truthy(b:write(s)); | |
| 198 test_byte(b, 1); | |
| 199 test_byte(b, 3); | |
| 200 test_byte(b, -1); | |
| 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 | 203 for i = -13, 13 do |
| 204 for j = -13, 13 do | |
| 205 test_byte(b, i, j); | |
| 206 end | |
| 207 end | |
| 208 end); | |
| 209 | |
| 210 it("works with characters > 127", function () | |
| 211 local b = dbuffer.new(); | |
| 212 b:write(string.char(0, 140)); | |
| 213 local r = { b:byte(1, 2) }; | |
| 214 assert.same({ 0, 140 }, r); | |
| 215 end); | |
| 216 | |
| 217 it("works on an empty buffer", function () | |
| 218 local b = dbuffer.new(); | |
| 219 assert.equal("", b:sub(1,1)); | |
| 220 end); | |
| 221 end); | |
| 222 end); |
