forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obfuscate_test.sh
executable file
·275 lines (227 loc) · 7.27 KB
/
obfuscate_test.sh
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
#!/usr/bin/env bash
# Copyright 2017-2020 The Verible Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Tests verible-verilog-obfuscate
declare -r MY_INPUT_FILE="${TEST_TMPDIR}/myinput.txt"
declare -r MY_INPUT_FILE2="${TEST_TMPDIR}/myinput2.txt"
declare -r MY_OUTPUT_FILE="${TEST_TMPDIR}/myoutput.txt"
declare -r MY_OUTPUT_FILE2="${TEST_TMPDIR}/myoutput2.txt"
declare -r MY_SAVEMAP_FILE="${TEST_TMPDIR}/save.map"
declare -r MY_EXPECT_FILE="${TEST_TMPDIR}/myexpect.txt"
obfuscator="$(rlocation ${TEST_WORKSPACE}/$1)"
difftool="$(rlocation ${TEST_WORKSPACE}/$2)"
###############################################################################
echo "### Simple obfuscation test."
cat >"${MY_INPUT_FILE}" <<EOF
module kjasdfASKJdsa18k ;endmodule
EOF
echo "Run obfuscator."
"${obfuscator}" < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
echo "Verify output equivalence."
"${difftool}" --mode=obfuscate "${MY_INPUT_FILE}" "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
###############################################################################
echo "### Input lexical error test."
cat >${MY_INPUT_FILE} <<EOF
module 1kjasdfASKJdsa18k ;endmodule
EOF
"${obfuscator}" < "${MY_INPUT_FILE}"
status="$?"
[[ $status == 1 ]] || {
echo "Expected exit code 1, but got $status"
exit 1
}
###############################################################################
echo "### Tests loading a pre-existing map."
cat >"${MY_SAVEMAP_FILE}" <<EOF
foo yR3
bar igE
asdas iuer8
baz H8u
EOF
cat >"${MY_INPUT_FILE}" <<EOF
module foo;
bar baz(.foo(bar));
endmodule
EOF
cat >"${MY_EXPECT_FILE}" <<EOF
module yR3;
igE H8u(.yR3(igE));
endmodule
EOF
echo "Run obfuscator. Load and apply previously used substitutions."
"${obfuscator}" --load_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
diff --strip-trailing-cr -u "${MY_OUTPUT_FILE}" "${MY_EXPECT_FILE}" || exit 1
###############################################################################
echo "### Testing reading malformed map file."
# Contains a malformed line.
cat >"${MY_SAVEMAP_FILE}" <<EOF
foo yR3
bar
EOF
cat >"${MY_INPUT_FILE}" <<EOF
module foo;
bar baz(.foo(bar));
endmodule
EOF
echo "Run obfuscator. Expect error with --load_map on nonexistent file."
"${obfuscator}" --load_map=does.not.exist.file < "${MY_INPUT_FILE}"
status="$?"
[[ $status == 1 ]] || {
echo "Expected exit code 1, but got $status"
exit 1
}
echo "Run obfuscator. Expect error with --load_map on malformed file."
"${obfuscator}" --load_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE}"
status="$?"
[[ $status == 1 ]] || {
echo "Expected exit code 1, but got $status"
exit 1
}
echo "--decode requires a --load_map file."
"${obfuscator}" --decode < "${MY_INPUT_FILE}"
status="$?"
[[ $status == 1 ]] || {
echo "Expected exit code 1, but got $status"
exit 1
}
###############################################################################
echo "### Testing obfuscate with --save_map"
cat >"${MY_INPUT_FILE}" <<EOF
module
foo_bar
;endmodule
EOF
cat >"${MY_INPUT_FILE2}" <<EOF
foo_bar + foo_bar
EOF
echo "Run obfuscator. Save substitutions."
"${obfuscator}" --save_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
# The input file has only one line that doesn't match 'module'.
# That line contains the encoded identifier of interest.
# Use that to generate the expected file.
foo_bar_encoded=$(grep -v module "${MY_OUTPUT_FILE}")
cat >"${MY_EXPECT_FILE}" <<EOF
${foo_bar_encoded} + ${foo_bar_encoded}
EOF
echo "Re-apply saved substitution on new file."
"${obfuscator}" --load_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE2}" > "${MY_OUTPUT_FILE2}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
diff --strip-trailing-cr -u "${MY_OUTPUT_FILE2}" "${MY_EXPECT_FILE}" || exit 1
###############################################################################
echo "Test obfuscate with --save_map to a bad path"
cat >"${MY_INPUT_FILE}" <<EOF
module foo;
bar baz(.foo(bar));
endmodule
EOF
echo "Run obfuscator. Expect error with --save_map."
"${obfuscator}" --save_map=does/not/exist/dir/file.map < "${MY_INPUT_FILE}"
status="$?"
[[ $status == 1 ]] || {
echo "Expected exit code 1, but got $status"
exit 1
}
###############################################################################
echo "Test obfuscate --decode mode"
cat >"${MY_INPUT_FILE}" <<EOF
module foo_bar;
foo bar(.baz(baz),
.clk(clk));
endmodule
EOF
echo "Run obfuscator. Save substitutions."
"${obfuscator}" --save_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
echo "Decode the previous output using saved substitutions."
"${obfuscator}" --decode --load_map="${MY_SAVEMAP_FILE}" < "${MY_OUTPUT_FILE}" > "${MY_OUTPUT_FILE2}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
diff --strip-trailing-cr -u "${MY_OUTPUT_FILE2}" "${MY_INPUT_FILE}" || exit 1
##############################################################################
echo "Test built-in functions untouched"
cat >"${MY_INPUT_FILE}" <<EOF
parameter a = sin(1); parameter b = ceil(2.5);
EOF
${obfuscator} < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
cat "${MY_OUTPUT_FILE}"
grep "sin(.*ceil(" "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "output does not contain preserved built-in function names"
exit 1
}
###############################################################################
echo "Test obfuscate --preserve_interface mode"
cat >"${MY_INPUT_FILE}" <<EOF
module foo_bar(input clk);
foo bar(.baz(baz),
.clk(clk));
endmodule
EOF
echo "Run obfuscator. Save substitutions."
"${obfuscator}" --preserve_interface --save_map="${MY_SAVEMAP_FILE}" < "${MY_INPUT_FILE}" > "${MY_OUTPUT_FILE}"
status="$?"
[[ $status == 0 ]] || {
echo "Expected exit code 0, but got $status"
exit 1
}
intact_names=(foo_bar clk)
echo "Verify the saved map does not change interface names."
for intact_name in ${intact_names[@]}; do
grep "^${intact_name} ${intact_name}[[:space:]]*\$" "${MY_SAVEMAP_FILE}" > /dev/null
status="$?"
[[ $status == 0 ]] || {
echo "Failed to check mapping for name: ${intact_name}"
cat "${MY_SAVEMAP_FILE}"
exit 1
}
done
###############################################################################
echo "PASS"