Home | History | Annotate | Download | only in tests
      1 /*
      2  * Test printstrn/umoven.
      3  *
      4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv (at) altlinux.org>
      5  * Copyright (c) 2017-2018 The strace developers.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "tests.h"
     32 
     33 #include <stdio.h>
     34 #include <string.h>
     35 #include <unistd.h>
     36 #include <asm/unistd.h>
     37 
     38 #include "scno.h"
     39 #include "test_ucopy.h"
     40 
     41 static const char *errstr;
     42 
     43 static void add_key(const char *addr, const unsigned int len)
     44 {
     45 	errstr = sprintrc(syscall(__NR_add_key, 0, 0, addr, len, -1));
     46 }
     47 
     48 static void
     49 test_printstrn_at(char *const p, const unsigned int test_max)
     50 {
     51 	unsigned int i;
     52 
     53 	for (i = 0; i <= test_max; ++i) {
     54 		add_key(p + (test_max - i), i);
     55 		printf("add_key(NULL, NULL, \"%.*s\", %u"
     56 		       ", KEY_SPEC_THREAD_KEYRING) = %s\n",
     57 		       (int) i, p + (test_max - i), i, errstr);
     58 	}
     59 }
     60 
     61 static void
     62 test_efault(const unsigned int test_max)
     63 {
     64 	char *p = tail_alloc(test_max);
     65 	memset(p, '/', test_max);
     66 	unsigned int i;
     67 
     68 	for (i = 0; i <= test_max; ++i) {
     69 		unsigned int j;
     70 		for (j = 1; j <= sizeof(long); ++j) {
     71 			add_key(p + (test_max - i), i + j);
     72 			printf("add_key(NULL, NULL, %p, %u"
     73 			       ", KEY_SPEC_THREAD_KEYRING) = %s\n",
     74 			       p + (test_max - i), i + j, errstr);
     75 		}
     76 	}
     77 }
     78 
     79 static void
     80 test_print_memory(char *const p, const unsigned int test_max)
     81 {
     82 	add_key(p, test_max);
     83 	printf("add_key(NULL, NULL, ");
     84 	print_quoted_memory(p, test_max);
     85 	printf(", %u, KEY_SPEC_THREAD_KEYRING) = %s\n", test_max, errstr);
     86 }
     87 
     88 void
     89 test_printstrn(const unsigned int test_max)
     90 {
     91 	/*
     92 	 * abcdefgh|
     93 	 * abcdefg|h
     94 	 * abcdef|gh
     95 	 * abcde|fgh
     96 	 * abcd|efgh
     97 	 * abc|defgh
     98 	 * ab|cdefgh
     99 	 * a|bcdefgh
    100 	 * |abcdefgh
    101 	 */
    102 	const unsigned int page_size = get_page_size();
    103 	char *p = tail_alloc(test_max + page_size);
    104 	fill_memory_ex(p, test_max + page_size, 'a', 'z' - 'a' + 1);
    105 
    106 	unsigned int i;
    107 	for (i = 1; i <= sizeof(long); ++i)
    108 		test_printstrn_at(p + i, test_max);
    109 	for (i = 0; i < sizeof(long); ++i)
    110 		test_printstrn_at(p + page_size - i, test_max);
    111 	test_efault(test_max);
    112 
    113 	fill_memory_ex(p, test_max + page_size, 0x00, 0xFF);
    114 	/* Test corner cases when octal quoting goes before digit */
    115 	for (i = 0; i < 11; i++)
    116 		p[2 + 3 * i] = '0' + i - 1;
    117 
    118 	test_print_memory(p, test_max);
    119 }
    120