Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tor
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
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Benjamin J. Thompson
Tor
Commits
4b362d00
Commit
4b362d00
authored
12 years ago
by
Nick Mathewson
Browse files
Options
Downloads
Patches
Plain Diff
Turn the read_file_until_eof tests into a single implementation
parent
d64bf286
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/test_util.c
+38
-81
38 additions, 81 deletions
src/test/test_util.c
with
38 additions
and
81 deletions
src/test/test_util.c
+
38
−
81
View file @
4b362d00
...
...
@@ -19,114 +19,71 @@
#endif
static
void
test_util_read_file_fifo_tiny_limit
(
void
*
arg
)
test_util_read_until_eof_impl
(
const
char
*
fname
,
size_t
file_len
,
size_t
read_limit
)
{
#ifndef _WIN32
char
*
fifo_name
=
NULL
;
char
*
test_str
=
NULL
;
char
*
str
=
NULL
;
size_t
sz
=
9999999
;
int
fd
=
-
1
;
int
read_fd
=
-
1
;
size_t
sz
;
(
void
)
arg
;
int
r
;
fifo_name
=
tor_strdup
(
get_fname
(
"tor_test_fifo_tiny"
));
fd
=
open
(
fifo_name
,
O_WRONLY
|
O_CREAT
,
0600
);
test_neq
(
fd
,
-
1
);
test_eq
(
write
(
fd
,
"short"
,
6
),
6
);
close
(
fd
);
fifo_name
=
tor_strdup
(
get_fname
(
fname
));
test_str
=
tor_malloc
(
file_len
);
crypto_rand
(
test_str
,
file_len
);
// purposely set limit shorter than what we wrote to the FIFO to
// test the maximum, and that it puts the NUL in the right spot
r
=
write_bytes_to_file
(
fifo_name
,
test_str
,
file_len
,
1
);
tt_int_op
(
r
,
==
,
0
);
fd
=
open
(
fifo_name
,
O_RDONLY
|
O_BINARY
);
tt_int_op
(
fd
,
>=
,
0
);
str
=
read_file_to_str_until_eof
(
fd
,
read_limit
,
&
sz
);
close
(
fd
);
tt_assert
(
str
!=
NULL
);
read_fd
=
open
(
fifo_name
,
O_RDONLY
);
str
=
read_file_to_str_until_eof
(
read_fd
,
4
,
&
sz
);
close
(
read_fd
);
if
(
read_limit
<
file_len
)
tt_int_op
(
sz
,
==
,
read_limit
);
else
tt_int_op
(
sz
,
==
,
file_len
);
test_eq
(
str
[
0
],
's'
);
test_eq
(
str
[
1
],
'h'
);
test_eq
(
str
[
2
],
'o'
);
test_eq
(
str
[
3
],
'r'
);
test_eq
(
str
[
4
],
'\0'
);
test_mem_op
(
test_str
,
==
,
str
,
sz
);
test_assert
(
str
[
sz
]
==
'\0'
);
done:
unlink
(
fifo_name
);
tor_free
(
fifo_name
);
tor_free
(
test_str
);
tor_free
(
str
);
#endif
}
static
void
test_util_read_file_
fifo_two_loops
(
void
*
arg
)
test_util_read_file_
eof_tiny_limit
(
void
*
arg
)
{
#ifndef _WIN32
char
*
fifo_name
=
NULL
;
char
*
str
=
NULL
;
char
data
[
2048
];
int
i
=
0
;
int
fd
=
-
1
;
int
read_fd
=
-
1
;
size_t
sz
;
(
void
)
arg
;
// purposely set limit shorter than what we wrote to the FIFO to
// test the maximum, and that it puts the NUL in the right spot
while
(
i
<
2048
)
{
data
[
i
]
=
(
char
)(
i
&
0xff
);
++
i
;
}
test_util_read_until_eof_impl
(
"tor_test_fifo_tiny"
,
5
,
4
);
}
static
void
test_util_read_file_eof_two_loops
(
void
*
arg
)
{
(
void
)
arg
;
// write more than 1024 bytes to the FIFO to test two passes through
// the loop in the method; if the re-alloc size is changed this
// should be updated as well.
fifo_name
=
tor_strdup
(
get_fname
(
"tor_fifo_test_2k"
));
fd
=
open
(
fifo_name
,
O_WRONLY
|
O_CREAT
,
0600
);
test_neq
(
fd
,
-
1
);
test_eq
(
write
(
fd
,
data
,
2048
),
2048
);
close
(
fd
);
read_fd
=
open
(
fifo_name
,
O_RDONLY
);
str
=
read_file_to_str_until_eof
(
read_fd
,
1024
*
1024
,
&
sz
);
close
(
read_fd
);
for
(
i
=
0
;
i
<
2048
;
++
i
)
{
test_eq
(
str
[
i
],
(
char
)(
i
&
0xff
));
}
done:
unlink
(
fifo_name
);
tor_free
(
fifo_name
);
tor_free
(
str
);
#endif
test_util_read_until_eof_impl
(
"tor_test_fifo_2k"
,
2048
,
10000
);
}
static
void
test_util_read_file_
fifo
_zero_bytes
(
void
*
arg
)
test_util_read_file_
eof
_zero_bytes
(
void
*
arg
)
{
#ifndef _WIN32
char
*
fifo_name
=
NULL
;
char
*
str
=
NULL
;
int
fd
=
-
1
;
int
read_fd
=
-
1
;
size_t
sz
;
(
void
)
arg
;
fifo_name
=
tor_strdup
(
get_fname
(
"tor_fifo_test_zero_bytes"
));
// zero-byte fifo
fd
=
open
(
fifo_name
,
O_WRONLY
|
O_CREAT
,
0600
);
test_neq
(
fd
,
-
1
);
close
(
fd
);
read_fd
=
open
(
fifo_name
,
O_RDONLY
);
str
=
read_file_to_str_until_eof
(
read_fd
,
1024
,
&
sz
);
close
(
read_fd
);
test_neq
(
str
,
NULL
);
test_eq
(
str
[
0
],
'\0'
);
done:
unlink
(
fifo_name
);
tor_free
(
fifo_name
);
tor_free
(
str
);
#endif
test_util_read_until_eof_impl
(
"tor_test_fifo_empty"
,
0
,
10000
);
}
static
void
...
...
@@ -3302,9 +3259,9 @@ struct testcase_t util_tests[] = {
UTIL_TEST
(
envnames
,
0
),
UTIL_TEST
(
make_environment
,
0
),
UTIL_TEST
(
set_env_var_in_sl
,
0
),
UTIL_TEST
(
read_file_
fifo
_tiny_limit
,
0
),
UTIL_TEST
(
read_file_
fifo
_two_loops
,
0
),
UTIL_TEST
(
read_file_
fifo
_zero_bytes
,
0
),
UTIL_TEST
(
read_file_
eof
_tiny_limit
,
0
),
UTIL_TEST
(
read_file_
eof
_two_loops
,
0
),
UTIL_TEST
(
read_file_
eof
_zero_bytes
,
0
),
END_OF_TESTCASES
};
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