Annotate

spec/util_jid_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
parent 12770:249bf1a53866
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local jid = require "util.jid";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 describe("util.jid", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("#join()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 assert.are.equal(jid.join("a", "b", "c"), "a@b/c", "builds full JID");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 assert.are.equal(jid.join("a", "b", nil), "a@b", "builds bare JID");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 assert.are.equal(jid.join(nil, "b", "c"), "b/c", "builds full host JID");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 assert.are.equal(jid.join(nil, "b", nil), "b", "builds bare host JID");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 assert.are.equal(jid.join(nil, nil, nil), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 assert.are.equal(jid.join("a", nil, nil), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 assert.are.equal(jid.join(nil, nil, "c"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 assert.are.equal(jid.join("a", nil, "c"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 end);
12190
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
16 it("should reject invalid arguments", function ()
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
17 assert.has_error(function () jid.join(false, "bork", nil) end)
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
18 assert.has_error(function () jid.join(nil, "bork", false) end)
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
19 assert.has_error(function () jid.join(false, false, false) end)
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
20 end)
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 describe("#split()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local function test(input_jid, expected_node, expected_server, expected_resource)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local rnode, rserver, rresource = jid.split(input_jid);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 assert.are.equal(expected_node, rnode, "split("..tostring(input_jid)..") failed");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 assert.are.equal(expected_server, rserver, "split("..tostring(input_jid)..") failed");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 assert.are.equal(expected_resource, rresource, "split("..tostring(input_jid)..") failed");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 -- Valid JIDs
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 test("node@server", "node", "server", nil );
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 test("node@server/resource", "node", "server", "resource" );
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 test("server", nil, "server", nil );
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 test("server/resource", nil, "server", "resource" );
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 test("server/resource@foo", nil, "server", "resource@foo" );
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 test("server/resource@foo/bar", nil, "server", "resource@foo/bar");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 -- Always invalid JIDs
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 test(nil, nil, nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 test("node@/server", nil, nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 test("@server", nil, nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 test("@server/resource", nil, nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 test("@/resource", nil, nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end);
12190
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
46 it("should reject invalid arguments", function ()
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
47 assert.has_error(function () jid.split(false) end)
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
48 end)
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
12767
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
51 describe("#prepped_split()", function()
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
52 local function test(input_jid, expected_node, expected_server, expected_resource)
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
53 local rnode, rserver, rresource = jid.prepped_split(input_jid);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
54 assert.are.equal(expected_node, rnode, "split("..tostring(input_jid)..") failed");
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
55 assert.are.equal(expected_server, rserver, "split("..tostring(input_jid)..") failed");
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
56 assert.are.equal(expected_resource, rresource, "split("..tostring(input_jid)..") failed");
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
57 end
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
58
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
59 it("should work", function()
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
60 -- Valid JIDs
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
61 test("node@server", "node", "server", nil );
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
62 test("node@server/resource", "node", "server", "resource" );
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
63 test("server", nil, "server", nil );
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
64 test("server/resource", nil, "server", "resource" );
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
65 test("server/resource@foo", nil, "server", "resource@foo" );
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
66 test("server/resource@foo/bar", nil, "server", "resource@foo/bar");
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
67
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
68 -- Always invalid JIDs
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
69 test(nil, nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
70 test("node@/server", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
71 test("@server", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
72 test("@server/resource", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
73 test("@/resource", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
74 test("@server/", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
75 test("server/", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
76 test("/resource", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
77 end);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
78 it("should reject invalid arguments", function ()
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
79 assert.has_error(function () jid.prepped_split(false) end)
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
80 end)
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
81 it("should strip empty root label", function ()
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
82 test("node@server.", "node", "server", nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
83 end);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
84 it("should fail for JIDs that fail stringprep", function ()
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
85 test("node@invalid-\128-server", nil, nil, nil);
12770
249bf1a53866 util.jid: Add test for invalid domain but valid UTF-8 (thanks jonas)
Matthew Wild <mwild1@gmail.com>
parents: 12767
diff changeset
86 test("node@invalid-\194\128-server", nil, nil, nil);
12767
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
87 test("<invalid node>@server", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
88 test("node@server/invalid-\000-resource", nil, nil, nil);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
89 end);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
90 end);
3b75943fa5c1 util.jid: Add missing test cases
Matthew Wild <mwild1@gmail.com>
parents: 12190
diff changeset
91
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 describe("#bare()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 assert.are.equal(jid.bare("user@host"), "user@host", "bare JID remains bare");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 assert.are.equal(jid.bare("host"), "host", "Host JID remains host");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 assert.are.equal(jid.bare("host/resource"), "host", "Host JID with resource becomes host");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 assert.are.equal(jid.bare("user@host/resource"), "user@host", "user@host JID with resource becomes user@host");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 assert.are.equal(jid.bare("user@/resource"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 assert.are.equal(jid.bare("@/resource"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 assert.are.equal(jid.bare("@/"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 assert.are.equal(jid.bare("/"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 assert.are.equal(jid.bare(""), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 assert.are.equal(jid.bare("@"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 assert.are.equal(jid.bare("user@"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 assert.are.equal(jid.bare("user@@"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 assert.are.equal(jid.bare("user@@host"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 assert.are.equal(jid.bare("user@@host/resource"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 assert.are.equal(jid.bare("user@host/"), nil, "invalid JID is nil");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 end);
12190
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
111 it("should reject invalid arguments", function ()
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
112 assert.has_error(function () jid.bare(false) end)
3616128cd2e3 util.jid: Explicitly check for nil rather than falsy
Kim Alvefur <zash@zash.se>
parents: 11055
diff changeset
113 end)
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 describe("#compare()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 assert.are.equal(jid.compare("host", "host"), true, "host should match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 assert.are.equal(jid.compare("host", "other-host"), false, "host should not match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 assert.are.equal(jid.compare("other-user@host/resource", "host"), true, "host should match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 assert.are.equal(jid.compare("other-user@host", "user@host"), false, "user should not match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 assert.are.equal(jid.compare("user@host", "host"), true, "host should match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 assert.are.equal(jid.compare("user@host/resource", "host"), true, "host should match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 assert.are.equal(jid.compare("user@host/resource", "user@host"), true, "user and host should match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 assert.are.equal(jid.compare("user@other-host", "host"), false, "host should not match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 assert.are.equal(jid.compare("user@other-host", "user@host"), false, "host should not match");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129
11055
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
130 local jid_escaping_test_vectors = {
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
131 -- From https://xmpp.org/extensions/xep-0106.xml#examples sans @example.com
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
132 [[space cadet]], [[space\20cadet]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
133 [[call me "ishmael"]], [[call\20me\20\22ishmael\22]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
134 [[at&t guy]], [[at\26t\20guy]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
135 [[d'artagnan]], [[d\27artagnan]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
136 [[/.fanboy]], [[\2f.fanboy]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
137 [[::foo::]], [[\3a\3afoo\3a\3a]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
138 [[<foo>]], [[\3cfoo\3e]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
139 [[user@host]], [[user\40host]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
140 [[c:\net]], [[c\3a\net]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
141 [[c:\\net]], [[c\3a\\net]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
142 [[c:\cool stuff]], [[c\3a\cool\20stuff]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
143 [[c:\5commas]], [[c\3a\5c5commas]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
144
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
145 -- Section 4.2
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
146 [[\3and\2is\5cool]], [[\5c3and\2is\5c5cool]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
147
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
148 -- From aioxmpp
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
149 [[\5c]], [[\5c5c]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
150 -- [[\5C]], [[\5C]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
151 [[\2plus\2is\4]], [[\2plus\2is\4]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
152 [[foo\bar]], [[foo\bar]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
153 [[foo\41r]], [[foo\41r]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
154 -- additional test vectors
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
155 [[call\20me]], [[call\5c20me]],
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
156 };
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
157
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
158 describe("#escape()", function ()
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
159 it("should work", function ()
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
160 for i = 1, #jid_escaping_test_vectors, 2 do
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
161 local original = jid_escaping_test_vectors[i];
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
162 local escaped = jid_escaping_test_vectors[i+1];
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
163
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
164 assert.are.equal(escaped, jid.escape(original), ("Escapes '%s' -> '%s'"):format(original, escaped));
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
165 end
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
166 end);
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
167 end)
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
168
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
169 describe("#unescape()", function ()
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
170 it("should work", function ()
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
171 for i = 1, #jid_escaping_test_vectors, 2 do
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
172 local original = jid_escaping_test_vectors[i];
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
173 local escaped = jid_escaping_test_vectors[i+1];
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
174
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
175 assert.are.equal(original, jid.unescape(escaped), ("Unescapes '%s' -> '%s'"):format(escaped, original));
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
176 end
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
177 end);
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
178 end)
5fb95410f89c util.jid: Add test coverage for XEP-0106: JID Escaping functions
Kim Alvefur <zash@zash.se>
parents: 8236
diff changeset
179
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 it("should work with nodes", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 local function test(_jid, expected_node)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 assert.are.equal(jid.node(_jid), expected_node, "Unexpected node for "..tostring(_jid));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 test("example.com", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 test("foo.example.com", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 test("foo.example.com/resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 test("foo.example.com/some resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 test("foo.example.com/some@resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 test("foo@foo.example.com/some@resource", "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 test("foo@example/some@resource", "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 test("foo@example/@resource", "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 test("foo@example@resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 test("foo@example", "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 test("foo", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 test(nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 it("should work with hosts", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 local function test(_jid, expected_host)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 assert.are.equal(jid.host(_jid), expected_host, "Unexpected host for "..tostring(_jid));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 test("example.com", "example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 test("foo.example.com", "foo.example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 test("foo.example.com/resource", "foo.example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 test("foo.example.com/some resource", "foo.example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 test("foo.example.com/some@resource", "foo.example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 test("foo@foo.example.com/some@resource", "foo.example.com");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 test("foo@example/some@resource", "example");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 test("foo@example/@resource", "example");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 test("foo@example@resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 test("foo@example", "example");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 test("foo", "foo");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 test(nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 it("should work with resources", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 local function test(_jid, expected_resource)
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 assert.are.equal(jid.resource(_jid), expected_resource, "Unexpected resource for "..tostring(_jid));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 test("example.com", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 test("foo.example.com", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 test("foo.example.com/resource", "resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 test("foo.example.com/some resource", "some resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 test("foo.example.com/some@resource", "some@resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 test("foo@foo.example.com/some@resource", "some@resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 test("foo@example/some@resource", "some@resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 test("foo@example/@resource", "@resource");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 test("foo@example@resource", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 test("foo@example", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 test("foo", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 test("/foo", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 test("@x/foo", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 test("@/foo", nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 test(nil, nil);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 end);