/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* sbrk31*/3233#include <sys/types.h>34#include <sys/stat.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>39#include <errno.h>40#include <err.h>4142#include "config.h"43#include "test.h"4445static46int47try_sbrk(long val)48{49void *rv;50rv = sbrk(val);51if (rv==(void *)-1) {52return errno;53}54return 0;55}5657static58void59enforce_sbrk(long val, const char *desc, int err)60{61int e;6263e = try_sbrk(val);64if (e != err && e==0) {65warnx("FAILURE: sbrk(%s): no error", desc);66return;67}68if (e != err) {69errno = e;70warn("FAILURE: sbrk(%s): wrong error", desc);71return;72}73warnx("passed: sbrk(%s)", desc);74}7576static77void78sbrk_bigpos(void)79{80enforce_sbrk(4096*1024*256, "huge positive", ENOMEM);81}8283static84void85sbrk_bigneg(void)86{87enforce_sbrk(-4096*1024*256, "huge negative", EINVAL);88}8990static91void92sbrk_neg(void)93{94enforce_sbrk(-8192, "too-large negative", EINVAL);95}9697static98void99sbrk_unalignedpos(void)100{101switch (try_sbrk(17)) {102case 0:103case EINVAL:104warnx("passed: sbrk(unaligned positive)");105break;106default:107warn("FAILURE: sbrk(unaligned positive): wrong error");108break;109}110}111112static113void114sbrk_unalignedneg(void)115{116switch (try_sbrk(-17)) {117case 0:118case EINVAL:119warnx("passed: sbrk(unaligned negative)");120break;121default:122warn("FAILURE: sbrk(unaligned negative): wrong error");123break;124}125}126127void128test_sbrk(void)129{130sbrk_neg();131sbrk_bigpos();132sbrk_bigneg();133sbrk_unalignedpos();134sbrk_unalignedneg();135}136137138139