994823c3675b8a5a55e2ca23f00260365584cbb8
[prosody.git] / tests / test_sasl.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21 --- WARNING! ---
22 -- This file contains a mix of encodings below. 
23 -- Many editors will unquestioningly convert these for you.
24 -- Please be careful :(  (I recommend Scite)
25 ---------------------------------
26
27 local   gmatch = string.gmatch;
28 local   t_concat, t_insert = table.concat, table.insert;
29 local   to_byte, to_char = string.byte, string.char;
30
31 local function _latin1toutf8(str)
32                 if not str then return str; end
33                 local p = {};
34                 for ch in gmatch(str, ".") do
35                         ch = to_byte(ch);
36                         if (ch < 0x80) then
37                                 t_insert(p, to_char(ch));
38                         elseif (ch < 0xC0) then
39                                 t_insert(p, to_char(0xC2, ch));
40                         else
41                                 t_insert(p, to_char(0xC3, ch - 64));
42                         end
43                 end
44                 return t_concat(p);
45         end
46
47 function latin1toutf8()
48         local function assert_utf8(latin, utf8)
49                         assert_equal(_latin1toutf8(latin), utf8, "Incorrect UTF8 from Latin1: "..tostring(latin));
50         end
51         
52         assert_utf8("", "")
53         assert_utf8("test", "test")
54         assert_utf8(nil, nil)
55         assert_utf8("foobar.råkat.se", "foobar.rÃ¥kat.se")
56 end