1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <CUnit/CUnit.h>
22 #include <string_operations.h>
23 
24 #include "file_operations.h"
25 #include "string_operations.h"
26 #include "hash.h"
27 #include "libfocunit.h"
28 
test_read_file_tokens()29 void test_read_file_tokens() {
30   char* teststring = "a\n^b\n c";
31   char* testfile = "/tmp/monkftest";
32 
33   FILE* file = fopen(testfile, "w");
34   CU_ASSERT_PTR_NOT_NULL(file);
35   fprintf(file, "%s", teststring);
36   fclose(file);
37 
38   GArray* tokens;
39   CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
40 
41   FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
42   Token token0 = g_array_index(tokens, Token, 0);
43   Token token1 = g_array_index(tokens, Token, 1);
44   Token token2 = g_array_index(tokens, Token, 2);
45   CU_ASSERT_EQUAL(token0.length, 1);
46   CU_ASSERT_EQUAL(token1.length, 1);
47   CU_ASSERT_EQUAL(token2.length, 1);
48   CU_ASSERT_EQUAL(token0.removedBefore, 0);
49   CU_ASSERT_EQUAL(token1.removedBefore, 2);
50   CU_ASSERT_EQUAL(token2.removedBefore, 2);
51   CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
52   CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
53   CU_ASSERT_EQUAL(token2.hashedContent, hash("c"));
54 
55   g_array_free(tokens, TRUE);
56 }
57 
test_read_file_tokens2()58 void test_read_file_tokens2() {
59   char* teststring = " * a\n *\n * b";
60   char* testfile = "/tmp/monkftest";
61 
62   FILE* file = fopen(testfile, "w");
63   CU_ASSERT_PTR_NOT_NULL(file);
64   fprintf(file, "%s", teststring);
65   fclose(file);
66 
67   GArray* tokens;
68   FO_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
69 
70   FO_ASSERT_EQUAL_FATAL(tokens->len, 2);
71   Token token0 = g_array_index(tokens, Token, 0);
72   Token token1 = g_array_index(tokens, Token, 1);
73   CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
74   CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
75   CU_ASSERT_EQUAL(token0.length, 1);
76   CU_ASSERT_EQUAL(token1.length, 1);
77   CU_ASSERT_EQUAL(token0.removedBefore, 3);
78   CU_ASSERT_EQUAL(token1.removedBefore, 7);
79 
80   g_array_free(tokens, TRUE);
81 }
82 
test_read_file_tokens_error()83 void test_read_file_tokens_error() {
84   GArray* tokens = (GArray*)0x17;
85   CU_ASSERT_FALSE(readTokensFromFile("not a file", &tokens, "\n\t\r^ "));
86   CU_ASSERT_EQUAL((GArray*)0x17, tokens);
87 }
88 
test_read_file_tokens_binaries()89 void test_read_file_tokens_binaries() {
90   char teststring[] = "a\n^b\0 c";
91   char* testfile = "/tmp/monkftest";
92 
93   FILE* file = fopen(testfile, "w");
94   CU_ASSERT_PTR_NOT_NULL(file);
95   fwrite(teststring, 1, sizeof (teststring), file);
96   fclose(file);
97 
98   GArray* tokens;
99   CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
100 
101   FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
102   Token token0 = g_array_index(tokens, Token, 0);
103   Token token1 = g_array_index(tokens, Token, 1);
104   Token token2 = g_array_index(tokens, Token, 2);
105   CU_ASSERT_EQUAL(token0.length, 1);
106   CU_ASSERT_EQUAL(token1.length, 1);
107   CU_ASSERT_EQUAL(token2.length, 1);
108   CU_ASSERT_EQUAL(token0.removedBefore, 0);
109   CU_ASSERT_EQUAL(token1.removedBefore, 2);
110   CU_ASSERT_EQUAL(token2.removedBefore, 2);
111   CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
112   CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
113   CU_ASSERT_EQUAL(token2.hashedContent, hash("c"));
114 
115   g_array_free(tokens, TRUE);
116 }
117 
binaryWrite(const char * testfile,const char * teststring)118 void binaryWrite(const char* testfile, const char* teststring)
119 {
120   FILE* file = fopen(testfile, "w");
121   CU_ASSERT_PTR_NOT_NULL(file);
122   fwrite(teststring, 1, strlen(teststring), file);
123   fclose(file);
124 }
125 
test_read_file_tokens_encodingConversion()126 void test_read_file_tokens_encodingConversion() {
127   char* testfile = "/tmp/monkftest";
128 
129   GArray* tokens;
130   binaryWrite(testfile, "a\n ß é c");
131   CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
132 
133   GArray* tokens1;
134   binaryWrite(testfile, "a\n \xdf\x0a \xe9\x0a c");
135   CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens1, "\n\t\r^ "));
136 
137   FO_ASSERT_FATAL(tokens->len > 0);
138 
139   FO_ASSERT_EQUAL_FATAL(tokens->len, tokens1->len);
140 
141   CU_ASSERT_EQUAL(
142     g_array_index(tokens, Token, 0).hashedContent,
143     g_array_index(tokens1, Token, 0).hashedContent
144   );
145   CU_ASSERT_EQUAL(
146     g_array_index(tokens, Token, 1).hashedContent,
147     g_array_index(tokens1, Token, 1).hashedContent
148   );
149   CU_ASSERT_EQUAL(
150     g_array_index(tokens, Token, 2).hashedContent,
151     g_array_index(tokens1, Token, 2).hashedContent
152   );
153 
154   g_array_free(tokens, TRUE);
155   g_array_free(tokens1, TRUE);
156 
157 }
158 
159 CU_TestInfo file_operations_testcases[] = {
160   {"Testing reading file tokens:", test_read_file_tokens},
161   {"Testing reading file tokens2:", test_read_file_tokens2},
162   {"Testing reading file tokens with a binary file:", test_read_file_tokens_binaries},
163   {"Testing reading file tokens with two different encodings return same token contents:", test_read_file_tokens_encodingConversion},
164   {"Testing reading file tokens from wrong file:", test_read_file_tokens_error},
165   CU_TEST_INFO_NULL
166 };
167