|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.vaadin.flow.internal; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class EncodeUtilTest { |
| 23 | + |
| 24 | + @Test(expected = NullPointerException.class) |
| 25 | + public void rfc5987Encode_withNull_nullPointerException() { |
| 26 | + EncodeUtil.rfc5987Encode(null); |
| 27 | + } |
| 28 | + |
| 29 | + @Test(expected = NullPointerException.class) |
| 30 | + public void rfc2047Encode_withNull_nullPointerException() { |
| 31 | + EncodeUtil.rfc2047Encode(null); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void rfc5987Encode_asciiCharacters() { |
| 36 | + String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$&'*+-.^_`|~"; |
| 37 | + Assert.assertEquals( |
| 38 | + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$&%27%2A+-.^_`|~", |
| 39 | + EncodeUtil.rfc5987Encode(input)); |
| 40 | + } |
| 41 | + |
| 42 | + // UTF-8 Basic Latin & Controls |
| 43 | + @Test |
| 44 | + public void rfc5987Encode_unicodeCharacters0to126() throws Exception { |
| 45 | + StringBuilder text = new StringBuilder(); |
| 46 | + for (int codePoint = 0; codePoint <= 126; codePoint++) { |
| 47 | + text.append(new String(new int[] { codePoint }, 0, 1)); |
| 48 | + } |
| 49 | + Assert.assertEquals( |
| 50 | + "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20!%22#$%25&%27%28%29%2A+%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D^_`abcdefghijklmnopqrstuvwxyz%7B|%7D~", |
| 51 | + EncodeUtil.rfc5987Encode(text.toString())); |
| 52 | + } |
| 53 | + |
| 54 | + // UTF-8 Latin-1 Supplement |
| 55 | + @Test |
| 56 | + public void rfc5987Encode_unicodeLatin1SupplementCharacters() |
| 57 | + throws Exception { |
| 58 | + Assert.assertEquals("%E2%82%AC%20%C3%BF", |
| 59 | + EncodeUtil.rfc5987Encode("€ ÿ")); |
| 60 | + } |
| 61 | + |
| 62 | + // UTF-8 Latin Extended A |
| 63 | + @Test |
| 64 | + public void rfc5987Encode_unicodeLatinExtendACharacters() throws Exception { |
| 65 | + Assert.assertEquals("%C4%80%C4%81", EncodeUtil.rfc5987Encode("Āā")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void rfc2047Encode_asciiCharacters() { |
| 70 | + String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$&'*+-.^_`|~ ?=\""; |
| 71 | + Assert.assertEquals( |
| 72 | + "=?UTF-8?Q?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$&'*+-.^=5F`|~_=3F=3D=22?=", |
| 73 | + EncodeUtil.rfc2047Encode(input)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void rfc2047Encode_nonAsciiCharacters() { |
| 78 | + String input = "Řřüñîçødë 1中文 € ÿĀā"; |
| 79 | + Assert.assertEquals( |
| 80 | + "=?UTF-8?Q?=C5=98=C5=99=C3=BC=C3=B1=C3=AE=C3=A7=C3=B8d=C3=AB_1=E4=B8=AD=E6=96=87_=E2=82=AC_=C3=BF=C4=80=C4=81?=", |
| 81 | + EncodeUtil.rfc2047Encode(input)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void isPureUSASCII_withAsciiOnly_returnTrue() { |
| 86 | + String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$&'*+-.^_`|~ ?=\""; |
| 87 | + Assert.assertTrue(EncodeUtil.isPureUSASCII(input)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void isPureUSASCII_withNonAscii_returnFalse() { |
| 92 | + String input = "Řřüñîçøë中文€ÿĀā"; |
| 93 | + input.chars().forEach(c -> { |
| 94 | + Assert.assertFalse( |
| 95 | + "Character " + (char) c + " should not be US-ASCII", |
| 96 | + EncodeUtil.isPureUSASCII(Character.toString((char) c))); |
| 97 | + }); |
| 98 | + } |
| 99 | +} |
0 commit comments