-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
base_types_tests.rs
376 lines (320 loc) · 13.5 KB
/
base_types_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (c) 2021, Facebook, Inc. and its affiliates
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
#![allow(clippy::blacklisted_name)]
use std::str::FromStr;
use base64ct::{Base64, Encoding};
use move_binary_format::file_format;
use crate::crypto::{get_key_pair_from_bytes, AuthoritySignature, KeyPair};
use crate::{
crypto::{get_key_pair, BcsSignable, Signature},
gas_coin::GasCoin,
object::Object,
SUI_FRAMEWORK_ADDRESS,
};
use super::*;
#[derive(Serialize, Deserialize)]
struct Foo(String);
impl BcsSignable for Foo {}
#[derive(Serialize, Deserialize)]
struct Bar(String);
impl BcsSignable for Bar {}
#[test]
fn test_signatures() {
let (addr1, sec1) = get_key_pair();
let (addr2, _sec2) = get_key_pair();
let foo = Foo("hello".into());
let foox = Foo("hellox".into());
let bar = Bar("hello".into());
let s = Signature::new(&foo, &sec1);
assert!(s.verify(&foo, addr1).is_ok());
assert!(s.verify(&foo, addr2).is_err());
assert!(s.verify(&foox, addr1).is_err());
assert!(s.verify(&bar, addr1).is_err());
}
#[test]
fn test_max_sequence_number() {
let max = SequenceNumber::MAX;
assert_eq!(max.0 * 2 + 1, std::u64::MAX);
}
#[test]
fn test_gas_coin_ser_deser_roundtrip() {
let id = ObjectID::random();
let coin = GasCoin::new(id, SequenceNumber::new(), 10);
let coin_bytes = coin.to_bcs_bytes();
let deserialized_coin: GasCoin = bcs::from_bytes(&coin_bytes).unwrap();
assert_eq!(deserialized_coin.id(), coin.id());
assert_eq!(deserialized_coin.value(), coin.value());
assert_eq!(deserialized_coin.version(), coin.version());
}
#[test]
fn test_increment_version() {
let id = ObjectID::random();
let version = SequenceNumber::from(257);
let value = 10;
let coin = GasCoin::new(id, version, value);
assert_eq!(coin.id(), &id);
assert_eq!(coin.value(), value);
assert_eq!(coin.version(), version);
let mut coin_obj = coin.to_object();
assert_eq!(&coin_obj.id(), coin.id());
assert_eq!(coin_obj.version(), coin.version());
// update contents, which should increase sequence number, but leave
// everything else the same
let old_contents = coin_obj.contents().to_vec();
let old_type_specific_contents = coin_obj.type_specific_contents().to_vec();
coin_obj.update_contents(old_contents);
assert_eq!(coin_obj.version(), version.increment());
assert_eq!(&coin_obj.id(), coin.id());
assert_eq!(
coin_obj.type_specific_contents(),
old_type_specific_contents
);
assert!(GasCoin::try_from(&coin_obj).unwrap().value() == coin.value());
}
#[test]
fn test_object_id_conversions() {}
#[test]
fn test_object_id_display() {
let hex = "ca843279e3427144cead5e4d5999a3d05999a3d0";
let upper_hex = "CA843279E3427144CEAD5E4D5999A3D05999A3D0";
let id = ObjectID::from_hex(hex).unwrap();
assert_eq!(format!("{:?}", id), format!("0x{hex}"));
assert_eq!(format!("{:X}", id), upper_hex);
assert_eq!(format!("{:x}", id), hex);
assert_eq!(format!("{:#x}", id), format!("0x{hex}"));
assert_eq!(format!("{:#X}", id), format!("0x{upper_hex}"));
}
#[test]
fn test_object_id_str_lossless() {
let id = ObjectID::from_hex("0000000000c0f1f95c5b1c5f0eda533eff269000").unwrap();
let id_empty = ObjectID::from_hex("0000000000000000000000000000000000000000").unwrap();
let id_one = ObjectID::from_hex("0000000000000000000000000000000000000001").unwrap();
assert_eq!(id.short_str_lossless(), "c0f1f95c5b1c5f0eda533eff269000",);
assert_eq!(id_empty.short_str_lossless(), "0",);
assert_eq!(id_one.short_str_lossless(), "1",);
}
#[test]
fn test_object_id_from_hex_literal() {
let hex_literal = "0x1";
let hex = "0000000000000000000000000000000000000001";
let obj_id_from_literal = ObjectID::from_hex_literal(hex_literal).unwrap();
let obj_id = ObjectID::from_hex(hex).unwrap();
assert_eq!(obj_id_from_literal, obj_id);
assert_eq!(hex_literal, obj_id.to_hex_literal());
// Missing '0x'
ObjectID::from_hex_literal(hex).unwrap_err();
// Too long
ObjectID::from_hex_literal("0x10000000000000000000000000000000000000000000000000000000001")
.unwrap_err();
}
#[test]
fn test_object_id_ref() {
let obj_id = ObjectID::new([1u8; ObjectID::LENGTH]);
let _: &[u8] = obj_id.as_ref();
}
#[test]
fn test_object_id_from_proto_invalid_length() {
let bytes = vec![1; 123];
ObjectID::from_bytes(bytes).unwrap_err();
}
#[test]
fn test_object_id_deserialize_from_json_value() {
let obj_id = ObjectID::random();
let json_value = serde_json::to_value(obj_id).expect("serde_json::to_value fail.");
let obj_id2: ObjectID =
serde_json::from_value(json_value).expect("serde_json::from_value fail.");
assert_eq!(obj_id, obj_id2)
}
#[test]
fn test_object_id_serde_json() {
let hex = "0xca843279e342714123456784cead5e4d5999a3d0";
let json_hex = "\"0xca843279e342714123456784cead5e4d5999a3d0\"";
let obj_id = ObjectID::from_hex_literal(hex).unwrap();
let json = serde_json::to_string(&obj_id).unwrap();
let json_obj_id: ObjectID = serde_json::from_str(json_hex).unwrap();
assert_eq!(json, json_hex);
assert_eq!(obj_id, json_obj_id);
}
#[test]
fn test_object_id_serde_not_human_readable() {
let obj_id = ObjectID::random();
let serialized = bincode::serialize(&obj_id).unwrap();
assert_eq!(obj_id.0.to_vec(), serialized);
let deserialized: ObjectID = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized, obj_id);
}
#[test]
fn test_object_id_serde_with_expected_value() {
let object_id_vec = vec![
71, 183, 32, 230, 10, 187, 253, 56, 195, 142, 30, 23, 38, 201, 102, 0, 130, 240, 199, 52,
];
let object_id = ObjectID::try_from(object_id_vec.clone()).unwrap();
let json_serialized = serde_json::to_string(&object_id).unwrap();
let bcs_serialized = bcs::to_bytes(&object_id).unwrap();
let expected_json_address = "\"0x47b720e60abbfd38c38e1e1726c9660082f0c734\"";
assert_eq!(expected_json_address, json_serialized);
assert_eq!(object_id_vec, bcs_serialized);
}
#[test]
fn test_object_id_zero_padding() {
let hex = "0x2";
let long_hex = "0x0000000000000000000000000000000000000002";
let long_hex_alt = "0000000000000000000000000000000000000002";
let obj_id_1 = ObjectID::from_hex_literal(hex).unwrap();
let obj_id_2 = ObjectID::from_hex_literal(long_hex).unwrap();
let obj_id_3 = ObjectID::from_hex(long_hex_alt).unwrap();
let obj_id_4: ObjectID = serde_json::from_str(&format!("\"{}\"", hex)).unwrap();
let obj_id_5: ObjectID = serde_json::from_str(&format!("\"{}\"", long_hex)).unwrap();
let obj_id_6: ObjectID = serde_json::from_str(&format!("\"{}\"", long_hex_alt)).unwrap();
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_1.0);
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_2.0);
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_3.0);
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_4.0);
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_5.0);
assert_eq!(SUI_FRAMEWORK_ADDRESS, obj_id_6.0);
}
#[test]
fn test_address_display() {
let hex = "ca843279e3427144cead5e4d5999a3d05999a3d0";
let upper_hex = "CA843279E3427144CEAD5E4D5999A3D05999A3D0";
let id = SuiAddress::from_str(hex).unwrap();
assert_eq!(format!("{:?}", id), format!("0x{hex}"));
assert_eq!(format!("{:X}", id), upper_hex);
assert_eq!(format!("{:x}", id), hex);
assert_eq!(format!("{:#x}", id), format!("0x{hex}"));
assert_eq!(format!("{:#X}", id), format!("0x{upper_hex}"));
}
#[test]
fn test_address_serde_not_human_readable() {
let address = SuiAddress::random_for_testing_only();
let serialized = bincode::serialize(&address).unwrap();
let bcs_serialized = bcs::to_bytes(&address).unwrap();
// bincode use 8 bytes for BYTES len and bcs use 1 byte
assert_eq!(serialized, bcs_serialized);
assert_eq!(address.0, serialized[..]);
let deserialized: SuiAddress = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized, address);
}
#[test]
fn test_address_serde_human_readable() {
let address = SuiAddress::random_for_testing_only();
let serialized = serde_json::to_string(&address).unwrap();
assert_eq!(format!("\"0x{}\"", hex::encode(address)), serialized);
let deserialized: SuiAddress = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, address);
}
#[test]
fn test_address_serde_with_expected_value() {
let address_vec = vec![
42, 202, 201, 60, 233, 75, 103, 251, 224, 56, 148, 252, 58, 57, 61, 244, 92, 124, 211, 191,
];
let address = SuiAddress::try_from(address_vec.clone()).unwrap();
let json_serialized = serde_json::to_string(&address).unwrap();
let bcs_serialized = bcs::to_bytes(&address).unwrap();
let expected_json_address = "\"0x2acac93ce94b67fbe03894fc3a393df45c7cd3bf\"";
assert_eq!(expected_json_address, json_serialized);
assert_eq!(address_vec, bcs_serialized);
}
#[test]
fn test_transaction_digest_serde_not_human_readable() {
let digest = TransactionDigest::random();
let serialized = bincode::serialize(&digest).unwrap();
let bcs_serialized = bcs::to_bytes(&digest).unwrap();
// bincode use 8 bytes for BYTES len and bcs use 1 byte
assert_eq!(serialized[8..], bcs_serialized[1..]);
assert_eq!(digest.0.to_vec(), serialized[8..]);
let deserialized: TransactionDigest = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized, digest);
}
#[test]
fn test_transaction_digest_serde_human_readable() {
let digest = TransactionDigest::random();
let serialized = serde_json::to_string(&digest).unwrap();
assert_eq!(
format!("\"{}\"", Base64::encode_string(&digest.0)),
serialized
);
let deserialized: TransactionDigest = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, digest);
}
#[test]
fn test_signature_serde_not_human_readable() {
let (_, key) = get_key_pair();
let sig = AuthoritySignature::new(&Foo("some data".to_string()), &key);
let serialized = bincode::serialize(&sig).unwrap();
let bcs_serialized = bcs::to_bytes(&sig).unwrap();
assert_eq!(serialized, bcs_serialized);
assert_eq!(sig.0.to_bytes(), serialized[..]);
let deserialized: AuthoritySignature = bincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized, sig);
}
#[test]
fn test_signature_serde_human_readable() {
let (_, key) = get_key_pair();
let sig = AuthoritySignature::new(&Foo("some data".to_string()), &key);
let serialized = serde_json::to_string(&sig).unwrap();
assert_eq!(
format!("\"{}\"", Base64::encode_string(sig.as_ref())),
serialized
);
let deserialized: AuthoritySignature = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, sig);
}
#[test]
fn test_object_id_from_empty_string() {
assert!(ObjectID::try_from("".to_string()).is_err());
assert!(ObjectID::from_str("").is_err());
}
#[test]
fn test_move_object_size_for_gas_metering() {
let object = Object::with_id_owner_for_testing(
ObjectID::random(),
SuiAddress::random_for_testing_only(),
);
let size = object.object_size_for_gas_metering();
let serialized = bcs::to_bytes(&object).unwrap();
// The result of object_size_for_gas_metering() will be smaller due to not including
// all the metadata data needed for serializing various types.
// If the following assertion breaks, it's likely you have changed MoveObject's fields.
// Make sure to adjust `object_size_for_gas_metering()` to include those changes.
assert_eq!(size + 3, serialized.len());
}
#[test]
fn test_move_package_size_for_gas_metering() {
let module = file_format::empty_module();
let package = Object::new_package(vec![module], TransactionDigest::genesis());
let size = package.object_size_for_gas_metering();
let serialized = bcs::to_bytes(&package).unwrap();
// If the following assertion breaks, it's likely you have changed MovePackage's fields.
// Make sure to adjust `object_size_for_gas_metering()` to include those changes.
assert_eq!(size + 5, serialized.len());
}
// A sample address in hex generated by the current address derivation algorithm.
#[cfg(test)]
const SAMPLE_ADDRESS: &str = "ee0437cf625b77af4d12bff98af1a88332b00638";
// Derive a sample address and public key tuple from KeyPair bytes.
fn derive_sample_address() -> (SuiAddress, KeyPair) {
let (address, pub_key) = get_key_pair_from_bytes(&[
10, 112, 5, 142, 174, 127, 187, 146, 251, 68, 22, 191, 128, 68, 84, 13, 102, 71, 77, 57,
92, 154, 128, 240, 158, 45, 13, 123, 57, 21, 194, 214, 189, 215, 127, 86, 129, 189, 1, 4,
90, 106, 17, 10, 123, 200, 40, 18, 34, 173, 240, 91, 213, 72, 183, 249, 213, 210, 39, 181,
105, 254, 59, 163,
]);
(address, pub_key)
}
// Required to capture address derivation algorithm updates that break some tests and deployments.
#[test]
fn test_address_backwards_compatibility() {
use hex;
let (address, _) = derive_sample_address();
assert_eq!(
address.to_vec(),
hex::decode(SAMPLE_ADDRESS).expect("Decoding failed"),
"If this test broke, then the algorithm for deriving addresses from public keys has \
changed. If this was intentional, please compute a new sample address in hex format \
from `derive_sample_address` and update the SAMPLE_ADDRESS const above with the new \
derived address hex value. Note that existing deployments (i.e. devnet) might \
also require updates if they use fixed values generated by the old algorithm."
);
}