@@ -164,6 +164,30 @@ func TestCapability(t *testing.T) {
164164 cfg .clientConfig .capability |= pnet .ClientSecureConnection
165165 },
166166 },
167+ {
168+ func (cfg * testConfig ) {
169+ cfg .backendConfig .capability &= ^ pnet .ClientCompress
170+ cfg .backendConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
171+ },
172+ func (cfg * testConfig ) {
173+ cfg .backendConfig .capability |= pnet .ClientCompress
174+ cfg .backendConfig .capability |= pnet .ClientZstdCompressionAlgorithm
175+ },
176+ },
177+ {
178+ func (cfg * testConfig ) {
179+ cfg .clientConfig .capability &= ^ pnet .ClientCompress
180+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
181+ },
182+ func (cfg * testConfig ) {
183+ cfg .clientConfig .capability |= pnet .ClientCompress
184+ cfg .clientConfig .capability |= pnet .ClientZstdCompressionAlgorithm
185+ },
186+ func (cfg * testConfig ) {
187+ cfg .clientConfig .capability |= pnet .ClientCompress
188+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
189+ },
190+ },
167191 }
168192
169193 tc := newTCPConnSuite (t )
@@ -387,3 +411,138 @@ func TestProxyProtocol(t *testing.T) {
387411 clean ()
388412 }
389413}
414+
415+ func TestCompressProtocol (t * testing.T ) {
416+ cfgs := [][]cfgOverrider {
417+ {
418+ func (cfg * testConfig ) {
419+ cfg .backendConfig .capability &= ^ pnet .ClientCompress
420+ cfg .backendConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
421+ },
422+ func (cfg * testConfig ) {
423+ cfg .backendConfig .capability |= pnet .ClientCompress
424+ cfg .backendConfig .capability |= pnet .ClientZstdCompressionAlgorithm
425+ },
426+ },
427+ {
428+ func (cfg * testConfig ) {
429+ cfg .clientConfig .capability &= ^ pnet .ClientCompress
430+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
431+ },
432+ func (cfg * testConfig ) {
433+ cfg .clientConfig .capability |= pnet .ClientCompress
434+ cfg .clientConfig .capability |= pnet .ClientZstdCompressionAlgorithm
435+ cfg .clientConfig .zstdLevel = 3
436+ },
437+ func (cfg * testConfig ) {
438+ cfg .clientConfig .capability |= pnet .ClientCompress
439+ cfg .clientConfig .capability |= pnet .ClientZstdCompressionAlgorithm
440+ cfg .clientConfig .zstdLevel = 9
441+ },
442+ func (cfg * testConfig ) {
443+ cfg .clientConfig .capability |= pnet .ClientCompress
444+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
445+ },
446+ },
447+ }
448+
449+ checker := func (t * testing.T , ts * testSuite , referCfg * testConfig ) {
450+ // If the client enables compression, client <-> proxy enables compression.
451+ if referCfg .clientConfig .capability & pnet .ClientCompress > 0 {
452+ require .Greater (t , ts .mp .authenticator .capability & pnet .ClientCompress , pnet .Capability (0 ))
453+ require .Greater (t , ts .mc .capability & pnet .ClientCompress , pnet .Capability (0 ))
454+ } else {
455+ require .Equal (t , pnet .Capability (0 ), ts .mp .authenticator .capability & pnet .ClientCompress )
456+ require .Equal (t , pnet .Capability (0 ), ts .mc .capability & pnet .ClientCompress )
457+ }
458+ // If both the client and the backend enables compression, proxy <-> backend enables compression.
459+ if referCfg .clientConfig .capability & referCfg .backendConfig .capability & pnet .ClientCompress > 0 {
460+ require .Greater (t , ts .mb .capability & pnet .ClientCompress , pnet .Capability (0 ))
461+ } else {
462+ require .Equal (t , pnet .Capability (0 ), ts .mb .capability & pnet .ClientCompress )
463+ }
464+ // If the client enables zstd compression, client <-> proxy enables zstd compression.
465+ zstdCap := pnet .ClientCompress | pnet .ClientZstdCompressionAlgorithm
466+ if referCfg .clientConfig .capability & zstdCap == zstdCap {
467+ require .Greater (t , ts .mp .authenticator .capability & pnet .ClientZstdCompressionAlgorithm , pnet .Capability (0 ))
468+ require .Greater (t , ts .mc .capability & pnet .ClientZstdCompressionAlgorithm , pnet .Capability (0 ))
469+ require .Equal (t , referCfg .clientConfig .zstdLevel , ts .mp .authenticator .zstdLevel )
470+ } else {
471+ require .Equal (t , pnet .Capability (0 ), ts .mp .authenticator .capability & pnet .ClientZstdCompressionAlgorithm )
472+ require .Equal (t , pnet .Capability (0 ), ts .mc .capability & pnet .ClientZstdCompressionAlgorithm )
473+ }
474+ // If both the client and the backend enables zstd compression, proxy <-> backend enables zstd compression.
475+ if referCfg .clientConfig .capability & referCfg .backendConfig .capability & zstdCap == zstdCap {
476+ require .Greater (t , ts .mb .capability & pnet .ClientZstdCompressionAlgorithm , pnet .Capability (0 ))
477+ require .Equal (t , referCfg .clientConfig .zstdLevel , ts .mb .zstdLevel )
478+ } else {
479+ require .Equal (t , pnet .Capability (0 ), ts .mb .capability & pnet .ClientZstdCompressionAlgorithm )
480+ }
481+ }
482+
483+ tc := newTCPConnSuite (t )
484+ cfgOverriders := getCfgCombinations (cfgs )
485+ for _ , cfgs := range cfgOverriders {
486+ referCfg := newTestConfig (cfgs ... )
487+ ts , clean := newTestSuite (t , tc , cfgs ... )
488+ ts .authenticateFirstTime (t , func (t * testing.T , ts * testSuite ) {
489+ checker (t , ts , referCfg )
490+ })
491+ ts .authenticateSecondTime (t , func (t * testing.T , ts * testSuite ) {
492+ checker (t , ts , referCfg )
493+ })
494+ clean ()
495+ }
496+ }
497+
498+ // After upgrading the backend, the backend capability may change.
499+ func TestUpgradeBackendCap (t * testing.T ) {
500+ cfgs := [][]cfgOverrider {
501+ {
502+ func (cfg * testConfig ) {
503+ cfg .clientConfig .capability &= ^ pnet .ClientCompress
504+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
505+ },
506+ func (cfg * testConfig ) {
507+ cfg .clientConfig .capability |= pnet .ClientCompress
508+ cfg .clientConfig .capability |= pnet .ClientZstdCompressionAlgorithm
509+ cfg .clientConfig .zstdLevel = 3
510+ },
511+ func (cfg * testConfig ) {
512+ cfg .clientConfig .capability |= pnet .ClientCompress
513+ cfg .clientConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
514+ },
515+ },
516+ {
517+ func (cfg * testConfig ) {
518+ cfg .backendConfig .capability &= ^ pnet .ClientCompress
519+ cfg .backendConfig .capability &= ^ pnet .ClientZstdCompressionAlgorithm
520+ },
521+ },
522+ }
523+
524+ tc := newTCPConnSuite (t )
525+ cfgOverriders := getCfgCombinations (cfgs )
526+ for _ , cfgs := range cfgOverriders {
527+ referCfg := newTestConfig (cfgs ... )
528+ ts , clean := newTestSuite (t , tc , cfgs ... )
529+ // Before upgrade, the backend doesn't support compression.
530+ ts .authenticateFirstTime (t , func (t * testing.T , ts * testSuite ) {
531+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientCompress , ts .mp .authenticator .capability & pnet .ClientCompress )
532+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientCompress , ts .mc .capability & pnet .ClientCompress )
533+ require .Equal (t , pnet .Capability (0 ), ts .mb .capability & pnet .ClientCompress )
534+ })
535+ // After upgrade, the backend also supports compression.
536+ ts .mb .backendConfig .capability |= pnet .ClientCompress
537+ ts .mb .backendConfig .capability |= pnet .ClientZstdCompressionAlgorithm
538+ ts .authenticateSecondTime (t , func (t * testing.T , ts * testSuite ) {
539+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientCompress , ts .mc .capability & pnet .ClientCompress )
540+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientCompress , ts .mp .authenticator .capability & pnet .ClientCompress )
541+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientCompress , ts .mb .capability & pnet .ClientCompress )
542+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientZstdCompressionAlgorithm , ts .mc .capability & pnet .ClientZstdCompressionAlgorithm )
543+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientZstdCompressionAlgorithm , ts .mp .authenticator .capability & pnet .ClientZstdCompressionAlgorithm )
544+ require .Equal (t , referCfg .clientConfig .capability & pnet .ClientZstdCompressionAlgorithm , ts .mb .capability & pnet .ClientZstdCompressionAlgorithm )
545+ })
546+ clean ()
547+ }
548+ }
0 commit comments