diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libc/uuid/uuidtest.c | 142 |
1 files changed, 137 insertions, 5 deletions
diff --git a/src/regress/lib/libc/uuid/uuidtest.c b/src/regress/lib/libc/uuid/uuidtest.c index 9dc838f2ba..9f6fd0175d 100644 --- a/src/regress/lib/libc/uuid/uuidtest.c +++ b/src/regress/lib/libc/uuid/uuidtest.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* $OpenBSD: uuidtest.c,v 1.1 2021/08/31 09:57:27 jasper Exp $ */ | 1 | /* $OpenBSD: uuidtest.c,v 1.2 2023/07/03 13:51:55 jasper Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2021 Jasper Lievisse Adriaanse <jasper@openbsd.org> | 3 | * Copyright (c) 2021, 2023 Jasper Lievisse Adriaanse <jasper@openbsd.org> |
| 4 | * | 4 | * |
| 5 | * Permission to use, copy, modify, and distribute this software for any | 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above | 6 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -26,13 +26,14 @@ | |||
| 26 | int | 26 | int |
| 27 | main(int argc, char **argv) | 27 | main(int argc, char **argv) |
| 28 | { | 28 | { |
| 29 | struct uuid uuid, uuid_want; | 29 | struct uuid uuid, uuid2, uuid_want; |
| 30 | char *uuid_str, *uuid_str_want; | 30 | char *uuid_str, *uuid_str_want; |
| 31 | uint32_t status; | 31 | uint32_t status; |
| 32 | int t = 1; | 32 | unsigned char bin[16]; |
| 33 | int rc, t = 1; | ||
| 33 | 34 | ||
| 34 | /* Test invalid input to uuid_from_string() */ | 35 | /* Test invalid input to uuid_from_string() */ |
| 35 | printf("[%d] uuid_from_string ", t); | 36 | printf("[%d] uuid_from_string (invalid) ", t); |
| 36 | uuid_str = "6fc3134d-011d-463d-a6b4-fe1f3a5e57dX"; | 37 | uuid_str = "6fc3134d-011d-463d-a6b4-fe1f3a5e57dX"; |
| 37 | uuid_from_string(uuid_str, &uuid, &status); | 38 | uuid_from_string(uuid_str, &uuid, &status); |
| 38 | if (status != uuid_s_invalid_string_uuid) { | 39 | if (status != uuid_s_invalid_string_uuid) { |
| @@ -44,6 +45,19 @@ main(int argc, char **argv) | |||
| 44 | printf("ok\n"); | 45 | printf("ok\n"); |
| 45 | t++; | 46 | t++; |
| 46 | 47 | ||
| 48 | /* Test a bad version gets recognized */ | ||
| 49 | printf("[%d] uuid_from_string (bad version) ", t); | ||
| 50 | uuid_str = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
| 51 | uuid_from_string(uuid_str, &uuid, &status); | ||
| 52 | if (status != uuid_s_bad_version) { | ||
| 53 | printf("failed to return uuid_s_bad_version for '%s'\n", | ||
| 54 | uuid_str); | ||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | printf("ok\n"); | ||
| 59 | t++; | ||
| 60 | |||
| 47 | /* Test valid input to uuid_from_string() */ | 61 | /* Test valid input to uuid_from_string() */ |
| 48 | printf("[%d] uuid_from_string ", t); | 62 | printf("[%d] uuid_from_string ", t); |
| 49 | uuid_str = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"; | 63 | uuid_str = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"; |
| @@ -65,6 +79,7 @@ main(int argc, char **argv) | |||
| 65 | printf("failed to return uuid_s_ok for '%s', got %d\n", uuid_str, status); | 79 | printf("failed to return uuid_s_ok for '%s', got %d\n", uuid_str, status); |
| 66 | return 1; | 80 | return 1; |
| 67 | } | 81 | } |
| 82 | |||
| 68 | ASSERT_EQ(uuid.time_low, uuid_want.time_low); | 83 | ASSERT_EQ(uuid.time_low, uuid_want.time_low); |
| 69 | ASSERT_EQ(uuid.time_mid, uuid_want.time_mid); | 84 | ASSERT_EQ(uuid.time_mid, uuid_want.time_mid); |
| 70 | ASSERT_EQ(uuid.time_hi_and_version, uuid_want.time_hi_and_version); | 85 | ASSERT_EQ(uuid.time_hi_and_version, uuid_want.time_hi_and_version); |
| @@ -125,5 +140,122 @@ main(int argc, char **argv) | |||
| 125 | printf("ok\n"); | 140 | printf("ok\n"); |
| 126 | t++; | 141 | t++; |
| 127 | 142 | ||
| 143 | /* | ||
| 144 | * Assuming the clock of the system running the test is ahead of the one | ||
| 145 | * where this test was written, we can test uuid_create along with | ||
| 146 | * uuid_compare here. | ||
| 147 | */ | ||
| 148 | printf("[%d] uuid_create ", t); | ||
| 149 | uuid_create(&uuid, &status); | ||
| 150 | if (status != uuid_s_ok) { | ||
| 151 | printf("uuid_create failed to return uuid_s_ok, got %d\n", | ||
| 152 | status); | ||
| 153 | return 1; | ||
| 154 | } | ||
| 155 | |||
| 156 | printf("ok\n"); | ||
| 157 | t++; | ||
| 158 | |||
| 159 | printf("[%d] uuid_compare ", t); | ||
| 160 | /* uuid was just generated, uuid2 was generated before. */ | ||
| 161 | uuid_from_string(uuid_str, &uuid2, &status); | ||
| 162 | rc = uuid_compare(&uuid, &uuid2, &status); | ||
| 163 | if ((status != uuid_s_ok) || (rc != 1)) { | ||
| 164 | printf("uuid_compare failed, expected 1 got: %d and status: %d\n", | ||
| 165 | rc, status); | ||
| 166 | return 1; | ||
| 167 | } | ||
| 168 | |||
| 169 | printf("ok\n"); | ||
| 170 | t++; | ||
| 171 | |||
| 172 | printf("[%d] uuid_equal ", t); | ||
| 173 | rc = uuid_equal(&uuid, &uuid, &status); | ||
| 174 | if ((status != uuid_s_ok) || (rc != 1)) { | ||
| 175 | printf("uuid_compare failed, expected 1 got: %d and status: %d\n", | ||
| 176 | rc, status); | ||
| 177 | return 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | printf("ok\n"); | ||
| 181 | t++; | ||
| 182 | |||
| 183 | printf("[%d] uuid_equal (nil) ", t); | ||
| 184 | uuid_create_nil(&uuid, &status); | ||
| 185 | rc = uuid_equal(&uuid, &uuid2, &status); | ||
| 186 | if ((status != uuid_s_ok) || (rc != 1)) { | ||
| 187 | printf("uuid_compare failed, expected 1 got: %d and status: %d\n", | ||
| 188 | rc, status); | ||
| 189 | return 1; | ||
| 190 | } | ||
| 191 | |||
| 192 | printf("ok\n"); | ||
| 193 | t++; | ||
| 194 | |||
| 195 | printf("[%d] uuid_hash ", t); | ||
| 196 | uint16_t hash = uuid_hash(&uuid_want, &status); | ||
| 197 | if ((status != uuid_s_ok) || (hash != 0x4fae)) { | ||
| 198 | printf("uuid_hash failed, expected 0x4fae got: 0x%04x and status: %d\n", | ||
| 199 | hash, status); | ||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | printf("ok\n"); | ||
| 204 | t++; | ||
| 205 | |||
| 206 | uuid_str_want = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"; | ||
| 207 | printf("[%d] uuid_enc_le ", t); | ||
| 208 | uuid_from_string(uuid_str_want, &uuid, &status); | ||
| 209 | /* | ||
| 210 | * Check two fields to ensure they're in the right order. | ||
| 211 | * If these two are ok, it's safe to assum the rest are too. | ||
| 212 | */ | ||
| 213 | uuid_enc_le(bin, &uuid); | ||
| 214 | if (bin[4] != 0xec || bin[5] != 0x7d) { | ||
| 215 | uuid_to_string(&uuid, &uuid_str, &status); | ||
| 216 | printf("uuid_enc_le failed, expected %s got %s\n", | ||
| 217 | uuid_str_want, uuid_str); | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | printf("ok\n"); | ||
| 222 | t++; | ||
| 223 | |||
| 224 | printf("[%d] uuid_dec_le ", t); | ||
| 225 | uuid_dec_le(bin, &uuid); | ||
| 226 | if (uuid_equal(&uuid, &uuid_want, &status) == 0) { | ||
| 227 | uuid_to_string(&uuid, &uuid_str, &status); | ||
| 228 | printf("uuid_dec_le failed, expected %s got %s\n", | ||
| 229 | uuid_str_want, uuid_str); | ||
| 230 | return 1; | ||
| 231 | } | ||
| 232 | |||
| 233 | printf("ok\n"); | ||
| 234 | t++; | ||
| 235 | |||
| 236 | printf("[%d] uuid_enc_be ", t); | ||
| 237 | uuid_enc_be(bin, &uuid); | ||
| 238 | if (bin[4] != 0x7d || bin[5] != 0xec) { | ||
| 239 | uuid_to_string(&uuid, &uuid_str, &status); | ||
| 240 | printf("uuid_enc_be failed, expected %s got %s\n", | ||
| 241 | uuid_str_want, uuid_str); | ||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | printf("ok\n"); | ||
| 246 | t++; | ||
| 247 | |||
| 248 | printf("[%d] uuid_dec_be ", t); | ||
| 249 | uuid_dec_be(bin, &uuid); | ||
| 250 | if (uuid_equal(&uuid, &uuid_want, &status) == 0) { | ||
| 251 | uuid_to_string(&uuid, &uuid_str, &status); | ||
| 252 | printf("uuid_dec_be failed, expected %s got %s\n", | ||
| 253 | uuid_str_want, uuid_str); | ||
| 254 | return 1; | ||
| 255 | } | ||
| 256 | |||
| 257 | printf("ok\n"); | ||
| 258 | t++; | ||
| 259 | |||
| 128 | return 0; | 260 | return 0; |
| 129 | } | 261 | } |
