|
| 1 | +// Copyright (C) 2025 Stacks Open Internet Foundation |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
1 | 15 | use std::io::Read;
|
2 | 16 |
|
3 | 17 | use test_case::test_case;
|
4 | 18 |
|
5 | 19 | use crate::errors::CodecError;
|
6 | 20 | use crate::representations::{
|
7 |
| - CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, ClarityName, ContractName, MAX_STRING_LEN, |
| 21 | + ClarityName, ContractName, CONTRACT_MAX_NAME_LENGTH, CONTRACT_MIN_NAME_LENGTH, MAX_STRING_LEN, |
8 | 22 | };
|
9 | 23 | use crate::stacks_common::codec::StacksMessageCodec;
|
10 | 24 |
|
@@ -80,13 +94,6 @@ fn test_clarity_name_serialization(name: &str) {
|
80 | 94 | assert_eq!(deserialized, name);
|
81 | 95 | }
|
82 | 96 |
|
83 |
| -#[test] |
84 |
| -fn test_clarity_name_serialization_too_long() { |
85 |
| - // This test can't be implemented with the current API since |
86 |
| - // ClarityName::try_from would reject oversized strings |
87 |
| - // and we can't construct invalid ClarityName instances directly |
88 |
| -} |
89 |
| - |
90 | 97 | // the first byte is the length of the buffer.
|
91 | 98 | #[test_case(vec![4, 0xFF, 0xFE, 0xFD, 0xFC].as_slice(), "Failed to parse Clarity name: could not contruct from utf8"; "invalid_utf8")]
|
92 | 99 | #[test_case(vec![2, b'2', b'i'].as_slice(), "Failed to parse Clarity name: InvalidClarityName(\"ClarityName\", \"2i\")"; "invalid_name")] // starts with number
|
@@ -138,6 +145,7 @@ fn test_contract_name_valid(name: &str) {
|
138 | 145 | #[test_case("hello}world"; "contains_curly_close")]
|
139 | 146 | #[test_case("hello(world"; "contains_parenthesis_open")]
|
140 | 147 | #[test_case("hello)world"; "contains_parenthesis_close")]
|
| 148 | +#[test_case(&"a".repeat(CONTRACT_MIN_NAME_LENGTH - 1); "too_short")] |
141 | 149 | #[test_case(&"a".repeat(MAX_STRING_LEN as usize + 1); "too_long")]
|
142 | 150 | fn test_contract_name_invalid(name: &str) {
|
143 | 151 | let result = ContractName::try_from(name.to_string());
|
@@ -168,13 +176,19 @@ fn test_contract_name_serialization(name: &str) {
|
168 | 176 | assert_eq!(deserialized, name);
|
169 | 177 | }
|
170 | 178 |
|
171 |
| -#[test_case(&"a".repeat(CONTRACT_MIN_NAME_LENGTH - 1); "too_short")] |
172 | 179 | #[test_case(&"a".repeat(CONTRACT_MAX_NAME_LENGTH + 1); "too_long")]
|
173 |
| -#[test_case(&"a".repeat(MAX_STRING_LEN as usize); "max_string_len")] |
174 |
| -fn test_contract_name_serialization_too_long_or_short(_name: &str) { |
175 |
| - // This test can't be implemented with the current API since |
176 |
| - // ContractName::try_from would reject invalid strings |
177 |
| - // and we can't construct invalid ContractName instances directly |
| 180 | +fn test_contract_name_serialization_too_long_or_short(name: &str) { |
| 181 | + let name = ContractName::try_from(name.to_string()).expect("should parse"); |
| 182 | + let mut buffer = Vec::with_capacity((name.len() + 1) as usize); |
| 183 | + let result = name.consensus_serialize(&mut buffer); |
| 184 | + assert!(result.is_err()); |
| 185 | + assert_eq!( |
| 186 | + result.unwrap_err().to_string(), |
| 187 | + format!( |
| 188 | + "Failed to serialize contract name: too short or too long: {}", |
| 189 | + name.len() |
| 190 | + ) |
| 191 | + ); |
178 | 192 | }
|
179 | 193 |
|
180 | 194 | // the first byte is the length of the buffer.
|
|
0 commit comments