Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / ocrandom / test / linux / randomtest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22
23 extern "C" {
24     #include "ocrandom.h"
25 }
26
27 #include "gtest/gtest.h"
28 #include "math.h"
29
30 #define ARR_SIZE (20)
31
32 int main(int argc, char* argv[]) {
33     testing::InitGoogleTest(&argc, argv);
34     return RUN_ALL_TESTS();
35 }
36
37 TEST(RandomGeneration,OCSeedRandom) {
38     EXPECT_EQ((uint32_t )0, OCSeedRandom());
39 }
40
41 TEST(RandomGeneration,OCGetRandomByte) {
42     uint8_t value = OCGetRandomByte();
43     EXPECT_LE((uint8_t )0, value);
44     EXPECT_GT(pow(2, 8), value);
45 }
46
47 TEST(RandomGeneration,OCGetRandom) {
48     uint32_t value = OCGetRandom();
49     EXPECT_LE((uint8_t )0, value);
50     EXPECT_GT(pow(2, 32), value);
51 }
52
53 TEST(RandomGeneration,OCFillRandomMem) {
54     uint8_t array[ARR_SIZE];
55     memset(array, 0, ARR_SIZE);
56     OCFillRandomMem(array + 1, ARR_SIZE - 2);
57
58     for (int i = 1; i <= ARR_SIZE - 2; i++) {
59         uint8_t value = array[i];
60         EXPECT_LE((uint8_t )0, value);
61         EXPECT_GT(pow(2, 8), value);
62     }
63     EXPECT_EQ((uint8_t )0, array[0]);
64     EXPECT_EQ((uint8_t )0, array[ARR_SIZE - 1]);
65 }
66
67 TEST(RandomGeneration, OCGenerateUuid)
68 {
69     EXPECT_EQ(RAND_UUID_INVALID_PARAM, OCGenerateUuid(NULL));
70
71     uint8_t uuid[16] = {};
72
73     EXPECT_EQ(RAND_UUID_OK, OCGenerateUuid(uuid));
74
75     EXPECT_FALSE(uuid[0] == '0' && uuid[1] == '0' &&
76                  uuid[2] == '0' && uuid[3] == '0' &&
77                  uuid[4] == '0' && uuid[5] == '0' &&
78                  uuid[6] == '0' && uuid[7] == '0' &&
79                  uuid[8] == '0' && uuid[9] == '0' &&
80                  uuid[10] == '0' && uuid[11] == '0' &&
81                  uuid[12] == '0' && uuid[13] == '0' &&
82                  uuid[14] == '0' && uuid[15] == '0');
83 }
84
85 TEST(RandomGeneration, OCGenerateUuidString)
86 {
87     EXPECT_EQ(RAND_UUID_INVALID_PARAM, OCGenerateUuidString(NULL));
88
89     char uuidString[37] = {};
90
91     EXPECT_EQ(RAND_UUID_OK, OCGenerateUuidString(uuidString));
92     EXPECT_EQ('\0', uuidString[36]);
93     EXPECT_EQ('-', uuidString[8]);
94     EXPECT_EQ('-', uuidString[13]);
95     EXPECT_EQ('-', uuidString[18]);
96     EXPECT_EQ('-', uuidString[23]);
97
98     for(int i = 0; i < 36; ++i)
99     {
100         EXPECT_TRUE(
101                 i == 8 || i == 13 || i == 18 || i == 23 ||
102                 (uuidString[i] >= 'a' && uuidString[i] <= 'f') ||
103                 (uuidString[i] >= '0' && uuidString[i] <= '9'))
104                 << "UUID Character out of range: "<< uuidString[i];
105     }
106 }