2021-03-10 16:22:49 +11:00
|
|
|
import { fc, testProp } from 'ava-fast-check';
|
|
|
|
|
|
2021-03-12 14:23:21 +11:00
|
|
|
import { WakuMessage } from '../gen/proto/waku/v2/waku_pb';
|
|
|
|
|
|
2021-03-10 16:22:49 +11:00
|
|
|
import { Message } from './waku_message';
|
|
|
|
|
|
2021-03-12 14:23:21 +11:00
|
|
|
// This test is more about documenting how protobuf library works than testing it
|
2021-03-15 15:45:41 +11:00
|
|
|
testProp('Protobuf round trip binary serialization', [fc.string()], (t, s) => {
|
2021-03-12 14:23:21 +11:00
|
|
|
const wakuMsg = new WakuMessage();
|
|
|
|
|
wakuMsg.setPayload(Buffer.from(s, 'utf-8'));
|
|
|
|
|
|
|
|
|
|
const binary = wakuMsg.serializeBinary();
|
|
|
|
|
const actual = WakuMessage.deserializeBinary(binary);
|
|
|
|
|
|
|
|
|
|
const payload = actual.getPayload();
|
|
|
|
|
|
|
|
|
|
let buf;
|
|
|
|
|
if (typeof payload === 'string') {
|
|
|
|
|
buf = Buffer.from(payload, 'base64');
|
|
|
|
|
} else {
|
|
|
|
|
buf = Buffer.from(payload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.deepEqual(s, buf.toString('utf-8'));
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-10 16:22:49 +11:00
|
|
|
testProp(
|
2021-03-15 15:45:41 +11:00
|
|
|
'Waku message round trip binary serialization',
|
2021-03-10 16:22:49 +11:00
|
|
|
[fc.string()],
|
|
|
|
|
(t, s) => {
|
2021-03-12 14:23:21 +11:00
|
|
|
const msg = Message.fromUtf8String(s);
|
2021-03-10 16:22:49 +11:00
|
|
|
const binary = msg.toBinary();
|
|
|
|
|
const actual = Message.fromBinary(binary);
|
|
|
|
|
|
2021-03-12 14:23:21 +11:00
|
|
|
t.true(
|
|
|
|
|
actual.isEqualTo(msg),
|
|
|
|
|
`${JSON.stringify(actual)}\n${JSON.stringify(msg)}`
|
|
|
|
|
);
|
2021-03-10 16:22:49 +11:00
|
|
|
}
|
|
|
|
|
);
|