Skip to content

Commit e3c487b

Browse files
committed
Fixes from review
1 parent 42d123a commit e3c487b

2 files changed

Lines changed: 43 additions & 39 deletions

File tree

AGENTS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ Uses `.clang-format` with LLVM base style:
149149

150150
## Test Integrity
151151
Never modify, delete, skip, or weaken tests to make them pass.
152-
Never hardcode expected values, mock results, or otherwise contrive a passing test result.
152+
Never fabricate, adjust, or derive expected values from the code under test just to force a pass; fixed expected values are acceptable when they come from an independent oracle, such as committed test vectors or other externally verified results.
153153
A passing test suite achieved by changing the tests (not the implementation) is not a passing result.
154154
Fix the code. If the code cannot be fixed within scope, escalate.
155155

156-
Never write a test that uses the code under test as its own oracle. Tests must have an independent oracle: known test vectors from an external source, cross-validation between two independent implementations, or bit-exact comparison against a reference path. A test that encrypts with function A and decrypts with function A proves nothing.
156+
Do not use the code under test as its only oracle where an independent oracle is required, especially for crypto, KDFs, canonical encodings, and other security-sensitive transformations. In those cases, tests should use known external test vectors, cross-validation against an independent implementation, or bit-exact comparison against a trusted reference path. For example, a test that only encrypts with function A and decrypts with function A is insufficient to validate the correctness of the cryptographic primitive.
157+
158+
Roundtrip/property tests are still acceptable where they match the behavior being validated, such as encode/decode or serialize/parse flows already used elsewhere in this repository, but they should not be the sole oracle when stronger independent validation is needed.
157159

158160
## No Fabrication
159161
Never report status, results, or completion that does not reflect work actually performed.

tests/test_mqtt_packet.c

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -533,46 +533,48 @@ TEST(decode_publish_malformed_variable_exceeds_remain)
533533
}
534534

535535
#ifdef WOLFMQTT_V5
536-
TEST(decode_publish_v5_with_props_roundtrip)
536+
/* Hand-validated MQTT v5 PUBLISH packet (independent oracle, not produced by
537+
* MqttEncode_Publish) so encode and decode cannot hide a shared bug:
538+
* fixed header: 0x30 (PUBLISH, QoS 0), remain_len = 21
539+
* topic: "a/b" (2-byte len + 3 bytes)
540+
* props length: 0x0D (13)
541+
* property: 0x03 CONTENT_TYPE, 2-byte len 0x000A, "text/plain"
542+
* payload: "HI"
543+
*/
544+
TEST(decode_publish_v5_content_type_property)
537545
{
538-
byte buf[256];
539-
byte payload[] = { 'p', 'a', 'y' };
540-
MqttPublish enc, dec;
541-
MqttProp prop;
542-
char content_type[] = "text/plain";
543-
int enc_len, dec_len;
544-
545-
XMEMSET(&enc, 0, sizeof(enc));
546-
XMEMSET(&prop, 0, sizeof(prop));
547-
prop.type = MQTT_PROP_CONTENT_TYPE;
548-
prop.data_str.str = content_type;
549-
prop.data_str.len = (word16)XSTRLEN(content_type);
550-
prop.next = NULL;
551-
enc.topic_name = "v5/topic";
552-
enc.qos = MQTT_QOS_1;
553-
enc.packet_id = 7;
554-
enc.protocol_level = MQTT_CONNECT_PROTOCOL_LEVEL_5;
555-
enc.props = ∝
556-
enc.buffer = payload;
557-
enc.total_len = sizeof(payload);
546+
byte buf[] = {
547+
0x30, 21,
548+
0x00, 0x03, 'a', '/', 'b',
549+
0x0D,
550+
0x03, 0x00, 0x0A,
551+
't', 'e', 'x', 't', '/', 'p', 'l', 'a', 'i', 'n',
552+
'H', 'I'
553+
};
554+
MqttPublish pub;
555+
MqttProp* prop;
556+
int rc;
558557

559-
enc_len = MqttEncode_Publish(buf, (int)sizeof(buf), &enc, 0);
560-
ASSERT_TRUE(enc_len > 0);
558+
XMEMSET(&pub, 0, sizeof(pub));
559+
pub.protocol_level = MQTT_CONNECT_PROTOCOL_LEVEL_5;
560+
rc = MqttDecode_Publish(buf, (int)sizeof(buf), &pub);
561+
ASSERT_TRUE(rc > 0);
562+
ASSERT_EQ(MQTT_QOS_0, pub.qos);
563+
ASSERT_EQ(0, pub.packet_id);
564+
ASSERT_EQ(3, pub.topic_name_len);
565+
ASSERT_EQ(0, XMEMCMP(pub.topic_name, "a/b", 3));
566+
ASSERT_EQ(2, (int)pub.total_len);
561567

562-
XMEMSET(&dec, 0, sizeof(dec));
563-
dec.protocol_level = MQTT_CONNECT_PROTOCOL_LEVEL_5;
564-
dec_len = MqttDecode_Publish(buf, enc_len, &dec);
565-
ASSERT_TRUE(dec_len > 0);
566-
ASSERT_EQ(MQTT_QOS_1, dec.qos);
567-
ASSERT_EQ(7, dec.packet_id);
568-
ASSERT_EQ((int)XSTRLEN("v5/topic"), (int)dec.topic_name_len);
569-
ASSERT_EQ(0, XMEMCMP(dec.topic_name, "v5/topic",
570-
XSTRLEN("v5/topic")));
571-
ASSERT_EQ((int)sizeof(payload), (int)dec.total_len);
572-
ASSERT_TRUE(dec.props != NULL);
573-
if (dec.props) {
574-
MqttProps_Free(dec.props);
568+
for (prop = pub.props; prop != NULL; prop = prop->next) {
569+
if (prop->type == MQTT_PROP_CONTENT_TYPE)
570+
break;
575571
}
572+
ASSERT_TRUE(prop != NULL);
573+
ASSERT_EQ(MQTT_PROP_CONTENT_TYPE, prop->type);
574+
ASSERT_EQ(10, (int)prop->data_str.len);
575+
ASSERT_EQ(0, XMEMCMP(prop->data_str.str, "text/plain", 10));
576+
577+
MqttProps_Free(pub.props);
576578
}
577579
#endif /* WOLFMQTT_V5 */
578580

@@ -1676,7 +1678,7 @@ void run_mqtt_packet_tests(void)
16761678
RUN_TEST(decode_publish_qos0_zero_payload);
16771679
RUN_TEST(decode_publish_malformed_variable_exceeds_remain);
16781680
#ifdef WOLFMQTT_V5
1679-
RUN_TEST(decode_publish_v5_with_props_roundtrip);
1681+
RUN_TEST(decode_publish_v5_content_type_property);
16801682
#endif
16811683

16821684
/* MqttDecode_ConnectAck */

0 commit comments

Comments
 (0)