Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 120 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,123 @@ const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
/*
function coverage testing:
- MailSystem
- write
- send
- Application
- getNames
- getRandomPerson
- selectNextPerson
- notifySelected
*/

// MailSystem test
test('MailSystem write test', (t) => {
const mail = new MailSystem();
const result = mail.write('Test');
assert.equal(result, 'Congrats, Test!');
});

// Test Stub
test('MailSystem send test', (t) => {
const mail = new MailSystem();
const originalRandom = Math.random;

// Test success case
Math.random = () => 1;
assert.equal(mail.send('Test', 'content'), true);

// Test failure case
Math.random = () => 0;
assert.equal(mail.send('Test', 'content'), false);

Math.random = originalRandom;
});

// Application tests
test('Application initialization test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

assert(Array.isArray(app.people));
assert(Array.isArray(app.selected));
assert.equal(app.people.length, 3);
assert.equal(app.selected.length, 0);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


test('Application selectNextPerson test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

const person1 = app.selectNextPerson();
assert(app.people.includes(person1));
assert.equal(app.selected.length, 1);

const person2 = app.selectNextPerson();
assert(app.people.includes(person2));
assert.equal(app.selected.length, 2);
assert.notEqual(person1, person2);

const person3 = app.selectNextPerson();
assert(app.people.includes(person3));
assert.equal(app.selected.length, 3);

// 當所有人都被選中後,應該返回 null
const person4 = app.selectNextPerson();
assert.equal(person4, null);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


// Mock Object
test('Application notifySelected test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

app.selectNextPerson(); // 選擇第一個人
app.selectNextPerson(); // 選擇第二個人

// Mock MailSystem methods
let writeCount = 0;
let sendCount = 0;
app.mailSystem.write = (name) => {
writeCount++;
return `Congrats, ${name}!`;
};
app.mailSystem.send = (name, context) => {
sendCount++;
return true;
};

app.notifySelected();
assert.equal(writeCount, 2);
assert.equal(sendCount, 2);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


52 changes: 44 additions & 8 deletions lab8/solve.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
#!/usr/bin/env python3

import angr,sys
def solve():
# 我們需要找到滿足以下條件的 8 個字元:
# 1. (input[0] ^ input[1]) == 0x55
# 2. (input[2] + input[3]) == 200
# 3. (input[4] * 3) == input[5]
# 4. (input[6] - input[7]) == 1
# 5. (input[1] + input[2] - input[3]) == 50
# 6. (input[5] ^ input[6]) == 0x2A

# 嘗試所有可能的值
for a in range(32, 127): # input[0]
for b in range(32, 127): # input[1]
if (a ^ b) != 0x55:
continue

for c in range(32, 127): # input[2]
for d in range(32, 127): # input[3]
if (c + d) != 200:
continue

if (b + c - d) != 50:
continue

for e in range(32, 127): # input[4]
f = e * 3 # input[5]
if f < 32 or f > 126:
continue

for g in range(32, 127): # input[6]
if (f ^ g) != 0x2A:
continue

h = g - 1 # input[7]
if h < 32 or h > 126:
continue

# 所有條件都滿足,輸出密鑰
key = chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h)
return key

return "No solution found!"

def main():
secret_key = b""
sys.stdout.buffer.write(secret_key)


if __name__ == '__main__':
main()
if __name__ == "__main__":
key = solve()
print(key)
Loading