1+ 'use strict'
2+ const helper = require ( '../test-helper' )
3+ const assert = require ( 'assert' )
4+ const suite = new helper . Suite ( )
5+
6+ // 测试Legacy实现的加密功能
7+ suite . testAsync ( 'legacy implementation tests' , async ( ) => {
8+ // 直接测试legacy实现
9+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
10+
11+ // 检查模块是否导出了必要的函数
12+ assert . strictEqual ( typeof crypto . gaussdbMd5PasswordHash , 'function' )
13+ assert . strictEqual ( typeof crypto . gaussdbSha256PasswordHash , 'function' )
14+ assert . strictEqual ( typeof crypto . randomBytes , 'function' )
15+ assert . strictEqual ( typeof crypto . deriveKey , 'function' )
16+ assert . strictEqual ( typeof crypto . sha256 , 'function' )
17+ assert . strictEqual ( typeof crypto . hashByName , 'function' )
18+ assert . strictEqual ( typeof crypto . hmacSha256 , 'function' )
19+ assert . strictEqual ( typeof crypto . md5 , 'function' )
20+ } )
21+
22+ suite . testAsync ( 'legacy gaussdbMd5PasswordHash function' , async ( ) => {
23+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
24+
25+ // 测试MD5密码哈希功能
26+ const user = 'testuser'
27+ const password = 'testpass'
28+ const salt = Buffer . from ( [ 1 , 2 , 3 , 4 ] )
29+
30+ const hashed = crypto . gaussdbMd5PasswordHash ( user , password , salt )
31+ assert . strictEqual ( typeof hashed , 'string' )
32+ assert ( hashed . startsWith ( 'md5' ) )
33+ assert . strictEqual ( hashed . length , 35 ) // 'md5' + 32位十六进制字符
34+ } )
35+
36+ suite . testAsync ( 'legacy gaussdbSha256PasswordHash function' , async ( ) => {
37+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
38+
39+ // 测试SHA256密码哈希功能
40+ const user = 'testuser'
41+ const password = 'testpass'
42+
43+ // 构造模拟的salt数据,符合GaussDB SHA256认证的数据结构
44+ // 结构: [4 bytes method][64 bytes random code][8 bytes token][4 bytes iteration]
45+ const data = Buffer . alloc ( 80 )
46+ data . writeInt32BE ( 1 , 0 ) // password method
47+ data . write ( 'A' . repeat ( 64 ) , 4 , 'ascii' ) // 64-byte random code
48+ data . write ( 'B' . repeat ( 8 ) , 68 , 'ascii' ) // 8-byte token
49+ data . writeInt32BE ( 1000 , 76 ) // iteration count
50+
51+ const hashed = crypto . gaussdbSha256PasswordHash ( user , password , data )
52+ assert . strictEqual ( typeof hashed , 'string' )
53+ } )
54+
55+ suite . testAsync ( 'legacy randomBytes function' , async ( ) => {
56+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
57+
58+ // 测试随机字节生成功能
59+ const bytes1 = crypto . randomBytes ( 16 )
60+ const bytes2 = crypto . randomBytes ( 16 )
61+
62+ assert ( bytes1 instanceof Buffer )
63+ assert . strictEqual ( bytes1 . length , 16 )
64+ assert ( bytes2 instanceof Buffer )
65+ assert . strictEqual ( bytes2 . length , 16 )
66+
67+ // 两次生成的随机数据应该不同
68+ assert . notStrictEqual ( bytes1 . toString ( 'hex' ) , bytes2 . toString ( 'hex' ) )
69+ } )
70+
71+ suite . testAsync ( 'legacy sha256 function' , async ( ) => {
72+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
73+
74+ // 测试SHA256哈希功能
75+ const data = Buffer . from ( 'hello world' )
76+ const hash = crypto . sha256 ( data )
77+
78+ assert ( hash instanceof Buffer )
79+ assert . strictEqual ( hash . length , 32 ) // SHA256 produces 32-byte output
80+ } )
81+
82+ suite . testAsync ( 'legacy md5 function' , async ( ) => {
83+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
84+
85+ // 测试MD5哈希功能
86+ const hash = crypto . md5 ( 'hello world' )
87+ assert . strictEqual ( typeof hash , 'string' )
88+ assert . strictEqual ( hash . length , 32 ) // MD5 produces 32-character hex string
89+ assert . strictEqual ( hash , '5eb63bbbe01eeed093cb22bb8f5acdc3' ) // known MD5 of 'hello world'
90+ } )
91+
92+ suite . testAsync ( 'legacy deriveKey function' , async ( ) => {
93+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
94+
95+ // 测试密钥派生功能
96+ const password = 'password'
97+ const salt = Buffer . from ( 'salt' )
98+ const iterations = 100
99+
100+ const key = await crypto . deriveKey ( password , salt , iterations )
101+ // nodeCrypto.pbkdf2Sync返回Buffer
102+ assert ( key instanceof Buffer )
103+ assert . strictEqual ( key . length , 32 ) // PBKDF2-SHA256 produces 32-byte key
104+ } )
105+
106+ suite . testAsync ( 'legacy hashByName function' , async ( ) => {
107+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
108+
109+ // 测试hashByName函数
110+ const data = Buffer . from ( 'hello world' )
111+
112+ // 测试SHA-256
113+ const sha256Hash = crypto . hashByName ( 'sha256' , data )
114+ assert ( sha256Hash instanceof Buffer )
115+ assert . strictEqual ( sha256Hash . length , 32 )
116+
117+ // 测试SHA-1
118+ const sha1Hash = crypto . hashByName ( 'sha1' , data )
119+ assert ( sha1Hash instanceof Buffer )
120+ assert . strictEqual ( sha1Hash . length , 20 )
121+ } )
122+
123+ suite . testAsync ( 'legacy hmacSha256 function' , async ( ) => {
124+ const crypto = require ( '../../../lib/crypto/utils-legacy' )
125+
126+ // 测试hmacSha256函数
127+ const key = Buffer . from ( 'key' )
128+ const message = 'message'
129+
130+ const hmac = crypto . hmacSha256 ( key , message )
131+ assert ( hmac instanceof Buffer )
132+ assert . strictEqual ( hmac . length , 32 ) // SHA-256 HMAC produces 32-byte output
133+ } )
0 commit comments