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

[YAML] Allow hex: prefix for octet strings #18996

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
{{else if (isCharString type)}}
chip::Span<const char>("{{definedValue}}garbage: not in length on purpose", {{utf8StringLength definedValue}});
{{else if (isOctetString type)}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral definedValue}}garbage: not in length on purpose"), {{definedValue.length}});
{{~#if (isHexString definedValue)}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringFromHexString definedValue}}garbage: not in length on purpose"), {{octetStringLengthFromHexString definedValue}});
{{else}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral definedValue}}garbage: not in length on purpose"), {{definedValue.length}});
{{/if}}
{{else}}
{{#if_is_strongly_typed_bitmap type}}
static_cast<{{zapTypeToEncodableClusterObjectType type ns=ns}}>({{asTypedLiteral definedValue type}});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@
VerifyOrReturn(CheckValue{{#if (isString type)}}AsString{{/if}}("{{label}}", {{actual}},
{{~#if (chip_tests_variables_has expected)}}{{expected}}
{{else if keepAsExpected}}{{expected}}
{{else if (isOctetString type)}}chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}})
{{else if (isOctetString type)}}
{{~#if (isHexString expected)}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringFromHexString expected}}"), {{octetStringLengthFromHexString expected}})
{{else}}
chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}})
{{/if}}
{{else if (isCharString type)}}chip::CharSpan("{{expected}}", {{utf8StringLength expected}})
{{else if (chip_tests_config_has expected)}}m{{asUpperCamelCase expected}}.HasValue() ? m{{asUpperCamelCase expected}}.Value() : {{asTypedLiteral (chip_tests_config_get_default_value expected) (chip_tests_config_get_type expected)}}
{{else}}{{asTypedExpression expected type}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
{{else}}
VerifyOrReturn(CheckValue{{#if (isString type)}}AsString{{/if}}("{{label}}", {{actual}},
{{~#if (chip_tests_variables_has expected)}}{{expected}}
{{~else if (isOctetString type)}}[[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral expected}}" length:{{expected.length}}]
{{~else if (isOctetString type)}}
{{~#if (isHexString expected)}}
[[NSData alloc] initWithBytes:"{{octetStringFromHexString expected}}" length:{{octetStringLengthFromHexString expected}}]
{{else}}
[[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral expected}}" length:{{expected.length}}]
{{/if}}
{{~else if (isCharString type)}}@"{{expected}}"
{{else if (chip_tests_config_has expected)}}m{{asUpperCamelCase expected}}.HasValue() ? m{{asUpperCamelCase expected}}.Value() : {{asTypedLiteral (chip_tests_config_get_default_value expected) (chip_tests_config_get_type expected)}}
{{~else}}{{asTypedExpressionFromObjectiveC expected type}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
{{#if (isCharString type)}}
@"{{definedValue}}";
{{else if (isOctetString type)}}
{{~#if (isHexString definedValue)}}
[[NSData alloc] initWithBytes:"{{octetStringFromHexString definedValue}}" length:{{octetStringLengthFromHexString definedValue}}];
{{else}}
[[NSData alloc] initWithBytes:"{{octetStringEscapedForCLiteral definedValue}}" length:{{definedValue.length}}];
{{/if}}
{{else}}
[NSNumber numberWith{{asObjectiveCNumberType definedValue type false}}:{{asTypedExpressionFromObjectiveC definedValue type}}];
{{/if}}
Expand Down
12 changes: 12 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,18 @@ tests:
response:
value: "Tes\x00ti\x00ng"

- label: "Write attribute OCTET_STRING with hex: format"
command: "writeAttribute"
attribute: "octet_string"
arguments:
value: "hex:000102030405"

- label: "Read attribute OCTET_STRING with hex: format"
command: "readAttribute"
attribute: "octet_string"
response:
value: "hex:000102030405"

- label: "Write attribute OCTET_STRING with weird chars"
command: "writeAttribute"
attribute: "octet_string"
Expand Down
32 changes: 32 additions & 0 deletions src/app/zap-templates/common/ClusterTestGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const kPICSName = 'PICS';
const kSaveAsName = 'saveAs';
const kFabricFiltered = 'fabricFiltered';

const kHexPrefix = 'hex:';

class NullObject {
toString()
{
Expand Down Expand Up @@ -826,6 +828,33 @@ function isLiteralNull(value, options)
return (value === null) || (value instanceof NullObject);
}

function isHexString(value)
{
return value && value.startsWith(kHexPrefix);
}

function octetStringFromHexString(value)
{
const hexString = value.substring(kHexPrefix.length);

if (hexString.length % 2) {
throw new Error("The provided hexadecimal string contains an even number of characters");
}

if (!(/^[0-9a-fA-F]+$/.test(hexString))) {
throw new Error("The provided hexadecimal string contains invalid hexadecimal character.");
}

const bytes = hexString.match(/(..)/g);
return bytes.map(byte => '\\x' + byte).join('');
}

function octetStringLengthFromHexString(value)
{
const hexString = value.substring(kHexPrefix.length);
return (hexString.length / 2);
}

function octetStringEscapedForCLiteral(value)
{
// Escape control characters, things outside the ASCII range, and single
Expand Down Expand Up @@ -996,3 +1025,6 @@ exports.chip_tests_only_cluster_commands = chip_tests_only_cluster_co
exports.chip_tests_only_cluster_command_parameters = chip_tests_only_cluster_command_parameters;
exports.chip_tests_only_cluster_responses = chip_tests_only_cluster_responses;
exports.chip_tests_only_cluster_response_parameters = chip_tests_only_cluster_response_parameters;
exports.isHexString = isHexString;
exports.octetStringLengthFromHexString = octetStringLengthFromHexString;
exports.octetStringFromHexString = octetStringFromHexString;
Loading