-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored support for IPv4-mapped addresses.
- Loading branch information
1 parent
e9a7328
commit 25a1215
Showing
10 changed files
with
108 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { isIPv4Mapped } from "./v4mapped"; | ||
|
||
describe("isIPv4Mapped", () => { | ||
test("should return true for valid IPv4-mapped IPv6 address", () => { | ||
expect(isIPv4Mapped("::ffff:192.168.0.1")).toBe(true); | ||
|
||
// Test case-insensitivity | ||
expect(isIPv4Mapped("::FFFF:127.0.0.1")).toBe(true); | ||
}); | ||
|
||
test("should return false for non-IPv4-mapped IPv6 address", () => { | ||
expect(isIPv4Mapped("2001:0db8:85a3:0000:0000:8a2e:0370:7334")).toBe(false); | ||
expect(isIPv4Mapped("fe80::1ff:fe23:4567:890a")).toBe(false); | ||
}); | ||
|
||
test("should return false for IPv4 address", () => { | ||
expect(isIPv4Mapped("192.168.0.1")).toBe(false); | ||
expect(isIPv4Mapped("127.0.0.1")).toBe(false); | ||
}); | ||
|
||
test("should return false for invalid IPv4-mapped IPv6 address", () => { | ||
expect(isIPv4Mapped("::ffff:999.999.999.999")).toBe(false); | ||
expect(isIPv4Mapped("::ffff:192.168.0.256")).toBe(false); | ||
expect(isIPv4Mapped("::ffff:192.168.0")).toBe(false); | ||
expect(isIPv4Mapped("::ffff:192.168.0.1.1")).toBe(false); | ||
}); | ||
|
||
test("should return false for malformed addresses", () => { | ||
expect(isIPv4Mapped("::ffff:192.168.0.1g")).toBe(false); | ||
expect(isIPv4Mapped("::ffff:192.168.0.")).toBe(false); | ||
expect(isIPv4Mapped("::ffff:192.168..0.1")).toBe(false); | ||
expect(isIPv4Mapped("::gggg:192.168.0.1")).toBe(false); | ||
}); | ||
|
||
test("should return false for empty string", () => { | ||
expect(isIPv4Mapped("")).toBe(false); | ||
}); | ||
|
||
test("should return false for null or undefined", () => { | ||
expect(isIPv4Mapped(null as unknown as string)).toBe(false); | ||
expect(isIPv4Mapped(undefined as unknown as string)).toBe(false); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Test for the presence of an IPv4-mapped address embedded in an IPv6 address. | ||
* | ||
* @param address - IPv6 address | ||
* @returns true if it is an IPv4-mapped address, false otherwise. | ||
*/ | ||
export function isIPv4Mapped(address: string): boolean { | ||
if(!/^::ffff:(\d{1,3}\.){3}\d{1,3}$/i.test(address)) { | ||
return false; | ||
} | ||
|
||
// Split the address apart into it's components and test for validity. | ||
const parts = address.split(/::ffff:/i)[1]?.split(".").map(Number); | ||
return parts?.length === 4 && parts.every(part => part >= 0 && part <= 255); | ||
} | ||
|
||
export function getIPFromV4Mapped(address: string): string | null { | ||
|
||
// Split the address apart into it's components and test for validity. | ||
return address.split(/^::ffff:/i)[1] ?? null; | ||
} | ||
|