Skip to content

Commit

Permalink
Merge branch 'b122552' into lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertDoggyLin authored May 9, 2024
2 parents 456d4c0 + 424f783 commit cf9a91c
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 18 deletions.
30 changes: 22 additions & 8 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@ const assert = require('assert');
const { MyClass, Student } = require('./main');

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
assert.equal(myClass.addStudent(69), -1);
const student = new Student();
const name = "John";
student.setName(name);
myClass.addStudent(student);
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const student = new Student();
const name = "John";
student.setName(name);
const id = myClass.addStudent(student);
assert.strictEqual(myClass.getStudentById(-1), null);
assert.strictEqual(myClass.getStudentById(50), null);
assert.strictEqual(myClass.getStudentById(id), student);
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
const student = new Student();
student.setName(69);
student.setName("John");
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
const student = new Student();
const name = "John"
assert.equal(student.getName(), '');
student.setName(name);
assert.strictEqual(student.getName(), name);
});
124 changes: 122 additions & 2 deletions lab2/main_test.js
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)
})
})
71 changes: 70 additions & 1 deletion lab3/main_test.js
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);
})
})
})
28 changes: 21 additions & 7 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@ const puppeteer = require('puppeteer');

// Navigate the page to a URL
await page.goto('https://pptr.dev/');
// Set screen size
await page.setViewport({width: 1080, height: 1024});

const searchBtn = '.DocSearch-Button';
await page.waitForSelector(searchBtn);
await page.click(searchBtn);

// Hints:
// Click search button
// Type into search box
// Wait for search result
// Get the `Docs` result section
// Click on first result in `Docs` section
// Locate the title
// Print the title
const input = '#docsearch-input'
await page.waitForSelector(input);
await page.type(input, 'chipi chipi chapa chapa');

const linkBtn = 'text/Experimental WebDriver BiDi support'
await page.waitForSelector(linkBtn);
await page.click(linkBtn);

// Locate the full title with a unique string
const textSelector = await page.waitForSelector('h1');

const fullTitle = await textSelector?.evaluate(el => el.textContent);

// Print the full title
console.log(fullTitle);

// Close the browser
await browser.close();
Expand Down
7 changes: 7 additions & 0 deletions lab7/Makefile
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
25 changes: 25 additions & 0 deletions lab7/README.md
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.
26 changes: 26 additions & 0 deletions lab7/login.c
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 added lab7/sol.py
Empty file.
42 changes: 42 additions & 0 deletions lab7/validate.sh
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:

0 comments on commit cf9a91c

Please sign in to comment.