Skip to content

Commit

Permalink
feat: publickey & password authentication
Browse files Browse the repository at this point in the history
Publickey authentication still has some issue when connecting to openssh servers
  • Loading branch information
Manaf941 committed Jun 12, 2024
1 parent f991cab commit 17955bf
Show file tree
Hide file tree
Showing 33 changed files with 1,545 additions and 119 deletions.
28 changes: 28 additions & 0 deletions __tests__/utils/BigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
decodeBigIntBE,
decodeBigIntLE,
encodeBigIntBE,
encodeBigIntLE,
} from "../../src/utils/BigInt"

describe("BigInt Utils", () => {
test("encode BigInt Big-Endian", () => {
const result = encodeBigIntBE(0x1234567890abcdefn)
expect(result.toString("hex")).toBe("1234567890abcdef")
})

test("encode BigInt Little-Endian", () => {
const result = encodeBigIntLE(0x1234567890abcdefn)
expect(result.toString("hex")).toBe("efcdab9078563412")
})

test("decode BigInt Big-Endian", () => {
const result = encodeBigIntBE(0x1234567890abcdefn)
expect(decodeBigIntBE(result)).toBe(0x1234567890abcdefn)
})

test("decode BigInt Little-Endian", () => {
const result = encodeBigIntLE(0x1234567890abcdefn)
expect(decodeBigIntLE(result)).toBe(0x1234567890abcdefn)
})
})
24 changes: 24 additions & 0 deletions __tests__/utils/BinaryBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { parseBinaryBoolean, serializeBinaryBoolean } from "../../src/utils/BinaryBoolean"

describe("BinaryBoolean Utils", () => {
test("Should be parseable", () => {
expect(parseBinaryBoolean(Buffer.from([0]))).toBe(false)
expect(parseBinaryBoolean(Buffer.from([1]))).toBe(true)
})

test("Should not be parseable", () => {
expect(() => parseBinaryBoolean(Buffer.from([2]))).toThrow()
expect(() => parseBinaryBoolean(Buffer.from([0, 1]))).toThrow()
})

test("Should serialize", () => {
expect(serializeBinaryBoolean(false)).toEqual(Buffer.from([0]))
expect(serializeBinaryBoolean(true)).toEqual(Buffer.from([1]))
})

test("Should not serialize", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
expect(() => serializeBinaryBoolean(null)).toThrow()
})
})
90 changes: 90 additions & 0 deletions __tests__/utils/Buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
readNextBuffer,
readNextUint8,
readNextUint32,
readNextBinaryBoolean,
serializeBuffer,
serializeUint8,
serializeUint32,
} from "../../src/utils/Buffer"

describe("Buffer Utils", () => {
describe("readNextBuffer", () => {
it("should read the next buffer correctly", () => {
const buffer = Buffer.from([0x00, 0x00, 0x00, 0x04, 0x61, 0x62, 0x63, 0x64])
const [data, remaining] = readNextBuffer(buffer)
expect(data).toStrictEqual(Buffer.from([0x61, 0x62, 0x63, 0x64]))
expect(remaining).toStrictEqual(Buffer.from([]))
})

it("should throw an error if buffer length is less than 4", () => {
const buffer = Buffer.from([0x00, 0x00, 0x00])
expect(() => {
readNextBuffer(buffer)
}).toThrow()
})
})
describe("readNextUint8", () => {
it("should read the next Uint8 correctly", () => {
const buffer = Buffer.from([0x01, 0x02, 0x03])
const [data, remaining] = readNextUint8(buffer)
expect(data).toBe(0x01)
expect(remaining).toStrictEqual(Buffer.from([0x02, 0x03]))
})
it("should throw an error if buffer length is less than 1", () => {
const buffer = Buffer.from([])
expect(() => {
readNextUint8(buffer)
}).toThrow()
})
})
describe("readNextUint32", () => {
it("should read the next Uint32 correctly", () => {
const buffer = Buffer.from([0x00, 0x00, 0x00, 0x01, 0x02, 0x03])
const [data, remaining] = readNextUint32(buffer)
expect(data).toBe(0x00000001)
expect(remaining).toStrictEqual(Buffer.from([0x02, 0x03]))
})
it("should throw an error if buffer length is less than 4", () => {
const buffer = Buffer.from([0x00, 0x00, 0x00])
expect(() => {
readNextUint32(buffer)
}).toThrow()
})
})
describe("readNextBinaryBoolean", () => {
it("should read the next BinaryBoolean correctly", () => {
const buffer = Buffer.from([0x01, 0x02])
const [data, remaining] = readNextBinaryBoolean(buffer)
expect(data).toBe(true)
expect(remaining).toStrictEqual(Buffer.from([0x02]))
})
it("should throw an error if buffer length is less than 1", () => {
const buffer = Buffer.from([])
expect(() => {
readNextBinaryBoolean(buffer)
}).toThrow()
})
})
describe("serializeBuffer", () => {
it("should serialize the buffer correctly", () => {
const buffer = Buffer.from([0x61, 0x62, 0x63, 0x64])
const serialized = serializeBuffer(buffer)
expect(serialized).toStrictEqual(
Buffer.from([0x00, 0x00, 0x00, 0x04, 0x61, 0x62, 0x63, 0x64]),
)
})
})
describe("serializeUint8", () => {
it("should serialize the Uint8 correctly", () => {
const serialized = serializeUint8(0x01)
expect(serialized).toStrictEqual(Buffer.from([0x01]))
})
})
describe("serializeUint32", () => {
it("should serialize the Uint32 correctly", () => {
const serialized = serializeUint32(0x00000001)
expect(serialized).toStrictEqual(Buffer.from([0x00, 0x00, 0x00, 0x01]))
})
})
})
14 changes: 14 additions & 0 deletions __tests__/utils/NameList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readNextNameList, serializeNameList } from "../../src/utils/NameList"

describe("NameList Utils", () => {
test("Should be parseable", () => {
expect(readNextNameList(Buffer.from("00000005612c622c63deadbeef", "hex"))).toEqual([
["a", "b", "c"],
Buffer.from("deadbeef", "hex"),
])
})

test("Should serialize", () => {
expect(serializeNameList(["a", "b", "c"]).toString("hex")).toEqual("00000005612c622c63")
})
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"type": "module",
"dependencies": {
"asn1js": "^3.0.5"
"asn1js": "^3.0.5",
"tweetnacl": "^1.0.3"
}
}
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 17955bf

Please sign in to comment.