Skip to content

Commit a395f20

Browse files
committed
IPv6 comparison tests
1 parent 81dfcee commit a395f20

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

api/IPAddress.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,31 @@ class IPAddress : public Printable {
5757
IPAddress(IPType ip_type);
5858
IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
5959
IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4, uint8_t o5, uint8_t o6, uint8_t o7, uint8_t o8, uint8_t o9, uint8_t o10, uint8_t o11, uint8_t o12, uint8_t o13, uint8_t o14, uint8_t o15, uint8_t o16);
60-
IPAddress(uint32_t address); // IPv4 only; see implementation note
60+
IPAddress(uint32_t address); // IPv4; see implementation note
6161
IPAddress(const uint8_t *address); // IPv4
6262
IPAddress(IPType ip_type, const uint8_t *address);
6363

6464
bool fromString(const char *address);
6565
bool fromString(const String &address) { return fromString(address.c_str()); }
6666

6767
// Overloaded cast operator to allow IPAddress objects to be used where a uint32_t is expected
68-
// IPv4 only; see implementation note
68+
// NOTE: IPv4 only; see implementation note
6969
operator uint32_t() const { return _type == IPv4 ? _address.dword[3] : 0; };
7070

7171
bool operator==(const IPAddress& addr) const;
7272
bool operator!=(const IPAddress& addr) const { return !(*this == addr); };
7373

74-
// IPv4 only; we don't know the length of the pointer
74+
// NOTE: IPv4 only; we don't know the length of the pointer
7575
bool operator==(const uint8_t* addr) const;
7676

7777
// Overloaded index operator to allow getting and setting individual octets of the address
7878
uint8_t operator[](int index) const;
7979
uint8_t& operator[](int index);
8080

8181
// Overloaded copy operators to allow initialisation of IPAddress objects from other types
82-
// IPv4 only
82+
// NOTE: IPv4 only
8383
IPAddress& operator=(const uint8_t *address);
84-
// IPv4 only; see implementation note
84+
// NOTE: IPv4 only; see implementation note
8585
IPAddress& operator=(uint32_t address);
8686

8787
virtual size_t printTo(Print& p) const;

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(TEST_SRCS
3535
src/IPAddress/test_IPAddress6.cpp
3636
src/IPAddress/test_operator_assignment.cpp
3737
src/IPAddress/test_operator_comparison.cpp
38+
src/IPAddress/test_operator_comparison6.cpp
3839
src/IPAddress/test_operator_parentheses.cpp
3940
src/IPAddress/test_printTo.cpp
4041
src/IPAddress/test_printTo6.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2020 Arduino. All rights reserved.
3+
*/
4+
5+
/**************************************************************************************
6+
* INCLUDE
7+
**************************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <IPAddress.h>
12+
13+
/**************************************************************************************
14+
* TEST CODE
15+
**************************************************************************************/
16+
17+
TEST_CASE ("Testing two basic constructs the same", "[IPAddress6-Operator-==-01]")
18+
{
19+
arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc);
20+
REQUIRE((ip1 == ip2) == true);
21+
}
22+
23+
TEST_CASE ("Testing two addresses different", "[IPAddress-Operator-==-02]")
24+
{
25+
arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1);
26+
REQUIRE((ip1 == ip2) == false);
27+
}
28+
29+
TEST_CASE ("Testing not equals different address is true", "[IPAddress-Operator-==-03]")
30+
{
31+
arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0xfd,0x12, 0x34,0x56, 0x78,0x9a, 0,1, 0,0, 0,0, 0,0, 0,1);
32+
REQUIRE((ip1 != ip2) == true);
33+
}
34+
35+
TEST_CASE ("Testing not equals same address is false", "[IPAddress-Operator-==-04]")
36+
{
37+
arduino::IPAddress ip1(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc);
38+
REQUIRE((ip1 != ip2) == false);
39+
}
40+
41+
// IPv4 and IPv6 differ based on type (irrespective of bytes)
42+
43+
TEST_CASE ("Testing IPv4 vs IPv6", "[IPAddress6-Operator-==-05]")
44+
{
45+
arduino::IPAddress ip1(10, 0, 0, 1), ip2(0x20,0x01, 0xd,0xb8, 1,2, 3,4, 5,6, 7,8, 9,0xa, 0xb,0xc);
46+
REQUIRE((ip1 == ip2) == false);
47+
}
48+
49+
TEST_CASE ("Testing IPv4 vs IPv6 equivalent IPv4-compatible address (deprecated)", "[IPAddress6-Operator-==-05]")
50+
{
51+
arduino::IPAddress ip1(10, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1);
52+
REQUIRE((ip1 == ip2) == false);
53+
}
54+
55+
TEST_CASE ("Testing IPv4 vs IPv6 localhost", "[IPAddress6-Operator-==-05]")
56+
{
57+
arduino::IPAddress ip1(127, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 10,0, 0,1);
58+
REQUIRE((ip1 == ip2) == false);
59+
}
60+
61+
TEST_CASE ("Testing IPv4 equivalent compatible address vs IPv6 localhost", "[IPAddress6-Operator-==-05]")
62+
{
63+
arduino::IPAddress ip1(0, 0, 0, 1), ip2(0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,1);
64+
REQUIRE((ip1 == ip2) == false);
65+
}

0 commit comments

Comments
 (0)