Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rust and js tests [AP-949] #1383

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion generator/sbpg/targets/test_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def render_source(output_dir, package_spec):
"""
Render and output to a directory given a package specification.
"""
if len(package_spec.tests) == 0:
return
path, name = package_spec.filepath
destination_filename = "%s/integration/%s.rs" % (output_dir, snake_case(name))
py_template = JENV.get_template(TEST_TEMPLATE_NAME)
Expand All @@ -59,6 +61,6 @@ def render_source(output_dir, package_spec):
def render_main(output_dir, package_specs):
destination_filename = "%s/integration/main.rs" % output_dir
py_template = JENV.get_template(TEST_MAIN_TEMPLATE_NAME)
test_names = [snake_case(p.filepath[1]) for p in package_specs]
test_names = [snake_case(p.filepath[1]) for p in package_specs if len(p.tests) > 0]
with open(destination_filename, 'w') as f:
f.write(py_template.render(test_names=test_names))
70 changes: 36 additions & 34 deletions javascript/tests/test_decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,47 @@ describe('test packages based on YAML test files', function () {
yamlTestFiles.forEach(function (filename) {
describe(filename, function () {
var yamlConfig = yaml.safeLoad(fs.readFileSync(filename));
yamlConfig.tests.map(function (testSpec, i) {
describe('test spec '+i, function () {
var msgBuffer = new Buffer(testSpec['raw_packet'], 'base64');
var decodeMsg = function () {
return decode(msgBuffer);
};
it('should parse binary sbp and payload', function () {
decodeMsg();
});
it('should have correct SBP fields', function () {
var msg = decodeMsg();
utils.verifyFields(testSpec.sbp, msg.sbp);
});
it('should have correct payload fields', function () {
var msg = decodeMsg();
utils.verifyFields(testSpec.msg.fields, msg.fields);
});
it('should serialize back to binary properly', function () {
var msg = decodeMsg();
assert.equal(msg.toBase64(), testSpec['raw_packet']);
});
it('should serialize back to JSON properly', function () {
var msg = decodeMsg();
if ("tests" in yamlConfig) {
yamlConfig.tests.map(function (testSpec, i) {
describe('test spec '+i, function () {
var msgBuffer = new Buffer(testSpec['raw_packet'], 'base64');
var decodeMsg = function () {
return decode(msgBuffer);
};
it('should parse binary sbp and payload', function () {
decodeMsg();
});
it('should have correct SBP fields', function () {
var msg = decodeMsg();
utils.verifyFields(testSpec.sbp, msg.sbp);
});
it('should have correct payload fields', function () {
var msg = decodeMsg();
utils.verifyFields(testSpec.msg.fields, msg.fields);
});
it('should serialize back to binary properly', function () {
var msg = decodeMsg();
assert.equal(msg.toBase64(), testSpec['raw_packet']);
});
it('should serialize back to JSON properly', function () {
var msg = decodeMsg();

var expected = JSON.parse(testSpec['raw_json']);
var expected = JSON.parse(testSpec['raw_json']);

// UInt64s are stringified as strings, not bare numbers in JSON, so...
var actual = JSON.parse(JSON.stringify(msg).replace(/"([0-9]+)"/, '$1'));
// UInt64s are stringified as strings, not bare numbers in JSON, so...
var actual = JSON.parse(JSON.stringify(msg).replace(/"([0-9]+)"/, '$1'));

assert.deepEqual(actual, expected);
});
it('should be identical to constructed message with identical fields', function () {
var msg = decodeMsg();
var msgTypeConstructor = messageTypesTable[msg.messageType];
var constructedMsg = constructMsg(msgTypeConstructor, msg.fields, msg.sbp.sender);
assert(msgBuffer.equals(constructedMsg.toBuffer()));
assert.deepEqual(actual, expected);
});
it('should be identical to constructed message with identical fields', function () {
var msg = decodeMsg();
var msgTypeConstructor = messageTypesTable[msg.messageType];
var constructedMsg = constructMsg(msgTypeConstructor, msg.fields, msg.sbp.sender);
assert(msgBuffer.equals(constructedMsg.toBuffer()));
});
});
});
});
}
});
});
});
Expand Down
169 changes: 90 additions & 79 deletions javascript/tests/test_dispatch_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,96 +33,107 @@ describe('test packages based on YAML descriptors, through the dispatcher', func
yamlTestFiles.forEach(function (filename) {
describe(filename, function () {
var yamlConfig = yaml.safeLoad(fs.readFileSync(filename));
yamlConfig.tests.map(function (testSpec, i) {
describe('test spec '+i, function () {
it('should parse binary sbp and payload', function (done) {
var rs = new Readable();
rs.push(new Buffer(testSpec['raw_packet'], 'base64'));
rs.push(null);
let ctx = {
testSpec,
done,
expectedCalls: 1,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});
if ("tests" in yamlConfig) {
yamlConfig.tests.map(function (testSpec, i) {
describe('test spec '+i, function () {
it('should parse binary sbp and payload', function (done) {
var rs = new Readable();
rs.push(new Buffer(testSpec['raw_packet'], 'base64'));
rs.push(null);
let ctx = {
testSpec,
done,
expectedCalls: 1,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});

it('should parse binary sbp and payload with leading extra preamble', function (done) {
var rs = new Readable();
rs.push(new Buffer([0x55]));
let expectedCalls = 0
let bufLength = 0;
while (bufLength < 500) {
var buf = new Buffer(testSpec['raw_packet'], 'base64');
rs.push(buf);
bufLength += buf.length;
expectedCalls++
}
rs.push(null);
it('should parse binary sbp and payload with leading extra preamble', function (done) {
var rs = new Readable();
rs.push(new Buffer([0x55]));
let expectedCalls = 0
let bufLength = 0;
while (bufLength < 500) {
var buf = new Buffer(testSpec['raw_packet'], 'base64');
rs.push(buf);
bufLength += buf.length;
expectedCalls++
}
rs.push(null);

let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});
let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});

// For both "corrupt preamble" tests, the corrupt "length" field could be much longer than the actual message.
// In a real-world case we will have a constant stream of data which will allow us to read that
// full length, and reframe after discovering that it's a corrupt preamble.
// In these cases, we just repeat the message several times to create an arbitrarily long stream.
it('should parse binary sbp and payload with leading extra preamble (2)', function (done) {
var rs = new Readable();
var bigBuf = new Buffer(0);
let expectedCalls = 0;
while (bigBuf.length < 500) {
bigBuf = Buffer.concat([bigBuf, new Buffer(testSpec['raw_packet'], 'base64')]);
expectedCalls++
}
rs.push(Buffer.concat([new Buffer([0x55]), bigBuf]));
rs.push(null);
// For both "corrupt preamble" tests, the corrupt "length" field could be much longer than the actual message.
// In a real-world case we will have a constant stream of data which will allow us to read that
// full length, and reframe after discovering that it's a corrupt preamble.
// In these cases, we just repeat the message several times to create an arbitrarily long stream.
it('should parse binary sbp and payload with leading extra preamble (2)', function (done) {
var rs = new Readable();
var bigBuf = new Buffer(0);
let expectedCalls = 0;
while (bigBuf.length < 500) {
bigBuf = Buffer.concat([bigBuf, new Buffer(testSpec['raw_packet'], 'base64')]);
expectedCalls++
}
rs.push(Buffer.concat([new Buffer([0x55]), bigBuf]));
rs.push(null);

let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});
let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});

it('should parse binary sbp and payload with leading truncated message', function (done) {
var rs = new Readable();
var packetBuf = new Buffer(testSpec['raw_packet'], 'base64');
rs.push(packetBuf.slice(0,packetBuf.length-5));
if (filename.indexOf('test_MsgFlashDone.yaml') === -1 &&
filename.indexOf('test_MsgM25FlashWriteStatus.yaml') === -1 &&
filename.indexOf('test_MsgBootloaderJumptoApp.yaml') === -1) {
it('should parse binary sbp and payload with leading truncated message', function (done) {
var rs = new Readable();
var packetBuf = new Buffer(testSpec['raw_packet'], 'base64');
rs.push(packetBuf.slice(0,packetBuf.length-5));

var requiredCalls = 1;
var requiredCalls = 1;

// `length` is longer than one full buffer for these corrupted messages
// similar issue to the above tests
if (filename.indexOf('test_MsgUartState.yaml') !== -1) {
requiredCalls = 10;
}
// `length` is longer than one full buffer for these corrupted messages
// similar issue to the above tests
if (filename.indexOf('test_MsgUartState.yaml') !== -1 ||
filename.indexOf('test_MsgSsrOrbitClockDepA.yaml') !== -1 ||
filename.indexOf('test_MsgSettingsReadByIndexReq.yaml') !== -1 ||
filename.indexOf('test_MsgResetFilters.yaml') !== -1 ||
filename.indexOf('test_MsgLinuxSysState.yaml') !== -1 ||
filename.indexOf('test_MsgStmUniqueIdResp.yaml') !== -1) {
requiredCalls = 10;
}

var expectedCalls;
for (expectedCalls = 0; expectedCalls < requiredCalls; expectedCalls++) {
rs.push(packetBuf);
}
rs.push(null);
var expectedCalls;
for (expectedCalls = 0; expectedCalls < requiredCalls; expectedCalls++) {
rs.push(packetBuf);
}
rs.push(null);

let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
let ctx = {
testSpec,
done,
expectedCalls,
callbacks: 0,
};
dispatch(rs, dispatchee.bind(ctx));
});
}
});
});
});
}
});
});
});
45 changes: 0 additions & 45 deletions rust/sbp/tests/integration/auto_check_sbp_gnss_gnss_structs.rs

This file was deleted.

This file was deleted.

Loading
Loading