Quellcode durchsuchen

implement additional test in mock VM

Signed-off-by: xermicus <cyrill@parity.io>
xermicus vor 2 Jahren
Ursprung
Commit
55e4f274ec

+ 2 - 0
tests/codegen_testcases/solidity/substrate_dispatch.sol

@@ -286,6 +286,7 @@ contract simple {
 	// CHECK: 	store (arg #3), %selector.temp.46
 	// CHECK: 	switch %selector.temp.46:
 	// CHECK: 		case uint32 3576764294: goto block #3
+	// NOT-CHECK: 	case uint32 2018875586: goto block #3
 	// CHECK: 		default: goto block #2
 	// CHECK: block2: # fb_or_recv
 	// CHECK: 	return code: function selector invalid
@@ -306,6 +307,7 @@ contract simple {
 	// CHECK: 	store (arg #3), %selector.temp.50
 	// CHECK: 	switch %selector.temp.50:
 	// CHECK: 		case uint32 2018875586: goto block #3
+	// NOT-CHECK: 	case uint32 3576764294: goto block #3
 	// CHECK: 		default: goto block #2
 	// CHECK: block2: # fb_or_recv
 	// CHECK: 	return code: function selector invalid

+ 20 - 7
tests/substrate.rs

@@ -923,13 +923,8 @@ impl MockSubstrate {
         self.invoke("call", input).unwrap();
     }
 
-    /// Call the "call" function with the given input and expect the contract to trap.
-    ///
-    /// `input` must contain the desired function selector.
-    ///
-    /// Only traps caused by an `unreachable` instruction are allowed. Other traps will panic instead.
-    pub fn raw_function_failure(&mut self, input: Vec<u8>) {
-        match self.invoke("call", input) {
+    fn raw_failure(&mut self, export: &str, input: Vec<u8>) {
+        match self.invoke(export, input) {
             Err(wasmi::Error::Trap(trap)) => match trap.trap_code() {
                 Some(TrapCode::UnreachableCodeReached) => (),
                 _ => panic!("trap: {trap:?}"),
@@ -939,6 +934,24 @@ impl MockSubstrate {
         }
     }
 
+    /// Call the "call" function with the given input and expect the contract to trap.
+    ///
+    /// `input` must contain the desired function selector.
+    ///
+    /// Only traps caused by an `unreachable` instruction are allowed. Other traps will panic instead.
+    pub fn raw_function_failure(&mut self, input: Vec<u8>) {
+        self.raw_failure("call", input);
+    }
+
+    /// Call the "deploy" function with the given input and expect the contract to trap.
+    ///
+    /// `input` must contain the desired function selector.
+    ///
+    /// Only traps caused by an `unreachable` instruction are allowed. Other traps will panic instead.
+    pub fn raw_constructor_failure(&mut self, input: Vec<u8>) {
+        self.raw_failure("deploy", input);
+    }
+
     pub fn heap_verify(&mut self) {
         let mem = self.0.data().memory.unwrap().data(&mut self.0);
         let memsize = mem.len();

+ 23 - 0
tests/substrate_tests/calls.rs

@@ -1041,3 +1041,26 @@ contract Flagger {
     );
     assert_eq!(u32::decode(&mut &runtime.output()[..]).unwrap(), voyager);
 }
+
+#[test]
+fn constructors_and_messages_distinct_in_dispatcher() {
+    let mut runtime = build_solidity(
+        r##"
+        contract c {
+            constructor() {}
+            function foo() public pure {}
+        }"##,
+    );
+
+    let constructor = 0xcdbf608du32.to_be_bytes().to_vec();
+    // Given this constructor selector works as intended
+    runtime.raw_constructor(constructor.clone());
+    // Expect calling the constructor via "call" to trap the contract
+    runtime.raw_function_failure(constructor);
+
+    let function = 0xc2985578u32.to_be_bytes().to_vec();
+    // Given this function selector works as intended
+    runtime.raw_function(function.clone());
+    // Expect calling the function via "deploy" to trap the contract
+    runtime.raw_constructor_failure(function);
+}