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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
//! The test infrastructure module provides alternative implementations of
//! `HasInitContext`, `HasReceiveContext`, `HasParameter`, `HasStateApi`,
//! and `HasHost` traits intended for testing.
//!
//! They allow writing unit tests directly in contract modules with little to no
//! external tooling, depending on what is required.
//!
//!
//! # Example
//!
//! ```rust
//! # use concordium_std::*;
//! # type MyReturnValue = u64;
//! # type State = u64;
//! // Some contract
//! #[init(contract = "noop")]
//! fn contract_init<S: HasStateApi>(
//! ctx: &impl HasInitContext,
//! state_builder: &mut StateBuilder<S>,
//! ) -> InitResult<State> {
//! // ...
//! # Ok(0)
//! }
//!
//! #[receive(contract = "noop", name = "receive", payable, enable_logger, mutable)]
//! fn contract_receive<S: HasStateApi>(
//! ctx: &impl HasReceiveContext,
//! host: &mut impl HasHost<State, StateApiType = S>,
//! amount: Amount,
//! logger: &mut impl HasLogger,
//! ) -> ReceiveResult<MyReturnValue> {
//! // ...
//! # Ok(0)
//! }
//!
//! #[cfg(test)]
//! mod tests {
//! use super::*;
//! use concordium_std::test_infrastructure::*;
//! #[test]
//! fn test_init() {
//! let mut ctx = TestInitContext::empty();
//! let mut state_builder = TestStateBuilder::new();
//! ctx.set_init_origin(AccountAddress([0u8; 32]));
//! let result = contract_init(&ctx, &mut state_builder);
//! // claim!(...)
//! }
//!
//! #[test]
//! fn test_receive() {
//! let mut ctx = TestReceiveContext::empty();
//! let mut host = TestHost::new(State::new(), TestStateBuilder::new());
//! ctx.set_owner(AccountAddress([0u8; 32]));
//! // ...
//! let mut logger = TestLogger::init();
//! host.setup_mock_entrypoint(
//! ContractAddress {
//! index: 0,
//! subindex: 0,
//! },
//! OwnedEntrypointName::new_unchecked("get".into()),
//! MockFn::returning_ok(MyReturnValue::new()),
//! );
//! let result: ReceiveResult<MyReturnValue> =
//! contract_receive(&ctx, &mut host, Amount::zero(), &mut logger);
//! // claim!(...)
//! }
//! }
//! ```
use self::trie::StateTrie;
use crate::{
boxed::Box,
cell::RefCell,
cmp,
collections::{BTreeMap, BTreeSet},
num,
rc::Rc,
*,
};
use convert::TryInto;
#[cfg(feature = "concordium-quickcheck")]
use quickcheck::*;
mod trie;
/// Placeholder for the context chain meta data.
/// All the fields are optionally set and the getting an unset field will result
/// in test failing.
/// For most cases it is used as part of either
/// [`TestInitContext`](struct.TestInitContext.html) or
/// [`TestReceiveContext`](struct.TestReceiveContext.html).
/// Use only in unit tests!
///
/// Defaults to having all of the fields unset
#[derive(Default, Clone)]
pub struct TestChainMeta {
pub(crate) slot_time: Option<SlotTime>,
}
/// Policy type used by init and receive contexts for testing.
/// This type should not be used directly, but rather through
/// its `HasPolicy` interface.
#[derive(Debug, Clone)]
pub struct TestPolicy {
/// Current position in the vector of policies. Used to implement
/// `next_item`.
position: usize,
policy: Policy<Rc<[(AttributeTag, AttributeValue)]>>,
}
impl TestPolicy {
fn new(policy: OwnedPolicy) -> Self {
let policy = Policy {
identity_provider: policy.identity_provider,
created_at: policy.created_at,
valid_to: policy.valid_to,
items: policy.items.into(),
};
Self {
position: 0,
policy,
}
}
}
/// Placeholder for the common data shared between the `InitContext` and
/// `ReceiveContext`. This type is a technicality, see `InitContext` and
/// `ReceiveContext` for the types to use.
///
/// # Default
/// Defaults to having all the fields unset, and constructing
/// [`TestChainMeta`](struct.TestChainMeta.html) using default.
#[derive(Default, Clone)]
#[doc(hidden)]
pub struct TestCommonData<'a> {
pub(crate) metadata: TestChainMeta,
pub(crate) parameter: Option<&'a [u8]>,
/// Policy of the creator. We keep the `Option` wrapper
/// in order that the user can be warned that they are using a policy.
/// Thus there is a distinction between `Some(Vec::new())` and `None`.
pub(crate) policies: Option<Vec<TestPolicy>>,
}
/// Context used for testing. The type parameter C is used to determine whether
/// this will be an init or receive context.
#[derive(Default, Clone)]
pub struct TestContext<'a, C> {
pub(crate) common: TestCommonData<'a>,
pub(crate) custom: C,
}
/// Placeholder for the initial context. All the fields can be set optionally
/// and the getting an unset field will result in calling
/// [`fail!`](../macro.fail.html). Use only in tests!
///
/// # Setters
/// Every field has a setter function prefixed with `set_`.
/// ### Example
/// Creating an empty context and setting the `init_origin`.
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let mut ctx = TestInitContext::empty();
/// ctx.set_init_origin(AccountAddress([0u8; 32]));
/// ```
/// ## Set chain meta data
/// Chain meta data is set using setters on the context or by setters on a
/// mutable reference of [`TestChainMeta`](struct.TestChainMeta.html).
///
/// ### Example
/// Creating an empty context and setting the `slot_time` metadata.
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let mut ctx = TestInitContext::empty();
/// ctx.set_metadata_slot_time(Timestamp::from_timestamp_millis(1609459200));
/// ```
/// or
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let mut ctx = TestInitContext::empty();
/// ctx.metadata_mut().set_slot_time(Timestamp::from_timestamp_millis(1609459200));
/// ```
///
/// # Use case example
///
/// ```rust
/// # use concordium_std::*;
/// type ParameterType = u64;
/// #[init(contract = "noop", parameter = "ParameterType")]
/// fn contract_init<S: HasStateApi>(
/// ctx: &impl HasInitContext,
/// state_builder: &mut StateBuilder<S>,
/// ) -> InitResult<()> {
/// let init_origin = ctx.init_origin();
/// let parameter: ParameterType = ctx.parameter_cursor().get()?;
/// Ok(())
/// }
///
/// #[cfg(test)]
/// mod tests {
/// use super::*;
/// use concordium_std::test_infrastructure::*;
/// #[test]
/// fn test() {
/// let ctx = TestInitContext::empty();
/// let mut state_builder = TestStateBuilder::new();
/// ctx.set_init_origin(AccountAddress([0u8; 32]));
/// let result = contract_init(&ctx, &mut state_builder);
/// // Reads the init_origin without any problems.
/// // But then fails because the parameter is not set.
/// }
/// }
/// ```
pub type TestInitContext<'a> = TestContext<'a, TestInitOnlyData>;
#[derive(Default)]
#[doc(hidden)]
pub struct TestInitOnlyData {
init_origin: Option<AccountAddress>,
}
/// Placeholder for the receiving context. All the fields can be set optionally
/// and the getting an unset field will result in calling
/// [`fail!`](../macro.fail.html). Use only in tests!
///
/// # Setters
/// Every field have a setter function prefixed with `set_`.
///
/// ### Example
/// Creating an empty context and setting the `init_origin`.
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let owner = AccountAddress([0u8; 32]);
/// let mut ctx = TestReceiveContext::empty();
/// ctx.set_owner(owner);
/// ctx.set_sender(Address::Account(owner));
/// ```
/// ## Set chain meta data
/// Chain meta data is set using setters on the context or by setters on a
/// mutable reference of [`TestChainMeta`](struct.TestChainMeta.html).
///
/// ### Example
/// Creating an empty context and setting the `slot_time` metadata.
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let mut ctx = TestReceiveContext::empty();
/// ctx.set_metadata_slot_time(Timestamp::from_timestamp_millis(1609459200));
/// ```
/// or
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// let mut ctx = TestReceiveContext::empty();
/// ctx.metadata_mut().set_slot_time(Timestamp::from_timestamp_millis(1609459200));
/// ```
///
/// # Use case example
/// Creating a context for running unit tests
/// ```rust
/// # use concordium_std::*;
/// # type ReceiveResult<A> = Result<A, ParseError>;
/// # type State = u64;
/// #[receive(contract = "mycontract", name = "receive", mutable)]
/// fn contract_receive<S: HasStateApi>(
/// ctx: &impl HasReceiveContext,
/// host: &mut impl HasHost<State, StateApiType = S>,
/// ) -> ReceiveResult<()> {
/// ensure!(ctx.sender().matches_account(&ctx.owner()));
/// // ...
/// Ok(())
/// }
///
/// #[cfg(test)]
/// mod tests {
/// use super::*;
/// use concordium_sc_base::test_infrastructure::*;
/// #[test]
/// fn test() {
/// let owner = AccountAddress([0u8; 32]);
/// let mut ctx = TestReceiveContext::empty();
/// ctx.set_owner(owner);
/// ctx.set_sender(Address::Account(owner));
/// // ...
/// let result: ReceiveResult<ActionsTree> = contract_receive(&ctx, 0, &mut logger, state);
/// }
/// }
/// ```
pub type TestReceiveContext<'a> = TestContext<'a, TestReceiveOnlyData>;
#[derive(Default)]
#[doc(hidden)]
pub struct TestReceiveOnlyData {
pub(crate) invoker: Option<AccountAddress>,
pub(crate) self_address: Option<ContractAddress>,
pub(crate) sender: Option<Address>,
pub(crate) owner: Option<AccountAddress>,
pub(crate) named_entrypoint: Option<OwnedEntrypointName>,
}
/// Test parameter cursor.
/// Should not be constructed directly, use [TestReceiveContext] or
/// [TestInitContext].
pub struct TestParameterCursor<'a> {
cursor: Cursor<&'a [u8]>,
}
impl<'a> TestParameterCursor<'a> {
fn new(data: &'a [u8]) -> Self {
TestParameterCursor {
cursor: Cursor::new(data),
}
}
}
impl<'a> AsRef<[u8]> for TestParameterCursor<'a> {
#[inline(always)]
fn as_ref(&self) -> &[u8] { self.cursor.as_ref() }
}
impl<'a> Seek for TestParameterCursor<'a> {
type Err = ();
#[inline(always)]
fn seek(&mut self, seek: SeekFrom) -> Result<u32, Self::Err> { self.cursor.seek(seek) }
#[inline(always)]
fn cursor_position(&self) -> u32 { self.cursor.cursor_position() }
}
impl<'a> Read for TestParameterCursor<'a> {
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> ParseResult<usize> { self.cursor.read(buf) }
}
impl<'a> HasParameter for TestParameterCursor<'a> {}
// Setters for testing-context
impl TestChainMeta {
/// Create an `TestChainMeta` where every field is unset, and getting any of
/// the fields will result in [`fail!`](../macro.fail.html).
pub fn empty() -> Self { Default::default() }
/// Set the block slot time
pub fn set_slot_time(&mut self, value: SlotTime) -> &mut Self {
self.slot_time = Some(value);
self
}
}
impl<'a, C> TestContext<'a, C> {
/// Push a new sender policy to the context.
/// When the first policy is pushed this will set the policy vector
/// to 'Some', even if it was undefined previously.
pub fn push_policy(&mut self, value: OwnedPolicy) -> &mut Self {
if let Some(policies) = self.common.policies.as_mut() {
policies.push(TestPolicy::new(value));
} else {
self.common.policies = Some(vec![TestPolicy::new(value)])
}
self
}
/// Set the policies to be defined, but an empty list.
/// Such a situation can not realistically happen on the chain,
/// but we provide functionality for it in case smart contract
/// writers wish to program defensively.
pub fn empty_policies(&mut self) -> &mut Self {
self.common.policies = Some(Vec::new());
self
}
pub fn set_parameter(&mut self, value: &'a [u8]) -> &mut Self {
self.common.parameter = Some(value);
self
}
/// Get a mutable reference to the chain meta data placeholder
pub fn metadata_mut(&mut self) -> &mut TestChainMeta { &mut self.common.metadata }
/// Set the metadata block slot time
pub fn set_metadata_slot_time(&mut self, value: SlotTime) -> &mut Self {
self.metadata_mut().set_slot_time(value);
self
}
}
impl<'a> TestInitContext<'a> {
/// Create an `TestInitContext` where every field is unset, and getting any
/// of the fields will result in [`fail!`](../macro.fail.html).
pub fn empty() -> Self { Default::default() }
/// Set `init_origin` in the `TestInitContext`
pub fn set_init_origin(&mut self, value: AccountAddress) -> &mut Self {
self.custom.init_origin = Some(value);
self
}
}
impl<'a> TestReceiveContext<'a> {
/// Create a `TestReceiveContext` where every field is unset, and getting
/// any of the fields will result in [`fail!`](../macro.fail.html).
pub fn empty() -> Self { Default::default() }
pub fn set_invoker(&mut self, value: AccountAddress) -> &mut Self {
self.custom.invoker = Some(value);
self
}
pub fn set_self_address(&mut self, value: ContractAddress) -> &mut Self {
self.custom.self_address = Some(value);
self
}
pub fn set_sender(&mut self, value: Address) -> &mut Self {
self.custom.sender = Some(value);
self
}
pub fn set_owner(&mut self, value: AccountAddress) -> &mut Self {
self.custom.owner = Some(value);
self
}
pub fn set_named_entrypoint(&mut self, value: OwnedEntrypointName) -> &mut Self {
self.custom.named_entrypoint = Some(value);
self
}
}
// Error handling when unwrapping
fn unwrap_ctx_field<A>(opt: Option<A>, name: &str) -> A {
match opt {
Some(v) => v,
None => fail!(
"Unset field on test context '{}', make sure to set all the fields necessary for the \
contract",
name
),
}
}
// Getters for testing-context
impl HasChainMetadata for TestChainMeta {
fn slot_time(&self) -> SlotTime { unwrap_ctx_field(self.slot_time, "metadata.slot_time") }
}
pub struct TestIterator {
items: Rc<[(AttributeTag, AttributeValue)]>,
position: usize,
}
impl HasPolicy for TestPolicy {
type Iterator = TestIterator;
fn identity_provider(&self) -> IdentityProvider { self.policy.identity_provider }
fn created_at(&self) -> Timestamp { self.policy.created_at }
fn valid_to(&self) -> Timestamp { self.policy.valid_to }
fn next_item(&mut self, buf: &mut [u8; 31]) -> Option<(AttributeTag, u8)> {
if let Some(item) = self.policy.items.get(self.position) {
let len = item.1.as_ref().len();
buf[0..len].copy_from_slice(item.1.as_ref());
self.position += 1;
Some((item.0, len as u8))
} else {
None
}
}
fn attributes(&self) -> Self::Iterator {
TestIterator {
items: self.policy.items.clone(),
position: 0,
}
}
}
impl Iterator for TestIterator {
type Item = (AttributeTag, AttributeValue);
fn next(&mut self) -> Option<Self::Item> {
let elem = self.items.get(self.position)?;
self.position += 1;
Some(*elem)
}
}
impl<'a, C> HasCommonData for TestContext<'a, C> {
type MetadataType = TestChainMeta;
type ParamType = TestParameterCursor<'a>;
type PolicyIteratorType = crate::vec::IntoIter<TestPolicy>;
type PolicyType = TestPolicy;
fn parameter_cursor(&self) -> Self::ParamType {
TestParameterCursor::new(unwrap_ctx_field(self.common.parameter, "parameter"))
}
fn metadata(&self) -> &Self::MetadataType { &self.common.metadata }
fn policies(&self) -> Self::PolicyIteratorType {
unwrap_ctx_field(self.common.policies.clone(), "policies").into_iter()
}
}
impl<'a> HasInitContext for TestInitContext<'a> {
type InitData = ();
fn open(_data: Self::InitData) -> Self { TestInitContext::default() }
fn init_origin(&self) -> AccountAddress {
unwrap_ctx_field(self.custom.init_origin, "init_origin")
}
}
impl<'a> HasReceiveContext for TestReceiveContext<'a> {
type ReceiveData = ();
fn open(_data: Self::ReceiveData) -> Self { TestReceiveContext::default() }
fn invoker(&self) -> AccountAddress { unwrap_ctx_field(self.custom.invoker, "invoker") }
fn self_address(&self) -> ContractAddress {
unwrap_ctx_field(self.custom.self_address, "self_address")
}
fn sender(&self) -> Address { unwrap_ctx_field(self.custom.sender, "sender") }
fn owner(&self) -> AccountAddress { unwrap_ctx_field(self.custom.owner, "owner") }
fn named_entrypoint(&self) -> OwnedEntrypointName {
unwrap_ctx_field(self.custom.named_entrypoint.clone(), "named_entrypoint")
}
}
/// A logger that simply accumulates all the logged items to be inspected at the
/// end of execution.
pub struct TestLogger {
pub logs: Vec<Vec<u8>>,
}
impl HasLogger for TestLogger {
fn init() -> Self {
Self {
logs: Vec::new(),
}
}
fn log_raw(&mut self, event: &[u8]) -> Result<(), LogError> {
if event.len() > constants::MAX_LOG_SIZE {
return Err(LogError::Malformed);
}
if self.logs.len() >= constants::MAX_NUM_LOGS {
return Err(LogError::Full);
}
self.logs.push(event.to_vec());
Ok(())
}
}
/// Reports back an error to the host when compiled to wasm
/// Used internally, not meant to be called directly by contract writers
#[doc(hidden)]
#[cfg(all(feature = "wasm-test", target_arch = "wasm32"))]
pub fn report_error(message: &str, filename: &str, line: u32, column: u32) {
let msg_bytes = message.as_bytes();
let filename_bytes = filename.as_bytes();
unsafe {
crate::prims::report_error(
msg_bytes.as_ptr(),
msg_bytes.len() as u32,
filename_bytes.as_ptr(),
filename_bytes.len() as u32,
line,
column,
)
};
}
/// Reports back an error to the host when compiled to wasm
/// Used internally, not meant to be called directly by contract writers
#[doc(hidden)]
#[cfg(not(all(feature = "wasm-test", target_arch = "wasm32")))]
pub fn report_error(_message: &str, _filename: &str, _line: u32, _column: u32) {}
#[derive(Debug, PartialEq, Eq)]
/// An error that is raised when operating with `Seek`, `Write`, `Read`, or
/// `HasStateEntry` trait methods of the `TestStateApi` type.
pub enum TestStateError {
/// The computation of the new offset would result in an overflow.
Overflow,
/// An error occurred when writing to the contract state.
Write,
/// The new offset would be out of bounds of the state.
Offset,
/// Some other error occurred.
Default,
/// The entry has been deleted (via delete_prefix).
EntryDeleted,
}
impl Default for TestStateError {
fn default() -> Self { Self::Default }
}
impl From<num::TryFromIntError> for TestStateError {
fn from(_: num::TryFromIntError) -> Self { TestStateError::Overflow }
}
impl From<TestStateError> for ParseError {
fn from(_: TestStateError) -> Self { ParseError::default() }
}
#[derive(Debug, Clone)]
/// A wrapper for the data stored in [`TestStateEntry`], which is used to match
/// the semantics of the host functions. Specifically, it is used to ensure that
/// interactions with a deleted entry result in a error.
pub enum TestStateEntryData {
/// The entry has been deleted.
EntryDeleted,
/// The entry exists and has data.
EntryExists(Vec<u8>),
}
impl TestStateEntryData {
/// Create a new TestStateEntryData::EntryExists with the data given.
pub(crate) fn new_from(data: Vec<u8>) -> Self { Self::EntryExists(data) }
/// Create a new TestStateEntryData::EntryExists with a new Vec.
pub(crate) fn new() -> Self { Self::EntryExists(Vec::new()) }
/// Tries to get the actual data. Returns an error if the entry has been
/// deleted.
pub(crate) fn data(&self) -> Result<&[u8], TestStateError> {
match self {
TestStateEntryData::EntryDeleted => Err(TestStateError::EntryDeleted),
TestStateEntryData::EntryExists(v) => Ok(v),
}
}
/// Tries to get the actual data as mutable. Returns an error if the entry
/// has been deleted.
pub(crate) fn data_mut(&mut self) -> Result<&mut Vec<u8>, TestStateError> {
match self {
TestStateEntryData::EntryDeleted => Err(TestStateError::EntryDeleted),
TestStateEntryData::EntryExists(v) => Ok(v),
}
}
}
impl From<Vec<u8>> for TestStateEntryData {
fn from(data: Vec<u8>) -> Self { Self::new_from(data) }
}
#[derive(Debug)]
/// A state entry used for testing. Implements [`HasStateEntry`].
pub struct TestStateEntry {
pub(crate) cursor: Cursor<Rc<RefCell<TestStateEntryData>>>,
pub(crate) key: Vec<u8>,
pub(crate) state_entry_id: StateEntryId,
}
impl TestStateEntry {
pub(crate) fn open(
data: Rc<RefCell<TestStateEntryData>>,
key: Vec<u8>,
state_entry_id: StateEntryId,
) -> Self {
Self {
cursor: Cursor::new(data),
key,
state_entry_id,
}
}
}
#[derive(Debug, Clone)]
/// A state api used for testing. Implements [`HasStateApi`].
pub struct TestStateApi {
trie: Rc<RefCell<StateTrie>>,
}
impl HasStateApi for TestStateApi {
type EntryType = TestStateEntry;
type IterType = trie::TestStateIter;
fn create_entry(&mut self, key: &[u8]) -> Result<Self::EntryType, StateError> {
self.trie.borrow_mut().create_entry(key)
}
fn lookup_entry(&self, key: &[u8]) -> Option<Self::EntryType> { self.trie.borrow().lookup(key) }
fn delete_entry(&mut self, entry: Self::EntryType) -> Result<(), StateError> {
self.trie.borrow_mut().delete_entry(entry)
}
fn delete_prefix(&mut self, prefix: &[u8]) -> Result<bool, StateError> {
self.trie.borrow_mut().delete_prefix(prefix)
}
fn iterator(&self, prefix: &[u8]) -> Result<Self::IterType, StateError> {
self.trie.borrow().iterator(prefix)
}
fn delete_iterator(&mut self, iter: Self::IterType) {
self.trie.borrow_mut().delete_iterator(iter);
}
}
/// An alias for [`StateMapIter`] that fixes the [`HasStateApi`] type to
/// [`TestStateApi`].
pub type TestStateMapIter<'a, K, V> = StateMapIter<'a, K, V, TestStateApi>;
/// An alias for [`StateMapIterMut`] that fixes the [`HasStateApi`] type to
/// [`TestStateApi`].
pub type TestStateMapIterMut<'a, K, V> = StateMapIterMut<'a, K, V, TestStateApi>;
/// An alias for [`StateSetIter`] that fixes the [`HasStateApi`] type to
/// [`TestStateApi`].
pub type TestStateSetIter<'a, T> = StateSetIter<'a, T, TestStateApi>;
impl TestStateApi {
/// Create a new empty state.
pub fn new() -> Self {
Self {
trie: Rc::new(RefCell::new(StateTrie::new())),
}
}
/// Make a deep clone of the state. Used for rollbacks.
pub(crate) fn clone_deep(&self) -> Self {
Self {
trie: Rc::new(RefCell::new(self.trie.borrow().clone_deep())),
}
}
}
impl Default for TestStateApi {
fn default() -> Self { Self::new() }
}
// Type alias for [`StateBuilder`], which fixes the [`HasHost`] type to
// [`TestStateApi`].
pub type TestStateBuilder = StateBuilder<TestStateApi>;
impl TestStateBuilder {
/// Create a new [`Self`] with an empty [`TestStateApi`].
pub fn new() -> Self { Self::open(TestStateApi::new()) }
}
/// A closure used in tests for mocking calls to
/// [`HasCryptoPrimitives::verify_ed25519_signature`].
#[cfg(not(feature = "crypto-primitives"))]
type MockFnVerifyEd25519 = Box<dyn FnMut(PublicKeyEd25519, SignatureEd25519, &[u8]) -> bool>;
/// A closure used in tests for mocking calls to
/// [`HasCryptoPrimitives::verify_ecdsa_secp256k1_signature`].
#[cfg(not(feature = "crypto-primitives"))]
type MockFnEcdsaSecp256k1 =
Box<dyn FnMut(PublicKeyEcdsaSecp256k1, SignatureEcdsaSecp256k1, [u8; 32]) -> bool>;
/// A closure used in tests for mocking calls to
/// [`HasCryptoPrimitives::hash_sha2_256`],
/// [`HasCryptoPrimitives::hash_sha3_256`], or [`HasCryptoPrimitives::
/// hash_keccak_256`].
#[cfg(not(feature = "crypto-primitives"))]
type MockFnHash<T> = Box<dyn FnMut(&[u8]) -> T>;
/// A [`HasCryptoPrimitives`] implementation used for unit testing smart
/// contracts.
///
/// You can test smart contracts that use the cryptographic primitives in
/// two different ways:
///
/// 1. By setting up mock responses for the functions you need, for example with
/// the `setup_hash_sha_256_mock` method.
/// 2. Or, by using the actual implementations. For this, you need to enable the
/// "crypto-primitives" feature.
pub struct TestCryptoPrimitives {
#[cfg(not(feature = "crypto-primitives"))]
verify_ed25519_signature_mock: RefCell<Option<MockFnVerifyEd25519>>,
#[cfg(not(feature = "crypto-primitives"))]
verify_ecdsa_secp256k1_signature_mock: RefCell<Option<MockFnEcdsaSecp256k1>>,
#[cfg(not(feature = "crypto-primitives"))]
hash_sha2_256_mock: RefCell<Option<MockFnHash<HashSha2256>>>,
#[cfg(not(feature = "crypto-primitives"))]
hash_sha3_256_mock: RefCell<Option<MockFnHash<HashSha3256>>>,
#[cfg(not(feature = "crypto-primitives"))]
hash_keccak_256_mock: RefCell<Option<MockFnHash<HashKeccak256>>>,
}
/// Create a new [`TestCryptoPrimitives`], for which no mocks has been set up.
impl Default for TestCryptoPrimitives {
fn default() -> Self { Self::new() }
}
impl TestCryptoPrimitives {
/// Create a new [`TestCryptoPrimitives`], for which no mocks has been set
/// up.
pub fn new() -> Self {
#[cfg(not(feature = "crypto-primitives"))]
return Self {
verify_ed25519_signature_mock: RefCell::new(None),
verify_ecdsa_secp256k1_signature_mock: RefCell::new(None),
hash_sha2_256_mock: RefCell::new(None),
hash_sha3_256_mock: RefCell::new(None),
hash_keccak_256_mock: RefCell::new(None),
};
#[cfg(feature = "crypto-primitives")]
Self {}
}
#[cfg(not(feature = "crypto-primitives"))]
/// Set up a mock for [`verify_ed25519_signature`][link].
///
/// This is not available if the "crypto-primitives" feature is enabled. For
/// more information on why, see the documentation of
/// [`TestCryptoPrimitives`].
///
/// [link]: HasCryptoPrimitives::verify_ed25519_signature
pub fn setup_verify_ed25519_signature_mock<F>(&self, mock: F)
where
F: FnMut(PublicKeyEd25519, SignatureEd25519, &[u8]) -> bool + 'static, {
*self.verify_ed25519_signature_mock.borrow_mut() = Some(Box::new(mock));
}
#[cfg(not(feature = "crypto-primitives"))]
/// Set up a mock for [`verify_ecdsa_secp256k1_signature`][link].
///
/// This is not available if the "crypto-primitives" feature is enabled. For
/// more information on why, see the documentation of
/// [`TestCryptoPrimitives`].
///
/// [link]: HasCryptoPrimitives::verify_ecdsa_secp256k1_signature
pub fn setup_verify_ecdsa_secp256k1_signature_mock<F>(&self, mock: F)
where
F: FnMut(PublicKeyEcdsaSecp256k1, SignatureEcdsaSecp256k1, [u8; 32]) -> bool + 'static,
{
*self.verify_ecdsa_secp256k1_signature_mock.borrow_mut() = Some(Box::new(mock));
}
#[cfg(not(feature = "crypto-primitives"))]
/// Set up a mock for
/// [`hash_sha2_256`](HasCryptoPrimitives::hash_sha2_256).
///
/// This is not available if the "crypto-primitives" feature is enabled. For
/// more information on why, see the documentation of
/// [`TestCryptoPrimitives`].
pub fn setup_hash_sha2_256_mock<F>(&self, mock: F)
where
F: FnMut(&[u8]) -> HashSha2256 + 'static, {
*self.hash_sha2_256_mock.borrow_mut() = Some(Box::new(mock));
}
#[cfg(not(feature = "crypto-primitives"))]
/// Set up a mock for
/// [`hash_sha3_256`](HasCryptoPrimitives::hash_sha3_256).
///
/// This is not available if the "crypto-primitives" feature is enabled. For
/// more information on why, see the documentation of
/// [`TestCryptoPrimitives`].
pub fn setup_hash_sha3_256_mock<F>(&self, mock: F)
where
F: FnMut(&[u8]) -> HashSha3256 + 'static, {
*self.hash_sha3_256_mock.borrow_mut() = Some(Box::new(mock));
}
#[cfg(not(feature = "crypto-primitives"))]
/// Set up a mock for
/// [`hash_keccak_256`](HasCryptoPrimitives::hash_keccak_256).
///
/// This is not available if the "crypto-primitives" feature is enabled. For
/// more information on why, see the documentation of
/// [`TestCryptoPrimitives`].
pub fn setup_hash_keccak_256_mock<F>(&self, mock: F)
where
F: FnMut(&[u8]) -> HashKeccak256 + 'static, {
*self.hash_keccak_256_mock.borrow_mut() = Some(Box::new(mock));
}
/// Fail with an error message that tells you to set up mocks
/// OR enable the crypto-primitives feature.
#[cfg(not(feature = "crypto-primitives"))]
fn fail_with_crypto_primitives_error(method_name: &str) -> ! {
fail!(
"To use {}, you need to either enable the \"concordium-std/crypto-primitives\" \
feature, which makes it use an actual implemenation, or set up a mock with the \
setup_{}_mock method on TestCryptoPrimitives.",
method_name,
method_name
)
}
}
impl HasCryptoPrimitives for TestCryptoPrimitives {
fn verify_ed25519_signature(
&self,
public_key: PublicKeyEd25519,
signature: SignatureEd25519,
message: &[u8],
) -> bool {
#[cfg(feature = "crypto-primitives")]
{
let signature = ed25519_zebra::Signature::try_from(&signature.0[..]);
let public_key = ed25519_zebra::VerificationKey::try_from(&public_key.0[..]);
match (signature, public_key) {
(Ok(ref signature), Ok(public_key)) => {
public_key.verify(signature, message).is_ok()
}
_ => false,
}
}
#[cfg(not(feature = "crypto-primitives"))]
{
if let Some(ref mut mock) = *self.verify_ed25519_signature_mock.borrow_mut() {
mock(public_key, signature, message)
} else {
Self::fail_with_crypto_primitives_error("verify_ed25519_signature")
}
}
}
fn verify_ecdsa_secp256k1_signature(
&self,
public_key: PublicKeyEcdsaSecp256k1,
signature: SignatureEcdsaSecp256k1,
message_hash: [u8; 32],
) -> bool {
#[cfg(feature = "crypto-primitives")]
{
let signature = secp256k1::ecdsa::Signature::from_compact(&signature.0[..]);
let public_key = secp256k1::PublicKey::from_slice(&public_key.0[..]);
let message_hash = secp256k1::Message::from_slice(&message_hash[..]);
match (signature, public_key, message_hash) {
(Ok(ref signature), Ok(public_key), Ok(message_hash)) => {
let verifier = secp256k1::Secp256k1::verification_only();
verifier.verify_ecdsa(&message_hash, signature, &public_key).is_ok()
}
_ => false,
}
}
#[cfg(not(feature = "crypto-primitives"))]
{
if let Some(ref mut mock) = *self.verify_ecdsa_secp256k1_signature_mock.borrow_mut() {
mock(public_key, signature, message_hash)
} else {
Self::fail_with_crypto_primitives_error("verify_ecdsa_secp256k1")
}
}
}
fn hash_sha2_256(&self, data: &[u8]) -> HashSha2256 {
#[cfg(feature = "crypto-primitives")]
{
use sha2::Digest;
HashSha2256(sha2::Sha256::digest(data).into())
}
#[cfg(not(feature = "crypto-primitives"))]
{
if let Some(ref mut mock) = *self.hash_sha2_256_mock.borrow_mut() {
mock(data)
} else {
Self::fail_with_crypto_primitives_error("hash_sha2_256")
}
}
}
fn hash_sha3_256(&self, data: &[u8]) -> HashSha3256 {
#[cfg(feature = "crypto-primitives")]
{
use sha3::Digest;
HashSha3256(sha3::Sha3_256::digest(data).into())
}
#[cfg(not(feature = "crypto-primitives"))]
{
if let Some(ref mut mock) = *self.hash_sha3_256_mock.borrow_mut() {
mock(data)
} else {
Self::fail_with_crypto_primitives_error("hash_sha3_256")
}
}
}
fn hash_keccak_256(&self, data: &[u8]) -> HashKeccak256 {
#[cfg(feature = "crypto-primitives")]
{
use sha3::Digest;
HashKeccak256(sha3::Keccak256::digest(data).into())
}
#[cfg(not(feature = "crypto-primitives"))]
{
if let Some(ref mut mock) = *self.hash_keccak_256_mock.borrow_mut() {
mock(data)
} else {
Self::fail_with_crypto_primitives_error("hash_keccak_256")
}
}
}
}
impl HasStateEntry for TestStateEntry {
type Error = TestStateError;
type StateEntryData = Rc<RefCell<TestStateEntryData>>;
type StateEntryKey = Vec<u8>;
#[inline(always)]
fn move_to_start(&mut self) { self.cursor.offset = 0; }
/// Get the size of the data in the entry.
/// Returns an error if the entry has been deleted with delete_prefix.
fn size(&self) -> Result<u32, Self::Error> {
Ok(self.cursor.data.borrow().data()?.len() as u32)
}
/// Truncate the entry.
/// Returns an error if the entry has been deleted with delete_prefix.
fn truncate(&mut self, new_size: u32) -> Result<(), Self::Error> {
let cur_size = self.size()?;
if cur_size > new_size {
self.resize(new_size)?;
}
Ok(())
}
/// Get a reference to the key.
fn get_key(&self) -> &[u8] { &self.key }
/// Resize the entry.
/// Returns an error if the entry has been deleted with delete_prefix.
fn resize(&mut self, new_size: u32) -> Result<(), Self::Error> {
let new_size = new_size as usize;
self.cursor.data.borrow_mut().data_mut()?.resize(new_size, 0);
if self.cursor.offset > new_size {
self.cursor.offset = new_size;
}
Ok(())
}
}
impl Read for TestStateEntry {
fn read(&mut self, buf: &mut [u8]) -> ParseResult<usize> {
let mut len = self.cursor.data.borrow().data()?.len() - self.cursor.offset;
if len > buf.len() {
len = buf.len();
}
if len > 0 {
buf[0..len].copy_from_slice(
&self.cursor.data.borrow().data()?[self.cursor.offset..self.cursor.offset + len],
);
self.cursor.offset += len;
Ok(len)
} else {
Ok(0)
}
}
}
impl Write for TestStateEntry {
type Err = TestStateError;
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Err> {
let end = self.cursor.offset + buf.len();
if self.cursor.data.borrow().data()?.len() < end {
self.resize(end.try_into()?)?;
}
let mut cursor_data = self.cursor.data.as_ref().borrow_mut();
let data = &mut cursor_data.data_mut()?[self.cursor.offset..];
let to_write = cmp::min(data.len(), buf.len());
data[..to_write].copy_from_slice(&buf[..to_write]);
self.cursor.offset += to_write;
Ok(to_write)
}
}
impl Seek for TestStateEntry {
type Err = TestStateError;
// TODO: This does _not_ match the semantics of Seek for StateEntry.
fn seek(&mut self, pos: SeekFrom) -> Result<u32, Self::Err> {
use self::TestStateError::*;
// This will fail immediately, if the entry has been deleted.
let len = self.cursor.data.borrow().data()?.len();
match pos {
SeekFrom::Start(x) => {
// We can set the position to just after the end of the current length.
let new_offset = x.try_into()?;
if new_offset <= len {
self.cursor.offset = new_offset;
Ok(x)
} else {
Err(Offset)
}
}
SeekFrom::End(x) => {
// cannot seek beyond end, nor before beginning
if x <= 0 {
let end: u32 = len.try_into()?;
let minus_x = x.checked_abs().ok_or(Overflow)?;
if let Some(new_pos) = end.checked_sub(minus_x.try_into()?) {
self.cursor.offset = new_pos.try_into()?;
Ok(new_pos)
} else {
Err(Offset)
}
} else {
Err(Offset)
}
}
SeekFrom::Current(x) => match x {
0 => Ok(self.cursor.offset.try_into()?),
x if x > 0 => {
let x = x.try_into()?;
let new_pos = self.cursor.offset.checked_add(x).ok_or(Overflow)?;
if new_pos <= len {
self.cursor.offset = new_pos;
new_pos.try_into().map_err(Self::Err::from)
} else {
Err(Offset)
}
}
x => {
let x = (-x).try_into()?;
let new_pos = self.cursor.offset.checked_sub(x).ok_or(Overflow)?;
self.cursor.offset = new_pos;
new_pos.try_into().map_err(Self::Err::from)
}
},
}
}
#[inline(always)]
fn cursor_position(&self) -> u32 { self.cursor.offset as u32 }
}
impl HasCallResponse for Cursor<Vec<u8>> {
fn size(&self) -> u32 { self.data.len() as u32 }
}
/// Holds a function used for mocking invocations of contracts with
/// `invoke_contract`.
pub struct MockFn<State> {
f: TestMockFn<State>,
}
/// The handler for a specific entrypoint. This is a boxed closure for good
/// ergonomics. It does however mean that in practice only closures which don't
/// capture any references may be put into it. This does prevent some example
/// uses, or at least forces them to use awkward patterns. In particular, a test
/// example where the closure would keep track of whether it is called or not
/// via incrementing a local variable is not going to be possible to express.
///
/// This might be improved in the future.
type TestMockFn<State> =
Box<dyn Fn(Parameter, Amount, &mut Amount, &mut State) -> CallContractResult<Cursor<Vec<u8>>>>;
impl<State> MockFn<State> {
/// Create a mock function which has access to `parameter`, `amount`,
/// `balance`, and `state`.
///
/// `parameter` and `amount` correspond to the values used in
/// `invoke_contract(.., parameter, .., amount)`.
/// `balance` and `state` correspond to the values from the contract you are
/// testing. They are used to simulate calls to the contract itself,
/// which can change the balance and state of the contract.
///
/// The function should return a pair (state_modified, return_value), where
/// state_modified should be set to `true`, if the function modifies the
/// state parameter. It should modify the `balance` and `state` in way
/// desired in the test, or in a way that the called contract is
/// intended to behave.
///
/// See also `returning_ok` and `returning_err` for when you need simple
/// mocks.
pub fn new<R, F>(mock_fn_return: F) -> Self
where
R: Serial,
F: Fn(Parameter, Amount, &mut Amount, &mut State) -> CallContractResult<R> + 'static, {
let mock_fn = Box::new(
move |parameter: Parameter, amount: Amount, balance: &mut Amount, state: &mut State| {
match mock_fn_return(parameter, amount, balance, state) {
Ok((modified, return_value)) => {
if let Some(return_value) = return_value {
Ok((modified, Some(Cursor::new(to_bytes(&return_value)))))
} else {
Ok((modified, None))
}
}
Err(e) => match e {
CallContractError::AmountTooLarge => Err(CallContractError::AmountTooLarge),
CallContractError::MissingAccount => Err(CallContractError::MissingAccount),
CallContractError::MissingContract => {
Err(CallContractError::MissingContract)
}
CallContractError::MissingEntrypoint => {
Err(CallContractError::MissingEntrypoint)
}
CallContractError::MessageFailed => Err(CallContractError::MessageFailed),
CallContractError::LogicReject {
reason,
return_value,
} => Err(CallContractError::LogicReject {
reason,
return_value: Cursor::new(to_bytes(&return_value)),
}),
CallContractError::Trap => Err(CallContractError::Trap),
},
}
},
);
Self {
f: mock_fn,
}
}
/// A helper that assumes that a V1 contract is invoked. This means that the
/// return value will **always** be present in case of success.
pub fn new_v1<R, F>(mock_fn_return: F) -> Self
where
R: Serial,
F: Fn(
Parameter,
Amount,
&mut Amount,
&mut State,
) -> Result<(bool, R), CallContractError<R>>
+ 'static, {
Self::new(move |p, a, b, s| {
mock_fn_return(p, a, b, s).map(|(modified, rv)| (modified, Some(rv)))
})
}
/// A helper that assumes that a V0 contract is invoked. This means that the
/// return value will **never** be present in case of success, and hence
/// does not have to be provided by the caller.
pub fn new_v0<R, F>(mock_fn_return: F) -> Self
where
R: Serial,
F: Fn(Parameter, Amount, &mut Amount, &mut State) -> Result<bool, CallContractError<R>>
+ 'static, {
Self::new(move |p, a, b, s| mock_fn_return(p, a, b, s).map(|modified| (modified, None)))
}
/// Create a simple mock function that returns `Ok` with the same
/// value every time, and signals the state is not changed.
pub fn returning_ok<R: Clone + Serial + 'static>(return_value: R) -> Self {
Self::new(move |_parameter, _amount, _balance, _state| -> CallContractResult<R> {
Ok((false, Some(return_value.clone())))
})
}
/// Create a simple mock function that returns `Err` with same error every
/// time.
pub fn returning_err<R: Clone + Serial + 'static>(error: CallContractError<R>) -> Self {
Self::new(
move |_parameter: Parameter,
_amount: Amount,
_balance: &mut Amount,
_state: &mut State|
-> CallContractResult<R> { Err(error.clone()) },
)
}
}
/// A map from contract address and entrypoints to mocking functions.
type MockFnMap<State> = BTreeMap<(ContractAddress, OwnedEntrypointName), MockFn<State>>;
/// A map from module references to the mocked result.
type MockUpgradeMap = BTreeMap<ModuleReference, UpgradeResult>;
/// A [`Host`](HasHost) implementation used for unit testing smart contracts.
///
/// The host provides a way to set up mock responses to transfers, and to
/// contract invocations. This allows testing a specific entrypoint of a
/// contract in isolation, assuming its dependents behave in the way specified
/// by the supplied mock functions.
///
/// Additionally, this host provides some inspection capability so that after
/// execution of an entrypoint tests can observe which accounts or contracts
/// were affected.
pub struct TestHost<State> {
/// Functions that mock responses to calls.
// This is Rc+RefCell because it needs to be cloneable. There might be another way to make the
// MockFn cloneable, but this seemed like the easiest option.
mocking_fns: Rc<RefCell<MockFnMap<State>>>,
/// Transfers the contract has made during its execution.
transfers: RefCell<Vec<(AccountAddress, Amount)>>,
/// The contract balance. This is updated during execution based on contract
/// invocations, e.g., a successful transfer from the contract decreases it.
contract_balance: RefCell<Amount>,
/// The address of the instance. This is used when querying its own balance.
contract_address: Option<ContractAddress>,
/// Map from module references to the mocked results of upgrading to this
/// particular module.
mocking_upgrades: RefCell<MockUpgradeMap>,
/// StateBuilder for the state.
state_builder: StateBuilder<TestStateApi>,
/// State of the instance.
state: State,
/// Account balances, is used when querying the balance of an account.
query_account_balances: RefCell<BTreeMap<AccountAddress, AccountBalance>>,
/// Contract balances, is used when querying the balance of a contract.
query_contract_balances: RefCell<BTreeMap<ContractAddress, Amount>>,
/// Current exchange rates, is used when querying the exchange rates.
query_exchange_rates: Option<ExchangeRates>,
/// List of accounts that will cause a contract invocation to fail.
missing_accounts: BTreeSet<AccountAddress>,
/// List of contracts that will cause a query for contract balance to result
/// in a missing contract error.
missing_contracts: BTreeSet<ContractAddress>,
}
impl<State: Serial + DeserialWithState<TestStateApi>> HasHost<State> for TestHost<State> {
type ReturnValueType = Cursor<Vec<u8>>;
type StateApiType = TestStateApi;
/// Perform a transfer to the given account if the contract has sufficient
/// balance.
///
/// By default, all accounts are assumed to exist, and transfers to them
/// will succeed (provided sufficient balance).
/// Use `make_account_missing` to test out transfers to accounts not on
/// chain.
///
/// Possible errors:
/// - [`TransferError::AmountTooLarge`]: Contract has insufficient funds.
/// - [`TransferError::MissingAccount`]: Attempted transfer to an account
/// set as missing with `make_account_missing`.
fn invoke_transfer(&self, receiver: &AccountAddress, amount: Amount) -> TransferResult {
if self.missing_accounts.contains(receiver) {
return Err(TransferError::MissingAccount);
}
if *self.contract_balance.borrow() >= amount {
*self.contract_balance.borrow_mut() -= amount;
self.transfers.borrow_mut().push((*receiver, amount));
// Update the receiver query balance.
if let Some(balance) = self.query_account_balances.borrow_mut().get_mut(receiver) {
balance.total += amount;
}
Ok(())
} else {
Err(TransferError::AmountTooLarge)
}
}
/// Invoke a contract entrypoint.
///
/// This uses the mock entrypoints set up with
/// `setup_mock_entrypoint`. The method will [fail] with a panic
/// if no responses were set for the given contract address and method.
///
/// If the invocation results in `Err(_)`, the host and state will be rolled
/// back. This means that the state and the logs of, e.g., transactions will
/// look as if the invocation never occurred. See also
/// [`TestHost::with_rollback`].
fn invoke_contract_raw(
&mut self,
to: &ContractAddress,
parameter: Parameter,
method: EntrypointName,
amount: Amount,
) -> CallContractResult<Self::ReturnValueType> {
self.commit_state();
let mocking_fns = self.mocking_fns.clone();
let mut mocking_fns_mut = mocking_fns.borrow_mut();
let handler = match mocking_fns_mut.get_mut(&(*to, OwnedEntrypointName::from(method))) {
Some(handler) => handler,
None => fail!(
"Mocking has not been set up for invoking contract {:?} with method '{}'.",
to,
method
),
};
// Check if the contract has sufficient balance.
if *self.contract_balance.borrow() < amount {
return Err(CallContractError::AmountTooLarge);
}
// Save a checkpoint for rolling back on errors.
let host_checkpoint = self.checkpoint();
// Invoke the handler.
let invocation_res = (handler.f)(
parameter,
amount,
&mut self.contract_balance.borrow_mut(),
&mut self.state,
);
match invocation_res {
Err(error) => {
// Roll back the host.
*self = host_checkpoint;
Err(error)
}
Ok((state_modified, res)) => {
// Update the contract balance if the invocation succeeded.
*self.contract_balance.borrow_mut() -= amount;
// Update the receiver query balance.
if let Some(balance) = self.query_contract_balances.borrow_mut().get_mut(to) {
*balance += amount;
}
// since the caller only modified (in principle) the in-memory state,
// we make sure to persist it to reflect what happens in actual calls
if state_modified {
self.commit_state();
}
Ok((state_modified, res))
}
}
}
/// Invoke a contract entrypoint.
///
/// This uses the mock entrypoints set up with
/// `setup_mock_entrypoint`. The method will [fail] with a panic
/// if no responses were set for the given contract address and method.
///
/// If the invocation results in `Err(_)`, the host and state will be rolled
/// back. This means that the state and the logs of, e.g., transactions will
/// look as if the invocation never occurred. See also
/// [`TestHost::with_rollback`].
fn invoke_contract_raw_read_only(
&self,
to: &ContractAddress,
parameter: Parameter,
method: EntrypointName,
amount: Amount,
) -> ReadOnlyCallContractResult<Self::ReturnValueType> {
let mocking_fns = self.mocking_fns.borrow();
let handler = match mocking_fns.get(&(*to, OwnedEntrypointName::from(method))) {
Some(handler) => handler,
None => fail!(
"Mocking has not been set up for invoking contract {:?} with method '{}'.",
to,
method
),
};
// Check if the contract has sufficient balance.
if amount.micro_ccd > 0 && *self.contract_balance.borrow() < amount {
return Err(CallContractError::AmountTooLarge);
}
// The callee will see state that is stored at this point.
let mut state = match State::deserial_with_state(
&self.state_builder.state_api,
&mut self
.state_builder
.state_api
.lookup_entry(&[])
.expect_report("Could not lookup the state root."),
) {
Ok(state) => state,
Err(e) => fail!("Failed to deserialize state: {:?}", e),
};
// Invoke the handler.
let (state_modified, res) =
(handler.f)(parameter, amount, &mut self.contract_balance.borrow_mut(), &mut state)?;
if state_modified {
fail!("State modified in a read-only contract call.");
}
// Update the contract balance if the invocation succeeded.
*self.contract_balance.borrow_mut() -= amount;
// Update the receiver query balance.
if let Some(balance) = self.query_contract_balances.borrow_mut().get_mut(to) {
*balance += amount;
}
Ok(res)
}
fn account_balance(&self, address: AccountAddress) -> QueryAccountBalanceResult {
if self.missing_accounts.contains(&address) {
Err(QueryAccountBalanceError)
} else if let Some(balances) = self.query_account_balances.borrow().get(&address) {
Ok(*balances)
} else {
fail!("No account balance for {:?} has been set up.", address)
}
}
fn contract_balance(&self, address: ContractAddress) -> QueryContractBalanceResult {
// If the contract address is set and matches, we return the contract balance
// instead.
if Some(address) == self.contract_address {
Ok(self.contract_balance.borrow().to_owned())
} else if self.missing_contracts.contains(&address) {
Err(QueryContractBalanceError)
} else if let Some(balances) = self.query_contract_balances.borrow().get(&address) {
Ok(*balances)
} else {
fail!("No contract balance for {:?} has been set up.", address)
}
}
fn exchange_rates(&self) -> ExchangeRates {
if let Some(exchange_rates) = self.query_exchange_rates {
exchange_rates
} else {
fail!("No exchange rates has been set up")
}
}
fn upgrade(&mut self, module: ModuleReference) -> UpgradeResult {
if let Some(result) = self.mocking_upgrades.borrow().get(&module) {
result.to_owned()
} else {
fail!(
"Mocking has not been set up for upgrading to this particular module reference: \
{:?}",
module
)
}
}
fn commit_state(&mut self) {
let mut root_entry = self
.state_builder
.state_api
.lookup_entry(&[])
.expect_report("commit_state: Cannot lookup state root.");
self.state.serial(&mut root_entry).expect_report("commit_state: Cannot serialize state.");
let new_state_size = root_entry
.size()
.expect_report("commit_state: Cannot get state size. Entry was deleted.");
root_entry
.truncate(new_state_size)
.expect_report("commit_state: Cannot truncate state. Entry was deleted.");
}
/// Get an immutable reference to the contract state.
fn state(&self) -> &State { &self.state }
/// Get a mutable reference to the contract state.
fn state_mut(&mut self) -> &mut State { &mut self.state }
/// Get the contract balance.
/// This can be set with `set_self_balance` and defaults to 0.
fn self_balance(&self) -> Amount { *self.contract_balance.borrow() }
/// Get the state builder.
fn state_builder(&mut self) -> &mut StateBuilder<Self::StateApiType> { &mut self.state_builder }
/// Get the state and the state builder.
fn state_and_builder(&mut self) -> (&mut State, &mut StateBuilder<Self::StateApiType>) {
(&mut self.state, &mut self.state_builder)
}
fn account_public_keys(&self, _address: AccountAddress) -> QueryAccountPublicKeysResult {
unimplemented!(
"The test infrastructure will be deprecated and so does not implement new \
functionality."
)
}
fn check_account_signature(
&self,
_address: AccountAddress,
_signatures: &AccountSignatures,
_data: &[u8],
) -> CheckAccountSignatureResult {
unimplemented!(
"The test infrastructure will be deprecated and so does not implement new \
functionality."
)
}
#[cfg(feature = "p7")]
fn contract_module_reference(
&self,
_address: ContractAddress,
) -> QueryContractModuleReferenceResult {
unimplemented!(
"The test infrastructure will be deprecated and so does not implement new \
functionality."
)
}
#[cfg(feature = "p7")]
fn contract_name(&self, _address: ContractAddress) -> QueryContractNameResult {
unimplemented!(
"The test infrastructure will be deprecated and so does not implement new \
functionality."
)
}
}
impl<State: Serial + DeserialWithState<TestStateApi>> TestHost<State> {
/// Create a new test host. **It is essential that any [`StateMap`],
/// [`StateSet`] or [`StateBox`] that exists in the provided `state` was
/// created with the `state_builder` that is supplied. Otherwise the
/// runtime error in the test.
pub fn new(state: State, mut state_builder: StateBuilder<TestStateApi>) -> Self {
let mut root_entry = state_builder
.state_api
.create_entry(&[])
.expect_report("TestHost::new: Could not store state root.");
state.serial(&mut root_entry).expect_report("TestHost::new: cannot serialize state.");
Self {
mocking_fns: Rc::new(RefCell::new(BTreeMap::new())),
transfers: RefCell::new(Vec::new()),
contract_balance: RefCell::new(Amount::zero()),
contract_address: None,
mocking_upgrades: RefCell::new(BTreeMap::new()),
state_builder,
state,
missing_accounts: BTreeSet::new(),
missing_contracts: BTreeSet::new(),
query_account_balances: RefCell::new(BTreeMap::new()),
query_contract_balances: RefCell::new(BTreeMap::new()),
query_exchange_rates: None,
}
}
/// Retrieve a reference to the underlying state builder.
pub fn state_builder(&mut self) -> &mut StateBuilder<TestStateApi> { &mut self.state_builder }
/// Set up a mock entrypoint for handling calls to `invoke_contract`.
///
/// If multiple handlers for the same entrypoint (to, method) are set up,
/// the latest handler will be used.
pub fn setup_mock_entrypoint(
&mut self,
to: ContractAddress,
method: OwnedEntrypointName,
handler: MockFn<State>,
) {
self.mocking_fns.borrow_mut().insert((to, method), handler);
}
/// Set up a mock for upgrading to a particular module.
///
/// If multiple mocks are setup for the same module, the latest will be
/// used.
pub fn setup_mock_upgrade(&mut self, module: ModuleReference, result: UpgradeResult) {
self.mocking_upgrades.borrow_mut().insert(module, result);
}
/// Set the contract balance.
/// NB: This should be the sum of the contract's initial balance and the
/// amount you wish to invoke it with.
///
/// Example:
/// ```rust
/// # use concordium_std::*;
/// # use concordium_std::test_infrastructure::*;
/// # fn contract_receive<S: HasStateApi>(
/// # ctx: &impl HasReceiveContext,
/// # host: &mut impl HasHost<u64, StateApiType = S>,
/// # amount: Amount,
/// # ) -> ReceiveResult<()> {
/// # Ok(())
/// # }
/// # let mut host = TestHost::new(0u64, TestStateBuilder::new());
/// # let ctx = TestReceiveContext::empty();
/// host.set_self_balance(Amount::from_ccd(10));
/// contract_receive(
/// &ctx,
/// &mut host,
/// // This amount is _not_ added to the balance of the contract,
/// // so calling `host.self_balance()` will return `10` initially.
/// // When a contract is executed by the node the amount that is being sent (`5`)
/// // is added to the balance of the contract, so that `host.self_balance()`
/// // already observes it.
/// Amount::from_ccd(5),
/// );
/// ```
pub fn set_self_balance(&mut self, amount: Amount) {
*self.contract_balance.borrow_mut() = amount;
}
/// Set the contract address.
/// This is used to redirect queries for the balance of the contract
/// itself.
///
/// This method panics if the address provided is already setup as a missing
/// contract or for contract balance queries.
pub fn set_self_address(&mut self, address: ContractAddress) {
if self.missing_contracts.contains(&address) {
fail!("The self_address is marked as a missing contract address.")
}
if self.query_contract_balances.borrow().get(&address).is_some() {
fail!(
"The self_address cannot be setup as a query contract balance. Either use another \
address as self_address or another address in 'setup_query_contract_balance'."
)
}
self.contract_address = Some(address);
}
/// Setup an account balance for an account.
/// This is used to resolve queries for account balances.
///
/// This method panics if the address provided is already setup as a missing
/// account.
///
/// Note: Setting up the account balance for the same address will overwrite
/// the previous setup for that address.
pub fn setup_query_account_balance(
&mut self,
address: AccountAddress,
account_balance: AccountBalance,
) {
if self.missing_accounts.contains(&address) {
fail!(
"Setting up a query account balance for the provided address is not possible, \
because the address is marked as a missing account."
)
}
self.query_account_balances.borrow_mut().insert(address, account_balance);
}
/// Setup a balance for a contract.
/// This is used to resolve queries for contract balances.
///
/// This method panics if the address provided is already setup as a missing
/// contract or as the self_address.
///
/// Note: Setting up the balance for the same address will overwrite the
/// previous setup for that address.
pub fn setup_query_contract_balance(&mut self, address: ContractAddress, balance: Amount) {
if self.missing_contracts.contains(&address) {
fail!(
"Setting up a query contract balance for the provided address is not possible, \
because the address is marked as a missing contract address."
)
}
if Some(address) == self.contract_address {
fail!(
"Setting up a query contract balance for the self_address is not possible, use \
'set_self_balance' instead."
)
}
self.query_contract_balances.borrow_mut().insert(address, balance);
}
/// Set the current exchange rates.
/// This is used for resolving a query for the current exchange rates.
///
/// Note: Setting this again overwrites the previously set exchange rates.
pub fn set_exchange_rates(&mut self, exchange_rates: ExchangeRates) {
self.query_exchange_rates = Some(exchange_rates);
}
/// Check whether a given transfer occured.
pub fn transfer_occurred(&self, receiver: &AccountAddress, amount: Amount) -> bool {
self.transfers.borrow().contains(&(*receiver, amount))
}
/// Get a list of all transfers that have occurred, in the order they
/// occurred in.
pub fn get_transfers(&self) -> Vec<(AccountAddress, Amount)> {
self.transfers.borrow().to_vec()
}
/// Get a list of all transfers to a specific account.
pub fn get_transfers_to(&self, account: AccountAddress) -> Vec<Amount> {
self.transfers
.borrow()
.iter()
.filter_map(|(acc, amount)| {
if *acc == account {
Some(*amount)
} else {
None
}
})
.collect()
}
/// Set an account to be missing. Any transfers to this account will result
/// in an [`TransferError::MissingAccount`] error.
///
/// This differs from the default, where all accounts are assumed to exist.
///
/// This method panics if the address provided is already setup for a query
/// account balance.
pub fn make_account_missing(&mut self, account: AccountAddress) {
if self.query_account_balances.borrow().get(&account).is_some() {
fail!(
"The account cannot be setup as a missing account. It is already setup for a \
query account balance."
)
}
self.missing_accounts.insert(account);
}
/// Set a contract to be missing. Any queries for the balance to this
/// contract will result in an [`QueryContractBalanceError`].
///
/// This method panics if the address provided is already setup for a query
/// contract balance or as the self_address.
pub fn make_contract_missing(&mut self, contract: ContractAddress) {
if self.contract_address == Some(contract) {
fail!("The address of the instance cannot be one of the missing contracts.")
}
if self.query_contract_balances.borrow().get(&contract).is_some() {
fail!(
"The contract address cannot be setup as a missing contract. It is already setup \
for a query contract balance."
)
}
self.missing_contracts.insert(contract);
}
}
impl<State: DeserialWithState<TestStateApi>> TestHost<State> {
/// Make a deep clone of the host, including the whole state and all
/// references to the state. Used for rolling back the host and state,
/// fx when using [`with_rollback`].
fn checkpoint(&self) -> Self {
let cloned_state_api = self.state_builder.state_api.clone_deep();
let state: State = cloned_state_api
.read_root()
.expect_report("Could not deserialize root entry from state clone");
Self {
mocking_fns: self.mocking_fns.clone(),
transfers: self.transfers.clone(),
contract_balance: self.contract_balance.clone(),
contract_address: self.contract_address,
mocking_upgrades: self.mocking_upgrades.clone(),
state_builder: StateBuilder {
state_api: cloned_state_api,
},
state,
missing_accounts: self.missing_accounts.clone(),
missing_contracts: self.missing_contracts.clone(),
query_account_balances: self.query_account_balances.clone(),
query_contract_balances: self.query_contract_balances.clone(),
query_exchange_rates: self.query_exchange_rates,
}
}
/// Helper function for invoking receive methods that respects the
/// transactional nature of invocations. That is, if the invocation
/// returns `Err(_)`, then the host and state is rolled back to a
/// checkpoint just before the invocation.
pub fn with_rollback<R, E>(
&mut self,
call: impl FnOnce(&mut TestHost<State>) -> Result<R, E>,
) -> Result<R, E> {
let checkpoint = self.checkpoint();
let res = call(self);
// Roll back on errors.
if res.is_err() {
*self = checkpoint;
}
res
}
}
#[cfg(all(feature = "wasm-test", feature = "concordium-quickcheck", target_arch = "wasm32"))]
use getrandom::register_custom_getrandom;
#[cfg(all(feature = "wasm-test", feature = "concordium-quickcheck", target_arch = "wasm32"))]
/// A custom function for generating random numbers.
/// There is no Wasm primitive to sample random numbers and this function
/// redirects calls to the `get_random` primitive (host function), which is
/// later handled by `TestHost`, where the actual random number generation
/// happens.
fn get_random(dest: &mut [u8]) -> Result<(), getrandom::Error> {
unsafe {
crate::prims::get_random(dest.as_mut_ptr(), dest.len() as u32);
}
Ok(())
}
#[cfg(all(feature = "wasm-test", feature = "concordium-quickcheck", target_arch = "wasm32"))]
// When compiling to Wasm, we register our own custom random number generation
// function, so all the calls, that depend on `getrandom` (like
// `from_entropy`) will call our function instead.
register_custom_getrandom!(get_random);
// Overall number of QuickCheck tests to run.
// Includes both *passed and discarded*.
// Note: when changing this constant, make sure that
// concordium_std_derive::QUICKCHECK_MAX_PASSED_TESTS is adjusted:
// - QUICKCHECK_MAX_PASSED_TESTS is capped by
// QUICKCHECK_MAX_WITH_DISCARDED_TESTS;
// - QUICKCHECK_MAX_WITH_DISCARDED_TESTS should be bigger allowing some test to
// be discarded (QuickCheck default is x100).
#[cfg(feature = "concordium-quickcheck")]
const QUICKCHECK_MAX_WITH_DISCARDED_TESTS: u64 = 100_000_000;
#[cfg(all(feature = "concordium-quickcheck", target_arch = "wasm32"))]
/// A customized QuickCheck test runner used for on-chain wasm code.
/// Adds support for reporting errors using the primitives available when
/// running Wasm code.
///
/// The `num_tests` parameter specifies how many random tests to run.
pub fn concordium_qc<A: Testable>(num_tests: u64, f: A) {
let mut qc = QuickCheck::new().tests(num_tests).max_tests(QUICKCHECK_MAX_WITH_DISCARDED_TESTS);
let res = qc.quicktest(f);
match res {
Ok(n_tests_passed) => {
if n_tests_passed == 0 {
// report a error is no tests were generated
let msg = "(No QuickCheck tests were generated)";
// calls `report_error` which is handled by `TestHost`
report_error(msg, file!(), line!(), column!())
}
}
Err(result) => {
let msg = format!("Failed with the counterexample: {:#?}", result);
// calls `report_error` which is handled by `TestHost`
report_error(&msg, file!(), line!(), column!());
}
}
}
#[cfg(all(feature = "concordium-quickcheck", not(target_arch = "wasm32")))]
/// A wrapper for QuickCheck test runner for non-wasm targets.
// The `num_tests` parameter specifies how many random tests to run.
pub fn concordium_qc<A: Testable>(num_tests: u64, f: A) {
QuickCheck::new().tests(num_tests).max_tests(QUICKCHECK_MAX_WITH_DISCARDED_TESTS).quickcheck(f)
}
#[cfg(test)]
mod test {
use super::TestStateApi;
use crate::{
cell::RefCell,
rc::Rc,
test_infrastructure::{TestStateBuilder, TestStateEntry},
Deletable, EntryRaw, HasStateApi, HasStateEntry, StateBTreeSet, StateMap, StateSet,
INITIAL_NEXT_ITEM_PREFIX,
};
use concordium_contracts_common::{to_bytes, Deserial, Read, Seek, SeekFrom, Write};
#[test]
fn test_testhost_balance_queries_reflect_transfers() {
use super::*;
let mut host = TestHost::new((), TestStateBuilder::new());
let self_address = ContractAddress::new(0, 0);
host.set_self_address(self_address);
host.set_self_balance(Amount::from_micro_ccd(3000));
let account = AccountAddress([0; 32]);
let account_balance =
AccountBalance::new(Amount::zero(), Amount::zero(), Amount::zero()).unwrap();
host.setup_query_account_balance(account, account_balance);
host.invoke_transfer(&account, Amount::from_micro_ccd(2000))
.expect("Transferring should succeed");
let account_new_balance = host.account_balance(account).expect("Should succeed");
let self_new_balance = host.contract_balance(self_address).expect("Should succeed");
assert_eq!(account_new_balance.total, Amount::from_micro_ccd(2000));
assert_eq!(self_new_balance, Amount::from_micro_ccd(1000));
}
#[test]
fn test_testhost_balance_queries_reflect_invoke() {
use super::*;
let mut host = TestHost::new((), TestStateBuilder::new());
let self_address = ContractAddress::new(0, 0);
host.set_self_address(self_address);
host.set_self_balance(Amount::from_micro_ccd(3000));
let other_address = ContractAddress::new(1, 0);
host.setup_query_contract_balance(other_address, Amount::from_micro_ccd(4000));
let entrypoint = OwnedEntrypointName::new_unchecked("test.method".to_string());
host.setup_mock_entrypoint(other_address, entrypoint.clone(), MockFn::returning_ok(()));
host.invoke_contract_raw(
&other_address,
Parameter::empty(),
entrypoint.as_entrypoint_name(),
Amount::from_micro_ccd(1000),
)
.expect("Invoke should succeed");
let other_new_balance = host.contract_balance(other_address).expect("Should succeed");
let self_new_balance = host.contract_balance(self_address).expect("Should succeed");
assert_eq!(other_new_balance, Amount::from_micro_ccd(5000));
assert_eq!(self_new_balance, Amount::from_micro_ccd(2000));
}
#[test]
// Perform a number of operations from Seek, Read, Write and HasStateApi
// classes on the TestStateApi structure and check that they behave as
// specified.
fn test_contract_state() {
let data = Rc::new(RefCell::new(vec![1; 100].into()));
let mut state = TestStateEntry::open(data, Vec::new(), 0);
assert_eq!(state.seek(SeekFrom::Start(100)), Ok(100), "Seeking to the end failed.");
assert_eq!(
state.seek(SeekFrom::Current(0)),
Ok(100),
"Seeking from current position with offset 0 failed."
);
assert!(
state.seek(SeekFrom::Current(1)).is_err(),
"Seeking from current position with offset 1 succeeded."
);
assert_eq!(state.cursor.offset, 100, "Cursor position changed on failed seek.");
assert_eq!(
state.seek(SeekFrom::Current(-1)),
Ok(99),
"Seeking from current position backwards with offset 1 failed."
);
assert!(state.seek(SeekFrom::Current(-100)).is_err(), "Seeking beyond beginning succeeds");
assert_eq!(state.seek(SeekFrom::Current(-99)), Ok(0), "Seeking to the beginning fails.");
assert_eq!(state.seek(SeekFrom::End(0)), Ok(100), "Seeking from end fails.");
assert!(
state.seek(SeekFrom::End(1)).is_err(),
"Seeking beyond the end succeeds but should fail."
);
assert_eq!(state.cursor.offset, 100, "Cursor position changed on failed seek.");
assert_eq!(
state.seek(SeekFrom::End(-20)),
Ok(80),
"Seeking from end leads to incorrect position."
);
assert_eq!(state.write(&[0; 21]), Ok(21), "Writing writes an incorrect amount of data.");
assert_eq!(state.cursor.offset, 101, "After writing the cursor is at the end.");
assert_eq!(state.write(&[0; 21]), Ok(21), "Writing again writes incorrect amount of data.");
let mut buf = [0; 30];
assert_eq!(state.read(&mut buf), Ok(0), "Reading from the end should read 0 bytes.");
assert_eq!(state.seek(SeekFrom::End(-20)), Ok(102));
assert_eq!(state.read(&mut buf), Ok(20), "Reading from offset 80 should read 20 bytes.");
assert_eq!(
&buf[0..20],
&state.cursor.data.borrow().data().expect("Entry was deleted")[80..100],
"Incorrect data was read."
);
assert_eq!(
state.cursor.offset, 122,
"After reading the offset is in the correct position."
);
assert!(state.reserve(222).is_ok(), "Could not increase state to 222.");
assert_eq!(state.write(&[2; 100]), Ok(100), "Should have written 100 bytes.");
assert_eq!(state.cursor.offset, 222, "After writing the offset should be 200.");
state.truncate(50).expect("Could not truncate state to 50.");
assert_eq!(state.cursor.offset, 50, "After truncation the state should be 50.");
assert_eq!(
state.write(&[1; 1000]),
Ok(1000),
"Writing at the end after truncation should succeed."
);
}
#[test]
fn test_contract_state_write() {
let data = Rc::new(RefCell::new(vec![0u8; 10].into()));
let mut state = TestStateEntry::open(data, Vec::new(), 0);
assert_eq!(state.write(&1u64.to_le_bytes()), Ok(8), "Incorrect number of bytes written.");
assert_eq!(
state.write(&2u64.to_le_bytes()),
Ok(8),
"State should be resized automatically."
);
assert_eq!(state.cursor.offset, 16, "Pos should be at the end.");
assert_eq!(
*state.cursor.data.as_ref().borrow().data().expect("Entry was deleted"),
vec![1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0],
"Correct data was written."
);
}
}