Skip to content

Commit

Permalink
add tests for removeLinebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed Apr 9, 2024
1 parent 551c105 commit f8edf9b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
31 changes: 20 additions & 11 deletions cpp/react-native-quick-base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ void installBase64(jsi::Runtime& jsiRuntime) {
if (arguments[1].isBool()) {
url = arguments[1].asBool();
}
std::string strBase64 = base64_encode(str, url);

return jsi::Value(jsi::String::createFromUtf8(runtime, strBase64));
try {
std::string strBase64 = base64_encode(str, url);
return jsi::Value(jsi::String::createFromUtf8(runtime, strBase64));
} catch (const std::runtime_error& error) {
throw jsi::JSError(runtime, error.what());
} catch (...) {
throw jsi::JSError(runtime, "unknown encoding error");
}
}
);
jsiRuntime.global().setProperty(jsiRuntime, "base64FromArrayBuffer", std::move(base64FromArrayBuffer));
Expand All @@ -63,14 +68,18 @@ void installBase64(jsi::Runtime& jsiRuntime) {
if (arguments[1].isBool()) {
removeLinebreaks = arguments[1].asBool();
}
std::string str = base64_decode(strBase64, removeLinebreaks);

jsi::Function arrayBufferCtor = runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer");
jsi::Object o = arrayBufferCtor.callAsConstructor(runtime, (int)str.length()).getObject(runtime);
jsi::ArrayBuffer buf = o.getArrayBuffer(runtime);
memcpy(buf.data(runtime), str.c_str(), str.size());

return o;
try {
std::string str = base64_decode(strBase64, removeLinebreaks);
jsi::Function arrayBufferCtor = runtime.global().getPropertyAsFunction(runtime, "ArrayBuffer");
jsi::Object o = arrayBufferCtor.callAsConstructor(runtime, (int)str.length()).getObject(runtime);
jsi::ArrayBuffer buf = o.getArrayBuffer(runtime);
memcpy(buf.data(runtime), str.c_str(), str.size());
return o;
} catch (const std::runtime_error& error) {
throw jsi::JSError(runtime, error.what());
} catch (...) {
throw jsi::JSError(runtime, "unknown decoding error");
}
}
);
jsiRuntime.global().setProperty(jsiRuntime, "base64ToArrayBuffer", std::move(base64ToArrayBuffer));
Expand Down
22 changes: 22 additions & 0 deletions example/src/tests/linebreaks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {expect} from 'chai';
import {toByteArray} from 'react-native-quick-base64';
import {describe, it} from '../MochaRNAdapter';
import {mapArr} from './util';

describe('linebreaks', () => {
// encoded `one\ntwo\nthree\nfour` in base64 online tool
const str = 'b25lCnR3bw==\ndGhyZWUKZm91cg==';

it('with linebreaks, leave them', () => {
expect(() => toByteArray(str)).to.throw(
/Input is not valid base64-encoded data/,
);
});

it('with linebreaks, remove them', () => {
const arr = toByteArray(str, true);
const actual = mapArr(arr, (byte: number) => String.fromCharCode(byte));
const expected = 'one\ntwothree\nfour';
expect(actual).to.equal(expected);
});
});
11 changes: 4 additions & 7 deletions example/src/tests/zero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const checks: string[] = [
'ends with a zero\0',
];

describe('zero handling', () => {
describe('zero (\\0)', () => {
for (let i = 0; i < checks.length; i++) {
const check = checks[i] as string;
it(`convert to base64 and back: '${check}'`, async () => {
Expand All @@ -31,10 +31,7 @@ describe('zero handling', () => {

const test = (data: Uint8Array, expected: string, descr: string) => {
it(`known zero values: ${descr}`, async () => {
const hex = Buffer.from(data).toString('hex');
console.log('hex', hex);
const actual = fromByteArray(data);
console.log('b64str', actual);
expect(actual).to.equal(expected);
});
};
Expand All @@ -45,15 +42,15 @@ describe('zero handling', () => {

// zer\0
const zer0 = new Uint8Array([122, 101, 114, 0]);
test(zer0, 'emVyAA==', 'zer\0');
test(zer0, 'emVyAA==', 'zer\\0');

// \0er0
const ero = new Uint8Array([0, 101, 114, 111]);
test(ero, 'AGVybw==', '\0ero');
test(ero, 'AGVybw==', '\\0ero');

// zer\0_value
const zer0_value = new Uint8Array([
122, 101, 114, 0, 95, 118, 97, 108, 117, 101,
]);
test(zer0_value, 'emVyAF92YWx1ZQ==', 'zer\0_value');
test(zer0_value, 'emVyAF92YWx1ZQ==', 'zer\\0_value');
});
1 change: 1 addition & 0 deletions example/src/useTestList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './tests/basics';
import './tests/convert';
import './tests/corrupt';
import './tests/url-safe';
import './tests/linebreaks';
import './tests/zero';
import './tests/big-data';

Expand Down

0 comments on commit f8edf9b

Please sign in to comment.