Fix issues in recently merged PR

This commit is contained in:
Alexey Andreev 2023-01-26 09:17:08 +01:00
parent ada086a864
commit 5c146c6de0
2 changed files with 62 additions and 53 deletions

View File

@ -38,6 +38,19 @@ public class TBase64 {
public byte[] encode(byte[] src) { public byte[] encode(byte[] src) {
return Base64Impl.encode(src, mapping, padding); return Base64Impl.encode(src, mapping, padding);
} }
public String encodeToString(byte[] src) {
var bytes = encode(src);
var chars = new char[bytes.length];
for (var i = 0; i < bytes.length; ++i) {
chars[i] = (char) (bytes[i] & 0xFF);
}
return new String(chars);
}
public Encoder withoutPadding() {
return new Encoder(mapping, false);
}
} }
public Decoder getDecoder() { public Decoder getDecoder() {
@ -58,5 +71,13 @@ public class TBase64 {
public byte[] decode(byte[] src) { public byte[] decode(byte[] src) {
return Base64Impl.decode(src, mapping); return Base64Impl.decode(src, mapping);
} }
public byte[] decode(String src) {
var bytes = new byte[src.length()];
for (var i = 0; i < bytes.length; ++i) {
bytes[i] = (byte) src.charAt(i);
}
return decode(bytes);
}
} }
} }

View File

@ -13,87 +13,75 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.teavm.classlib.impl; package org.teavm.classlib.java.util;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner;
import org.teavm.junit.WholeClassCompilation;
@RunWith(TeaVMTestRunner.class) @RunWith(TeaVMTestRunner.class)
@WholeClassCompilation @WholeClassCompilation
public class Base64Test { public class Base64Test {
@Test @Test
public void decoderWorks() { public void decoderWorks() {
Decoder decoder = Base64.getDecoder(); assertEquals("q", decode("cQ=="));
assertEquals("q", decoder.decode("cQ==")); assertEquals("qw", decode("cXc="));
assertEquals("qw", decoder.decode("cXc=")); assertEquals("qwe", decode("cXdl"));
assertEquals("qwe", decoder.decode("cXdl")); assertEquals("qwer", decode("cXdlcg=="));
assertEquals("qwer", decoder.decode("cXdlcg==")); assertEquals("qwert", decode("cXdlcnQ="));
assertEquals("qwert", decoder.decode("cXdlcnQ=")); assertEquals("qwerty", decode("cXdlcnR5"));
assertEquals("qwerty", decoder.decode("cXdlcnR5")); assertEquals("qwertyu", decode("cXdlcnR5dQ=="));
assertEquals("qwertyu", decoder.decode("cXdlcnR5dQ==")); assertEquals("юзернейм:пароль", decode("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw="));
assertEquals("юзернейм:пароль", decoder.decode("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw="));
} }
@Test @Test
public void encoderWorks() { public void encoderWorks() {
Encoder encoder = Base64.getEncoder(); assertEquals("cQ==", encode("q"));
assertEquals("cQ==", encoder.encode("q")); assertEquals("cXc=", encode("qw"));
assertEquals("cXc=", encoder.encode("qw")); assertEquals("cXdl", encode("qwe"));
assertEquals("cXdl", encoder.encode("qwe")); assertEquals("cXdlcg==", encode("qwer"));
assertEquals("cXdlcg==", encoder.encode("qwer")); assertEquals("cXdlcnQ=", encode("qwert"));
assertEquals("cXdlcnQ=", encoder.encode("qwert")); assertEquals("cXdlcnR5", encode("qwerty"));
assertEquals("cXdlcnR5", encoder.encode("qwerty")); assertEquals("cXdlcnR5dQ==", encode("qwertyu"));
assertEquals("cXdlcnR5dQ==", encoder.encode("qwertyu")); assertEquals("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw=", encode("юзернейм:пароль"));
assertEquals("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw=", encoder.encode("юзернейм:пароль"));
} }
@Test @Test
public void urlDecoderWorks() { public void urlDecoderWorks() {
Decoder decoder = Base64.getUrlDecoder(); assertEquals("q", decode("cQ"));
assertEquals("q", decoder.decode("cQ")); assertEquals("qw", decode("cXc"));
assertEquals("qw", decoder.decode("cXc")); assertEquals("qwe", decode("cXdl"));
assertEquals("qwe", decoder.decode("cXdl")); assertEquals("qwer", decode("cXdlcg"));
assertEquals("qwer", decoder.decode("cXdlcg")); assertEquals("qwerty", decode("cXdlcnR5"));
assertEquals("qwerty", decoder.decode("cXdlcnR5")); assertEquals("qwertyu", decode("cXdlcnR5dQ"));
assertEquals("qwertyu", decoder.decode("cXdlcnR5dQ")); assertEquals("юзернейм:пароль", decode("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw"));
assertEquals("юзернейм:пароль", decoder.decode("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw"));
} }
@Test @Test
public void urlEncoderWorks() { public void urlEncoderWorks() {
Encoder encoder = Base64.getUrlEncoder(); assertEquals("cQ", encodeNoPad("q"));
assertEquals("cQ", encoder.encodeNoPad("q")); assertEquals("cXc", encodeNoPad("qw"));
assertEquals("cXc", encoder.encodeNoPad("qw")); assertEquals("cXdl", encodeNoPad("qwe"));
assertEquals("cXdl", encoder.encodeNoPad("qwe")); assertEquals("cXdlcg", encodeNoPad("qwer"));
assertEquals("cXdlcg", encoder.encodeNoPad("qwer")); assertEquals("cXdlcnQ", encodeNoPad("qwert"));
assertEquals("cXdlcnQ", encoder.encodeNoPad("qwert")); assertEquals("cXdlcnR5", encodeNoPad("qwerty"));
assertEquals("cXdlcnR5", encoder.encodeNoPad("qwerty")); assertEquals("cXdlcnR5dQ", encodeNoPad("qwertyu"));
assertEquals("cXdlcnR5dQ", encoder.encodeNoPad("qwertyu")); assertEquals("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw", encodeNoPad("юзернейм:пароль"));
assertEquals("0Y7Qt9C10YDQvdC10LnQvDrQv9Cw0YDQvtC70Yw", encoder.encode("юзернейм:пароль"));
} }
private String decode(String text) { private String decode(String text) {
try { return new String(Base64.getDecoder().decode(text), StandardCharsets.UTF_8);
return new String(Base64Impl.decode(text.getBytes("UTF-8")), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
} }
private String encode(String text) { private String encode(String text) {
try { return Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
return new String(Base64Impl.encode(text.getBytes("UTF-8"), true), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
} }
private String encodeNoPad(String text) { private String encodeNoPad(String text) {
try { return Base64.getEncoder().withoutPadding().encodeToString(text.getBytes(StandardCharsets.UTF_8));
return new String(Base64Impl.encode(text.getBytes("UTF-8"), false), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
} }
} }