1 /*- 2 * Copyright (c) 2013 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Pawel Jakub Dawidek under sponsorship from 6 * the FreeBSD Foundation. 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 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include <sys/nv.h> 30 31 #include <errno.h> 32 #include <fcntl.h> 33 #include <stdio.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 #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF) 48 49 int 50 main(void) 51 { 52 const nvlist_t *cnvl; 53 nvlist_t *nvl; 54 size_t size; 55 56 printf("1..83\n"); 57 58 nvl = nvlist_create(0); 59 60 CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true")); 61 nvlist_add_bool(nvl, "nvlist/bool/true", true); 62 CHECK(nvlist_error(nvl) == 0); 63 CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true); 64 65 CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false")); 66 nvlist_add_bool(nvl, "nvlist/bool/false", false); 67 CHECK(nvlist_error(nvl) == 0); 68 CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false); 69 70 CHECK(!nvlist_exists_number(nvl, "nvlist/number/0")); 71 nvlist_add_number(nvl, "nvlist/number/0", 0); 72 CHECK(nvlist_error(nvl) == 0); 73 CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0); 74 75 CHECK(!nvlist_exists_number(nvl, "nvlist/number/1")); 76 nvlist_add_number(nvl, "nvlist/number/1", 1); 77 CHECK(nvlist_error(nvl) == 0); 78 CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1); 79 80 CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1")); 81 nvlist_add_number(nvl, "nvlist/number/-1", -1); 82 CHECK(nvlist_error(nvl) == 0); 83 CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1); 84 85 CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX")); 86 nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX); 87 CHECK(nvlist_error(nvl) == 0); 88 CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX); 89 90 CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN")); 91 nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN); 92 CHECK(nvlist_error(nvl) == 0); 93 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN); 94 95 CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX")); 96 nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX); 97 CHECK(nvlist_error(nvl) == 0); 98 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX); 99 100 CHECK(!nvlist_exists_string(nvl, "nvlist/string/")); 101 nvlist_add_string(nvl, "nvlist/string/", ""); 102 CHECK(nvlist_error(nvl) == 0); 103 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0); 104 105 CHECK(!nvlist_exists_string(nvl, "nvlist/string/x")); 106 nvlist_add_string(nvl, "nvlist/string/x", "x"); 107 CHECK(nvlist_error(nvl) == 0); 108 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0); 109 110 CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz")); 111 nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"); 112 CHECK(nvlist_error(nvl) == 0); 113 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0); 114 115 CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")); 116 nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO); 117 CHECK(nvlist_error(nvl) == 0); 118 CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"))); 119 120 CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x")); 121 nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1); 122 CHECK(nvlist_error(nvl) == 0); 123 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0); 124 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0); 125 CHECK(size == 1); 126 127 CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz")); 128 nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")); 129 CHECK(nvlist_error(nvl) == 0); 130 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 131 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 132 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz")); 133 134 CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist")); 135 nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl); 136 CHECK(nvlist_error(nvl) == 0); 137 cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist"); 138 CHECK(nvlist_get_bool(cnvl, "nvlist/bool/true") == true); 139 CHECK(nvlist_get_bool(cnvl, "nvlist/bool/false") == false); 140 CHECK(nvlist_get_number(cnvl, "nvlist/number/0") == 0); 141 CHECK(nvlist_get_number(cnvl, "nvlist/number/1") == 1); 142 CHECK((int)nvlist_get_number(cnvl, "nvlist/number/-1") == -1); 143 CHECK(nvlist_get_number(cnvl, "nvlist/number/UINT64_MAX") == UINT64_MAX); 144 CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MIN") == INT64_MIN); 145 CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MAX") == INT64_MAX); 146 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/"), "") == 0); 147 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/x"), "x") == 0); 148 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0); 149 /* TODO */ 150 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", NULL), "x", 1) == 0); 151 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", &size), "x", 1) == 0); 152 CHECK(size == 1); 153 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 154 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 155 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz")); 156 157 CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true); 158 CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false); 159 CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0); 160 CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1); 161 CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1); 162 CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX); 163 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN); 164 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX); 165 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0); 166 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0); 167 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0); 168 CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"))); 169 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0); 170 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0); 171 CHECK(size == 1); 172 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 173 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0); 174 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz")); 175 176 nvlist_destroy(nvl); 177 178 return (0); 179 } 180