1
1
import { expect } from 'chai' ;
2
- import { BinMsg , MessageHeader } from '../../src/cmap/commands' ;
2
+ import { BinMsg , MessageHeader , OpResponseOptions } from '../../src/cmap/commands' ;
3
3
import { BSONError } from 'bson' ;
4
4
import * as BSON from '../../src/bson' ;
5
5
@@ -11,16 +11,10 @@ const msgHeader: MessageHeader = {
11
11
} ;
12
12
13
13
// when top-level key writeErrors contains an error message that has invalid utf8
14
- const msgBodyInvalidUtf8WriteErrors = Buffer . from (
15
- '0000000000ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00' ,
16
- 'hex'
17
- ) ;
18
-
19
- const badutf8inputtodeserialize = Buffer . from (
20
- 'ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00' ,
21
- 'hex'
22
- ) ;
23
-
14
+ const invalidUtf8ErrorMsg =
15
+ '0000000000ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00' ;
16
+ const msgBodyInvalidUtf8WriteErrors = Buffer . from ( invalidUtf8ErrorMsg , 'hex' ) ;
17
+ const invalidUtf8ErrorMsgDeserializeInput = Buffer . from ( invalidUtf8ErrorMsg . substring ( 10 ) , 'hex' ) ;
24
18
const invalidUtf8InWriteErrorsJSON = {
25
19
n : 0 ,
26
20
writeErrors : [
@@ -41,17 +35,11 @@ const invalidUtf8InWriteErrorsJSON = {
41
35
} ;
42
36
43
37
// when another top-level key besides writeErrors has invalid utf8
44
- const nKeyWithInvalidUtf8 = Buffer . from (
45
- 'cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000' ,
46
- 'hex'
47
- ) ;
48
-
49
- const msgBodyNKeyWithInvalidUtf8 = Buffer . from (
50
- '0000000000cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000' ,
51
- 'hex'
52
- ) ;
53
-
54
- const invalidUtf8InOtherKeyJSON = {
38
+ const nKeyWithInvalidUtf8 =
39
+ '0000000000cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000' ;
40
+ const nKeyWithInvalidUtf8DeserializeInput = Buffer . from ( nKeyWithInvalidUtf8 . substring ( 10 ) , 'hex' ) ;
41
+ const msgBodyNKeyWithInvalidUtf8 = Buffer . from ( nKeyWithInvalidUtf8 , 'hex' ) ;
42
+ const invalidUtf8InNKeyJSON = {
55
43
n : '��' ,
56
44
writeErrors : [
57
45
{
@@ -71,30 +59,35 @@ const invalidUtf8InOtherKeyJSON = {
71
59
} ;
72
60
73
61
describe ( 'BinMsg' , function ( ) {
74
- it ( 'should throw error if trying to toggle validation settings for keys other than writeErrors', function ( ) {
62
+ context ( 'when validation is disabled for writeErrors', function ( ) {
75
63
const binMsgInvalidUtf8ErrorMsg = new BinMsg (
76
64
Buffer . alloc ( 0 ) ,
77
65
msgHeader ,
78
66
msgBodyInvalidUtf8WriteErrors
79
67
) ;
80
- expect ( ( ) => binMsgInvalidUtf8ErrorMsg . parse ( { validation : { utf8 : { n : false } } } ) ) . to . throw (
81
- Error ,
82
- 'Can only toggle validation settings for writeErrors key'
83
- ) ;
68
+ const options : OpResponseOptions = { validation : { utf8 : { writeErrors : false } } } ;
69
+
70
+ it ( 'contains replacement characters for invalid utf8 in writeError object' , function ( ) {
71
+ expect ( BSON . deserialize ( invalidUtf8ErrorMsgDeserializeInput , options ) ) . to . deep . equals (
72
+ invalidUtf8InWriteErrorsJSON
73
+ ) ;
74
+ } ) ;
75
+
76
+ it ( 'should not throw invalid utf8 error' , function ( ) {
77
+ expect ( ( ) => binMsgInvalidUtf8ErrorMsg . parse ( options ) ) . to . not . throw ( ) ;
78
+ } ) ;
84
79
} ) ;
85
80
86
- it ( 'should not throw invalid utf8 error when validation disabled for writeErrors' , function ( ) {
81
+ it ( 'should throw error if trying to toggle validation settings for keys other than writeErrors' , function ( ) {
87
82
const binMsgInvalidUtf8ErrorMsg = new BinMsg (
88
83
Buffer . alloc ( 0 ) ,
89
84
msgHeader ,
90
85
msgBodyInvalidUtf8WriteErrors
91
86
) ;
92
- expect ( ( ) =>
93
- binMsgInvalidUtf8ErrorMsg . parse ( { validation : { utf8 : { writeErrors : false } } } )
94
- ) . to . not . throw ( ) ;
95
- expect (
96
- BSON . deserialize ( badutf8inputtodeserialize , { validation : { utf8 : { writeErrors : false } } } )
97
- ) . to . deep . equals ( invalidUtf8InWriteErrorsJSON ) ;
87
+ expect ( ( ) => binMsgInvalidUtf8ErrorMsg . parse ( { validation : { utf8 : { n : false } } } ) ) . to . throw (
88
+ Error ,
89
+ 'Can only toggle validation settings for writeErrors key'
90
+ ) ;
98
91
} ) ;
99
92
100
93
it ( 'should by default disable validation for writeErrors if no validation specified' , function ( ) {
@@ -112,6 +105,28 @@ describe('BinMsg', function () {
112
105
expect ( ( ) => binMsgInvalidUtf8ErrorMsg . parse ( options ) ) . to . not . throw ( ) ;
113
106
} ) ;
114
107
108
+ context (
109
+ 'when another key has invalid utf8 and validation is enabled for writeErrors' ,
110
+ function ( ) {
111
+ const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg (
112
+ Buffer . alloc ( 0 ) ,
113
+ msgHeader ,
114
+ msgBodyNKeyWithInvalidUtf8
115
+ ) ;
116
+ const option : OpResponseOptions = { validation : { utf8 : { writeErrors : true } } } ;
117
+
118
+ it ( 'should not throw invalid utf8 error' , function ( ) {
119
+ expect ( ( ) => binMsgAnotherKeyWithInvalidUtf8 . parse ( option ) ) . to . not . throw ( ) ;
120
+ } ) ;
121
+
122
+ it ( 'contains replacement characters for invalid utf8 key' , function ( ) {
123
+ expect ( BSON . deserialize ( nKeyWithInvalidUtf8DeserializeInput , option ) ) . to . deep . equals (
124
+ invalidUtf8InNKeyJSON
125
+ ) ;
126
+ } ) ;
127
+ }
128
+ ) ;
129
+
115
130
it ( 'should throw invalid utf8 error when validation enabled for writeErrors' , function ( ) {
116
131
const binMsgInvalidUtf8ErrorMsg = new BinMsg (
117
132
Buffer . alloc ( 0 ) ,
@@ -123,22 +138,6 @@ describe('BinMsg', function () {
123
138
) . to . throw ( BSONError , 'Invalid UTF-8 string in BSON document' ) ;
124
139
} ) ;
125
140
126
- it ( 'should not throw when another key has invalid utf8 and writeErrors is validated' , function ( ) {
127
- const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg (
128
- Buffer . alloc ( 0 ) ,
129
- msgHeader ,
130
- msgBodyNKeyWithInvalidUtf8
131
- ) ;
132
- expect ( ( ) =>
133
- binMsgAnotherKeyWithInvalidUtf8 . parse ( { validation : { utf8 : { writeErrors : true } } } )
134
- ) . to . not . throw ( ) ;
135
- expect (
136
- BSON . deserialize ( nKeyWithInvalidUtf8 , {
137
- validation : { utf8 : { writeErrors : true } }
138
- } )
139
- ) . to . deep . equals ( invalidUtf8InOtherKeyJSON ) ;
140
- } ) ;
141
-
142
141
it ( 'should throw error when another key has invalid utf8 and writeErrors is not validated' , function ( ) {
143
142
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg (
144
143
Buffer . alloc ( 0 ) ,
0 commit comments