You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let name = ClarityName::try_from(name.to_string()).unwrap();
228
+
229
+
letmut buffer = Vec::new();
230
+
name.consensus_serialize(&mut buffer)
231
+
.unwrap_or_else(|_| panic!("Serialization should succeed for name: {name}"));
232
+
233
+
// Should have length byte followed by the string bytes
234
+
assert_eq!(buffer[0], name.len());
235
+
assert_eq!(&buffer[1..], name.as_bytes());
236
+
237
+
// Test deserialization
238
+
let deserialized = ClarityName::consensus_deserialize(&mut buffer.as_slice()).unwrap();
239
+
assert_eq!(deserialized, name);
240
+
}
241
+
242
+
#[test]
243
+
fntest_clarity_name_serialization_too_long(){
244
+
let name = ClarityName("a".repeat(MAX_STRING_LENasusize + 1));
245
+
letmut buffer = Vec::new();
246
+
let result = name.consensus_serialize(&mut buffer);
247
+
assert!(result.is_err());
248
+
assert_eq!(
249
+
result.unwrap_err().to_string(),
250
+
"Failed to serialize clarity name: too long"
251
+
);
252
+
}
253
+
254
+
// the first byte is the length of the buffer.
255
+
#[test_case(vec![4,0xFF,0xFE,0xFD,0xFC].as_slice(),"Failed to parse Clarity name: could not contruct from utf8";"invalid_utf8")]
256
+
#[test_case(vec![2,b'2',b'i'].as_slice(),"Failed to parse Clarity name: InvalidClarityName(\"ClarityName\", \"2i\")";"invalid_name")]// starts with number
257
+
#[test_case(vec![MAX_STRING_LEN + 1].as_slice(),"Failed to deserialize clarity name: too long";"too_long")]
258
+
#[test_case(vec![3,b'a'].as_slice(),"failed to fill whole buffer";"wrong_length")]
let result = name.consensus_serialize(&mut buffer);
342
+
assert!(result.is_err());
343
+
assert_eq!(
344
+
result.unwrap_err().to_string(),
345
+
format!(
346
+
"Failed to serialize contract name: too short or too long: {}",
347
+
name.len()
348
+
)
349
+
);
350
+
}
351
+
352
+
// the first byte is the length of the buffer.
353
+
#[test_case(vec![4,0xFF,0xFE,0xFD,0xFC].as_slice(),"Failed to parse Contract name: could not construct from utf8";"invalid_utf8")]
354
+
#[test_case(vec![2,b'2',b'i'].as_slice(),"Failed to parse Contract name: InvalidContractName(\"ContractName\", \"2i\")";"invalid_name")]// starts with number
355
+
#[test_case(vec![MAX_STRING_LEN + 1].as_slice(),&format!("Failed to deserialize contract name: too short or too long: {}",MAX_STRING_LEN + 1);"too_long")]
356
+
#[test_case(vec![3,b'a'].as_slice(),"failed to fill whole buffer";"wrong_length")]
0 commit comments