FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_file_operations.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <CUnit/CUnit.h>
11 #include <string_operations.h>
12 
13 #include "file_operations.h"
14 #include "string_operations.h"
15 #include "hash.h"
16 #include "libfocunit.h"
17 
18 void test_read_file_tokens() {
19  char* teststring = "a\n^b\n c";
20  char* testfile = "/tmp/monkftest";
21 
22  FILE* file = fopen(testfile, "w");
23  CU_ASSERT_PTR_NOT_NULL(file);
24  fprintf(file, "%s", teststring);
25  fclose(file);
26 
27  GArray* tokens;
28  CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
29 
30  FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
31  Token token0 = g_array_index(tokens, Token, 0);
32  Token token1 = g_array_index(tokens, Token, 1);
33  Token token2 = g_array_index(tokens, Token, 2);
34  CU_ASSERT_EQUAL(token0.length, 1);
35  CU_ASSERT_EQUAL(token1.length, 1);
36  CU_ASSERT_EQUAL(token2.length, 1);
37  CU_ASSERT_EQUAL(token0.removedBefore, 0);
38  CU_ASSERT_EQUAL(token1.removedBefore, 2);
39  CU_ASSERT_EQUAL(token2.removedBefore, 2);
40  CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
41  CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
42  CU_ASSERT_EQUAL(token2.hashedContent, hash("c"));
43 
44  g_array_free(tokens, TRUE);
45 }
46 
47 void test_read_file_tokens2() {
48  char* teststring = " * a\n *\n * b";
49  char* testfile = "/tmp/monkftest";
50 
51  FILE* file = fopen(testfile, "w");
52  CU_ASSERT_PTR_NOT_NULL(file);
53  fprintf(file, "%s", teststring);
54  fclose(file);
55 
56  GArray* tokens;
57  FO_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
58 
59  FO_ASSERT_EQUAL_FATAL(tokens->len, 2);
60  Token token0 = g_array_index(tokens, Token, 0);
61  Token token1 = g_array_index(tokens, Token, 1);
62  CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
63  CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
64  CU_ASSERT_EQUAL(token0.length, 1);
65  CU_ASSERT_EQUAL(token1.length, 1);
66  CU_ASSERT_EQUAL(token0.removedBefore, 3);
67  CU_ASSERT_EQUAL(token1.removedBefore, 7);
68 
69  g_array_free(tokens, TRUE);
70 }
71 
72 void test_read_file_tokens_error() {
73  GArray* tokens = (GArray*)0x17;
74  CU_ASSERT_FALSE(readTokensFromFile("not a file", &tokens, "\n\t\r^ "));
75  CU_ASSERT_EQUAL((GArray*)0x17, tokens);
76 }
77 
78 void test_read_file_tokens_binaries() {
79  char teststring[] = "a\n^b\0 c";
80  char* testfile = "/tmp/monkftest";
81 
82  FILE* file = fopen(testfile, "w");
83  CU_ASSERT_PTR_NOT_NULL(file);
84  fwrite(teststring, 1, sizeof (teststring), file);
85  fclose(file);
86 
87  GArray* tokens;
88  CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
89 
90  FO_ASSERT_EQUAL_FATAL(tokens->len, 3);
91  Token token0 = g_array_index(tokens, Token, 0);
92  Token token1 = g_array_index(tokens, Token, 1);
93  Token token2 = g_array_index(tokens, Token, 2);
94  CU_ASSERT_EQUAL(token0.length, 1);
95  CU_ASSERT_EQUAL(token1.length, 1);
96  CU_ASSERT_EQUAL(token2.length, 1);
97  CU_ASSERT_EQUAL(token0.removedBefore, 0);
98  CU_ASSERT_EQUAL(token1.removedBefore, 2);
99  CU_ASSERT_EQUAL(token2.removedBefore, 2);
100  CU_ASSERT_EQUAL(token0.hashedContent, hash("a"));
101  CU_ASSERT_EQUAL(token1.hashedContent, hash("b"));
102  CU_ASSERT_EQUAL(token2.hashedContent, hash("c"));
103 
104  g_array_free(tokens, TRUE);
105 }
106 
107 void binaryWrite(const char* testfile, const char* teststring)
108 {
109  FILE* file = fopen(testfile, "w");
110  CU_ASSERT_PTR_NOT_NULL(file);
111  fwrite(teststring, 1, strlen(teststring), file);
112  fclose(file);
113 }
114 
115 void test_read_file_tokens_encodingConversion() {
116  char* testfile = "/tmp/monkftest";
117 
118  GArray* tokens;
119  binaryWrite(testfile, "a\n ß é c");
120  CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens, "\n\t\r^ "));
121 
122  GArray* tokens1;
123  binaryWrite(testfile, "a\n \xdf\x0a \xe9\x0a c");
124  CU_ASSERT_TRUE_FATAL(readTokensFromFile(testfile, &tokens1, "\n\t\r^ "));
125 
126  FO_ASSERT_FATAL(tokens->len > 0);
127 
128  FO_ASSERT_EQUAL_FATAL(tokens->len, tokens1->len);
129 
130  CU_ASSERT_EQUAL(
131  g_array_index(tokens, Token, 0).hashedContent,
132  g_array_index(tokens1, Token, 0).hashedContent
133  );
134  CU_ASSERT_EQUAL(
135  g_array_index(tokens, Token, 1).hashedContent,
136  g_array_index(tokens1, Token, 1).hashedContent
137  );
138  CU_ASSERT_EQUAL(
139  g_array_index(tokens, Token, 2).hashedContent,
140  g_array_index(tokens1, Token, 2).hashedContent
141  );
142 
143  g_array_free(tokens, TRUE);
144  g_array_free(tokens1, TRUE);
145 
146 }
147 
148 CU_TestInfo file_operations_testcases[] = {
149  {"Testing reading file tokens:", test_read_file_tokens},
150  {"Testing reading file tokens2:", test_read_file_tokens2},
151  {"Testing reading file tokens with a binary file:", test_read_file_tokens_binaries},
152  {"Testing reading file tokens with two different encodings return same token contents:", test_read_file_tokens_encodingConversion},
153  {"Testing reading file tokens from wrong file:", test_read_file_tokens_error},
154  CU_TEST_INFO_NULL
155 };