diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..d0d0a4d4 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -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 \ No newline at end of file +/* + 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'); + } +}); + + diff --git a/lab8/solve.py b/lab8/solve.py index 9ab3ee2f..f8089f09 100755 --- a/lab8/solve.py +++ b/lab8/solve.py @@ -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)