Skip to content

Commit 1726e8e

Browse files
authored
OSS-694: Validate input parameters (#149)
* OSS-694: Added nullable validation for input parameters in public methods * OSS-694: Added extention AssertNotNull and changed all null checking to AssertNotNull * OSS-694: Added null exception test
1 parent 2731bb5 commit 1726e8e

File tree

14 files changed

+75
-31
lines changed

14 files changed

+75
-31
lines changed

FaunaDB.Client.Test/ClientTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,26 @@ public async Task TestTimeDiff()
26572657
Assert.AreEqual(2L, res3.To<long>().Value);
26582658
}
26592659

2660+
[Test]
2661+
public void GetClientExceptionTest()
2662+
{
2663+
var exception1 = Assert.Throws<ArgumentNullException>(
2664+
() => new FaunaClient(secret: null, endpoint: faunaEndpoint)
2665+
);
2666+
2667+
Assert.That(exception1.Message, Does.Contain("secret"));
2668+
2669+
var exception2 = Assert.Throws<ArgumentNullException>(
2670+
() => new FaunaClient(secret: faunaSecret, endpoint: null)
2671+
);
2672+
Assert.That(exception2.Message, Does.Contain("endpoint"));
2673+
2674+
var exception3 = Assert.Throws<ArgumentNullException>(
2675+
() => new FaunaClient(secret: null, endpoint: null)
2676+
);
2677+
Assert.That(exception3.Message, Does.Contain("secret"));
2678+
}
2679+
26602680
private async Task<Value> NewCollectionWithValues(string colName, string indexName, int size = 10, bool indexWithAllValues = false)
26612681
{
26622682
RefV aCollection = (await client.Query(CreateCollection(Obj("name", colName))))

FaunaDB.Client/Client/DefaultClientIO.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12+
using FaunaDB.Errors;
1213

1314
namespace FaunaDB.Client
1415
{
@@ -31,6 +32,11 @@ class DefaultClientIO : IClientIO
3132

3233
internal DefaultClientIO(HttpClient client, AuthenticationHeaderValue authHeader, LastSeen lastSeen, Uri endpoint, TimeSpan? timeout, Version httpVersion)
3334
{
35+
client.AssertNotNull(nameof(client));
36+
authHeader.AssertNotNull(nameof(authHeader));
37+
endpoint.AssertNotNull(nameof(endpoint));
38+
lastSeen.AssertNotNull(nameof(lastSeen));
39+
3440
this.client = client;
3541
this.authHeader = authHeader;
3642
this.lastSeen = lastSeen;
@@ -207,16 +213,13 @@ static class HttpRequestExtensions
207213

208214
public static void SetTimeout(this HttpRequestMessage request, TimeSpan? timeout)
209215
{
210-
if (request == null)
211-
throw new ArgumentNullException(nameof(request));
212-
216+
request.AssertNotNull(nameof(request));
213217
request.Properties[TimeoutPropertyKey] = timeout;
214218
}
215219

216220
public static TimeSpan? GetTimeout(this HttpRequestMessage request)
217221
{
218-
if (request == null)
219-
throw new ArgumentNullException(nameof(request));
222+
request.AssertNotNull(nameof(request));
220223

221224
if (request.Properties.TryGetValue(TimeoutPropertyKey, out var value) && value is TimeSpan timeout)
222225
return timeout;

FaunaDB.Client/Client/FaunaClient.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public FaunaClient(
4646
/// </summary>
4747
public FaunaClient(IClientIO clientIO)
4848
{
49+
clientIO.AssertNotNull(nameof(clientIO));
4950
this.clientIO = clientIO;
5051
}
5152

@@ -246,6 +247,9 @@ static IClientIO CreateClient(
246247
HttpClient httpClient = null,
247248
Version httpVersion = null)
248249
{
250+
secret.AssertNotNull(nameof(secret));
251+
endpoint.AssertNotNull(nameof(endpoint));
252+
249253
return new DefaultClientIO(
250254
secret: secret,
251255
endpoint: new Uri(endpoint),

FaunaDB.Client/Client/LastSeen.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Threading;
32

43
/// <summary>

FaunaDB.Client/Client/StreamingEventHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class StreamingEventHandler : IObservable<Value>, IDisposable
1818

1919
public StreamingEventHandler(Stream dataSource)
2020
{
21+
dataSource.AssertNotNull(nameof(dataSource));
2122
observers = new List<IObserver<Value>>();
2223
streamReader = new StreamReader(dataSource);
2324
}

FaunaDB.Client/Client/StreamingEventMonitor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using FaunaDB.Types;
3+
using FaunaDB.Errors;
34

45
namespace FaunaDB.Client
56
{
@@ -23,6 +24,7 @@ public StreamingEventMonitor(Action<Value> onNext, Action<Exception> onError, Ac
2324

2425
public void Subscribe(StreamingEventHandler provider)
2526
{
27+
provider.AssertNotNull(nameof(provider));
2628
cancellation = provider.Subscribe(this);
2729
this.provider = provider;
2830
this.provider.RequestData();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace FaunaDB.Errors
4+
{
5+
static class NotNullValidator
6+
{
7+
public static void AssertNotNull(this object value, string valueName)
8+
{
9+
if (value == null)
10+
{
11+
throw new ArgumentNullException(valueName);
12+
}
13+
}
14+
}
15+
}

FaunaDB.Client/Query/PageHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using FaunaDB.Client;
55
using FaunaDB.Types;
6+
using FaunaDB.Errors;
67

78
namespace FaunaDB.Query
89
{
@@ -27,6 +28,9 @@ public PageHelper(FaunaClient client,
2728
Expr events = null,
2829
Expr sources = null)
2930
{
31+
client.AssertNotNull(nameof(client));
32+
set.AssertNotNull(nameof(set));
33+
3034
this.client = client;
3135
this.set = set;
3236
this.ts = ts;

FaunaDB.Client/Query/Unescaped.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FaunaDB.Types;
2+
using FaunaDB.Errors;
23
using Newtonsoft.Json;
34
using System.Linq;
45
using System;
@@ -16,9 +17,7 @@ class UnescapedObject : Expr
1617

1718
public UnescapedObject(IReadOnlyDictionary<string, Expr> values)
1819
{
19-
if (values == null)
20-
throw new ArgumentNullException(nameof(values));
21-
20+
values.AssertNotNull(nameof(values));
2221
Values = values;
2322
}
2423

@@ -74,17 +73,13 @@ class UnescapedArray : Expr
7473

7574
public UnescapedArray(IReadOnlyList<Expr> value)
7675
{
77-
if (value == null)
78-
throw new ArgumentNullException(nameof(value));
79-
76+
value.AssertNotNull(nameof(value));
8077
Value = value;
8178
}
8279

8380
public UnescapedArray(params Expr[] value)
8481
{
85-
if (value == null)
86-
throw new ArgumentNullException(nameof(value));
87-
82+
value.AssertNotNull(nameof(value));
8883
Value = value;
8984
}
9085

FaunaDB.Client/Types/ArrayV.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FaunaDB.Query;
22
using FaunaDB.Utils;
3+
using FaunaDB.Errors;
34
using Newtonsoft.Json;
45
using System;
56
using System.Collections;
@@ -28,18 +29,14 @@ internal ArrayV(params Value[] values) : this((IEnumerable<Value>)values)
2829

2930
internal ArrayV(IEnumerable<Value> values)
3031
{
32+
values.AssertNotNull(nameof(values));
3133
Value = new List<Value>(values);
32-
33-
if (values == null)
34-
throw new ArgumentNullException(nameof(values));
3534
}
3635

3736
internal ArrayV(IReadOnlyList<Value> value)
3837
{
38+
value.AssertNotNull(nameof(value));
3939
Value = value;
40-
41-
if (value == null)
42-
throw new ArgumentNullException(nameof(value));
4340
}
4441

4542
internal ArrayV(Action<Action<Value>> builder)

0 commit comments

Comments
 (0)