Improve jid.split() and jid.bare() to pass new test cases with invalid JIDs
authorMatthew Wild <mwild1@gmail.com>
Fri, 21 Nov 2008 05:02:53 +0000 (05:02 +0000)
committerMatthew Wild <mwild1@gmail.com>
Fri, 21 Nov 2008 05:02:53 +0000 (05:02 +0000)
tests/test_util_jid.lua
util/jid.lua

index 1dbd72b7ba0ddee2e70bab00e3b8f6618db2aa47..7a6160088d42417bb27866bb00205ca1462aa1da 100644 (file)
@@ -11,4 +11,24 @@ function split(split)
        test("server",                  nil,    "server", nil           );
        test("server/resource",         nil,    "server", "resource"    );
        test(nil,                       nil,    nil     , nil           );
+
+       test("node@/server", nil, nil, nil , nil );
+end
+
+function bare(bare)
+       assert_equal(bare("user@host"), "user@host", "bare JID remains bare");
+       assert_equal(bare("host"), "host", "Host JID remains host");
+       assert_equal(bare("host/resource"), "host", "Host JID with resource becomes host");
+       assert_equal(bare("user@host/resource"), "user@host", "user@host JID with resource becomes user@host");
+       assert_equal(bare("user@/resource"), nil, "invalid JID is nil");
+       assert_equal(bare("@/resource"), nil, "invalid JID is nil");
+       assert_equal(bare("@/"), nil, "invalid JID is nil");
+       assert_equal(bare("/"), nil, "invalid JID is nil");
+       assert_equal(bare(""), nil, "invalid JID is nil");
+       assert_equal(bare("@"), nil, "invalid JID is nil");
+       assert_equal(bare("user@"), nil, "invalid JID is nil");
+       assert_equal(bare("user@@"), nil, "invalid JID is nil");
+       assert_equal(bare("user@@host"), nil, "invalid JID is nil");
+       assert_equal(bare("user@@host/resource"), nil, "invalid JID is nil");
+       assert_equal(bare("user@host/"), nil, "invalid JID is nil");
 end
index c9ea5b736f7a3b3a445c13141bbad4446d209625..b35ddc82c54bd305995533ed9b8d820ec80c39db 100644 (file)
@@ -1,20 +1,28 @@
 
 local match = string.match;
-
+local tostring = tostring;
+local print = print
 module "jid"
 
 function split(jid)
        if not jid then return; end
        -- TODO verify JID, and return; if invalid
-       local node = match(jid, "^([^@]+)@");
-       local server = (node and match(jid, ".-@([^@/]+)")) or match(jid, "^([^@/]+)");
-       local resource = match(jid, "/(.+)$");
-       return node, server, resource;
+       local node, nodelen = match(jid, "^([^@]+)@()");
+       local host, hostlen = match(jid, "^([^@/]+)()", nodelen)
+       if node and not host then return nil, nil, nil; end
+       local resource = match(jid, "^/(.+)$", hostlen);
+       if (not host) or ((not resource) and #jid >= hostlen) then return nil, nil, nil; end
+       return node, host, resource;
 end
 
 function bare(jid)
        local node, host = split(jid);
-       return node.."@"..host;
+       if node and host then
+               return node.."@"..host;
+       elseif host then
+               return host;
+       end
+       return nil;
 end
 
-return _M;
\ No newline at end of file
+return _M;