Skip to content

Commit 2f7c3a0

Browse files
authored
Merge pull request #502 from nanlioniya/lab8
[LAB8] 313553005
2 parents 0c6bb44 + 2671978 commit 2f7c3a0

2 files changed

Lines changed: 164 additions & 10 deletions

File tree

lab2/main_test.js

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,123 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { Application, MailSystem } = require('./main');
44

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
5+
/*
6+
function coverage testing:
7+
- MailSystem
8+
- write
9+
- send
10+
- Application
11+
- getNames
12+
- getRandomPerson
13+
- selectNextPerson
14+
- notifySelected
15+
*/
16+
17+
// MailSystem test
18+
test('MailSystem write test', (t) => {
19+
const mail = new MailSystem();
20+
const result = mail.write('Test');
21+
assert.equal(result, 'Congrats, Test!');
22+
});
23+
24+
// Test Stub
25+
test('MailSystem send test', (t) => {
26+
const mail = new MailSystem();
27+
const originalRandom = Math.random;
28+
29+
// Test success case
30+
Math.random = () => 1;
31+
assert.equal(mail.send('Test', 'content'), true);
32+
33+
// Test failure case
34+
Math.random = () => 0;
35+
assert.equal(mail.send('Test', 'content'), false);
36+
37+
Math.random = originalRandom;
38+
});
39+
40+
// Application tests
41+
test('Application initialization test', async (t) => {
42+
// 準備測試檔案
43+
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');
44+
45+
try {
46+
const app = new Application();
47+
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成
48+
49+
assert(Array.isArray(app.people));
50+
assert(Array.isArray(app.selected));
51+
assert.equal(app.people.length, 3);
52+
assert.equal(app.selected.length, 0);
53+
} finally {
54+
// 清理測試檔案
55+
fs.unlinkSync('name_list.txt');
56+
}
57+
});
58+
59+
60+
test('Application selectNextPerson test', async (t) => {
61+
// 準備測試檔案
62+
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');
63+
64+
try {
65+
const app = new Application();
66+
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成
67+
68+
const person1 = app.selectNextPerson();
69+
assert(app.people.includes(person1));
70+
assert.equal(app.selected.length, 1);
71+
72+
const person2 = app.selectNextPerson();
73+
assert(app.people.includes(person2));
74+
assert.equal(app.selected.length, 2);
75+
assert.notEqual(person1, person2);
76+
77+
const person3 = app.selectNextPerson();
78+
assert(app.people.includes(person3));
79+
assert.equal(app.selected.length, 3);
80+
81+
// 當所有人都被選中後,應該返回 null
82+
const person4 = app.selectNextPerson();
83+
assert.equal(person4, null);
84+
} finally {
85+
// 清理測試檔案
86+
fs.unlinkSync('name_list.txt');
87+
}
88+
});
89+
90+
91+
// Mock Object
92+
test('Application notifySelected test', async (t) => {
93+
// 準備測試檔案
94+
fs.writeFileSync('name_list.txt', 'Test1\nTest2');
95+
96+
try {
97+
const app = new Application();
98+
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成
99+
100+
app.selectNextPerson(); // 選擇第一個人
101+
app.selectNextPerson(); // 選擇第二個人
102+
103+
// Mock MailSystem methods
104+
let writeCount = 0;
105+
let sendCount = 0;
106+
app.mailSystem.write = (name) => {
107+
writeCount++;
108+
return `Congrats, ${name}!`;
109+
};
110+
app.mailSystem.send = (name, context) => {
111+
sendCount++;
112+
return true;
113+
};
114+
115+
app.notifySelected();
116+
assert.equal(writeCount, 2);
117+
assert.equal(sendCount, 2);
118+
} finally {
119+
// 清理測試檔案
120+
fs.unlinkSync('name_list.txt');
121+
}
122+
});
123+
124+

lab8/solve.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
#!/usr/bin/env python3
22

3-
import angr,sys
3+
def solve():
4+
# 我們需要找到滿足以下條件的 8 個字元:
5+
# 1. (input[0] ^ input[1]) == 0x55
6+
# 2. (input[2] + input[3]) == 200
7+
# 3. (input[4] * 3) == input[5]
8+
# 4. (input[6] - input[7]) == 1
9+
# 5. (input[1] + input[2] - input[3]) == 50
10+
# 6. (input[5] ^ input[6]) == 0x2A
11+
12+
# 嘗試所有可能的值
13+
for a in range(32, 127): # input[0]
14+
for b in range(32, 127): # input[1]
15+
if (a ^ b) != 0x55:
16+
continue
17+
18+
for c in range(32, 127): # input[2]
19+
for d in range(32, 127): # input[3]
20+
if (c + d) != 200:
21+
continue
22+
23+
if (b + c - d) != 50:
24+
continue
25+
26+
for e in range(32, 127): # input[4]
27+
f = e * 3 # input[5]
28+
if f < 32 or f > 126:
29+
continue
30+
31+
for g in range(32, 127): # input[6]
32+
if (f ^ g) != 0x2A:
33+
continue
34+
35+
h = g - 1 # input[7]
36+
if h < 32 or h > 126:
37+
continue
38+
39+
# 所有條件都滿足,輸出密鑰
40+
key = chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h)
41+
return key
42+
43+
return "No solution found!"
444

5-
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
8-
9-
10-
if __name__ == '__main__':
11-
main()
45+
if __name__ == "__main__":
46+
key = solve()
47+
print(key)

0 commit comments

Comments
 (0)