-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
335 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,126 @@ | ||
const test = require('node:test'); | ||
const { describe, it, mock, beforeEach, afterEach } = 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 | ||
// Remember to use Stub, Mock, and Spy when necessary | ||
|
||
describe("test mail system", ()=>{ | ||
it("Test MailSystem.write", () => { | ||
const mailSystem = new MailSystem(); | ||
const input = ['john', "小明", ""]; | ||
const expectedOutput = [ | ||
"Congrats, john!", | ||
"Congrats, 小明!", | ||
"Congrats, !" | ||
] | ||
input.forEach((input, index)=>{ | ||
assert.equal(mailSystem.write(input), expectedOutput[index]); | ||
}) | ||
}) | ||
|
||
it("Test MailSystem.send", (context) => { | ||
const mailSystem = new MailSystem(); | ||
const randomValues = [0.3, 1, 0.489215, 0.48, 0, -3, 50]; | ||
randomValues.forEach((val)=>{ | ||
context.mock.method(Math, 'random', () => val); | ||
assert.equal(mailSystem.send("test", "test"), val>0.5); | ||
assert.equal(Math.random.mock.calls.length, 1) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
const util = require('util') | ||
describe("test Application system", ()=>{ | ||
const mockData = "john\nAlbert\n黃曉明\n\n321\na;dklfj \nKim, Jong-Chi"; | ||
const mockPeople = [123, 456] | ||
const mockSelected = [789, 101112] | ||
const mockMailSystem = { | ||
write(name){return "mock mail system write result"}, | ||
send(name, context){return "mock mail system send result"} | ||
} | ||
beforeEach(()=>{ | ||
mock.restoreAll(); | ||
}) | ||
afterEach(()=>{ | ||
mock.restoreAll(); | ||
}) | ||
|
||
it("Test Application constructor", async (t) => { | ||
t.mock.method(Application.prototype, "getNames", async ()=>[mockPeople, mockSelected]) | ||
const app = new Application() | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
assert.equal(app.people, mockPeople) | ||
assert.equal(app.selected, mockSelected) | ||
}) | ||
|
||
it("Test Application.getName", async () => { | ||
mock.method(util, 'promisify', (fun) => | ||
(fileName) => | ||
new Promise((resolve, reject)=>{ | ||
resolve(mockData); | ||
}) | ||
); | ||
delete require.cache[require.resolve('./main')] | ||
const { Application } = require('./main'); | ||
const app = new Application(); | ||
const res = await app.getNames(); | ||
assert.deepEqual(res, [mockData.split('\n'), []]) | ||
}) | ||
|
||
it("Test Application.getRandomPerson", async (t) => { | ||
t.mock.method(Application.prototype, "getNames", async ()=>[[1, 2, 3, 4, 5], [1, 2, 3]]) | ||
const app = new Application(); | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
const randomVals = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]; | ||
const expectedOut = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]; | ||
randomVals.forEach((e, i)=>{ | ||
t.mock.method(Math, 'random', () => e); | ||
assert.equal(app.getRandomPerson(e), expectedOut[i]); | ||
}) | ||
}) | ||
|
||
it("test Application.selectNextPerson all selected", async(t)=>{ | ||
t.mock.method(Application.prototype, "getNames", async ()=>[[1, 2, 3], [1, 2, 3]]) | ||
const app = new Application(); | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
assert.equal(app.selectNextPerson(), null) | ||
}) | ||
it("test Application.selectNextPerson first attempt success", async (t)=>{ | ||
t.mock.method(Application.prototype, "getNames", async ()=>[[1, 2, 3], [2, 3]]) | ||
t.mock.method(Application.prototype, "getRandomPerson", () => 1) | ||
const app = new Application(); | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
console.log(app.getRandomPerson()) | ||
assert.equal(app.selectNextPerson(), 1) | ||
}) | ||
it("test Application.selectNextPerson third attempt success", async (t)=>{ | ||
let counter = 3; | ||
t.mock.method(Application.prototype, "getNames", async ()=>[[1, 2, 3], [2, 3]]); | ||
t.mock.method(Application.prototype, "getRandomPerson", () => counter--); | ||
const app = new Application(); | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
assert.equal(app.selectNextPerson(), 1) | ||
assert.equal(app.getRandomPerson.mock.calls.length, 3) | ||
}) | ||
|
||
it("test Application.notifySelected", async (t)=>{ | ||
t.mock.method(Application.prototype, "getNames", async ()=>[[1, 2, 3], [2, 3]]); | ||
t.mock.method(Application.prototype, "getRandomPerson", () => counter--); | ||
const app = new Application(); | ||
app.mailSystem = mockMailSystem; | ||
t.mock.method(mockMailSystem, "send") | ||
t.mock.method(mockMailSystem, "write") | ||
const timer = new Promise((resolve, reject)=>{setTimeout(()=>{resolve()}, 100)}); | ||
await timer; | ||
app.notifySelected(); | ||
assert.equal(mockMailSystem.send.mock.calls.length, 2) | ||
assert.equal(mockMailSystem.write.mock.calls.length, 2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,74 @@ | ||
const {describe, it} = require('node:test'); | ||
const {describe, it, before, beforeEach, after, afterEach, mock} = require('node:test'); | ||
const assert = require('assert'); | ||
const { Calculator } = require('./main'); | ||
|
||
// TODO: write your tests here | ||
describe('Calculator.exp', ()=>{ | ||
const calculator = new Calculator(); | ||
const inputs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
beforeEach(()=>{ | ||
mock.restoreAll(); | ||
}) | ||
|
||
it('testing not number or infinity input', (t)=>{ | ||
const oppErr = new Error('unsupported operand type'); | ||
t.mock.method(Number, "isFinite", (val)=> false ) | ||
inputs.forEach(e=>{ | ||
assert.throws(()=>{calculator.exp(e)}, oppErr) | ||
}) | ||
}) | ||
|
||
it('testing infinity result', (t)=>{ | ||
const overflowErr = new Error('overflow'); | ||
t.mock.method(Math, "exp", (val)=> Infinity ) | ||
inputs.forEach(e=>{ | ||
assert.throws(()=>{calculator.exp(e)}, overflowErr) | ||
}) | ||
}) | ||
|
||
it('testing normal input', (t)=>{ | ||
inputs.forEach(e=>{ | ||
t.mock.method(Math, "exp", (val)=> e ) | ||
assert.equal(calculator.exp(e), e); | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Calculator.log', ()=>{ | ||
const calculator = new Calculator(); | ||
const inputs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
beforeEach(()=>{ | ||
mock.restoreAll(); | ||
}) | ||
|
||
it('testing not number or infinity input', (t)=>{ | ||
const oppErr = new Error('unsupported operand type'); | ||
t.mock.method(Number, "isFinite", (val)=> false ) | ||
inputs.forEach(e=>{ | ||
assert.throws(()=>{calculator.log(e)}, oppErr) | ||
}) | ||
}) | ||
|
||
it('testing negative infinity result', (t)=>{ | ||
const domainErr1 = new Error('math domain error (1)'); | ||
t.mock.method(Math, "log", (val)=> -Infinity ) | ||
inputs.forEach(e=>{ | ||
assert.throws(()=>{calculator.log(e)}, domainErr1) | ||
}) | ||
}) | ||
|
||
it('testing negative infinity result', (t)=>{ | ||
const domainErr2 = new Error('math domain error (2)'); | ||
t.mock.method(Number, "isNaN", (val)=> -true ) | ||
inputs.forEach(e=>{ | ||
assert.throws(()=>{calculator.log(e)}, domainErr2) | ||
}) | ||
}) | ||
|
||
it('testing normal input', (t)=>{ | ||
inputs.forEach(e=>{ | ||
t.mock.method(Math, "log", (val)=> e ) | ||
assert.equal(calculator.log(e), e); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
login: login.o | ||
|
||
login.o: login.c | ||
|
||
.PHONY: clean | ||
clean: | ||
rm login login.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Lab7 | ||
|
||
## Introduction | ||
|
||
In this lab, you will write a python script with Angr to find the password in executalbe file named 'login'. | ||
|
||
## Preparation (Important!!!) | ||
|
||
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`) | ||
2. `git checkout -b lab7` (**NOT** your student ID !!!) | ||
|
||
## Requirement | ||
|
||
1. (100%) Detect the condition that login will print 'Login successful' if login success and print 'Login failed' if login fail, find the input of successful condition by Angr. | ||
|
||
Please note that you must not alter files other than `sol.py` or just print the input. You will get 0 points if | ||
|
||
1. you modify other files to achieve requirements. | ||
2. you can't pass all CI on your PR. | ||
|
||
## Submission | ||
|
||
You need to open a pull request to your branch (e.g. 311XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements. | ||
|
||
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
int encrypt(int a1, int a2) { | ||
if ( a1 <= 0x40 || a1 > 90 ) { | ||
puts("Login failed"); | ||
exit(1); | ||
} | ||
return (0x1F * a2 + a1 - 65) % 26 + 65; | ||
} | ||
|
||
int main(void) { | ||
char secret[0x20] = "VXRRJEURXDASBFHM"; | ||
char pwd[0x20] = {0}; | ||
|
||
printf("Enter the password: "); | ||
scanf("%16s", pwd); | ||
for ( int j = 0; j < 0x10; ++j ) | ||
pwd[j] = encrypt(pwd[j], j + 8); | ||
if ( !strcmp(secret, pwd) ) | ||
puts("Login successful"); | ||
else | ||
puts("Login failed"); | ||
return 0; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
# Check for unwanted files | ||
for file in *; do | ||
if [[ $file != "login.c" && $file != "sol.py" && $file != "Makefile" && $file != "README.md" && $file != "validate.sh" ]]; then | ||
echo "[!] Unwanted file detected: $file." | ||
exit 1 | ||
fi | ||
done | ||
|
||
test_path="${BASH_SOURCE[0]}" | ||
solution_path="$(realpath .)" | ||
tmp_dir=$(mktemp -d -t lab7-XXXXXXXXXX) | ||
answer="" | ||
|
||
cd $tmp_dir | ||
|
||
pip install angr | ||
rm -rf * | ||
cp $solution_path/Makefile . | ||
cp $solution_path/*.c . | ||
cp $solution_path/sol.py . | ||
|
||
make | ||
result=$(python3 sol.py) | ||
if [[ $result != "b'HETOBRCUVWOBFEBB'" ]]; then | ||
echo "[!] Expected: " | ||
echo "b'HETOBRCUVWOBFEBB'" | ||
echo "" | ||
echo "[!] Actual: " | ||
echo $result | ||
echo "" | ||
exit 1 | ||
else | ||
echo "[V] Pass" | ||
fi | ||
|
||
rm -rf $tmp_dir | ||
|
||
exit 0 | ||
|
||
# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2: |