Skip to content

Commit

Permalink
Add test and fix caching_sha2_password (#142)
Browse files Browse the repository at this point in the history
* ci run tests with DB password

* fix caching_sha2_password encrypt

* fix ci with mysql:8 unix socket

* ci disallow tcp when testing unix socket
  • Loading branch information
lideming authored Nov 26, 2022
1 parent 14e4036 commit 8c1fde1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ jobs:
run: |
sudo mkdir -p /var/run/mysqld/tmp
sudo chmod -R 777 /var/run/mysqld
docker container run --rm -d -p 3306:3306 \
docker container run --name mysql --rm -d -p 3306:3306 \
-v /var/run/mysqld:/var/run/mysqld \
-v /var/run/mysqld/tmp:/tmp \
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
-e MYSQL_ROOT_PASSWORD=root \
${{ matrix.DB_VERSION }}
./.github/workflows/wait-for-mysql.sh
- name: Run tests (TCP)
Expand All @@ -61,7 +61,8 @@ jobs:
if [[ "${{ matrix.DB_VERSION }}" == "mysql:5.5" ]]; then
SOCKPATH=/var/run/mysqld/tmp/mysql.sock
fi
echo "DROP USER 'root'@'localhost';" | docker exec -i mysql mysql -proot
DB_SOCKPATH=$SOCKPATH TEST_METHODS=unix \
deno test --unstable --allow-env --allow-net=127.0.0.1:3306 \
deno test --unstable --allow-env \
--allow-read=/var/run/mysqld/ --allow-write=/var/run/mysqld/ \
./test.ts
10 changes: 3 additions & 7 deletions .github/workflows/wait-for-mysql.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/bin/sh

echo "Waiting for MySQL"
for i in `seq 1 10`;
for i in `seq 1 30`;
do
result="$(echo '\q' | mysql -h 127.0.0.1 -uroot -P 3306 2>&1 > /dev/null)"
if [ "$result" = "" ]; then
echo "Success waiting for MySQL"
exit 0
fi
echo '\q' | mysql -h 127.0.0.1 -uroot --password=root -P 3306 && exit 0
>&2 echo "MySQL is waking up"
sleep 10
sleep 1
done

echo "Failed waiting for MySQL" && exit 1
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type { Deferred } from "https://deno.land/[email protected]/async/mod.ts";
export { deferred, delay } from "https://deno.land/[email protected]/async/mod.ts";
export { format as byteFormat } from "https://deno.land/x/[email protected]/mod.ts";
export { createHash } from "https://deno.land/[email protected]/hash/mod.ts";
export { decode as base64Decode } from "https://deno.land/[email protected]/encoding/base64.ts";
export type {
SupportedAlgorithm,
} from "https://deno.land/[email protected]/hash/mod.ts";
Expand Down
4 changes: 2 additions & 2 deletions src/auth_plugin/caching_sha2_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function start(
): Promise<handler> {
scramble = scramble_;
password = password_;
return { done: false, next: await authMoreResponse };
return { done: false, next: authMoreResponse };
}

async function authMoreResponse(packet: ReceivePacket): Promise<handler> {
Expand All @@ -31,7 +31,7 @@ async function authMoreResponse(packet: ReceivePacket): Promise<handler> {
if (statusFlag === AuthStatusFlags.FullAuth) {
authMoreData = new Uint8Array([REQUEST_PUBLIC_KEY]);
done = false;
next = await encryptWithKey;
next = encryptWithKey;
}
if (statusFlag === AuthStatusFlags.FastPath) {
done = false;
Expand Down
10 changes: 8 additions & 2 deletions src/auth_plugin/crypt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { base64Decode } from "../../deps.ts";

async function encryptWithPublicKey(
key: string,
data: Uint8Array,
): Promise<ArrayBuffer> {
const pemHeader = "-----BEGIN PUBLIC KEY-----\n";
const pemFooter = "\n-----END PUBLIC KEY-----";
key = key.trim();
key = key.substring(pemHeader.length, key.length - pemFooter.length);
const importedKey = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(key),
"spki",
base64Decode(key),
{ name: "RSA-OAEP", hash: "SHA-256" },
false,
["encrypt"],
Expand Down
2 changes: 1 addition & 1 deletion src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Connection {
await this.nextPacket();
}
if (result.next) {
result = result.next(receive);
result = await result.next(receive);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions test.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ const { DB_PORT, DB_NAME, DB_PASSWORD, DB_USER, DB_HOST, DB_SOCKPATH } = Deno
.env.toObject();
const port = DB_PORT ? parseInt(DB_PORT) : 3306;
const db = DB_NAME || "test";
const password = DB_PASSWORD;
const password = DB_PASSWORD || "root";
const username = DB_USER || "root";
const hostname = DB_HOST || "127.0.0.1";
const sockPath = DB_SOCKPATH || "/var/run/mysqld/mysqld.sock";
const testMethods =
Deno.env.get("TEST_METHODS")?.split(",") as ("tcp" | "unix")[] || ["tcp"];
const unixSocketOnly = testMethods.length === 1 && testMethods[0] === "unix";

const config: ClientConfig = {
timeout: 10000,
Expand All @@ -31,10 +34,7 @@ export function testWithClient(
tests.push([fn, overrideConfig]);
}

export function registerTests(methods?: ("tcp" | "unix")[]) {
if (!methods) {
methods = Deno.env.get("TEST_METHODS")?.split(",") as any || ["tcp"];
}
export function registerTests(methods: ("tcp" | "unix")[] = testMethods) {
if (methods!.includes("tcp")) {
tests.forEach(([fn, overrideConfig]) => {
Deno.test({
Expand Down Expand Up @@ -83,6 +83,7 @@ export async function createTestDB() {
...config,
poolSize: 1,
db: undefined,
socketPath: unixSocketOnly ? sockPath : undefined,
});
await client.execute(`CREATE DATABASE IF NOT EXISTS ${db}`);
await client.close();
Expand Down

0 comments on commit 8c1fde1

Please sign in to comment.