1 /*- 2 * Copyright (c) 2013 The FreeBSD Foundation 3 * 4 * This software was developed by Pawel Jakub Dawidek under sponsorship from 5 * the FreeBSD Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/nv.h> 30 31 #include <errno.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.h> 36 37 static int ntest = 1; 38 39 #define CHECK(expr) do { \ 40 if ((expr)) \ 41 printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__); \ 42 else \ 43 printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\ 44 ntest++; \ 45 } while (0) 46 47 int 48 main(void) 49 { 50 const nvlist_t *cnvl; 51 nvlist_t *nvl; 52 void *ptr; 53 size_t size; 54 int fd; 55 56 printf("1..52\n"); 57 58 nvl = nvlist_create(0); 59 60 CHECK(!nvlist_exists_string(nvl, "nvlist/string/")); 61 ptr = strdup(""); 62 CHECK(ptr != NULL); 63 nvlist_move_string(nvl, "nvlist/string/", ptr); 64 CHECK(nvlist_error(nvl) == 0); 65 CHECK(nvlist_exists_string(nvl, "nvlist/string/")); 66 CHECK(ptr == nvlist_get_string(nvl, "nvlist/string/")); 67 68 CHECK(!nvlist_exists_string(nvl, "nvlist/string/x")); 69 ptr = strdup("x"); 70 CHECK(ptr != NULL); 71 nvlist_move_string(nvl, "nvlist/string/x", ptr); 72 CHECK(nvlist_error(nvl) == 0); 73 CHECK(nvlist_exists_string(nvl, "nvlist/string/x")); 74 CHECK(ptr == nvlist_get_string(nvl, "nvlist/string/x")); 75 76 CHECK(!nvlist_exists_string(nvl, 77 "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 78 ptr = strdup("abcdefghijklmnopqrstuvwxyz"); 79 CHECK(ptr != NULL); 80 nvlist_move_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", 81 ptr); 82 CHECK(nvlist_error(nvl) == 0); 83 CHECK(nvlist_exists_string(nvl, 84 "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 85 CHECK(ptr == 86 nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 87 88 CHECK(!nvlist_exists_descriptor(nvl, 89 "nvlist/descriptor/STDERR_FILENO")); 90 fd = dup(STDERR_FILENO); 91 CHECK(fd >= 0); 92 nvlist_move_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", fd); 93 CHECK(nvlist_error(nvl) == 0); 94 CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")); 95 CHECK(fd == 96 nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")); 97 98 CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x")); 99 ptr = malloc(1); 100 CHECK(ptr != NULL); 101 memcpy(ptr, "x", 1); 102 nvlist_move_binary(nvl, "nvlist/binary/x", ptr, 1); 103 CHECK(nvlist_error(nvl) == 0); 104 CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x")); 105 CHECK(ptr == nvlist_get_binary(nvl, "nvlist/binary/x", NULL)); 106 CHECK(ptr == nvlist_get_binary(nvl, "nvlist/binary/x", &size)); 107 CHECK(size == 1); 108 109 CHECK(!nvlist_exists_binary(nvl, 110 "nvlist/binary/abcdefghijklmnopqrstuvwxyz")); 111 ptr = malloc(sizeof("abcdefghijklmnopqrstuvwxyz")); 112 CHECK(ptr != NULL); 113 memcpy(ptr, "abcdefghijklmnopqrstuvwxyz", 114 sizeof("abcdefghijklmnopqrstuvwxyz")); 115 nvlist_move_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", 116 ptr, sizeof("abcdefghijklmnopqrstuvwxyz")); 117 CHECK(nvlist_error(nvl) == 0); 118 CHECK(nvlist_exists_binary(nvl, 119 "nvlist/binary/abcdefghijklmnopqrstuvwxyz")); 120 CHECK(ptr == nvlist_get_binary(nvl, 121 "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL)); 122 CHECK(ptr == nvlist_get_binary(nvl, 123 "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size)); 124 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz")); 125 126 CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist")); 127 ptr = nvlist_clone(nvl); 128 CHECK(ptr != NULL); 129 nvlist_move_nvlist(nvl, "nvlist/nvlist", ptr); 130 CHECK(nvlist_error(nvl) == 0); 131 CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist")); 132 CHECK(ptr == nvlist_get_nvlist(nvl, "nvlist/nvlist")); 133 134 CHECK(nvlist_exists_string(nvl, "nvlist/string/")); 135 CHECK(nvlist_exists_string(nvl, "nvlist/string/x")); 136 CHECK(nvlist_exists_string(nvl, 137 "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 138 CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")); 139 CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x")); 140 CHECK(nvlist_exists_binary(nvl, 141 "nvlist/binary/abcdefghijklmnopqrstuvwxyz")); 142 CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist")); 143 144 cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist"); 145 CHECK(nvlist_exists_string(cnvl, "nvlist/string/")); 146 CHECK(nvlist_exists_string(cnvl, "nvlist/string/x")); 147 CHECK(nvlist_exists_string(cnvl, 148 "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 149 CHECK(nvlist_exists_descriptor(cnvl, 150 "nvlist/descriptor/STDERR_FILENO")); 151 CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/x")); 152 CHECK(nvlist_exists_binary(cnvl, 153 "nvlist/binary/abcdefghijklmnopqrstuvwxyz")); 154 155 nvlist_destroy(nvl); 156 157 return (0); 158 } 159