TestStringPrepProfiles.java revision 7935b1839a081ed19ae0d33029ad3c09632a2caa
1/*
2 *******************************************************************************
3 * Copyright (C) 2009, International Business Machines Corporation and         *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7package com.ibm.icu.dev.test.stringprep;
8
9import com.ibm.icu.dev.test.TestFmwk;
10import com.ibm.icu.text.StringPrep;
11import com.ibm.icu.text.StringPrepParseException;
12
13/**
14 * @author Michael Ow
15 *
16 */
17public class TestStringPrepProfiles extends TestFmwk {
18    public static void main(String[] args) throws Exception {
19        new TestStringPrepProfiles().run(args);
20    }
21
22    /*
23     * The format of the test cases should be the following:
24     * {
25     *     Profile name
26     *     src string1
27     *     expected result1
28     *     src string2
29     *     expected result2
30     *     ...
31     * }
32     *
33     * *Note: For expected failures add FAIL to beginning of the source string and for expected result use "FAIL".
34     */
35    private static String[][] testCases = {
36        {
37            "RFC4013_SASLPREP",
38            "user:\u00A0\u0AC6\u1680\u00ADpassword1",
39            "user: \u0AC6 password1"
40        },
41        {
42            "RFC4011_MIB",
43            "Policy\u034F\u200DBase\u0020d\u1806\u200C",
44            "PolicyBase d"
45        },
46        {
47            "RFC4505_TRACE",
48            "Anony\u0020\u00A0mous\u3000\u0B9D\u034F\u00AD",
49            "Anony\u0020\u00A0mous\u3000\u0B9D\u034F\u00AD"
50        },
51        {
52            "RFC4518_LDAP",
53            "Ldap\uFB01\u00ADTest\u0020\u00A0\u2062ing",
54            "LdapfiTest  ing"
55        },
56        {
57            "RFC4518_LDAP_CI",
58            "Ldap\uFB01\u00ADTest\u0020\u00A0\u2062ing12345",
59            "ldapfitest  ing12345"
60        },
61        {
62            "RFC3920_RESOURCEPREP",
63            "ServerXM\u2060\uFE00\uFE09PP s p ",
64            "ServerXMPP s p "
65        },
66        {
67            "RFC3920_NODEPREP",
68            "Server\u200DXMPPGreEK\u03D0",
69            "serverxmppgreek\u03B2"
70        },
71        {
72            "RFC3722_ISCSI",
73            "InternetSmallComputer\uFB01\u0032\u2075Interface",
74            "internetsmallcomputerfi25interface",
75            "FAILThisShouldFailBecauseOfThis\u002F",
76            "FAIL"
77        },
78        {
79            "RFC3530_NFS4_CS_PREP",
80            "\u00ADUser\u2060Name@ \u06DDDOMAIN.com",
81            "UserName@ \u06DDDOMAIN.com"
82        },
83        {
84            "RFC3530_NFS4_CS_PREP_CI",
85            "\u00ADUser\u2060Name@ \u06DDDOMAIN.com",
86            "username@ \u06DDdomain.com"
87        },
88        {
89            "RFC3530_NFS4_CIS_PREP",
90            "AA\u200C\u200D @@DomAin.org",
91            "aa @@domain.org"
92        },
93        {
94            "RFC3530_NFS4_MIXED_PREP_PREFIX",
95            "PrefixUser \u007F\uFB01End",
96            "PrefixUser \u007FfiEnd"
97        },
98        {
99            "RFC3530_NFS4_MIXED_PREP_SUFFIX",
100            "SuffixDomain \u007F\uFB01EnD",
101            "suffixdomain \u007Ffiend"
102        }
103    };
104
105    private int getOptionFromProfileName(String profileName) {
106        if (profileName.equals("RFC4013_SASLPREP")) {
107            return StringPrep.RFC4013_SASLPREP;
108        } else if (profileName.equals("RFC4011_MIB")) {
109            return StringPrep.RFC4011_MIB;
110        } else if (profileName.equals("RFC4505_TRACE")) {
111            return StringPrep.RFC4505_TRACE;
112        } else if (profileName.equals("RFC4518_LDAP")) {
113            return StringPrep.RFC4518_LDAP;
114        } else if (profileName.equals("RFC4518_LDAP_CI")) {
115            return StringPrep.RFC4518_LDAP_CI;
116        } else if (profileName.equals("RFC3920_RESOURCEPREP")) {
117            return StringPrep.RFC3920_RESOURCEPREP;
118        } else if (profileName.equals("RFC3920_NODEPREP")) {
119            return StringPrep.RFC3920_NODEPREP;
120        } else if (profileName.equals("RFC3722_ISCSI")) {
121            return StringPrep.RFC3722_ISCSI;
122        } else if (profileName.equals("RFC3530_NFS4_CS_PREP")) {
123            return StringPrep.RFC3530_NFS4_CS_PREP;
124        } else if (profileName.equals("RFC3530_NFS4_CS_PREP_CI")) {
125            return StringPrep.RFC3530_NFS4_CS_PREP_CI;
126        } else if (profileName.equals("RFC3530_NFS4_CIS_PREP")) {
127            return StringPrep.RFC3530_NFS4_CIS_PREP;
128        } else if (profileName.equals("RFC3530_NFS4_MIXED_PREP_PREFIX")) {
129            return StringPrep.RFC3530_NFS4_MIXED_PREP_PREFIX;
130        } else if (profileName.equals("RFC3530_NFS4_MIXED_PREP_SUFFIX")) {
131            return StringPrep.RFC3530_NFS4_MIXED_PREP_SUFFIX;
132        }
133
134        // Should not happen.
135        return -1;
136    }
137
138    public void TestProfiles() {
139        String profileName = null;
140        StringPrep sprep = null;
141        String result = null;
142        String src = null;
143        String expected = null;
144
145        for (int i = 0; i < testCases.length; i++) {
146            for (int j = 0; j < testCases[i].length; j++) {
147                if (j == 0) {
148                    profileName = testCases[i][j];
149
150                    sprep = StringPrep.getInstance(getOptionFromProfileName(profileName));
151                } else {
152                    src = testCases[i][j];
153                    expected = testCases[i][++j];
154                    try {
155                        result = sprep.prepare(src, StringPrep.ALLOW_UNASSIGNED);
156                        if (src.startsWith("FAIL")) {
157                            errln("Failed: Expected error for Test[" + i +"] Profile: " + profileName);
158                        } else if (!result.equals(expected)) {
159                            errln("Failed: Test[" + i + "] Result string does not match expected string for StringPrep test for profile: " + profileName);
160                        }
161                    } catch (StringPrepParseException ex) {
162                        if (!src.startsWith("FAIL")) {
163                            errln("Failed: Test[" + i + "] StringPrep profile " + profileName + " got error: " + ex);
164                        }
165                    }
166                }
167            }
168        }
169    }
170}
171