1 | <?php |
||
8 | use BitWasp\Bitcoin\Script\Interpreter\Interpreter; |
||
9 | |||
10 | abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase |
||
11 | { |
||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | private $scriptFlagNames; |
||
16 | |||
17 | /** |
||
18 | * @var NetworkInterface |
||
19 | */ |
||
20 | protected $network; |
||
21 | |||
22 | /** |
||
23 | * AbstractTestCase constructor. |
||
24 | * @param null $name |
||
25 | * @param array $data |
||
26 | * @param string $dataName |
||
27 | */ |
||
28 | public function __construct($name = null, array $data = [], $dataName = '') |
||
29 | { |
||
30 | parent::__construct($name, $data, $dataName); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param string $name |
||
35 | * @return array |
||
36 | */ |
||
37 | public function jsonDataFile(string $name): array |
||
38 | { |
||
39 | $contents = $this->dataFile($name); |
||
40 | $decoded = json_decode($contents, true); |
||
41 | if (false === $decoded || json_last_error() !== JSON_ERROR_NONE) { |
||
42 | throw new \RuntimeException('Invalid JSON file ' . $name); |
||
43 | } |
||
44 | |||
45 | return $decoded; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param string $filename |
||
50 | * @return string |
||
51 | */ |
||
52 | public function dataFile(string $filename): string |
||
53 | { |
||
54 | $contents = file_get_contents($this->dataPath($filename)); |
||
55 | if (false === $contents) { |
||
56 | throw new \RuntimeException('Failed to data file ' . $filename); |
||
57 | } |
||
58 | return $contents; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $file |
||
63 | * @return string |
||
64 | */ |
||
65 | public function dataPath(string $file): string |
||
66 | { |
||
67 | return __DIR__ . '/../tests/Data/' . $file; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | public function calcMapScriptFlags(): array |
||
74 | { |
||
75 | if (null === $this->scriptFlagNames) { |
||
76 | $this->scriptFlagNames = [ |
||
77 | "NONE" => Interpreter::VERIFY_NONE, |
||
78 | "P2SH" => Interpreter::VERIFY_P2SH, |
||
79 | "STRICTENC" => Interpreter::VERIFY_STRICTENC, |
||
80 | "DERSIG" => Interpreter::VERIFY_DERSIG, |
||
81 | "LOW_S" => Interpreter::VERIFY_LOW_S, |
||
82 | "SIGPUSHONLY" => Interpreter::VERIFY_SIGPUSHONLY, |
||
83 | "MINIMALDATA" => Interpreter::VERIFY_MINIMALDATA, |
||
84 | "NULLDUMMY" => Interpreter::VERIFY_NULL_DUMMY, |
||
85 | "DISCOURAGE_UPGRADABLE_NOPS" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_NOPS, |
||
86 | "CLEANSTACK" => Interpreter::VERIFY_CLEAN_STACK, |
||
87 | "CHECKLOCKTIMEVERIFY" => Interpreter::VERIFY_CHECKLOCKTIMEVERIFY, |
||
88 | "CHECKSEQUENCEVERIFY" => Interpreter::VERIFY_CHECKSEQUENCEVERIFY, |
||
89 | "WITNESS" => Interpreter::VERIFY_WITNESS, |
||
90 | "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM" => Interpreter::VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, |
||
91 | "MINIMALIF" => Interpreter::VERIFY_MINIMALIF, |
||
92 | "NULLFAIL" => Interpreter::VERIFY_NULLFAIL, |
||
93 | ]; |
||
94 | } |
||
95 | |||
96 | return $this->scriptFlagNames; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $string |
||
101 | * @return int |
||
102 | */ |
||
103 | public function getScriptFlagsFromString(string $string): int |
||
104 | { |
||
105 | $mapFlagNames = $this->calcMapScriptFlags(); |
||
106 | if (strlen($string) === 0) { |
||
107 | return Interpreter::VERIFY_NONE; |
||
108 | } |
||
109 | |||
110 | $flags = 0; |
||
111 | $words = explode(",", $string); |
||
112 | foreach ($words as $word) { |
||
113 | if (!isset($mapFlagNames[$word])) { |
||
114 | throw new \RuntimeException('Unknown verification flag: ' . $word); |
||
115 | } |
||
116 | |||
117 | $flags |= $mapFlagNames[$word]; |
||
118 | } |
||
119 | |||
120 | return $flags; |
||
121 | } |
||
122 | } |
||
123 |