Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array mpc as service demo transformed to be a cypress test #277

Merged
merged 15 commits into from
Apr 30, 2024
Merged
21 changes: 16 additions & 5 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,30 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Start Server
run: node demos/support/server.ts & echo $! > SERVER_PID
- name: Run Cypress Test for Array MPC as a Service
run: |
echo "Running Cypress test: array-mpc-as-a-service"
node demos/array-mpc-as-a-service/server.js & echo $! > SERVER_PID
sleep 5
echo "Initiating three computational parties"
node demos/array-mpc-as-a-service/compute-party.js config.json &
node demos/array-mpc-as-a-service/compute-party.js config.json &
node demos/array-mpc-as-a-service/compute-party.js config.json &
sleep 5
echo "Running the cypress test for two input parties"
npx cypress run --config-file demos/cypress.config.ts --spec "demos/array-mpc-as-a-service/test.cy.ts"
kill $(cat SERVER_PID) || true

- name: Run Cypress Tests
- name: Run Cypress Tests for Standard Tests
run: |
TESTS=("array-concat" "array-binary-search" "array-merge-sort" "array-substring" "array-bubble-sort" "array-shell-sort" "the-valentine-question" "array-shuffle")
for TEST in "${TESTS[@]}"
do
echo "Running Cypress test: $TEST"
node demos/support/server.ts & echo $! > SERVER_PID
sleep 5
npx cypress run --config-file demos/cypress.config.ts --spec "demos/$TEST/test.cy.ts"
kill $(cat SERVER_PID) # stop the server
node demos/support/server.ts & echo $! > SERVER_PID
kill $(cat SERVER_PID)
done

- name: Stop Server
Expand Down
5 changes: 3 additions & 2 deletions demos/array-mpc-as-a-service/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
<body>
<h1>Connect JIFF</h1>
<label for="computation_id">Computation ID</label><input id="computation_id" value="test"/><br/><br/>
<button id="connectButton" onclick="connect();">Connect</button>
<button id="connectButton" onclick="connect(1);connect(2);">Connect</button>
<br/><br/>
<hr/>
<h1>Sum and Dot Numbers under MPC as a service</h1>
<label for="number">Enter your array: </label> <br/><br/>
<textarea id="inputText" rows="5" cols="35">[1, 3, 2]</textarea> &nbsp; <button onclick="submit();" disabled="disabled" id="processButton">Start</button><br/>
<textarea id="inputText1" rows="5" cols="35">[1, 3, 2]</textarea> &nbsp; <button onclick="submit(1);" id="submit1">Start</button><br/>
<textarea id="inputText2" rows="5" cols="35">[1, 3, 2]</textarea> &nbsp; <button onclick="submit(2);" id="submit2">Start</button><br/>
<div id="output"></div>
</body>
</html>
49 changes: 19 additions & 30 deletions demos/array-mpc-as-a-service/client.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
/**
* Do not modify this file unless you have to.
* This file has UI handlers.
*/

let worker = new Worker('./web-worker.js');
/* global config */

// eslint-disable-next-line no-unused-vars
function connect() {
function connect(party_id) {

Check failure on line 5 in demos/array-mpc-as-a-service/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/client.js#L5

'connect' is defined but never used.
$('#connectButton').prop('disabled', true);
var computation_id = $('#computation_id').val();

var options = { party_count: config.party_count };
options.onError = function (_, error) {
$('#output').append("<p class='error'>"+error+'</p>');
};

var hostname = window.location.hostname.trim();
var port = window.location.port;
Expand All @@ -31,16 +24,18 @@
}

hostname = hostname + ':' + port;
// eslint-disable-next-line no-undef
var jiff = mpc.connect(hostname, computation_id, options, config);
jiff.wait_for(config.compute_parties, function () {
$('#processButton').attr('disabled', false); $('#output').append('<p>Connected to the compute parties!</p>');
worker.postMessage({
type: 'init_' + String(party_id),
hostname: hostname,
computation_id: computation_id,
options: options,
config: config
});
}

// eslint-disable-next-line no-unused-vars
function submit() {
var arr = JSON.parse(document.getElementById('inputText').value);
function submit(party_id) {

Check failure on line 37 in demos/array-mpc-as-a-service/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/client.js#L37

'submit' is defined but never used.
var arr = JSON.parse(document.getElementById('inputText' + String(party_id)).value);

if (arr.length !== config.input_length) {
alert('Please input an array of length ' + config.input_length + '.');
Expand All @@ -53,22 +48,16 @@
return;
}
}

$('#processButton').attr('disabled', true);
$('#output').append('<p>Starting...</p>');

// eslint-disable-next-line no-undef
var promise = mpc.compute(arr);
promise.then(function (opened_array) {
var results = {
sum: opened_array[0],
product: opened_array[1]
};
handleResult(results);
worker.postMessage({
type: 'compute' + String(party_id),
input: arr
});
}

function handleResult(results) {
$('#output').append('<p>The sum is ' + results.sum + ' and the inner product is ' + results.product + '.</p>');
$('#button').attr('disabled', false);
}
worker.onmessage = function (e) {
if ($('#output').is(':empty') && (e.data.type === 'result1' || e.data.type === 'result2')) {
$('#output').append('<p>The sum is ' + e.data.result[0] + ' and the inner product is ' + e.data.result[1] + '.</p>');

Check warning on line 60 in demos/array-mpc-as-a-service/client.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/client.js#L60

HTML passed in to function '$('#output').append'
$('#button').attr('disabled', false);
}
};
31 changes: 31 additions & 0 deletions demos/array-mpc-as-a-service/test.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
describe('Array mpc as a service', () => {
before(() => {
// Load the fixture data before the tests
cy.fixture('mpc_input.json').as('inputData');

Check failure on line 4 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L4

Unsafe call of an `any` typed value.

Check failure on line 4 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L4

Unsafe member access .fixture on an `any` value.
});

it('MPC as a service on Arrays', () => {
// Visit the HTML page
cy.visit('array-mpc-as-a-service/client.html');

Check failure on line 9 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L9

Unsafe call of an `any` typed value.

Check failure on line 9 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L9

Unsafe member access .visit on an `any` value.

// Load the input data and interact with the UI
cy.get('@inputData').then((inputData) => {

Check failure on line 12 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L12

Unsafe call of an `any` typed value.

Check failure on line 12 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L12

Unsafe member access .get on an `any` value.
const arrayInput1 = (inputData as any)['array-mpc-as-a-service']['1'] as number[];

Check warning on line 13 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L13

Unexpected any. Specify a different type.

Check failure on line 13 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L13

Unsafe member access ['array-mpc-as-a-service'] on an `any` value.
const arrayInput2 = (inputData as any)['array-mpc-as-a-service']['2'] as number[];

Check warning on line 14 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L14

This assertion is unnecessary since it does not change the type of the expression.

Check warning on line 14 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L14

Unexpected any. Specify a different type.

Check failure on line 14 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L14

Unsafe member access ['array-mpc-as-a-service'] on an `any` value.

// Start JIFF Clients
cy.get('#connectButton').click();

Check failure on line 17 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L17

Unsafe member access .get on an `any` value.

// Input the array1
cy.get('#inputText1').clear().type(JSON.stringify(arrayInput1));

Check failure on line 20 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L20

Unsafe call of an `any` typed value.

Check failure on line 20 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L20

Unsafe member access .get on an `any` value.
cy.get('#submit1').click();

Check failure on line 21 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L21

Unsafe call of an `any` typed value.

Check failure on line 21 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L21

Unsafe member access .get on an `any` value.

// Input the array2
cy.get('#inputText2').clear().type(JSON.stringify(arrayInput2));

Check failure on line 24 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L24

Unsafe call of an `any` typed value.

Check failure on line 24 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L24

Unsafe member access .get on an `any` value.
cy.get('#submit2').click();

// Check the output
cy.get('#output').should('contain', 'The sum is 12 and the inner product is 14');

Check failure on line 28 in demos/array-mpc-as-a-service/test.cy.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/test.cy.ts#L28

Unsafe call of an `any` typed value.
});
});
});
27 changes: 27 additions & 0 deletions demos/array-mpc-as-a-service/web-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
importScripts('./client-mpc.js', '../../dist/jiff-client.js', '../../lib/ext/jiff-client-restful.js', );

let instance1;
let instance2;

self.onmessage = function (event) {
const data = event.data;

switch (data.type) {
case 'init_1':
instance1 = mpc.connect(data.hostname, data.computation_id, data.options, data.config);

Check failure on line 11 in demos/array-mpc-as-a-service/web-worker.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/web-worker.js#L11

'mpc' is not defined.
break;
case 'init_2':
instance2 = mpc.connect(data.hostname, data.computation_id, data.options, data.config);
break;
case 'compute1':
mpc.compute(data.input, instance1).then((result) => {

Check failure on line 17 in demos/array-mpc-as-a-service/web-worker.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

demos/array-mpc-as-a-service/web-worker.js#L17

'mpc' is not defined.
self.postMessage({ result: result, type: 'result1' });
});
break;
case 'compute2':
mpc.compute(data.input, instance2).then((result) => {
self.postMessage({ result: result, type: 'result2' });
});
break;
}
};
3 changes: 2 additions & 1 deletion demos/fixtures/mpc_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"array-merge-sort": {"1": [1, 3, 2, 4], "2":[1, 3, 2, 4]},
"array-shell-sort": {"1": [1, 3, 2, 4], "2":[1, 3, 2, 4]},
"array-shuffle": {"1": [1, 3, 2], "2":[1, 3, 2]},
"array-substring": {"1": "abcde", "2":"c"}
"array-substring": {"1": "abcde", "2":"c"},
"array-mpc-as-a-service": {"1": [1, 3, 2], "2":[1, 3, 2]}
}
2 changes: 1 addition & 1 deletion lib/ext/jiff-client-restful.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
jiff.socket.connected = true;
jiff.socket.connect = function () {
var connectListeners = jiff.socket.listeners('connect');
for (var i = 1; i < connectListeners.length; i++) {
for (var i = 0; i < connectListeners.length; i++) {
connectListeners[i]();
}
};
Expand Down
Loading
Loading