Path: blob/main/lib/libc/tests/stdio/gets_s_test.c
39530 views
/*-1* Copyright (c) 2017 Cyril S. E. Schubert2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425#include <assert.h>26#include <stdint.h>27#include <stdio.h>28#include <stdlib.h>29#include <sys/types.h>30#include <unistd.h>31#include <sys/wait.h>3233#include <atf-c.h>3435static errno_t error_code;36static const char * message;3738void39h(const char * msg, void * ptr __unused, errno_t error)40{41error_code = error;42message = msg;43}4445/* null ptr */46ATF_TC_WITHOUT_HEAD(null_ptr);47ATF_TC_BODY(null_ptr, tc)48{49ATF_CHECK_MSG(gets_s(NULL, 1) == NULL,50"gets_s() failed to handle NULL pointer");51}5253/* normal */54ATF_TC_WITHOUT_HEAD(normal);55ATF_TC_BODY(normal, tc)56{57pid_t kidpid;58int fd[2];59int nfd;6061// close(STDIN_FILENO);62// close(STDOUT_FILENO);63pipe(fd);6465if ((kidpid = fork()) == 0) {66char b[10];6768close(fd[1]);69nfd = dup2(fd[0], 0);70close(fd[0]);71stdin = fdopen(nfd, "r");72ATF_CHECK_MSG(gets_s(b, sizeof(b)) == 0, "gets_s() normal failed");73fclose(stdin);74} else {75int stat;7677close(fd[0]);78stdout = fdopen(fd[1], "w");79puts("a sting");80fclose(stdout);81(void) waitpid(kidpid, &stat, WEXITED);82}83}8485/* n > rmax */86ATF_TC_WITHOUT_HEAD(n_gt_rmax);87ATF_TC_BODY(n_gt_rmax, tc)88{89char b;9091ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL,92"gets_s() n > RSIZE_MAX");93}9495/* n == 0 */96ATF_TC_WITHOUT_HEAD(n_eq_zero);97ATF_TC_BODY(n_eq_zero, tc)98{99char b;100101ATF_CHECK_MSG(gets_s(&b, 0) == NULL, "gets_s() n is zero");102}103104/* n > rmax, handler */105ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);106ATF_TC_BODY(n_gt_rmax_handler, tc)107{108char b;109110error_code = 0;111message = NULL;112set_constraint_handler_s(h);113ATF_CHECK_MSG(gets_s(&b, RSIZE_MAX + 1) == NULL, "gets_s() n > RSIZE_MAX");114ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);115ATF_CHECK_MSG(strcmp(message, "gets_s : n > RSIZE_MAX") == 0, "gets_s(): incorrect error message");116}117118/* n == 0, handler */119ATF_TC_WITHOUT_HEAD(n_eq_zero_handler);120ATF_TC_BODY(n_eq_zero_handler, tc)121{122char b;123124error_code = 0;125message = NULL;126set_constraint_handler_s(h);127ATF_CHECK(gets_s(&b, 0) == NULL);128ATF_CHECK_MSG(error_code > 0, "gets_s() error code is %d", error_code);129ATF_CHECK_MSG(strcmp(message, "gets_s : n == 0") == 0, "gets_s(): incorrect error message");130}131132ATF_TP_ADD_TCS(tp)133{134ATF_TP_ADD_TC(tp, null_ptr);135ATF_TP_ADD_TC(tp, normal);136ATF_TP_ADD_TC(tp, n_gt_rmax);137ATF_TP_ADD_TC(tp, n_eq_zero);138ATF_TP_ADD_TC(tp, n_gt_rmax_handler);139ATF_TP_ADD_TC(tp, n_eq_zero_handler);140return (atf_no_error());141}142143144