From 0499706dd711c89e74546b57b5dd149a4962dd4c Mon Sep 17 00:00:00 2001 From: tnoo436792 <30401693+tnoo436792@users.noreply.github.com> Date: Mon, 8 Apr 2024 09:14:25 +0800 Subject: [PATCH 1/5] Update main_test.js --- lab2/main_test.js | 88 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..c4595554 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,88 @@ -const test = require('node:test'); +const TestLab = require('node:test'); +const Test = require('node:test'); const assert = require('assert'); +const fileSystem = require('fs'); + +// 模擬 fileSystem 模組的 readFile 方法 +TestLab.mock.method(fileSystem, 'readFile', (file, options, callback) => { +Test.mock.method(fileSystem, 'readFile', (file, options, callback) => { + callback(null, 'ray\nnina\neric'); +}); + 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 +// 測試 MailSystem 的 write 方法 +TestLab('MailSystem_write()', () => { +Test('MailSystem_write()', () => { + const mailSystem = new MailSystem(); + assert.strictEqual(mailSystem.write('ray'), 'Congrats, ray!'); + assert.strictEqual(mailSystem.write(null), 'Congrats, null!'); + assert.strictEqual(mailSystem.write('3345678'), 'Congrats, 3345678!'); +}); + +// 測試 MailSystem 的 send 方法 +TestLab('MailSystem_send()', () => { +Test('MailSystem_send()', () => { + const mailSystem = new MailSystem(); + const name = 'ray'; + TestLab.mock.method(Math, 'random', () => 0.7); // 成功概率高 + Test.mock.method(Math, 'random', () => 0.7); + assert.strictEqual(mailSystem.send(name, 'success'), true); + TestLab.mock.method(Math, 'random', () => 0.3); // 失敗概率高 + Test.mock.method(Math, 'random', () => 0.3); + assert.strictEqual(mailSystem.send(name, 'fail'), false); +}); + +// 測試 Application 的 getNames 方法 +TestLab('Application_getNames()', async () => { +Test('Application_getNames()', async () => { + const application = new Application(); + const nameList = ['ray', 'nina', 'eric']; + const names = await application.getNames(); + assert.deepStrictEqual(names, [nameList, []]); +}); + +// 測試 Application 的 getRandomPerson 方法 +TestLab('Application_getRandomPerson()', async () => { +Test('Application_getRandomPerson()', async () => { + const application = new Application(); + const names = await application.getNames(); + TestLab.mock.method(Math, 'random', () => 0); + Test.mock.method(Math, 'random', () => 0); + assert.strictEqual(application.getRandomPerson(), 'ray'); + TestLab.mock.method(Math, 'random', () => 0.4); + Test.mock.method(Math, 'random', () => 0.4); + assert.strictEqual(application.getRandomPerson(), 'nina'); + TestLab.mock.method(Math, 'random', () => 0.7); + Test.mock.method(Math, 'random', () => 0.7); + assert.strictEqual(application.getRandomPerson(), 'eric'); +}); + +// 測試 Application 的 selectNextPerson 方法 +TestLab('Application_selectNextPerson()', async () => { +Test('Application_selectNextPerson()', async () => { + const application = new Application(); + const names = await application.getNames(); + application.selected = ['ray']; + let counter = 0; + TestLab.mock.method(application, 'getRandomPerson', () => { + Test.mock.method(application, 'getRandomPerson', () => { + if (counter < names[0].length) { + return names[0][counter++]; + } else { +@@ -68,12 +68,12 @@ TestLab('Application_selectNextPerson()', async () => { +}); + +// 測試 Application 的 notifySelected 方法 +TestLab('Application_notifySelected()', async () => { +Test('Application_notifySelected()', async () => { + const application = new Application(); + application.people = ['ray', 'nina', 'eric']; + application.selected = ['ray', 'nina', 'eric']; + application.mailSystem.send = TestLab.mock.fn(application.mailSystem.send); + application.mailSystem.write = TestLab.mock.fn(application.mailSystem.write); + application.mailSystem.send = Test.mock.fn(application.mailSystem.send); + application.mailSystem.write = Test.mock.fn(application.mailSystem.write); + application.notifySelected(); + assert.strictEqual(application.mailSystem.send.mock.calls.length, 3); + assert.strictEqual(application.mailSystem.write.mock.calls.length, 3); From af271026ea71b973e60217a9dcd9c58bdb25d1cb Mon Sep 17 00:00:00 2001 From: tnoo436792 <30401693+tnoo436792@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:37:42 +0800 Subject: [PATCH 2/5] Update main_test.js From ae38e6e81aa66057c1fd4c9a0c63183d3a76f68f Mon Sep 17 00:00:00 2001 From: tnoo436792 <30401693+tnoo436792@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:38:59 +0800 Subject: [PATCH 3/5] Update main_test.js --- lab2/main_test.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index c4595554..9d4484da 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,10 +1,8 @@ -const TestLab = require('node:test'); const Test = require('node:test'); const assert = require('assert'); const fileSystem = require('fs'); // 模擬 fileSystem 模組的 readFile 方法 -TestLab.mock.method(fileSystem, 'readFile', (file, options, callback) => { Test.mock.method(fileSystem, 'readFile', (file, options, callback) => { callback(null, 'ray\nnina\neric'); }); @@ -12,7 +10,6 @@ Test.mock.method(fileSystem, 'readFile', (file, options, callback) => { const { Application, MailSystem } = require('./main'); // 測試 MailSystem 的 write 方法 -TestLab('MailSystem_write()', () => { Test('MailSystem_write()', () => { const mailSystem = new MailSystem(); assert.strictEqual(mailSystem.write('ray'), 'Congrats, ray!'); @@ -21,20 +18,16 @@ Test('MailSystem_write()', () => { }); // 測試 MailSystem 的 send 方法 -TestLab('MailSystem_send()', () => { Test('MailSystem_send()', () => { const mailSystem = new MailSystem(); const name = 'ray'; - TestLab.mock.method(Math, 'random', () => 0.7); // 成功概率高 Test.mock.method(Math, 'random', () => 0.7); assert.strictEqual(mailSystem.send(name, 'success'), true); - TestLab.mock.method(Math, 'random', () => 0.3); // 失敗概率高 Test.mock.method(Math, 'random', () => 0.3); assert.strictEqual(mailSystem.send(name, 'fail'), false); }); // 測試 Application 的 getNames 方法 -TestLab('Application_getNames()', async () => { Test('Application_getNames()', async () => { const application = new Application(); const nameList = ['ray', 'nina', 'eric']; @@ -43,46 +36,41 @@ Test('Application_getNames()', async () => { }); // 測試 Application 的 getRandomPerson 方法 -TestLab('Application_getRandomPerson()', async () => { Test('Application_getRandomPerson()', async () => { const application = new Application(); const names = await application.getNames(); - TestLab.mock.method(Math, 'random', () => 0); Test.mock.method(Math, 'random', () => 0); assert.strictEqual(application.getRandomPerson(), 'ray'); - TestLab.mock.method(Math, 'random', () => 0.4); Test.mock.method(Math, 'random', () => 0.4); assert.strictEqual(application.getRandomPerson(), 'nina'); - TestLab.mock.method(Math, 'random', () => 0.7); Test.mock.method(Math, 'random', () => 0.7); assert.strictEqual(application.getRandomPerson(), 'eric'); }); // 測試 Application 的 selectNextPerson 方法 -TestLab('Application_selectNextPerson()', async () => { Test('Application_selectNextPerson()', async () => { const application = new Application(); const names = await application.getNames(); application.selected = ['ray']; let counter = 0; - TestLab.mock.method(application, 'getRandomPerson', () => { Test.mock.method(application, 'getRandomPerson', () => { - if (counter < names[0].length) { - return names[0][counter++]; - } else { -@@ -68,12 +68,12 @@ TestLab('Application_selectNextPerson()', async () => { + return ['ray', 'nina', 'eric'][counter++]; + }); + assert.strictEqual(application.selectNextPerson(), 'nina'); + assert.deepStrictEqual(application.selected, ['ray', 'nina']); + assert.strictEqual(application.selectNextPerson(), 'eric'); + assert.deepStrictEqual(application.selected, ['ray', 'nina', 'eric']); + assert.strictEqual(application.selectNextPerson(), null); }); // 測試 Application 的 notifySelected 方法 -TestLab('Application_notifySelected()', async () => { Test('Application_notifySelected()', async () => { const application = new Application(); application.people = ['ray', 'nina', 'eric']; application.selected = ['ray', 'nina', 'eric']; - application.mailSystem.send = TestLab.mock.fn(application.mailSystem.send); - application.mailSystem.write = TestLab.mock.fn(application.mailSystem.write); application.mailSystem.send = Test.mock.fn(application.mailSystem.send); application.mailSystem.write = Test.mock.fn(application.mailSystem.write); application.notifySelected(); assert.strictEqual(application.mailSystem.send.mock.calls.length, 3); assert.strictEqual(application.mailSystem.write.mock.calls.length, 3); +}); From d7abd8247e154dcfdf8a871b751e76c2b6930eb8 Mon Sep 17 00:00:00 2001 From: tnoo436792 <30401693+tnoo436792@users.noreply.github.com> Date: Mon, 8 Apr 2024 15:40:25 +0800 Subject: [PATCH 4/5] Update main_test.js From 2b2cc959b8fd2735b191f122ee65323df6ed114e Mon Sep 17 00:00:00 2001 From: tnoo436792 <30401693+tnoo436792@users.noreply.github.com> Date: Wed, 19 Jun 2024 22:17:19 +0800 Subject: [PATCH 5/5] Update Answer.md --- lab6/Answer.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lab6/Answer.md b/lab6/Answer.md index fabc82e6..5347e914 100644 --- a/lab6/Answer.md +++ b/lab6/Answer.md @@ -3,10 +3,44 @@ ID: ### Fuzz Monitor ``` - +american fuzzy lop ++4.08c {default} (../src/bmpcomp) [fast] +┌─ process timing ────────────────────────────────────┬─ overall results ────┐ +│ run time : 0 days, 0 hrs, 35 min, 22 sec │ cycles done : 2 │ +│ last new find : 0 days, 0 hrs, 15 min, 58 sec │ corpus count : 16 │ +│last saved crash : 0 days, 0 hrs, 35 min, 19 sec │saved crashes : 1 │ +│ last saved hang : 0 days, 0 hrs, 35 min, 16 sec │ saved hangs : 2 │ +├─ cycle progress ─────────────────────┬─ map coverage┴──────────────────────┤ +│ now processing : 14.8 (87.5%) │ map density : 0.00% / 0.00% │ +│ runs timed out : 0 (0.00%) │ count coverage : 2.55 bits/tuple │ +├─ stage progress ─────────────────────┼─ findings in depth ─────────────────┤ +│ now trying : splice 6 │ favored items : 3 (18.75%) │ +│ stage execs : 7/12 (58.33%) │ new edges on : 3 (18.75%) │ +│ total execs : 12.6k │ total crashes : 6212 (1 saved) │ +│ exec speed : 0.00/sec (zzzz...) │ total tmouts : 2408 (0 saved) │ +├─ fuzzing strategy yields ────────────┴─────────────┬─ item geometry ───────┤ +│ bit flips : disabled (default, enable with -D) │ levels : 5 │ +│ byte flips : disabled (default, enable with -D) │ pending : 7 │ +│ arithmetics : disabled (default, enable with -D) │ pend fav : 0 │ +│ known ints : disabled (default, enable with -D) │ own finds : 15 │ +│ dictionary : n/a │ imported : 0 │ +│havoc/splice : 15/3046, 1/9327 │ stability : 100.00% │ +│py/custom/rq : unused, unused, unused, unused ├───────────────────────┘ +│ trim/eff : 99.98%/92, disabled │ [cpu000: 75%] +└─ strategy: explore ────────── state: in progress ──┘ ``` ### Run Crash Result ``` +size of Herder 54 +AddressSanitizer:DEADLYSIGNAL +================================================================= +==70360==ERROR: AddressSanitizer: stack-overflow on address 0x7ffd37a9b7d8 (pc 0x56019c8d1ee3 bp 0x7ffd3dafc2b0 sp 0x7ffd37a9b7e0 T0) + #0 0x56019c8d1ee3 in main /home/kali/Desktop/112-spring-software-testing-and-secure-programming-511558018/lab6/src/hw0302.c:46 + #1 0x7f20002456c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 + #2 0x7f2000245784 in __libc_start_main_impl ../csu/libc-start.c:360 + #3 0x56019c8d2900 in _start (/home/kali/Desktop/112-spring-software-testing-and-secure-programming-511558018/lab6/src/bmpcomp+0x2900) (BuildId: 1607eebff355e3d1d8f0d2e259395a3b03ac8705) + +SUMMARY: AddressSanitizer: stack-overflow /home/kali/Desktop/112-spring-software-testing-and-secure-programming-511558018/lab6/src/hw0302.c:46 in main +==70360==ABORTING ```