Skip to content

Commit

Permalink
Merge branch 'remove/events' into fix/mem-leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur22 authored Jan 20, 2025
2 parents 3ae80f8 + d8a6212 commit c739495
Show file tree
Hide file tree
Showing 251 changed files with 10,772 additions and 4,294 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/wpt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defaults:
shell: bash

jobs:
streams:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -18,9 +18,16 @@ jobs:
with:
go-version: 1.23.x
check-latest: true
- name: Run tests
# TODO: combine WebPlatform tests checkout & patch into the singe step
- name: Run Streams Tests
run: |
set -x
cd js/modules/k6/experimental/streams/tests
sh checkout.sh
go test ../... -tags=wpt
- name: Run Webcrypto Tests
run: |
set -x
cd js/modules/k6/webcrypto/tests
sh checkout.sh
go test ./... -tags=wpt
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ script.js

/packaging/.env
/packaging/*.gpg

# web platform tests
js/modules/k6/webcrypto/tests/wpt/
17 changes: 16 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,15 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib

conf = cliConf.Apply(fileConf)

warnOnShortHandOverride(conf.Options, runnerOpts, "script", gs.Logger)
conf = conf.Apply(Config{Options: runnerOpts})

conf = conf.Apply(envConf).Apply(cliConf)
warnOnShortHandOverride(conf.Options, envConf.Options, "env", gs.Logger)
conf = conf.Apply(envConf)

warnOnShortHandOverride(conf.Options, cliConf.Options, "cli", gs.Logger)
conf = conf.Apply(cliConf)

conf = applyDefault(conf)

// TODO(imiric): Move this validation where it makes sense in the configuration
Expand All @@ -215,6 +221,15 @@ func getConsolidatedConfig(gs *state.GlobalState, cliConf Config, runnerOpts lib
return conf, nil
}

func warnOnShortHandOverride(a, b lib.Options, bName string, logger logrus.FieldLogger) {
if a.Scenarios != nil &&
(b.Duration.Valid || b.Iterations.Valid || b.Stages != nil || b.Scenarios != nil) {
logger.Warnf(
"%q level configuration overrode scenarios configuration entirely",
bName)
}
}

// applyDefault applies the default options value if it is not specified.
// This happens with types which are not supported by "gopkg.in/guregu/null.v3".
//
Expand Down
4 changes: 3 additions & 1 deletion cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ func getConfigConsolidationTestCases() []configConsolidationTestCase {
env: []string{"K6_ITERATIONS=25"},
cli: []string{"--vus", "12"},
},
exp{},
exp{
logWarning: true,
},
verifySharedIters(I(12), I(25)),
},
{
Expand Down
11 changes: 11 additions & 0 deletions examples/webcrypto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# k6 webcrypto examples

In this directory, you will find examples of how to use the k6's `webcrypto` module in your k6 scripts.

> [!IMPORTANT]
> We do run the tests based on these examples, that's why we have a simple convention for each example:
>
> * It should do any `console.log`. Since we try to detect that output (log) contain `INFO` keyword.
> * It should NOT `try/catch` exceptions. Since we try to detect if keywords like `"Uncaught"` and `"ERRO"` should not appear in the output (logs).
See [`../../js/modules/k6/webcrypto/cmd_run_test.go`](../../js/modules/k6/webcrypto/cmd_run_test.go) for more details.
54 changes: 54 additions & 0 deletions examples/webcrypto/derive_bits/derive-bits-ecdh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
// Generate a key pair for Alice
const aliceKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);

// Generate a key pair for Bob
const bobKeyPair = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256",
},
true,
["deriveKey", "deriveBits"]
);

// Derive shared secret for Alice
const aliceSharedSecret = await deriveSharedSecret(
aliceKeyPair.privateKey,
bobKeyPair.publicKey
);

// Derive shared secret for Bob
const bobSharedSecret = await deriveSharedSecret(
bobKeyPair.privateKey,
aliceKeyPair.publicKey
);

console.log("alice shared secret: " + printArrayBuffer(aliceSharedSecret));
console.log("bob shared secret: " + printArrayBuffer(bobSharedSecret));
}

async function deriveSharedSecret(privateKey, publicKey) {
return crypto.subtle.deriveBits(
{
name: "ECDH",
public: publicKey, // An ECDH public key from the other party
},
privateKey, // Your ECDH private key
256 // the number of bits to derive
);
}

const printArrayBuffer = (buffer) => {
let view = new Uint8Array(buffer);
return Array.from(view);
};
22 changes: 22 additions & 0 deletions examples/webcrypto/digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { crypto } from "k6/experimental/webcrypto";

export default function () {
crypto.subtle.digest("SHA-256", stringToArrayBuffer("Hello, world!")).then(
(hash) => {
console.log(arrayBufferToHex(hash));
},
(err) => {
throw err;
}
);
}

function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}

function stringToArrayBuffer(s) {
return Uint8Array.from(new String(s), (x) => x.charCodeAt(0));
}
53 changes: 53 additions & 0 deletions examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-cbc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
const key = await crypto.subtle.generateKey(
{
name: "AES-CBC",
length: 256,
},
true,
["encrypt", "decrypt"]
);

const encoded = stringToArrayBuffer("Hello, World!");
const iv = crypto.getRandomValues(new Uint8Array(16));

const ciphertext = await crypto.subtle.encrypt(
{
name: "AES-CBC",
iv: iv,
},
key,
encoded
);

const plaintext = await crypto.subtle.decrypt(
{
name: "AES-CBC",
iv: iv,
},
key,
ciphertext
);

console.log(
"deciphered text == original text: ",
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)
);
}

function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}

function stringToArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
55 changes: 55 additions & 0 deletions examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-ctr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
const key = await crypto.subtle.generateKey(
{
name: "AES-CTR",
length: 256,
},
true,
["encrypt", "decrypt"]
);

const encoded = string2ArrayBuffer("Hello World");
const counter = crypto.getRandomValues(new Uint8Array(16));

const ciphertext = await crypto.subtle.encrypt(
{
name: "AES-CTR",
counter,
length: 64,
},
key,
encoded
);

const plaintext = await crypto.subtle.decrypt(
{
name: "AES-CTR",
counter,
length: 64,
},
key,
ciphertext
);

console.log(
"deciphered text == original text: ",
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)
);
}

function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}

function string2ArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
53 changes: 53 additions & 0 deletions examples/webcrypto/encrypt_decrypt/encrypt-decrypt-aes-gcm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
const key = await crypto.subtle.generateKey(
{
name: "AES-GCM",
length: 256,
},
true,
["encrypt", "decrypt"]
);

const encoded = string2ArrayBuffer("Hello World");
const iv = crypto.getRandomValues(new Uint8Array(12));

const ciphertext = await crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: iv,
},
key,
encoded
);

const plaintext = await crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: iv,
},
key,
ciphertext
);

console.log(
"deciphered text == original text: ",
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)
);
}

function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}

function string2ArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
59 changes: 59 additions & 0 deletions examples/webcrypto/encrypt_decrypt/encrypt-decrypt-rsa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
const keyPair = await crypto.subtle.generateKey(
{
name: "RSA-OAEP",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: { name: "SHA-1" },
},
true,
["encrypt", "decrypt"]
);

const encoded = stringToArrayBuffer("Hello, World!");

const cipherText = await crypto.subtle.encrypt(
{
name: "RSA-OAEP",
},
keyPair.publicKey,
encoded
);

// ciphertext.byteLength * 8, vector.privateKey.algorithm.modulusLength
console.log("cipherText's byteLength: ", cipherText.byteLength * 8);
console.log(
"algorithm's modulusLength: ",
keyPair.privateKey.algorithm.modulusLength
);

const plaintext = await crypto.subtle.decrypt(
{
name: "RSA-OAEP",
},
keyPair.privateKey,
cipherText
);

console.log(
"deciphered text == original text: ",
arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)
);
}

function arrayBufferToHex(buffer) {
return [...new Uint8Array(buffer)]
.map((x) => x.toString(16).padStart(2, "0"))
.join("");
}

function stringToArrayBuffer(str) {
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
Loading

0 comments on commit c739495

Please sign in to comment.