Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Ayaka Koshibe
2014-09-13 22:19:02 -0700
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
16698a3f1b21acc391e6739e64b5cac75b524b72
16698a3f
1 parent
1a100983
Tests for IPAddress and VLANID
Change-Id: If8183366428c9b4fb14f78005922b2229cff1456
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
13 deletions
utils/misc/src/main/java/org/onlab/packet/IPAddress.java
utils/misc/src/main/java/org/onlab/packet/VLANID.java
utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
utils/misc/src/test/java/org/onlab/packet/VLANIDTest.java
utils/misc/src/main/java/org/onlab/packet/IPAddress.java
View file @
16698a3
...
...
@@ -40,12 +40,11 @@ public class IPAddress {
* @return an IP address
*/
public
static
IPAddress
valueOf
(
int
address
)
{
byte
[]
bytes
=
new
byte
[]
{
(
byte
)
((
address
>>
24
)
&
0xff
),
(
byte
)
((
address
>>
16
)
&
0xff
),
(
byte
)
((
address
>>
8
)
&
0xff
),
(
byte
)
((
address
>>
0
)
&
0xff
)
};
byte
[]
bytes
=
new
byte
[
INET_LEN
];
for
(
int
i
=
0
;
i
<
INET_LEN
;
i
++)
{
bytes
[
i
]
=
(
byte
)
((
address
>>
(
INET_LEN
-
(
i
+
1
))
*
8
)
&
0xff
);
}
return
new
IPAddress
(
Version
.
INET
,
bytes
);
}
...
...
@@ -87,12 +86,16 @@ public class IPAddress {
return
Arrays
.
copyOf
(
this
.
octets
,
INET_LEN
);
}
/**
* Returns the integral value of this IP address.
*
* @return the IP address's value as an integer
*/
public
int
toInt
()
{
int
address
=
((
octets
[
0
]
<<
24
)
|
(
octets
[
1
]
<<
16
)
|
(
octets
[
2
]
<<
8
)
|
(
octets
[
3
]
<<
0
));
int
address
=
0
;
for
(
int
i
=
0
;
i
<
INET_LEN
;
i
++)
{
address
|=
octets
[
i
]
<<
((
INET_LEN
-
(
i
+
1
))
*
8
);
}
return
address
;
}
...
...
utils/misc/src/main/java/org/onlab/packet/VLANID.java
View file @
16698a3
...
...
@@ -7,9 +7,9 @@ public class VLANID {
private
final
short
value
;
// Based on convention used elsewhere? Check and change if needed
p
rivate
static
final
short
UNTAGGED
=
(
short
)
0xffff
;
p
ublic
static
final
short
UNTAGGED
=
(
short
)
0xffff
;
// A VLAN ID is actually 12 bits of a VLAN tag.
p
rivate
static
final
short
MAX_VLAN
=
4095
;
p
ublic
static
final
short
MAX_VLAN
=
4095
;
protected
VLANID
()
{
this
.
value
=
UNTAGGED
;
...
...
utils/misc/src/test/java/org/onlab/packet/IPAddressTest.java
0 → 100644
View file @
16698a3
package
org
.
onlab
.
packet
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.util.Arrays
;
import
org.junit.Test
;
import
org.onlab.packet.IPAddress.Version
;
import
com.google.common.testing.EqualsTester
;
public
class
IPAddressTest
{
private
static
final
byte
[]
BYTES1
=
new
byte
[]
{
0x0
,
0x0
,
0x0
,
0xa
};
private
static
final
byte
[]
BYTES2
=
new
byte
[]
{
0x0
,
0x0
,
0x0
,
0xb
};
private
static
final
int
INTVAL1
=
10
;
private
static
final
int
INTVAL2
=
12
;
private
static
final
String
STRVAL
=
"0.0.0.11"
;
@Test
public
void
testEquality
()
{
IPAddress
ip1
=
IPAddress
.
valueOf
(
BYTES1
);
IPAddress
ip2
=
IPAddress
.
valueOf
(
BYTES2
);
IPAddress
ip3
=
IPAddress
.
valueOf
(
INTVAL1
);
IPAddress
ip4
=
IPAddress
.
valueOf
(
INTVAL2
);
IPAddress
ip5
=
IPAddress
.
valueOf
(
STRVAL
);
new
EqualsTester
().
addEqualityGroup
(
ip1
,
ip3
)
.
addEqualityGroup
(
ip2
,
ip5
)
.
addEqualityGroup
(
ip4
)
.
testEquals
();
}
@Test
public
void
basics
()
{
IPAddress
ip4
=
IPAddress
.
valueOf
(
BYTES1
);
assertEquals
(
"incorrect IP Version"
,
Version
.
INET
,
ip4
.
version
());
assertEquals
(
"faulty toOctets()"
,
Arrays
.
equals
(
new
byte
[]
{
0x0
,
0x0
,
0x0
,
0xa
},
ip4
.
toOctets
()),
true
);
assertEquals
(
"faulty toInt()"
,
INTVAL1
,
ip4
.
toInt
());
assertEquals
(
"faulty toString()"
,
"0.0.0.10"
,
ip4
.
toString
());
}
}
utils/misc/src/test/java/org/onlab/packet/VLANIDTest.java
0 → 100644
View file @
16698a3
package
org
.
onlab
.
packet
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
org.junit.Test
;
import
com.google.common.testing.EqualsTester
;
public
class
VLANIDTest
{
@Test
public
void
testEquality
()
{
VLANID
vlan1
=
VLANID
.
vlanId
((
short
)
-
1
);
VLANID
vlan2
=
VLANID
.
vlanId
((
short
)
100
);
VLANID
vlan3
=
VLANID
.
vlanId
((
short
)
100
);
new
EqualsTester
().
addEqualityGroup
(
VLANID
.
vlanId
(),
vlan1
)
.
addEqualityGroup
(
vlan2
,
vlan3
)
.
addEqualityGroup
(
VLANID
.
vlanId
((
short
)
10
));
}
@Test
public
void
basics
()
{
// purposefully create UNTAGGED VLAN
VLANID
vlan1
=
VLANID
.
vlanId
((
short
)
10
);
VLANID
vlan2
=
VLANID
.
vlanId
((
short
)
-
1
);
assertEquals
(
"incorrect VLAN value"
,
10
,
vlan1
.
toShort
());
assertEquals
(
"invalid untagged value"
,
VLANID
.
UNTAGGED
,
vlan2
.
toShort
());
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testIllicitVLAN
()
{
VLANID
.
vlanId
((
short
)
5000
);
}
}
Please
register
or
login
to post a comment