1 /*
2  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include <sepol/policydb/policydb.h>
31 
32 #include "CuTest.h"
33 #include "test_cil_lexer.h"
34 
35 #include "../../src/cil_lexer.h"
36 
test_cil_lexer_setup(CuTest * tc)37 void test_cil_lexer_setup(CuTest *tc) {
38    char *test_str = "(test \"qstring\");comment\n";
39    uint32_t str_size = strlen(test_str);
40    char *buffer = malloc(str_size + 2);
41 
42    memset(buffer+str_size, 0, 2);
43    strncpy(buffer, test_str, str_size);
44 
45    int rc = cil_lexer_setup(buffer, str_size + 2);
46    CuAssertIntEquals(tc, SEPOL_OK, rc);
47 
48    free(buffer);
49 }
50 
test_cil_lexer_next(CuTest * tc)51 void test_cil_lexer_next(CuTest *tc) {
52    char *test_str = "(test \"qstring\") ;comment\n";
53    uint32_t str_size = strlen(test_str);
54    char *buffer = malloc(str_size + 2);
55 
56    memset(buffer+str_size, 0, 2);
57    strcpy(buffer, test_str);
58 
59    cil_lexer_setup(buffer, str_size + 2);
60 
61    struct token test_tok;
62 
63    int rc = cil_lexer_next(&test_tok);
64    CuAssertIntEquals(tc, SEPOL_OK, rc);
65 
66    CuAssertIntEquals(tc, OPAREN, test_tok.type);
67    CuAssertStrEquals(tc, "(", test_tok.value);
68    CuAssertIntEquals(tc, 1, test_tok.line);
69 
70    rc = cil_lexer_next(&test_tok);
71    CuAssertIntEquals(tc, SEPOL_OK, rc);
72 
73    CuAssertIntEquals(tc, SYMBOL, test_tok.type);
74    CuAssertStrEquals(tc, "test", test_tok.value);
75    CuAssertIntEquals(tc, 1, test_tok.line);
76 
77    rc = cil_lexer_next(&test_tok);
78    CuAssertIntEquals(tc, SEPOL_OK, rc);
79 
80    CuAssertIntEquals(tc, QSTRING, test_tok.type);
81    CuAssertStrEquals(tc, "\"qstring\"", test_tok.value);
82    CuAssertIntEquals(tc, 1, test_tok.line);
83 
84    rc = cil_lexer_next(&test_tok);
85    CuAssertIntEquals(tc, SEPOL_OK, rc);
86 
87    CuAssertIntEquals(tc, CPAREN, test_tok.type);
88    CuAssertStrEquals(tc, ")", test_tok.value);
89    CuAssertIntEquals(tc, 1, test_tok.line);
90 
91    rc = cil_lexer_next(&test_tok);
92    CuAssertIntEquals(tc, SEPOL_OK, rc);
93 
94    CuAssertIntEquals(tc, COMMENT, test_tok.type);
95    CuAssertStrEquals(tc, ";comment", test_tok.value);
96    CuAssertIntEquals(tc, 1, test_tok.line);
97 
98    free(buffer);
99 }
100 
101