~thestr4ng3r/mymcplus

ref: f2fbe2dbe8235c39413f51f979e8516e0392d4ce mymcplus/test/test_memorycard.py -rw-r--r-- 9.9 KiB
f2fbe2dbFlorian Märkl Rename Test Scripts 3 years ago
                                                                                
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
import mymc


def md5(fn):
    import hashlib
    return hashlib.md5(open(fn, "rb").read()).hexdigest()


def patch_fixed_time(monkeypatch, mod):
    def tod_now():
        return 42, 37, 22, 20, 4, 2018
    monkeypatch.setattr(mod, "tod_now", tod_now)


def patch_localtime(monkeypatch):
    import time
    def localtime(secs=None):
        return time.gmtime(secs)
    monkeypatch.setattr(time, "localtime", localtime)


def test_ls(monkeypatch, capsys, data):
    patch_localtime(monkeypatch)
    mymc.main(["mymc",
               "-i", data.join("mc01.ps2").strpath,
               "ls"])

    output = capsys.readouterr()
    assert output.err == ""
    assert output.out == ("rwx--d----+----       4 2018-04-21 14:53:07 .\n"
                          "-wx--d----+--H-       0 2018-04-21 14:53:00 ..\n"
                          "rwx--d-------H-       4 2018-04-21 14:53:01 BEDATA-SYSTEM\n"
                          "rwx--d----+----       5 2018-04-21 14:53:09 BESCES-50501REZ\n")


def test_extract(capsys, data, tmpdir):
    out_file = tmpdir.join("BESCES-50501REZ").strpath

    mymc.main(["mymc",
               "-i", data.join("mc01.ps2").strpath,
               "extract", "-o", out_file, "BESCES-50501REZ/BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.err == ""
    assert output.out == ""

    assert md5(out_file) == "5388344a2d4bb429b9a18ff683a8a691"


def test_add(monkeypatch, capsys, mc01_copy, tmpdir):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)
    patch_localtime(monkeypatch)

    file = tmpdir.join("helloworld.txt").strpath
    with open(file, "w") as f:
         f.write("Hello World!\n")

    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "add", file])

    output = capsys.readouterr()
    assert output.out == ""
    assert output.err == ""

    mymc.main(["mymc",
               "-i", mc_file,
               "ls"])

    output = capsys.readouterr()
    assert output.out == ("rwx--d----+----       5 2018-04-20 13:37:42 .\n"
                          "-wx--d----+--H-       0 2018-04-21 14:53:00 ..\n"
                          "rwx--d-------H-       4 2018-04-21 14:53:01 BEDATA-SYSTEM\n"
                          "rwx--d----+----       5 2018-04-21 14:53:09 BESCES-50501REZ\n"
                          "rwx-f-----+----      13 2018-04-20 13:37:42 helloworld.txt\n")
    assert output.err == ""

    assert md5(mc_file) == "faa75353a97328c7d8fe38756c38fdd9"


def test_check_ok(capsys, data):
    mymc.main(["mymc",
               "-i", data.join("mc01.ps2").strpath,
               "check"])

    output = capsys.readouterr()
    assert output.out == "No errors found.\n"
    assert output.err == ""


def test_check_root_directory(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath
    with open(mc_file, "r+b") as f:
        f.seek(0x200)
        f.write(b"\x13\x37")

    assert md5(mc_file) == "bec7e8c3884806024b9eb9599dc4315f"

    mymc.main(["mymc",
               "-i", mc_file,
               "check"])

    output = capsys.readouterr()
    assert output.err == mc_file + ": Root directory damaged.\n"
    assert output.out == ""

# TODO: Should probably make more tests for check


def test_clear(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "clear", "-x", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.err == ""
    assert output.out == ""

    assert md5(mc_file) == "defaeba9b480676e8666dd4f3ff16643"


def test_set(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "set", "-K", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.err == ""
    assert output.out == ""

    assert md5(mc_file) == "d235a085e75a8201bd417b127ccd8908"


def test_delete(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "delete", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.err == ""
    assert output.out == ""

    assert md5(mc_file) == "143e640ccf3f22e48e1d1d4b10300d57"


def test_df(capsys, data):
    mc_file = data.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "df"])

    output = capsys.readouterr()
    assert output.out == mc_file + ": 8268800 bytes free.\n"
    assert output.err == ""


def test_dir(capsys, data):
    mc_file = data.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "dir", "-a"])

    output = capsys.readouterr()
    assert output.out == ("BEDATA-SYSTEM                    Your System\n"
                          "   5KB Not Protected             Configuration\n"
                          "\n"
                          "BESCES-50501REZ                  Rez\n"
                          "  53KB Not Protected             \n"
                          "\n"
                          "8,075 KB Free\n")
    assert output.err == ""


def test_format(monkeypatch, capsys, tmpdir):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)

    mc_file = tmpdir.join("mc.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "format"])

    output = capsys.readouterr()
    assert output.out == ""
    assert output.err == ""

    assert md5(mc_file) == "18ab430278362e6e70ce7cda9081888f"


def test_mkdir(monkeypatch, capsys, mc01_copy):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)

    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "mkdir", "p0rn"])

    output = capsys.readouterr()
    assert output.out == ""
    assert output.err == ""

    assert md5(mc_file) == "2be30a14246f34cdb157ea68f4905b85"


def test_remove(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "remove", "BESCES-50501REZ/BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.out == ""
    assert output.err == ""

    assert md5(mc_file) == "5d0ffec85ad1dc9a371e0ead55f4932b"


def test_remove_nonempty(capsys, mc01_copy):
    mc_file = mc01_copy.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "remove", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.out == ""
    assert output.err == "BESCES-50501REZ: directory not empty\n"


def test_export_psu(capsys, data, tmpdir):
    mc_file = data.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "export", "-d", tmpdir.strpath, "-p", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.out == "Exporing BESCES-50501REZ to BESCES-50501REZ.psu\n"
    assert output.err == ""

    assert md5(tmpdir.join("BESCES-50501REZ.psu").strpath) == "d86c82e559c8250c894fbbc4405d8789"


def test_export_max(capsys, data, tmpdir):
    mc_file = data.join("mc01.ps2").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "export", "-d", tmpdir.strpath, "-m", "BESCES-50501REZ"])

    output = capsys.readouterr()
    assert output.out == "Exporing BESCES-50501REZ to BESCES-50501REZ.max\n"

    assert md5(tmpdir.join("BESCES-50501REZ.max").strpath) == "3f63d38668a0a5a5fa508ab8c3bb469a"


def test_import_psu(monkeypatch, capsys, data, mc02_copy):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)

    mc_file = mc02_copy.join("mc02.ps2").strpath
    psu_file = data.join("BESCES-50501REZ.psu").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "import", psu_file])

    output = capsys.readouterr()
    assert output.out == "Importing " + psu_file + " to BESCES-50501REZ\n"
    assert output.err == ""

    assert md5(mc_file) == "4085992c23fc38d6c4ece5303dc77e74"


def test_import_max(monkeypatch, capsys, data, mc02_copy):
    import ps2mc
    import ps2save
    patch_fixed_time(monkeypatch, ps2mc)
    patch_fixed_time(monkeypatch, ps2save)

    mc_file = mc02_copy.join("mc02.ps2").strpath
    max_file = data.join("BESCES-50501REZ.max").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "import", max_file])

    output = capsys.readouterr()
    assert output.out == "Importing " + max_file + " to BESCES-50501REZ\n"

    assert md5(mc_file) == "0e2dd8d53f05f6debe7d93aa726fc6e6"


def test_import_sps(monkeypatch, capsys, data, mc02_copy):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)

    mc_file = mc02_copy.join("mc02.ps2").strpath
    sps_file = data.join("BESCES-50501REZ.sps").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "import", sps_file])

    output = capsys.readouterr()
    assert output.out == "Importing " + sps_file + " to BESCES-50501REZ\n"
    assert output.err == ""

    assert md5(mc_file) == "9726ad34016df2586eb9663a12f9ba02"


def test_import_xps(monkeypatch, capsys, data, mc02_copy):
    import ps2mc
    patch_fixed_time(monkeypatch, ps2mc)

    mc_file = mc02_copy.join("mc02.ps2").strpath
    xps_file = data.join("BESCES-50501REZ.xps").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "import", xps_file])

    output = capsys.readouterr()
    assert output.out == "Importing " + xps_file + " to BESCES-50501REZ\n"
    assert output.err == ""

    assert md5(mc_file) == "9726ad34016df2586eb9663a12f9ba02"


def test_import_cbs(monkeypatch, capsys, data, mc02_copy):
    import ps2mc
    import ps2save
    patch_fixed_time(monkeypatch, ps2mc)
    patch_fixed_time(monkeypatch, ps2save)

    mc_file = mc02_copy.join("mc02.ps2").strpath
    cbs_file = data.join("BESCES-50501REZ.cbs").strpath

    mymc.main(["mymc",
               "-i", mc_file,
               "import", cbs_file])

    output = capsys.readouterr()
    assert output.out == "Importing " + cbs_file + " to BESCES-50501REZ\n"
    assert output.err == ""

    assert md5(mc_file) == "897b0fbd1965dc02a442e1723bd3df27"