Motr  M0
uuid.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "lib/string.h"
24 #include "lib/uuid.h"
25 #include "lib/errno.h" /* ENOENT */
26 #include "ut/ut.h"
27 
28 static char *nil_uuid = "00000000-0000-0000-0000-000000000000"; /* nil UUID */
29 static char *uuid1 = "abcdef01-2345-6789-abcd-ef0123456789"; /* lc */
30 static char *uuid2 = "98765432-10AB-CDEF-FEDC-BA0123456789"; /* uc */
31 static char *uuid3 = "9876543210ABCDEFFEDCBA0123456789"; /* uc */
32 static char *bad1 = "bad1";
33 static char *bad_uuids_len_ok[] = { /* len ok in all cases */
34  "abcdef0101-2345-6789-abcd-0123456789", /* field lengths wrong */
35  "0123456-1234-12345-1234-123456789abc", /* field lengths wrong */
36  "X176543-10ab-0Xdef-fedc-0xa123456789", /* invalid chr @front */
37  "9876543M-10ab-cdef-fedc-ba0123456789", /* invalid chr in str */
38  "987650x4-10ab-cdef-fedc-ba0123456789", /* 0x in str */
39  "0x765432-10ab-0Xde-fedc-0xa123456789", /* 0x and 0X @front */
40 };
41 static char *bad_uuids_short[] = { /* short in one field */
42  "abcdef1-2345-6789-abcd-ef0123456789",
43  "abcdef01-234-6789-abcd-ef0123456789",
44  "abcdef01-2345-678-abcd-ef0123456789",
45  "abcdef01-2345-6789-abc-ef0123456789",
46  "abcdef01-2345-6789-abcd-ef012345678",
47 };
48 static char *bad_uuids_long[] = { /* long in one field */
49  "abcdef012-2345-6789-abcd-ef0123456789",
50  "abcdef01-23456-6789-abcd-ef0123456789",
51  "abcdef01-2345-6789a-abcd-ef0123456789",
52  "abcdef01-2345-6789-abcde-ef0123456789",
53  "abcdef01-2345-6789-abcd-ef0123456789a",
54 };
55 
56 static bool test_identity_op(const char *str)
57 {
58  struct m0_uint128 u1;
59  struct m0_uint128 u2;
60  char buf[M0_UUID_STRLEN+1];
61  int rc;
62 
63  rc = m0_uuid_parse(str, &u1);
64  if (rc != 0)
65  return false;
67  rc = m0_uuid_parse(buf, &u2);
68  if (rc != 0)
69  return false;
70  return (u1.u_hi == u2.u_hi) && (u1.u_lo == u2.u_lo);
71 }
72 
73 struct m0_uint128 uuid[1000];
74 void m0_test_lib_uuid(void)
75 {
76  struct m0_uint128 u;
77  int rc;
78  int i;
79  int j;
80 
82  M0_UT_ASSERT(rc == 0);
83  M0_UT_ASSERT(u.u_hi == 0);
84  M0_UT_ASSERT(u.u_lo == 0);
86 
87  rc = m0_uuid_parse(uuid1, &u);
88  M0_UT_ASSERT(rc == 0);
89  M0_UT_ASSERT(u.u_hi == 0xabcdef0123456789);
90  M0_UT_ASSERT(u.u_lo == 0xabcdef0123456789);
92 
93  rc = m0_uuid_parse(uuid2, &u);
94  M0_UT_ASSERT(rc == 0);
95  M0_UT_ASSERT(u.u_hi == 0x9876543210abcdef);
96  M0_UT_ASSERT(u.u_lo == 0xfedcba0123456789);
98 
99  rc = m0_uuid_parse(uuid3, &u);
100  M0_UT_ASSERT(rc == 0);
101  M0_UT_ASSERT(u.u_hi == 0x9876543210abcdef);
102  M0_UT_ASSERT(u.u_lo == 0xfedcba0123456789);
104 
105  rc = m0_uuid_parse(bad1, &u);
106  M0_UT_ASSERT(rc == -EINVAL);
107 
108  for (i = 0; i < ARRAY_SIZE(bad_uuids_len_ok); ++i) {
111  M0_UT_ASSERT(rc == -EINVAL);
112  }
113 
114  for (i = 0; i < ARRAY_SIZE(bad_uuids_short); ++i) {
117  M0_UT_ASSERT(rc == -EINVAL);
118  }
119 
120  for (i = 0; i < ARRAY_SIZE(bad_uuids_long); ++i) {
123  M0_UT_ASSERT(rc == -EINVAL);
124  }
125 
126  for (i = 0; i < ARRAY_SIZE(uuid); ++i)
128  for (i = 0; i < ARRAY_SIZE(uuid); ++i)
129  for (j = i + 1; j < ARRAY_SIZE(uuid); ++j)
130  M0_UT_ASSERT(m0_uint128_cmp(&uuid[i], &uuid[j]) != 0);
131 }
132 M0_EXPORTED(m0_test_lib_uuid);
133 
134 /*
135  * Local variables:
136  * c-indentation-style: "K&R"
137  * c-basic-offset: 8
138  * tab-width: 8
139  * fill-column: 80
140  * scroll-step: 1
141  * End:
142  */
M0_INTERNAL int m0_uint128_cmp(const struct m0_uint128 *u0, const struct m0_uint128 *u1)
Definition: misc.c:45
struct m0_uint128 uuid[1000]
Definition: uuid.c:73
static char * uuid1
Definition: uuid.c:29
M0_INTERNAL void m0_uuid_generate(struct m0_uint128 *u)
Definition: uuid.c:44
static char * uuid3
Definition: uuid.c:31
union @126 u
Definition: sock.c:887
static char * bad_uuids_len_ok[]
Definition: uuid.c:33
M0_INTERNAL int m0_uuid_parse(const char *str, struct m0_uint128 *val)
Definition: uuid.c:77
int i
Definition: dir.c:1033
static char * bad_uuids_short[]
Definition: uuid.c:41
static char * nil_uuid
Definition: uuid.c:28
uint64_t u_hi
Definition: types.h:36
M0_INTERNAL void m0_uuid_format(const struct m0_uint128 *val, char *buf, size_t len)
Definition: uuid.c:107
static char * uuid2
Definition: uuid.c:30
void m0_test_lib_uuid(void)
Definition: uuid.c:74
static char * bad1
Definition: uuid.c:32
uint64_t u_lo
Definition: types.h:37
static bool test_identity_op(const char *str)
Definition: uuid.c:56
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static char * bad_uuids_long[]
Definition: uuid.c:48