forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNetworkTypeTests.cs
More file actions
136 lines (118 loc) · 4.67 KB
/
NetworkTypeTests.cs
File metadata and controls
136 lines (118 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
using NpgsqlTypes;
using NUnit.Framework;
namespace Npgsql.Tests.Types;
/// <summary>
/// Tests on PostgreSQL numeric types
/// </summary>
/// <remarks>
/// https://www.postgresql.org/docs/current/static/datatype-net-types.html
/// </remarks>
class NetworkTypeTests : TestBase
{
[Test]
public Task Inet_v4_as_IPAddress()
=> AssertType(IPAddress.Parse("192.168.1.1"), "192.168.1.1/32", "inet", skipArrayCheck: true);
[Test]
public Task Inet_v4_array_as_IPAddress_array()
=> AssertType(
new[]
{
IPAddress.Parse("192.168.1.1"),
IPAddress.Parse("192.168.1.2")
},
"{192.168.1.1,192.168.1.2}", "inet[]");
[Test]
public Task Inet_v6_as_IPAddress()
=> AssertType(
IPAddress.Parse("2001:1db8:85a3:1142:1000:8a2e:1370:7334"),
"2001:1db8:85a3:1142:1000:8a2e:1370:7334/128",
"inet",
skipArrayCheck: true);
[Test]
public Task Inet_v6_array_as_IPAddress_array()
=> AssertType(
new[]
{
IPAddress.Parse("2001:1db8:85a3:1142:1000:8a2e:1370:7334"),
IPAddress.Parse("2001:1db8:85a3:1142:1000:8a2e:1370:7335")
},
"{2001:1db8:85a3:1142:1000:8a2e:1370:7334,2001:1db8:85a3:1142:1000:8a2e:1370:7335}", "inet[]");
[Test, IssueLink("https://github.com/dotnet/corefx/issues/33373")]
public Task IPAddress_Any()
=> AssertTypeWrite(IPAddress.Any, "0.0.0.0/32", "inet", skipArrayCheck: true);
[Test]
public Task IPNetwork_as_cidr()
=> AssertType(
new IPNetwork(IPAddress.Parse("192.168.1.0"), 24),
"192.168.1.0/24",
"cidr");
#pragma warning disable CS0618 // NpgsqlCidr is obsolete
[Test]
public Task NpgsqlCidr_as_Cidr()
=> AssertType(
new NpgsqlCidr(IPAddress.Parse("192.168.1.0"), netmask: 24),
"192.168.1.0/24",
"cidr",
valueTypeEqualsFieldType: false);
#pragma warning restore CS0618
[Test]
public Task Inet_v4_as_NpgsqlInet()
=> AssertType(
new NpgsqlInet(IPAddress.Parse("192.168.1.1"), 24),
"192.168.1.1/24",
"inet",
valueTypeEqualsFieldType: false);
[Test]
public Task Inet_v6_as_NpgsqlInet()
=> AssertType(
new NpgsqlInet(IPAddress.Parse("2001:1db8:85a3:1142:1000:8a2e:1370:7334"), 24),
"2001:1db8:85a3:1142:1000:8a2e:1370:7334/24",
"inet",
valueTypeEqualsFieldType: false);
[Test]
public Task Macaddr()
=> AssertType(PhysicalAddress.Parse("08-00-2B-01-02-03"), "08:00:2b:01:02:03", "macaddr");
[Test]
public async Task Macaddr8()
{
await using var conn = await OpenConnectionAsync();
if (conn.PostgreSqlVersion < new Version(10, 0))
Assert.Ignore("macaddr8 only supported on PostgreSQL 10 and above");
await AssertType(PhysicalAddress.Parse("08-00-2B-01-02-03-04-05"), "08:00:2b:01:02:03:04:05",
"macaddr8", dataTypeInference: DataTypeInference.Mismatch);
}
[Test]
public async Task Macaddr8_write_with_6_bytes()
{
await using var conn = await OpenConnectionAsync();
if (conn.PostgreSqlVersion < new Version(10, 0))
Assert.Ignore("macaddr8 only supported on PostgreSQL 10 and above");
await AssertTypeWrite(PhysicalAddress.Parse("08-00-2B-01-02-03"), "08:00:2b:ff:fe:01:02:03",
"macaddr8", dataTypeInference: DataTypeInference.Mismatch);
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/835")]
public async Task Macaddr_multiple()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT unnest(ARRAY['08-00-2B-01-02-03'::MACADDR, '08-00-2B-01-02-04'::MACADDR])", conn);
await using var r = await cmd.ExecuteReaderAsync();
r.Read();
var p1 = (PhysicalAddress)r[0];
r.Read();
var p2 = (PhysicalAddress)r[0];
Assert.That(p1, Is.EqualTo(PhysicalAddress.Parse("08-00-2B-01-02-03")));
Assert.That(p2, Is.EqualTo(PhysicalAddress.Parse("08-00-2B-01-02-04")));
}
[Test]
public async Task Macaddr_write_validation()
{
await using var conn = await OpenConnectionAsync();
if (conn.PostgreSqlVersion < new Version(10, 0))
Assert.Ignore("macaddr8 only supported on PostgreSQL 10 and above");
await AssertTypeUnsupportedWrite<PhysicalAddress, ArgumentException>(PhysicalAddress.Parse("08-00-2B-01-02-03-04-05"), "macaddr");
}
}