Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/tests/stdio/gets_s_test.c
/*- | |||||
* Copyright (c) 2017 Cyril S. E. Schubert. All rights reserved. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include <assert.h> | |||||
#include <stdio.h> | |||||
#include <sys/types.h> | |||||
ed: This include needs to be in a separate block above `<assert.h>`. | |||||
cyAuthorUnsubmitted Not Done Inline ActionsSorry about this. I was in a bit of a hurry to put it out there prior to tinderbox and testing. cy: Sorry about this. I was in a bit of a hurry to put it out there prior to tinderbox and testing. | |||||
#include <unistd.h> | |||||
#include <wait.h> | |||||
edUnsubmitted Done Inline ActionsDoesn't this need to be <sys/wait.h>? Do we even provide a header with that name? ed: Doesn't this need to be `<sys/wait.h>`? Do we even provide a header with that name? | |||||
Not Done Inline ActionsThe <sys/...> headers need to be above the others. ed: The `<sys/...>` headers need to be above the others. | |||||
#include <atf-c.h> | |||||
static errno_t e; | |||||
static const char * restrict m; | |||||
edUnsubmitted Done Inline ActionsCould these have more descriptive names? Also no need to put restrict here. This keyword only makes sense on function arguments. ed: Could these have more descriptive names? Also no need to put `restrict` here. This keyword only… | |||||
cyAuthorUnsubmitted Not Done Inline ActionsThat had crossed my mind. cy: That had crossed my mind. | |||||
void | |||||
h(const char * restrict msg, void * restrict ptr __unused, errno_t error) | |||||
{ | |||||
e = error; | |||||
m = msg; | |||||
} | |||||
/* null ptr */ | |||||
ATF_TC_WITHOUT_HEAD(null_ptr); | |||||
ATF_TC_BODY(null_ptr, tc) | |||||
{ | |||||
assert(gets_s(NULL, 1) != 0); | |||||
edUnsubmitted Done Inline ActionsWouldn't it be better to use ATF_CHECK*() here? ed: Wouldn't it be better to use `ATF_CHECK*()` here? | |||||
} | |||||
/* normal */ | |||||
ATF_TC_WITHOUT_HEAD(normal); | |||||
ATF_TC_BODY(normal, tc) | |||||
{ | |||||
pid_t kidpid; | |||||
int fd[2]; | |||||
close(STDIN_FILENO); | |||||
close(STDOUT_FILENO); | |||||
pipe(fd); | |||||
assert((kidpid == fork()) >=0); | |||||
edUnsubmitted Done Inline ActionsThat == is not intended, right? Also: missing whitespace between >= and 0. ed: That `==` is not intended, right? Also: missing whitespace between `>=` and `0`. | |||||
cyAuthorUnsubmitted Not Done Inline ActionsOops. cy: Oops. | |||||
if (kidpid == 0) { | |||||
char b[10]; | |||||
close(fd[1]); | |||||
stdin = fdopen(fd[0], "r"); | |||||
assert(gets_s(&b, sizeof(b)) == 0); | |||||
close(fd[0]); | |||||
exit (0); | |||||
} else { | |||||
int stat; | |||||
close(fd[0]); | |||||
stdout = fdopen(fd[1], "w"); | |||||
puts("a sting"); | |||||
(void) wait(&stat); | |||||
exit (0); | |||||
} | |||||
} | |||||
/* n > rmax */ | |||||
ATF_TC_WITHOUT_HEAD(n_gt_rmax); | |||||
ATF_TC_BODY(n_gt_rmax, tc) | |||||
{ | |||||
char b; | |||||
assert(gets_s(&b, RSIZE_MAX + 1) != 0); | |||||
} | |||||
/* n == 0 */ | |||||
ATF_TC_WITHOUT_HEAD(n_lt_zero); | |||||
ATF_TC_BODY(n_eq_zero, tc) | |||||
{ | |||||
char b; | |||||
assert(memset_s(&b, 0) != 0); | |||||
} | |||||
/* n > rmax, handler */ | |||||
ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler); | |||||
ATF_TC_BODY(n_gt_rmax_handler, tc) | |||||
{ | |||||
char b; | |||||
e = 0; | |||||
m = NULL; | |||||
set_constraint_handler_s(h); | |||||
assert(gets_s(&b, RSIZE_MAX + 1) != 0); | |||||
assert(e > 0); | |||||
assert(strcmp(m, "gets_s : n > RSIZE_MAX") == 0); | |||||
} | |||||
/* n == 0, handler */ | |||||
ATF_TC_WITHOUT_HEAD(n_lt_zero_handler); | |||||
ATF_TC_BODY(n_eq_zero_handler, tc) | |||||
{ | |||||
char b; | |||||
e = 0; | |||||
m = NULL; | |||||
set_constraint_handler_s(h); | |||||
assert(gets_s(&b, -1) != 0); | |||||
assert(e > 0); | |||||
assert(strcmp(m, "gets_s : n == 0") == 0); | |||||
} | |||||
ATF_TP_ADD_TCS(tp) | |||||
{ | |||||
ATF_TP_ADD_TC(tp, null_ptr) | |||||
ATF_TP_ADD_TC(tp, normal) | |||||
ATF_TP_ADD_TC(tp, n_gt_rmax) | |||||
ATF_TP_ADD_TC(tp, n_eq_zero) | |||||
ATF_TP_ADD_TC(tp, n_gt_rmax_handler) | |||||
ATF_TP_ADD_TC(tp, n_eq_zero_handler) | |||||
return (atf_no_error()); | |||||
} |
This include needs to be in a separate block above <assert.h>.