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