File tree Expand file tree Collapse file tree 9 files changed +147
-0
lines changed Expand file tree Collapse file tree 9 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -554,6 +554,14 @@ public void WriteBytes(byte[] value)
554
554
/// <param name="value">A string.</param>
555
555
public void WriteCString ( UTF8Encoding encoding , string value )
556
556
{
557
+ if ( value == null )
558
+ {
559
+ throw new ArgumentNullException ( "value" ) ;
560
+ }
561
+ if ( value . IndexOf ( '\0 ' ) != - 1 )
562
+ {
563
+ throw new ArgumentException ( "CStrings cannot contain nulls." , "value" ) ;
564
+ }
557
565
ThrowIfDisposed ( ) ;
558
566
559
567
var maxLength = encoding . GetMaxByteCount ( value . Length ) + 1 ;
Original file line number Diff line number Diff line change @@ -495,6 +495,14 @@ public void WriteMinKey(string name)
495
495
/// <param name="name">The name of the element.</param>
496
496
public virtual void WriteName ( string name )
497
497
{
498
+ if ( name == null )
499
+ {
500
+ throw new ArgumentNullException ( "name" ) ;
501
+ }
502
+ if ( name . IndexOf ( '\0 ' ) != - 1 )
503
+ {
504
+ throw new ArgumentException ( "Element names cannot contain nulls." , "name" ) ;
505
+ }
498
506
if ( _disposed ) { throw new ObjectDisposedException ( this . GetType ( ) . Name ) ; }
499
507
if ( _state != BsonWriterState . Name )
500
508
{
Original file line number Diff line number Diff line change @@ -374,6 +374,10 @@ public BsonMemberMap SetElementName(string elementName)
374
374
{
375
375
throw new ArgumentNullException ( "elementName" ) ;
376
376
}
377
+ if ( elementName . IndexOf ( '\0 ' ) != - 1 )
378
+ {
379
+ throw new ArgumentException ( "Element names cannot contain nulls." , "elementName" ) ;
380
+ }
377
381
if ( _frozen ) { ThrowFrozenException ( ) ; }
378
382
379
383
_elementName = elementName ;
Original file line number Diff line number Diff line change @@ -39,6 +39,14 @@ public abstract class StandardDiscriminatorConvention : IDiscriminatorConvention
39
39
/// <param name="elementName">The element name.</param>
40
40
protected StandardDiscriminatorConvention ( string elementName )
41
41
{
42
+ if ( elementName == null )
43
+ {
44
+ throw new ArgumentNullException ( "elementName" ) ;
45
+ }
46
+ if ( elementName . IndexOf ( '\0 ' ) != - 1 )
47
+ {
48
+ throw new ArgumentException ( "Element names cannot contain nulls." , "elementName" ) ;
49
+ }
42
50
_elementName = elementName ;
43
51
}
44
52
Original file line number Diff line number Diff line change 13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System ;
16
17
using System . IO ;
17
18
using System . Text ;
18
19
using MongoDB . Bson ;
20
+ using MongoDB . Bson . IO ;
19
21
using MongoDB . Bson . Serialization ;
20
22
using NUnit . Framework ;
21
23
@@ -127,5 +129,23 @@ public void TestReadStringTwoCharactersDecoderException()
127
129
Assert . AreEqual ( 15 , bytes . Length ) ;
128
130
var ex = Assert . Throws < DecoderFallbackException > ( ( ) => { BsonSerializer . Deserialize < BsonDocument > ( bytes ) ; } ) ;
129
131
}
132
+
133
+ [ Test ]
134
+ public void TestWriteCStringThrowsWhenValueContainsNulls ( )
135
+ {
136
+ using ( var bsonBuffer = new BsonBuffer ( ) )
137
+ {
138
+ Assert . Throws < ArgumentException > ( ( ) => { bsonBuffer . WriteCString ( ( UTF8Encoding ) Encoding . UTF8 , "a\0 b" ) ; } ) ;
139
+ }
140
+ }
141
+
142
+ [ Test ]
143
+ public void TestWriteCStringThrowsWhenValueIsNull ( )
144
+ {
145
+ using ( var bsonBuffer = new BsonBuffer ( ) )
146
+ {
147
+ Assert . Throws < ArgumentNullException > ( ( ) => { bsonBuffer . WriteCString ( ( UTF8Encoding ) Encoding . UTF8 , null ) ; } ) ;
148
+ }
149
+ }
130
150
}
131
151
}
Original file line number Diff line number Diff line change
1
+ /* Copyright 2010-2013 10gen Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System ;
17
+ using MongoDB . Bson . IO ;
18
+ using NUnit . Framework ;
19
+
20
+ namespace MongoDB . BsonUnitTests . IO
21
+ {
22
+ [ TestFixture ]
23
+ public class BsonWriterTests
24
+ {
25
+ [ Test ]
26
+ public void TestWriteNameThrowsWhenValueContainsNulls ( )
27
+ {
28
+ using ( var bsonWriter = BsonWriter . Create ( BsonBinaryWriterSettings . Defaults ) )
29
+ {
30
+ Assert . Throws < ArgumentException > ( ( ) => { bsonWriter . WriteName ( "a\0 b" ) ; } ) ;
31
+ }
32
+ }
33
+
34
+ [ Test ]
35
+ public void TestWriteNameThrowsWhenValueIsNull ( )
36
+ {
37
+ using ( var bsonWriter = BsonWriter . Create ( BsonBinaryWriterSettings . Defaults ) )
38
+ {
39
+ Assert . Throws < ArgumentNullException > ( ( ) => { bsonWriter . WriteName ( null ) ; } ) ;
40
+ }
41
+ }
42
+ }
43
+ }
Original file line number Diff line number Diff line change 81
81
</Compile >
82
82
<Compile Include =" BsonExtensionMethodsTests.cs" />
83
83
<Compile Include =" BsonUtilsTests.cs" />
84
+ <Compile Include =" IO\BsonWriterTests.cs" />
84
85
<Compile Include =" IO\ByteArrayBufferTests.cs" />
85
86
<Compile Include =" IO\MultiChunkBufferTests.cs" />
86
87
<Compile Include =" Jira\CSharp728Tests.cs" />
118
119
<Compile Include =" Serialization\Conventions\NamedIdConventionsTests.cs" />
119
120
<Compile Include =" Serialization\Conventions\PropertyFinderConventionsTests.cs" />
120
121
<Compile Include =" Serialization\Conventions\ReadWriteMemberFinderConventionsTests.cs" />
122
+ <Compile Include =" Serialization\Conventions\StandardDiscriminatorConventionTests.cs" />
121
123
<Compile Include =" Serialization\Conventions\StringObjectIdGeneratorConventionsTests.cs" />
122
124
<Compile Include =" Serialization\IdGenerators\AscendingGuidGeneratorTests.cs" />
123
125
<Compile Include =" Serialization\IdGenerators\CombGuidGeneratorTests.cs" />
Original file line number Diff line number Diff line change 13
13
* limitations under the License.
14
14
*/
15
15
16
+ using System ;
16
17
using MongoDB . Bson ;
17
18
using MongoDB . Bson . Serialization ;
18
19
using MongoDB . Bson . Serialization . IdGenerators ;
@@ -68,6 +69,22 @@ public void TestIsReadOnlyPropertyOfAField()
68
69
Assert . IsFalse ( memberMap . IsReadOnly ) ;
69
70
}
70
71
72
+ [ Test ]
73
+ public void TestSetElementNameThrowsWhenElementNameContainsNulls ( )
74
+ {
75
+ var classMap = new BsonClassMap < TestClass > ( cm => cm . AutoMap ( ) ) ;
76
+ var memberMap = classMap . GetMemberMap ( "Property" ) ;
77
+ Assert . Throws < ArgumentException > ( ( ) => { memberMap . SetElementName ( "a\0 b" ) ; } ) ;
78
+ }
79
+
80
+ [ Test ]
81
+ public void TestSetElementNameThrowsWhenElementNameIsNull ( )
82
+ {
83
+ var classMap = new BsonClassMap < TestClass > ( cm => cm . AutoMap ( ) ) ;
84
+ var memberMap = classMap . GetMemberMap ( "Property" ) ;
85
+ Assert . Throws < ArgumentNullException > ( ( ) => { memberMap . SetElementName ( null ) ; } ) ;
86
+ }
87
+
71
88
[ Test ]
72
89
public void TestSettingAField ( )
73
90
{
Original file line number Diff line number Diff line change
1
+ /* Copyright 2010-2013 10gen Inc.
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+
16
+ using System ;
17
+ using MongoDB . Bson . Serialization . Conventions ;
18
+ using NUnit . Framework ;
19
+
20
+ namespace MongoDB . BsonUnitTests . Serialization . Conventions
21
+ {
22
+ [ TestFixture ]
23
+ public class StandardDiscriminatorConventionTests
24
+ {
25
+ [ Test ]
26
+ public void TestConstructorThrowsWhenElementNameContainsNulls ( )
27
+ {
28
+ Assert . Throws < ArgumentException > ( ( ) => { var discriminatorConvention = new ScalarDiscriminatorConvention ( "a\0 b" ) ; } ) ;
29
+ }
30
+
31
+ [ Test ]
32
+ public void TestConstructorThrowsWhenElementNameIsNull ( )
33
+ {
34
+ Assert . Throws < ArgumentNullException > ( ( ) => { var discriminatorConvention = new ScalarDiscriminatorConvention ( null ) ; } ) ;
35
+ }
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments