Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
id-iot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
id-iot
Commits
f1cf9064
Commit
f1cf9064
authored
9 years ago
by
Covertness
Browse files
Options
Downloads
Patches
Plain Diff
add test case
parent
ece3d26d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Cargo.toml
+4
-1
4 additions, 1 deletion
Cargo.toml
src/client.rs
+22
-0
22 additions, 0 deletions
src/client.rs
src/lib.rs
+1
-0
1 addition, 0 deletions
src/lib.rs
src/packet.rs
+33
-0
33 additions, 0 deletions
src/packet.rs
with
60 additions
and
1 deletion
Cargo.toml
+
4
−
1
View file @
f1cf9064
...
@@ -17,3 +17,6 @@ threadpool = "0.1"
...
@@ -17,3 +17,6 @@ threadpool = "0.1"
url
=
"0.2.36"
url
=
"0.2.36"
num
=
"0.1"
num
=
"0.1"
rand
=
"0.3"
rand
=
"0.3"
[dev-dependencies]
quickcheck
=
"*"
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/client.rs
+
22
−
0
View file @
f1cf9064
...
@@ -139,3 +139,25 @@ impl CoAPClient {
...
@@ -139,3 +139,25 @@ impl CoAPClient {
}
}
}
}
}
}
#[cfg(test)]
mod
test
{
use
super
::
*
;
use
packet
::{
Packet
,
PacketType
};
#[test]
fn
test_request_error_url
()
{
assert!
(
CoAPClient
::
request
(
"http://127.0.0.1"
)
.is_err
());
assert!
(
CoAPClient
::
request
(
"coap://127.0.0."
)
.is_err
());
assert!
(
CoAPClient
::
request
(
"127.0.0.1"
)
.is_err
());
}
#[test]
fn
test_reply_error
()
{
let
client
=
CoAPClient
::
new
(
"127.0.0.1:5683"
)
.unwrap
();
let
mut
packet
=
Packet
::
new
();
packet
.header
.set_type
(
PacketType
::
Acknowledgement
);
assert!
(
client
.reply
(
&
packet
,
b"Test"
.to_vec
())
.is_err
());
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/lib.rs
+
1
−
0
View file @
f1cf9064
...
@@ -71,6 +71,7 @@ extern crate threadpool;
...
@@ -71,6 +71,7 @@ extern crate threadpool;
extern
crate
url
;
extern
crate
url
;
extern
crate
num
;
extern
crate
num
;
extern
crate
rand
;
extern
crate
rand
;
#[cfg(test)]
extern
crate
quickcheck
;
pub
use
server
::
CoAPServer
;
pub
use
server
::
CoAPServer
;
pub
use
client
::
CoAPClient
;
pub
use
client
::
CoAPClient
;
...
...
This diff is collapsed.
Click to expand it.
src/packet.rs
+
33
−
0
View file @
f1cf9064
...
@@ -229,9 +229,16 @@ impl Packet {
...
@@ -229,9 +229,16 @@ impl Packet {
idx
+=
1
;
idx
+=
1
;
if
delta
==
13
{
if
delta
==
13
{
if
idx
>=
buf
.len
()
{
return
Err
(
ParseError
::
InvalidOptionLength
);
}
delta
=
buf
[
idx
]
as
usize
+
13
;
delta
=
buf
[
idx
]
as
usize
+
13
;
idx
+=
1
;
idx
+=
1
;
}
else
if
delta
==
14
{
}
else
if
delta
==
14
{
if
idx
+
1
>=
buf
.len
()
{
return
Err
(
ParseError
::
InvalidOptionLength
);
}
delta
=
(
u16
::
from_be
(
u8_to_unsigned_be!
(
buf
,
idx
,
idx
+
1
,
u16
))
+
269
)
as
usize
;
delta
=
(
u16
::
from_be
(
u8_to_unsigned_be!
(
buf
,
idx
,
idx
+
1
,
u16
))
+
269
)
as
usize
;
idx
+=
2
;
idx
+=
2
;
}
else
if
delta
==
15
{
}
else
if
delta
==
15
{
...
@@ -239,9 +246,17 @@ impl Packet {
...
@@ -239,9 +246,17 @@ impl Packet {
}
}
if
length
==
13
{
if
length
==
13
{
if
idx
>=
buf
.len
()
{
return
Err
(
ParseError
::
InvalidOptionLength
);
}
length
=
buf
[
idx
]
as
usize
+
13
;
length
=
buf
[
idx
]
as
usize
+
13
;
idx
+=
1
;
idx
+=
1
;
}
else
if
length
==
14
{
}
else
if
length
==
14
{
if
idx
+
1
>=
buf
.len
()
{
return
Err
(
ParseError
::
InvalidOptionLength
);
}
length
=
(
u16
::
from_be
(
u8_to_unsigned_be!
(
buf
,
idx
,
idx
+
1
,
u16
))
+
269
)
as
usize
;
length
=
(
u16
::
from_be
(
u8_to_unsigned_be!
(
buf
,
idx
,
idx
+
1
,
u16
))
+
269
)
as
usize
;
idx
+=
2
;
idx
+=
2
;
}
else
if
length
==
15
{
}
else
if
length
==
15
{
...
@@ -251,6 +266,9 @@ impl Packet {
...
@@ -251,6 +266,9 @@ impl Packet {
options_number
+=
delta
;
options_number
+=
delta
;
let
end
=
idx
+
length
;
let
end
=
idx
+
length
;
if
end
>
buf
.len
()
{
return
Err
(
ParseError
::
InvalidOptionLength
);
}
let
options_value
=
buf
[
idx
..
end
]
.to_vec
();
let
options_value
=
buf
[
idx
..
end
]
.to_vec
();
if
options
.contains_key
(
&
options_number
)
{
if
options
.contains_key
(
&
options_number
)
{
...
@@ -474,4 +492,19 @@ mod test {
...
@@ -474,4 +492,19 @@ mod test {
packet
.payload
=
"Hello"
.as_bytes
()
.to_vec
();
packet
.payload
=
"Hello"
.as_bytes
()
.to_vec
();
assert_eq!
(
packet
.to_bytes
()
.unwrap
(),
vec!
(
0x64
,
0x45
,
0x13
,
0xFD
,
0xD0
,
0xE2
,
0x4D
,
0xAC
,
0xFF
,
0x48
,
0x65
,
0x6C
,
0x6C
,
0x6F
));
assert_eq!
(
packet
.to_bytes
()
.unwrap
(),
vec!
(
0x64
,
0x45
,
0x13
,
0xFD
,
0xD0
,
0xE2
,
0x4D
,
0xAC
,
0xFF
,
0x48
,
0x65
,
0x6C
,
0x6C
,
0x6F
));
}
}
#[test]
fn
test_malicious_packet
()
{
use
quickcheck
::{
QuickCheck
,
TestResult
};
fn
run
(
x
:
Vec
<
u8
>
)
->
TestResult
{
match
Packet
::
from_bytes
(
&
x
[
..
])
{
Ok
(
packet
)
=>
{
TestResult
::
from_bool
(
packet
.get_token
()
.len
()
==
packet
.header
.get_token_length
()
as
usize
)
},
Err
(
_
)
=>
TestResult
::
passed
()
}
}
QuickCheck
::
new
()
.tests
(
10000
)
.quickcheck
(
run
as
fn
(
Vec
<
u8
>
)
->
TestResult
)
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment