@@ -5,6 +5,8 @@ import * as path from "node:path";
55import * as tls from "node:tls" ;
66import { afterEach , describe , expect , test } from "vitest" ;
77import {
8+ _encodeInteger ,
9+ _expandIpv6 ,
810 generateCertificate ,
911 generateCertificateFiles ,
1012} from "../src/certificate.js" ;
@@ -137,6 +139,62 @@ describe("generateCertificate", () => {
137139 } ) ;
138140} ) ;
139141
142+ describe ( "encodeInteger" , ( ) => {
143+ test ( "should encode zero as a single zero byte" , ( ) => {
144+ const result = _encodeInteger ( 0 ) ;
145+ // DER INTEGER tag=0x02, length=1, value=0x00
146+ expect ( result ) . toEqual ( Buffer . from ( [ 0x02 , 0x01 , 0x00 ] ) ) ;
147+ } ) ;
148+
149+ test ( "should add leading zero when high bit is set on number" , ( ) => {
150+ // 128 = 0x80, high bit is set so a leading 0x00 must be prepended
151+ const result = _encodeInteger ( 128 ) ;
152+ // DER INTEGER tag=0x02, length=2, value=0x00 0x80
153+ expect ( result ) . toEqual ( Buffer . from ( [ 0x02 , 0x02 , 0x00 , 0x80 ] ) ) ;
154+ } ) ;
155+
156+ test ( "should add leading zero when high bit is set on Buffer" , ( ) => {
157+ const buf = Buffer . from ( [ 0x80 , 0x01 ] ) ;
158+ const result = _encodeInteger ( buf ) ;
159+ // DER INTEGER tag=0x02, length=3, value=0x00 0x80 0x01
160+ expect ( result ) . toEqual ( Buffer . from ( [ 0x02 , 0x03 , 0x00 , 0x80 , 0x01 ] ) ) ;
161+ } ) ;
162+ } ) ;
163+
164+ describe ( "expandIpv6" , ( ) => {
165+ test ( "should pad groups in a fully-expanded IPv6 address" , ( ) => {
166+ const result = _expandIpv6 ( "2001:db8:0:0:0:0:0:1" ) ;
167+ expect ( result ) . toBe ( "2001:0db8:0000:0000:0000:0000:0000:0001" ) ;
168+ } ) ;
169+
170+ test ( "should expand :: with empty left side" , ( ) => {
171+ const result = _expandIpv6 ( "::1" ) ;
172+ expect ( result ) . toBe ( "0000:0000:0000:0000:0000:0000:0000:0001" ) ;
173+ } ) ;
174+
175+ test ( "should expand :: with empty right side" , ( ) => {
176+ const result = _expandIpv6 ( "fe80::" ) ;
177+ expect ( result ) . toBe ( "fe80:0000:0000:0000:0000:0000:0000:0000" ) ;
178+ } ) ;
179+
180+ test ( "should expand :: in the middle of an address" , ( ) => {
181+ const result = _expandIpv6 ( "2001:db8::1" ) ;
182+ expect ( result ) . toBe ( "2001:0db8:0000:0000:0000:0000:0000:0001" ) ;
183+ } ) ;
184+ } ) ;
185+
186+ describe ( "generateCertificate with fully-expanded IPv6" , ( ) => {
187+ test ( "should handle a fully-expanded IPv6 altName without ::" , ( ) => {
188+ const result = generateCertificate ( {
189+ altNames : [
190+ { type : "ip" , value : "2001:0db8:0000:0000:0000:0000:0000:0001" } ,
191+ ] ,
192+ } ) ;
193+ const x509 = new crypto . X509Certificate ( result . cert ) ;
194+ expect ( x509 . subjectAltName ) . toContain ( "IP Address:2001:DB8" ) ;
195+ } ) ;
196+ } ) ;
197+
140198describe ( "generateCertificateFiles" , ( ) => {
141199 let tmpDir : string ;
142200
0 commit comments