Change test new_label_from_socket
[platform/core/test/security-tests.git] / tests / libsmack-tests / test_cases.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 /*
18  * @file        test_cases.cpp
19  * @author      Pawel Polawski (p.polawski@samsung.com)
20  * @author      Jan Olszak (j.olszak@samsung.com)
21  * @version     1.0
22  * @brief       libprivilege test runer
23  */
24
25 #include <string>
26 #include <sstream>
27 #include <fcntl.h>
28 #include <dpl/test/test_runner.h>
29 #include <dpl/log/log.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33 #include <sys/smack.h>
34 #include <sys/xattr.h>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <sys/wait.h>
38
39 #define TEST_SUBJECT "test_subject"
40 #define TEST_OBJECT "test_oject"
41 #define TEST_OBJECT_2 "test_oject_2"
42
43 #define SOCK_PATH "/tmp/test-smack-socket"
44
45 std::vector<std::string> accessesBasic = { "r", "w", "x", "wx", "rx", "rw", "rwx", "rwxat" };
46
47
48
49
50 int files_compare(int fd1, int fd2)
51 {
52     int result = 0;
53
54     //for getting files sizes
55     struct stat fs1, fs2;
56
57     //handlers for mmap()
58     void * h1 = MAP_FAILED;
59     void * h2 = MAP_FAILED;
60
61     //getting files information
62     if(fstat(fd1, &fs1) == -1) {
63         perror("fstat");
64         return -1;
65     }
66     if(fstat(fd2, &fs2) == -1) {
67         perror("fstat");
68         return -1;
69     }
70
71     if(fs1.st_size != fs2.st_size)  //if files are identical size will be the same
72         return -1;
73
74     //mapping files to process memory
75     if((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) == MAP_FAILED) {
76         result = -1;
77         goto end;
78     }
79     if((h2 = mmap(0, fs2.st_size, PROT_READ, MAP_SHARED, fd2, 0 )) == MAP_FAILED) {
80         result = -1;
81         goto end;
82     }
83
84     result = memcmp(h1, h2, fs1.st_size);
85
86     //cleaning after mmap()
87 end:
88     if(h2 != MAP_FAILED)
89         munmap(h2, fs2.st_size);
90     if(h1 != MAP_FAILED)
91         munmap(h1, fs1.st_size);
92
93     return result;
94 }
95
96
97 RUNNER_TEST_GROUP_INIT(libsmack)
98 /**
99  * Helper method to reset privileges at the begginning of tests.
100  */
101 void clean_up(){
102     struct smack_accesses * rules = NULL;
103     int result = smack_accesses_new(&rules);
104     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
105
106     // CLEAN UP
107     smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","rwxat");
108     smack_accesses_apply(rules);
109     smack_accesses_free(rules);
110
111     // PREINIT CHECK.
112     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") != 1, "Rule has previous privileges after cleaning up!");
113     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"w") != 1, "Rule has previous privileges after cleaning up!");
114     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"x") != 1, "Rule has previous privileges after cleaning up!");
115     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"a") != 1, "Rule has previous privileges after cleaning up!");
116     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"t") != 1, "Rule has previous privileges after cleaning up!");
117 }
118
119 /**
120  * Checking if subject has any access to object
121  */
122 bool checkNoAccesses(const char *subject, const char *object){
123     int result;
124     result = smack_have_access(subject, object,"r");
125     if(result==1){
126         return false;
127     }
128     result = smack_have_access(subject, object,"w");
129     if(result==1){
130         return false;
131     }
132     result = smack_have_access(subject, object,"x");
133     if(result==1){
134         return false;
135     }
136     result = smack_have_access(subject, object,"a");
137     if(result==1){
138         return false;
139     }
140     result = smack_have_access(subject, object,"t");
141     if(result==1){
142         return false;
143     }
144     return true;
145 }
146
147 int removeAccessesAll()
148 {
149     struct smack_accesses * rules = NULL;
150     int result = smack_accesses_new(&rules);
151     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
152
153     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_01", "", "rxwat");
154     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_02", "", "rxwat");
155     result = smack_accesses_add_modify(rules, "test_subject_01", "test_object_03", "", "rxwat");
156     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_01", "", "rxwat");
157     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_02", "", "rxwat");
158     result = smack_accesses_add_modify(rules, "test_subject_02", "test_object_03", "", "rxwat");
159     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_01", "", "rxwat");
160     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_02", "", "rxwat");
161     result = smack_accesses_add_modify(rules, "test_subject_03", "test_object_03", "", "rxwat");
162
163     smack_accesses_apply(rules);
164     RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
165     smack_accesses_free(rules);
166 }
167
168
169 /**
170  * Add a new access with smack_accesses_add_modify()
171  */
172 RUNNER_TEST(smack_accesses_add_modify_test_1){
173     int result;
174
175     clean_up();
176
177     struct smack_accesses * rules = NULL;
178     result = smack_accesses_new(&rules);
179
180     // THE TEST
181     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"xr","");
182     RUNNER_ASSERT_MSG(result == 0, "Unable to add modify by empty rules");
183     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
184
185     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"xr");
186     RUNNER_ASSERT_MSG(result == 1, "Rule modified (added 'xr'), but no change made.");
187
188     // CLEAN UP
189     clean_up();
190     smack_accesses_free(rules);
191 }
192
193
194 /**
195  * Test if rules are applied in the right order, and modification works.
196  */
197 RUNNER_TEST(smack_accesses_add_modify_test_2){
198     int result;
199     struct smack_accesses * rules = NULL;
200     result = smack_accesses_new(&rules);
201     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
202
203     clean_up();
204
205     // THE TEST
206     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"r","");
207     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
208
209     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
210     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
211
212     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
213     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") == 0, "Modification didn't work");
214
215     // CLEAN UP
216     clean_up();
217     smack_accesses_free(rules);
218 }
219
220
221 /**
222  * Test if rules are applied in the right order, and modification works.
223  * Using different smack_accesses list to add and delete.
224  */
225 RUNNER_TEST(smack_accesses_add_modify_test_3){
226     int result;
227     struct smack_accesses * rules = NULL;
228     result = smack_accesses_new(&rules);
229     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
230
231     clean_up();
232
233     // THE TEST
234     // Add r privilage
235     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"r","");
236     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
237     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
238     RUNNER_ASSERT_MSG(smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r") == 1, "Adding privileges didn't work");
239     smack_accesses_free(rules);
240
241     // Revoke r privilege
242     result = smack_accesses_new(&rules);
243     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
244     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
245     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
246     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
247
248     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
249     RUNNER_ASSERT_MSG(result == 0, "Modification didn't work, rule has still 'r' privileges.");
250
251     // CLEAN UP
252     clean_up();
253     smack_accesses_free(rules);
254 }
255
256 /**
257  * Add a list of privileges and then revoke just ONE of them.
258  */
259 RUNNER_TEST(smack_accesses_add_modify_test_4){
260     int result;
261     struct smack_accesses * rules = NULL;
262     result = smack_accesses_new(&rules);
263     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
264
265     clean_up();
266
267     // THE TEST
268     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwxat","");
269     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
270     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
271
272     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
273     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
274     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
275
276     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"awxt");
277     RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule should have 'awxt' privileges.");
278     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
279     RUNNER_ASSERT_MSG(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
280
281     // CLEAN UP
282     clean_up();
283     smack_accesses_free(rules);
284 }
285
286 /**
287  * Add a list of privileges and then revoke just ONE of them.
288  * Without applying privileges in between those actions.
289  */
290 RUNNER_TEST(smack_accesses_add_modify_test_5){
291     int result;
292     struct smack_accesses * rules = NULL;
293     result = smack_accesses_new(&rules);
294     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
295
296     clean_up();
297
298     // THE TEST
299     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwxat","");
300     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
301
302     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"","r");
303     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
304     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
305
306     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"awxt");
307     RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule should have 'awxt' privileges.");
308     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
309     RUNNER_ASSERT_MSG(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
310
311     // CLEAN UP
312     clean_up();
313     smack_accesses_free(rules);
314 }
315
316
317 /**
318  * Add a list of privileges and then revoke just TWO of them.
319  */
320 RUNNER_TEST(smack_accesses_add_modify_test_6){
321     int result;
322     struct smack_accesses * rules = NULL;
323     result = smack_accesses_new(&rules);
324     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
325
326     clean_up();
327
328     // THE TEST
329     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"rwt","");
330     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
331     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
332
333     result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,"ax","rt");
334     RUNNER_ASSERT_MSG(result == 0, "Unable to modify rule.");
335     RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
336
337     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"wax");
338     RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule should have 'wax' privileges.");
339     result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,"r");
340     RUNNER_ASSERT_MSG(result != 1, "Modification didn't work. Rule should NOT have 'r' privilege.");
341
342     // CLEAN UP
343     clean_up();
344     smack_accesses_free(rules);
345 }
346
347 /**
348  * Run smack_accesses_add_modify with the same accesses_add and accesses_del.
349  */
350 RUNNER_TEST(smack_accesses_add_modify_test_7){
351     unsigned int i;
352     int result;
353
354     struct smack_accesses * rules = NULL;
355
356     for(i = 0; i<accessesBasic.size(); ++i){
357         result = smack_accesses_new(&rules);
358             RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
359
360         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str(),accessesBasic[i].c_str());
361             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
362         RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
363
364         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
365             " Error while checking smack access. Accesses exist.");
366
367         // CLEAN UP
368         clean_up();
369         smack_accesses_free(rules);
370     }
371 }
372
373 /**
374  * Revoke subject with previously added rules and revoke it again.
375  */
376 RUNNER_TEST(smack_revoke_subject_test_1){
377     unsigned int i;
378     int result;
379
380     struct smack_accesses * rules = NULL;
381
382     for(i = 0; i<accessesBasic.size(); ++i){
383
384         // Creating and adding rules with TEST_OBJECT and TEST_OBJECT_2
385         result = smack_accesses_new(&rules);
386             RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
387         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str(),"");
388         result = smack_accesses_add_modify(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str(),"");
389             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
390         RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
391         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
392         RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
393         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
394         RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
395
396         // Revoking subject
397         result = smack_revoke_subject(TEST_SUBJECT);
398         RUNNER_ASSERT_MSG(result == 0, "Revoking subject didn't work.");
399
400         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
401             " Revoke didn't work. Accesses exist.");
402         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
403             " Revoke didn't work. Accesses exist.");
404
405
406         // Revoking subject again
407         result = smack_revoke_subject(TEST_SUBJECT);
408         RUNNER_ASSERT_MSG(result == 0, "Revoking subject didn't work.");
409
410         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
411             " Revoke didn't work. Accesses exist.");
412         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
413             " Revoke didn't work. Accesses exist.");
414
415         smack_accesses_free(rules);
416     }
417 }
418
419 /**
420  * Clearing accesses
421  */
422 RUNNER_TEST(smack_accesses_clear_test_1){
423     unsigned int i;
424     int result;
425
426     struct smack_accesses * rules = NULL;
427
428     for(i = 0; i<accessesBasic.size(); ++i){
429
430         // Creating and adding rules with TEST_OBJECT and TEST_OBJECT_2
431         result = smack_accesses_new(&rules);
432             RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
433         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
434             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
435         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
436             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
437         RUNNER_ASSERT_MSG(smack_accesses_apply(rules) == 0, "Unable to apply rules");
438
439         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
440         RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
441         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
442         RUNNER_ASSERT_MSG(result == 1, "Modification didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
443
444         smack_accesses_free(rules);
445
446         // Creating and clearing rules with TEST_OBJECT
447         result = smack_accesses_new(&rules);
448             RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
449         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
450             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
451         result = smack_accesses_clear(rules);
452         RUNNER_ASSERT_MSG(result == 0, "Clearing rules didn't work.");
453
454         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT,accessesBasic[i].c_str());
455         RUNNER_ASSERT_MSG(result == 0, "Clearing rules didn't work. Rule " << accessesBasic[i].c_str() << " does exist.");
456         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
457         RUNNER_ASSERT_MSG(result == 1, "Clearing rules didn't work. Rule " << accessesBasic[i].c_str() << " does not exist.");
458
459         smack_accesses_free(rules);
460
461         // Creating and clearing rules with TEST_OBJECT_2
462         result = smack_accesses_new(&rules);
463             RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
464
465         result = smack_accesses_add(rules,TEST_SUBJECT, TEST_OBJECT_2,accessesBasic[i].c_str());
466             RUNNER_ASSERT_MSG(result == 0, "Unable to modify accesses instance");
467         result = smack_accesses_clear(rules);
468         RUNNER_ASSERT_MSG(result == 0, "Clearing rules didn't work.");
469
470         smack_accesses_free(rules);
471
472         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT),
473             " Clear didn't work. Accesses exist.");
474         RUNNER_ASSERT_MSG(checkNoAccesses(TEST_SUBJECT, TEST_OBJECT_2),
475             " Clear didn't work. Accesses exist.");
476     }
477 }
478
479 RUNNER_TEST(smack01_storing_and_restoring_rules)
480 {
481     /*
482      * author: Pawel Polawski
483      * test: smack_accesses_new, smack_accesses_add, smack_accesses_add_modify, smack_accesses_add_from_file, 
484      *       smack_accesses_free, smack_accesses_save
485      * description: This test case will create structure holding SMACK rules and add new one to it. Next rules will be
486      *              stored and restored from file.
487      * expect: Rules created and stored in file should be identical to predefined template.
488      */
489
490     struct smack_accesses * rules = NULL;       //rules prepared in this test case
491     struct smack_accesses * import_test = NULL; //rules imported from file
492
493     int result;             //result of each operation to be tested by RUNNER_ASSERT
494     int fd, tmp, sample;    //file descripptors for save / restore rules tests
495
496     //int smack_accesses_new(struct smack_accesses **accesses);
497     result = smack_accesses_new(&rules);        //rules struct init
498     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
499     result = smack_accesses_new(&import_test);  //rules struct init
500     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
501
502     //opening files
503     fd = open("/tmp/smack01_rules", O_RDWR | O_CREAT | O_TRUNC, 0644);  //for export prepared rules
504     RUNNER_ASSERT_MSG(fd >= 0, "Unable to create /tmp/smack01_rules");
505     tmp = open("/tmp/smack01_tmp", O_RDWR | O_CREAT | O_TRUNC, 0644);   //for import rules exported before
506     RUNNER_ASSERT_MSG(fd >= 0, "Unable to create /tmp/smack01_tmp");
507     sample = open("/etc/smack/test_smack_rules", O_RDONLY, 0644);             //reference preinstalled rules
508     RUNNER_ASSERT_MSG(sample >= 0, "Unable to open /etc/smack/test_smack_rules");
509
510     //int smack_accesses_add(struct smack_accesses *handle, const char *subject,
511     //               const char *object, const char *access_type);
512     result = smack_accesses_add(rules, "writer", "book", "rw");
513     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
514     result = smack_accesses_add(rules, "reader", "book", "wx");
515     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
516
517     //int smack_accesses_add_modify(struct smack_accesses *handle, const char *subject,
518     //               const char *object, const char *access_add, const char *access_del);
519     result = smack_accesses_add_modify(rules, "reader", "book", "r", "wx");
520     RUNNER_ASSERT_MSG(0 == result, "Unable to modify smack rules");
521
522     //int smack_accesses_save(struct smack_accesses *handle, int fd);
523     result = smack_accesses_save(rules, fd);
524     RUNNER_ASSERT_MSG(0 == result, "Unable to save smack_accesses instance in file");
525
526     //int smack_accesses_add_from_file(struct smack_accesses *accesses, int fd);
527     result = lseek(fd, 0, SEEK_SET);
528     RUNNER_ASSERT_MSG(result == 0, "lseek() error");
529     result = smack_accesses_add_from_file(import_test, fd);
530     RUNNER_ASSERT_MSG(result == 0, "Unable to import rules from file");
531
532     result = smack_accesses_save(import_test, tmp);
533     RUNNER_ASSERT_MSG(result == 0, "Unable to save smack_accesses instance in file");
534
535     result = files_compare(fd, tmp);    //comparing rules saved in file, restored from it and stored one more time
536     RUNNER_ASSERT_MSG(result == 0, "No match in stored and restored rules");
537
538     result = files_compare(tmp, sample);    //comparing rules stored in file with reference preinstalled rules
539     RUNNER_ASSERT_MSG(result == 0, "No match in stored rules and pattern file");
540
541     //void smack_accesses_free(struct smack_accesses *handle);
542     smack_accesses_free(rules);
543     smack_accesses_free(import_test);
544
545     //closing file descriptors
546     close(fd);
547     close(tmp);
548     close(sample);
549 }
550
551 RUNNER_TEST(smack02_aplying_rules_into_kernel)
552 {
553     /*
554      * author: Pawel Polawski
555      * test: smack_accesses_apply, smack_have_access, smack_revoke_subject, smack_accesses_clear, smack_accesses_new,
556      *       smack_accesses_add, smack_accesses_free
557      * description: In this test case aplying rules to kernel will be tested. After that function for test
558      *              accesses will be used.
559      * expect: In case of correct rules access should be granted.
560      */
561
562     //CAP_MAC_ADMIN needed for process to be able to change rules in kernel (apllying, removing)
563
564     struct smack_accesses * rules = NULL;       //rules prepared in this test case
565     int result;                                 //for storing functions results
566
567     result = smack_accesses_new(&rules);        //rules struct init
568     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
569
570     //adding test rules to struct
571     result = smack_accesses_add(rules, "writer", "book", "rwx");
572     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
573     result = smack_accesses_add(rules, "reader", "book", "r");
574     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
575     result = smack_accesses_add(rules, "spy", "book", "rwx");
576     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
577
578     //int smack_accesses_apply(struct smack_accesses *handle);
579     result = smack_accesses_apply(rules);       //applying rules to kernel
580     RUNNER_ASSERT_MSG(result == 0, "Unable to apply rules into kernel");
581
582     //int smack_have_access(const char *subject, const char *object,
583     //                  const char *access_type);
584     result = smack_have_access("spy", "book", "rwx");       //should have access - rule exist
585     RUNNER_ASSERT_MSG(result == 1, "Error while checking Smack access");
586     result = smack_have_access("reader", "book", "rwx");    //should have no access - wrong rule, should be "r" only
587     RUNNER_ASSERT_MSG(result == 0, "Error while checking Smack access");
588     result = smack_have_access("mars", "book", "rwx");      //should have no acces - rule not exist
589     RUNNER_ASSERT_MSG(result == 0, "Error while checking Smack access");
590
591     //int smack_revoke_subject(const char *subject);
592     result = smack_revoke_subject("snickers");  //this subject do not exist in kernel rules
593     RUNNER_ASSERT_MSG(result == 0, "Error in removing not existing subject from kernel");
594     result = smack_revoke_subject("spy");       //this subject exist in kernel rules
595     RUNNER_ASSERT_MSG(result == 0, "Error in removing existing subject from kernel");
596
597     result = smack_have_access("spy", "book", "rwx");                   //testing access after revoke_subject() from kernel
598     RUNNER_ASSERT_MSG(result == 0, "Error in acces aplied to kernel");  //now spy should have no access
599     result = smack_have_access("spy", "book", "-----");                 //and should have "-----" rule
600     RUNNER_ASSERT_MSG(result == 1, "Error in acces aplied to kernel");
601
602     result = smack_accesses_add(rules, "twix", "book", "rwx");          //for create new rule as a consequence of use accesses_clear() below
603     RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
604
605     //int smack_accesses_clear(struct smack_accesses *handle);
606     result = smack_accesses_clear(rules);    //"spy" removed before by using smack_revoke_subject()
607     RUNNER_ASSERT_MSG(result == 0, "Error in clearing rules in kernel");
608
609     result = smack_have_access("writer", "book", "rwx");                //testing acces after acces_clear()
610     RUNNER_ASSERT_MSG(result == 0, "Error in acces aplied to kernel");  //now writer also should have no access
611     result = smack_have_access("writer", "book", "-----");              //and should have "-----" rule
612     RUNNER_ASSERT_MSG(result == 1, "Error in acces aplied to kernel");
613     result = smack_have_access("twix", "book", "-----");                //rule created by calling accesses_clear()
614     RUNNER_ASSERT_MSG(result == 1, "Error in acces aplied to kernel");
615
616     //free resources
617     smack_accesses_free(rules);
618 }
619
620 //pairs of rules for test with mixed cases, different length and mixed order
621 char * rules_tab[] = {
622                         "reader1",  "-",                "-----",
623                         "reader2",  "--------",         "-----",
624                         "reader3",  "RwXaT",            "rwxat",
625                         "reader4",  "RrrXXXXTTT",       "r-x-t",
626                         "reader5",  "-r-w-a-t",         "rw-at",
627                         "reader6",  "",                 "-----",
628                         "reader7",  "xa--Rt---W",       "rwxat",
629                         "reader8",  "#Ax[T].!~W@1}",    "-wxat"
630                      };
631
632 RUNNER_TEST(smack03_mixed_rule_string_add)
633 {
634     /*
635      * author: Pawel Polawski
636      * test: smack_have_access, smack_accesses_new, smack_accesses_add, smack_accesses_apply, smack_accesses_free
637      * description: In thist test case rules based on mixed string are added to kernel.
638      *              Strings are presented above and contains lower / upper case alpha, numbers and special signs.
639      * expect: Rules should be parsed correct and aplied to kernel.
640      */
641
642     //In thist test case mixed string are used as rules applied to kernel, next they are
643     //readed and compared with correct form of rules
644
645     struct smack_accesses * rules = NULL;       //rules prepared in this test case
646     int result;                                 //for storing functions results
647     int i;
648
649     result = smack_accesses_new(&rules);        //rules struct init
650     RUNNER_ASSERT_MSG(result == 0, "Unable to create smack_accesses instance");
651
652     //adding test rules with mixed string
653     for(i = 0; i < (3 * 8) ; i += 3) {
654         result = smack_accesses_add(rules, rules_tab[i], "book", rules_tab[i + 1]); //using mixed rules from table
655         RUNNER_ASSERT_MSG(result == 0, "Unable to add smack rules");
656     }
657
658     //clearing
659     //FIXME: Using clear() here can cover error in accesses_apply() function
660     //result = smack_accesses_clear(rules);
661     //RUNNER_ASSERT_MSG(result == 0, "Error in clearing rules in kernel");
662
663     //applying rules to kernel
664     result = smack_accesses_apply(rules);
665     RUNNER_ASSERT_MSG(result == 0, "Unable to apply rules into kernel");
666
667     //checking accesses using normal rules
668     for(i = 0; i < (3 * 8) ; i += 3) {
669         result = smack_have_access(rules_tab[i], "book", rules_tab[i + 2]); //using normal rules from table
670         RUNNER_ASSERT_MSG(result == 1, "Error while checking Smack access");
671     }
672
673     //free resources
674     smack_accesses_free(rules);
675
676 }
677
678 RUNNER_TEST(smack04_mixed_rule_string_have_access)
679 {
680     /*
681      * author: Pawel Polawski
682      * test: smack_have_access
683      * description: In this test case we testing aplied before SMACK rules and comparing them using mixed strings.
684      * expect: Subjects should have accesses to the objects.
685      */
686
687     //In this test case we checking previous aplied rules but for compare mixed strings are used
688
689     int result;
690     int i;
691
692     //rules were added in previous RUNNER_TEST section
693     //checking accesses using mixed rules
694     for(i = 0; i < (3 * 8) ; i += 3) {
695         result = smack_have_access(rules_tab[i], "book", rules_tab[i + 1]); //using mixed rules from table
696         RUNNER_ASSERT_MSG(result == 1, "Error while checking Smack access");
697     }
698 }
699
700 //RUNNER_TEST(smackXX_accesses_add_modify)
701 //{
702 //IDEAS FOR TESTS
703 // - what if we want to apply rule that is already in kernel?
704 // - tests for smack_accesses_add_modify() + smack_have_access() (check if add_modify sets the proper rule)
705 // - smack_accesses_add_modify("subject", "object", "rwx", "rwx") should create empty rule
706 //}
707
708 RUNNER_TEST(smack05_self_label)
709 {
710     /*
711      * author: Pawel Polawski
712      * test: smack_set_label_for_self, smack_new_label_from_self
713      * description: In this test case process test it own default label. Next label is changed
714      *              and tested one more time if change was successfull.
715      * expect: Proces should have default "-" label and can change it to the oter one.
716      */
717
718     //In this test case process will manipulate it own label
719
720     char * label = NULL;
721     int result;
722     int fd;
723
724     const int B_SIZE = 8;
725     char buff[B_SIZE];
726
727     char * def_rule = "_";
728
729     //int smack_new_label_from_self(char **label);
730     result = smack_new_label_from_self(&label);
731     RUNNER_ASSERT_MSG(result == 0, "Error in getting self label");
732
733     //comparing this label with default one "_"
734     result = strcmp(label, def_rule);
735     RUNNER_ASSERT_MSG(result == 0, "Wrong default process label");
736
737     //comparing this rule with received from /proc/self/attr/current
738     fd = open("/proc/self/attr/current", O_RDONLY, 0644);
739     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /proc/self/attr/current");
740     result = read(fd, buff, B_SIZE);
741     RUNNER_ASSERT_MSG(result >= 0, "Error in reading from file /proc/self/attr/current");
742     result = strncmp(buff, def_rule, result);
743     RUNNER_ASSERT_MSG(result == 0, "Wrong default process rule");
744
745     free(label);
746
747     //now time for setting labels:
748
749     //int smack_set_label_for_self(const char *label);
750     result = smack_set_label_for_self("cola");
751     RUNNER_ASSERT_MSG(result == 0, "Error in setting self label");
752
753     //checking new label using smack function
754     result = smack_new_label_from_self(&label);
755     RUNNER_ASSERT_MSG(result == 0, "Error in getting self label");
756     result = strcmp(label, "cola");
757     RUNNER_ASSERT_MSG(result == 0, "Wrong process label");
758
759     //checking new label using /proc/self/attr/current
760     result = lseek(fd, 0, SEEK_SET);    //going to the file beginning
761     RUNNER_ASSERT_MSG(result == 0, "lseek() error");
762     result = read(fd, buff, B_SIZE);
763     RUNNER_ASSERT_MSG(result >= 0, "Error in reading from file /proc/self/attr/current");
764     result = strncmp(buff, "cola", result);
765     RUNNER_ASSERT_MSG(result == 0, "Proces rule in /proc/self/attr/current other than set");
766
767     free(label);
768     close(fd);
769 }
770
771 //RUNNER_TEST(smackXX_parent_child_label)
772 //{
773     //In this test case parent process and child labels will be tested
774     //Parent will fork and check child's label. First fork will be with default "_" parent label,
775     //second one witch changed label.
776 //}
777
778 //bellow function is from libsmack.c witch changed name
779 char * xattr(enum smack_label_type type)
780 {
781     switch (type) {
782         case SMACK_LABEL_ACCESS:
783             return "security.SMACK64";
784         case SMACK_LABEL_EXEC:
785             return "security.SMACK64EXEC";
786         case SMACK_LABEL_MMAP:
787             return "security.SMACK64MMAP";
788         case SMACK_LABEL_TRANSMUTE:
789             return "security.SMACK64TRANSMUTE";
790         case SMACK_LABEL_IPIN:
791             return "security.SMACK64IPIN";
792         case SMACK_LABEL_IPOUT:
793             return "security.SMACK64IPOUT";
794         default:
795          /*  Should not reach this point */
796         return NULL;
797     }
798 }
799
800 //TODO: In bellow RUNNER_TEST add  lget / lset functions to be testet the same way as normal get / set
801 RUNNER_TEST(smack06_get_set_label)
802 {
803     /*
804      * author: Pawel Polawski
805      * test: smack_getlabel, smack_setlabel
806      * description: In this test case file label is tested using SMACK API functions and system xattr functions.
807      *              Functions tested here is used for normal files.
808      * expect: Function should return default label, and the new one after change it.
809      */
810
811     //In this test case will be tested setting and getting file label
812     //If file is symbolic link functions should follow it
813
814     //SMACK xattr from libsmack.c:
815     //
816     //case SMACK_LABEL_ACCESS:
817     //    return "security.SMACK64";
818     //case SMACK_LABEL_EXEC:
819     //    return "security.SMACK64EXEC";
820     //case SMACK_LABEL_MMAP:
821     //    return "security.SMACK64MMAP";
822     //case SMACK_LABEL_TRANSMUTE:
823     //    return "security.SMACK64TRANSMUTE";
824     //case SMACK_LABEL_IPIN:
825     //    return "security.SMACK64IPIN";
826     //case SMACK_LABEL_IPOUT:
827     //    return "security.SMACK64IPOUT";
828
829     int result;
830     char * label = NULL;
831
832     const int B_SIZE = 8;
833     char buff[B_SIZE];
834
835     char * file_path = "/etc/smack/test_smack_rules";
836
837
838     //preparing environment by restoring default "_" label
839     result = smack_setlabel(file_path, "_", SMACK_LABEL_ACCESS);
840     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
841
842
843     //int smack_getlabel(const char *path, char** label,
844     //          enum smack_label_type type);
845     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
846     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
847     //get label, should be default "_"
848     result = strcmp(label, "_");
849     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
850     free(label);
851     //get label using xattr function
852     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
853     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
854     //check label, should match the one readed by smack function
855     result = strncmp(buff, "_", result);
856     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
857
858
859     //int smack_setlabel(const char *path, const char* label,
860     //          enum smack_label_type type);
861     result = smack_setlabel(file_path, "fanta", SMACK_LABEL_ACCESS);
862     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
863
864
865     //get label using smack function
866     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
867     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
868     //get label, should be default "fanta"
869     result = strcmp(label, "fanta");
870     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
871     free(label);
872     //get label using xattr function
873     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
874     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
875     //check label, should match the one readed by smack function
876     result = strncmp(buff, "fanta", result);
877     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
878 }
879
880 //RUNNER_TEST(smackXX_get_label_exec)
881 //{
882     //In this test case EXEC label will be tested
883     //by setting this type of label, reading it and testing executed binary exit status
884 //}
885
886 RUNNER_TEST(smack07_l_get_set_label)
887 {
888     /*
889      * author: Pawel Polawski
890      * test: smack_lgetlabel, smack_lsetlabel, smack_getlabel
891      * description: Functions tested here are similar to one from previous test case. The difference
892      *              is that in case of symbolic link they follows it and operates on file pointed by it.
893      * expect: All label manipulations should affect file pointed by symbolic link.
894      */
895
896     int result;
897     char * label = NULL;
898
899     const int B_SIZE = 8;
900     char buff[B_SIZE];
901
902     char * file_path = "/etc/smack/test_smack_rules_lnk";
903
904
905     //preparing environment by restoring default "_" label
906     result = smack_lsetlabel(file_path, "_", SMACK_LABEL_ACCESS);
907     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
908     result = smack_setlabel(file_path, "_", SMACK_LABEL_ACCESS);
909     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
910
911
912     //int smack_lgetlabel(const char *path, char** label,
913     //          enum smack_label_type type);
914     result = smack_lgetlabel(file_path, &label, SMACK_LABEL_ACCESS);
915     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
916     //get label of symbolic link, should be default "_"
917     result = strcmp(label, "_");
918     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
919     free(label);
920     //get label using xattr function
921     result = lgetxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
922     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
923     //check label, should match the one readed by smack function
924     result = strncmp(buff, "_", result);
925     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
926
927
928     //int smack_lsetlabel(const char *path, const char* label,
929     //          enum smack_label_type type);
930     result = smack_lsetlabel(file_path, "7up", SMACK_LABEL_ACCESS);
931     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
932     //and set label for file pointed by link
933     result = smack_setlabel(file_path, "mirinda", SMACK_LABEL_ACCESS);
934     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
935
936
937     //get label using smack function
938     result = smack_lgetlabel(file_path, &label, SMACK_LABEL_ACCESS);
939     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
940     //check label, should be "7up"
941     result = strcmp(label, "7up");
942     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
943     free(label);
944     //get label using xattr function
945     result = lgetxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
946     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
947     //check label, should match the one readed by smack function
948     result = strncmp(buff, "7up", result);
949     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
950
951
952     //now similar to above, but folowing symbolic link set before to "mirinda"
953     result = smack_getlabel(file_path, &label, SMACK_LABEL_ACCESS);
954     RUNNER_ASSERT_MSG(result == 0, "Error gettin label of file pointed by symbolic link");
955     //now label should be "mirinda" for file instead of "7up" set for link
956     result = strcmp(label, "mirinda");
957     RUNNER_ASSERT_MSG(result == 0, "Wrong label of file pointed by symbolic link");
958     free(label);
959     //get label using xattr function
960     result = getxattr(file_path, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
961     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
962     //check label, should match the one readed by smack function
963     result = strncmp(buff, "mirinda", result);
964     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
965 }
966
967 RUNNER_TEST(smack08_f_get_set_label)
968 {
969     /*
970      * author: Pawel Polawski
971      * test: smack_fgetlabel, smack_fsetlabel
972      * description: This test case is similar to test case smack06 above. The difference
973      *              is that argument is file descriptor instead of file path.
974      *              Function not follow symbolic link and operates directly on it.
975      * expect: All label manipulations should affect symbolic link itself.
976      */
977
978     int result;
979     char * label = NULL;
980
981     const int B_SIZE = 8;
982     char buff[B_SIZE];
983
984     int fd;
985     char * file_path = "/etc/smack/test_smack_rules";
986
987     fd = open(file_path, O_RDWR, 0644);             //reference preinstalled rules
988     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /etc/smack/test_smack_rules");
989
990     //preparing environment by restoring default "_" label
991     result = smack_fsetlabel(fd, "_", SMACK_LABEL_ACCESS);
992     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
993
994
995     //int smack_fgetlabel(int fd, char** label,
996     //          enum smack_label_type type);
997     result = smack_fgetlabel(fd, &label, SMACK_LABEL_ACCESS);
998     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
999     //check label, should be "_"
1000     result = strcmp(label, "_");
1001     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
1002     free(label);
1003     //get label using xattr function
1004     result = fgetxattr(fd, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
1005     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
1006     //check label, should match the one readed by smack function
1007     result = strncmp(buff, "_", result);
1008     RUNNER_ASSERT_MSG(result == 0, "Wrong file default label");
1009
1010
1011     //int smack_fsetlabel(int fd, const char* label,
1012     //          enum smack_label_type type);
1013     result = smack_fsetlabel(fd, "sprite", SMACK_LABEL_ACCESS);
1014     RUNNER_ASSERT_MSG(result == 0, "Error in setting ACCESS label for file");
1015
1016
1017     //get label using smack function
1018     result = smack_fgetlabel(fd, &label, SMACK_LABEL_ACCESS);
1019     RUNNER_ASSERT_MSG(result == 0, "Error in getting smack ACCESS label from file");
1020     //check label, should be "sprite"
1021     result = strcmp(label, "sprite");
1022     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
1023     free(label);
1024     //get label using xattr function
1025     result = fgetxattr(fd, xattr(SMACK_LABEL_ACCESS), buff, B_SIZE);
1026     RUNNER_ASSERT_MSG(result > 0, "Error in getting xattr from file");
1027     //check label, should match the one readed by smack function
1028     result = strncmp(buff, "sprite", result);
1029     RUNNER_ASSERT_MSG(result == 0, "Wrong file label");
1030
1031
1032     close(fd);
1033 }
1034
1035 RUNNER_TEST(smack10_adding_removing_rules)
1036 {
1037     unsigned int i;
1038     int result;
1039
1040     struct smack_accesses * rulesBasic = NULL;
1041
1042     for(i = 0; i<accessesBasic.size(); ++i)
1043     {
1044         // Creating rules
1045         result = smack_accesses_new(&rulesBasic);
1046         RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1047
1048         // Adding accesses
1049         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1050         RUNNER_ASSERT_MSG(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1051
1052         // Applying rules
1053         result = smack_accesses_apply(rulesBasic);
1054         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1055
1056         // Checking if accesses were created
1057         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1058         RUNNER_ASSERT_MSG(result == 1,
1059                 " Error while checking smack access. Result: " << result);
1060
1061         smack_accesses_free(rulesBasic);
1062         rulesBasic = NULL;
1063
1064         // Deleting all rules
1065         clean_up();
1066     }
1067
1068     for(i = 0; i<3; ++i)
1069     {
1070         // --- Creating rules (r or w or x)
1071         result = smack_accesses_new(&rulesBasic);
1072         RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1073
1074         // Adding accesses
1075         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1076         RUNNER_ASSERT_MSG(result == 0, "Unable to add rulesBasic. Result: " << result);
1077
1078         // Applying rules
1079         result = smack_accesses_apply(rulesBasic);
1080         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1081         // Checking if accesses were created
1082         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1083         RUNNER_ASSERT_MSG(result == 1,
1084                 " Error while checking smack access. Result: " << result);
1085
1086         // Checking if wrong accesses were not created
1087         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i+3].c_str());
1088         RUNNER_ASSERT_MSG(result == 0,
1089                 " Error while checking smack access. Result: " << result);
1090
1091         // --- Modifying accesses (r for wx or w for rx or x for rw)
1092         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,accessesBasic[i+3].c_str(),accessesBasic[i].c_str());
1093         RUNNER_ASSERT_MSG(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1094
1095         // Applying rules
1096         result = smack_accesses_apply(rulesBasic);
1097         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1098
1099         // Checking if accesses were created
1100         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i+3].c_str());
1101         RUNNER_ASSERT_MSG(result == 1,
1102                 " Error while checking smack access. Result: " << result);
1103
1104         // Checking if wrong accesses were not created
1105         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1106         RUNNER_ASSERT_MSG(result == 0,
1107                 " Error while checking smack access. Result: " << result);
1108
1109         smack_accesses_free(rulesBasic);
1110         rulesBasic = NULL;
1111
1112         // --- Creating complementary rules (r or w or x)
1113         result = smack_accesses_new(&rulesBasic);
1114         RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1115
1116         // Adding accesses
1117         result = smack_accesses_add(rulesBasic, TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1118         RUNNER_ASSERT_MSG(result == 0, "Unable to add rulesBasic. Result: " << result);
1119
1120         // Checking if accesses were created
1121         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i+3].c_str());
1122         RUNNER_ASSERT_MSG(result == 1,
1123                 " Error while checking smack access. Result: " << result);
1124
1125         // Applying rules
1126         result = smack_accesses_apply(rulesBasic);
1127         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1128
1129         // Checking if accesses were created
1130         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1131         RUNNER_ASSERT_MSG(result == 1,
1132                 " Error while checking smack access. Result: " << result);
1133
1134         // --- Modifying accesses (adding rwx and removing r or w or x)
1135         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,"rwx",accessesBasic[i].c_str());
1136         RUNNER_ASSERT_MSG(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1137
1138         // Applying rules
1139         result = smack_accesses_apply(rulesBasic);
1140         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1141
1142         // Checking if accesses were created
1143         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i+3].c_str());
1144         RUNNER_ASSERT_MSG(result == 1,
1145                 " Error while checking smack access. Result: " << result);
1146
1147         // Checking if wrong accesses were not created
1148         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[i].c_str());
1149         RUNNER_ASSERT_MSG(result == 0,
1150                 " Error while checking smack access. Result: " << result);
1151
1152         // --- Adding crossing accesses (rx or rw or wx)
1153         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,accessesBasic[3+((i+1)%3)].c_str(),"");
1154         RUNNER_ASSERT_MSG(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1155
1156         // Applying rules
1157         result = smack_accesses_apply(rulesBasic);
1158         RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1159
1160         // Checking if accesses were created
1161         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, accessesBasic[3+((i+1)%3)].c_str());
1162         RUNNER_ASSERT_MSG(result == 1,
1163                 " Error while checking smack access. Result: " << result);
1164
1165         result = smack_have_access(TEST_SUBJECT, TEST_OBJECT, "rwx");
1166         RUNNER_ASSERT_MSG(result == 1,
1167                 " Error while checking smack access. Result: " << result);
1168
1169         // Deleting all rules
1170         result = smack_accesses_add_modify(rulesBasic,TEST_SUBJECT, TEST_OBJECT,"","rwx");
1171         RUNNER_ASSERT_MSG(result == 0, "Unable to add modify rulesBasic. Result: " << result);
1172
1173         result = smack_accesses_apply(rulesBasic);
1174         RUNNER_ASSERT_MSG(result == 0, "Error while checking smack access. Result: " << result);
1175
1176         smack_accesses_free(rulesBasic);
1177         rulesBasic = NULL;
1178
1179         // Deleting all rules
1180         clean_up();
1181     }
1182 }
1183
1184 RUNNER_TEST(smack11_saving_loading_rules)
1185 {
1186     int result;
1187     int fd;
1188
1189     struct smack_accesses * rulesBasic = NULL;
1190
1191     // Pre-cleanup
1192     removeAccessesAll();
1193
1194     // Creating rules
1195     result = smack_accesses_new(&rulesBasic);
1196     RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1197
1198     // Loading file with rwxat rules - test_smack_rules_full
1199     fd = open("/etc/smack/test_smack_rules_full", O_RDONLY, 0644);
1200     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /etc/smack/test_smack_rules_full");
1201
1202     // Adding rules from file
1203     result = smack_accesses_add_from_file(rulesBasic, fd);
1204     close(fd);
1205     RUNNER_ASSERT_MSG(result == 0, "Error importing accesses from file");
1206
1207     // Applying rules
1208     result = smack_accesses_apply(rulesBasic);
1209     RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1210
1211     // Checking rules
1212     result = smack_have_access("test_subject_01", "test_object_01", "rwxat");
1213     RUNNER_ASSERT_MSG(result == 1,
1214         " Error while checking smack accesses.");
1215     result = smack_have_access("test_subject_01", "test_object_02", "rwxat");
1216     RUNNER_ASSERT_MSG(result == 1,
1217         " Error while checking smack accesses.");
1218     result = smack_have_access("test_subject_01", "test_object_03", "rwxat");
1219     RUNNER_ASSERT_MSG(result == 1,
1220         " Error while checking smack accesses.");
1221     result = smack_have_access("test_subject_02", "test_object_01", "rwxat");
1222     RUNNER_ASSERT_MSG(result == 1,
1223         " Error while checking smack accesses.");
1224     result = smack_have_access("test_subject_02", "test_object_02", "rwxat");
1225     RUNNER_ASSERT_MSG(result == 1,
1226         " Error while checking smack accesses.");
1227     result = smack_have_access("test_subject_02", "test_object_03", "rwxat");
1228     RUNNER_ASSERT_MSG(result == 1,
1229         " Error while checking smack accesses.");
1230     result = smack_have_access("test_subject_03", "test_object_01", "rwxat");
1231     RUNNER_ASSERT_MSG(result == 1,
1232         " Error while checking smack accesses.");
1233     result = smack_have_access("test_subject_03", "test_object_02", "rwxat");
1234     RUNNER_ASSERT_MSG(result == 1,
1235         " Error while checking smack accesses.");
1236     result = smack_have_access("test_subject_03", "test_object_03", "rwxat");
1237     RUNNER_ASSERT_MSG(result == 1,
1238         " Error while checking smack accesses.");
1239
1240     // Removing rules
1241     removeAccessesAll();
1242
1243     smack_accesses_free(rulesBasic);
1244
1245     // Creating rules
1246     result = smack_accesses_new(&rulesBasic);
1247     RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1248
1249      // Loading file with partial wrong rules - test_smack_rules2
1250     fd = open("/etc/smack/test_smack_rules2", O_RDONLY, 0644);
1251     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /etc/smack/test_smack_rules2");
1252
1253     // Adding rules from file
1254     result = smack_accesses_add_from_file(rulesBasic, fd);
1255     close(fd);
1256     RUNNER_ASSERT_MSG(result == 0, "Accesses were loaded from file");
1257
1258     // Applying rules
1259     result = smack_accesses_apply(rulesBasic);
1260     RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1261
1262     // Checking rules
1263     RUNNER_ASSERT_MSG(checkNoAccesses("test_subject_01", "test_object_01"),
1264         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Accesses exist.");
1265     result = smack_have_access("test_subject_01", "test_object_02", "rwat");
1266     RUNNER_ASSERT_MSG(result == 1,
1267         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1268     result = smack_have_access("test_subject_01", "test_object_03", "wat");
1269     RUNNER_ASSERT_MSG(result == 1,
1270         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1271     RUNNER_ASSERT_MSG(checkNoAccesses("test_subject_02", "test_object_01"),
1272         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Accesses exist.");
1273     result = smack_have_access("test_subject_02", "test_object_02", "wa-ft");
1274     RUNNER_ASSERT_MSG(result == 1,
1275         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1276     result = smack_have_access("test_subject_02", "test_object_03", "wr");
1277     RUNNER_ASSERT_MSG(result == 1,
1278         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1279     result = smack_have_access("test_subject_03", "test_object_01", "a");
1280     RUNNER_ASSERT_MSG(result == 1,
1281         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1282     result = smack_have_access("test_subject_03", "test_object_02", "rwat");
1283     RUNNER_ASSERT_MSG(result == 1,
1284         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1285     result = smack_have_access("test_subject_03", "test_object_03", "w");
1286     RUNNER_ASSERT_MSG(result == 1,
1287         " Error while checking smack access loaded from /etc/smack/test_smack_rules2. Result: " << result );
1288
1289     // Removing rules
1290     removeAccessesAll();
1291
1292     smack_accesses_free(rulesBasic);
1293
1294     // Creating rules
1295     result = smack_accesses_new(&rulesBasic);
1296     RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1297
1298     // Loading file with partial wrong rules - test_smack_rules3
1299     fd = open("/etc/smack/test_smack_rules3", O_RDONLY, 0644);
1300     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /etc/smack/test_smack_rules3");
1301
1302     // Adding rules from file
1303     result = smack_accesses_add_from_file(rulesBasic, fd);
1304     close(fd);
1305     RUNNER_ASSERT_MSG(result != 0, "Accesses were loaded from file");
1306
1307     // Applying rules
1308     result = smack_accesses_apply(rulesBasic);
1309     RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1310
1311     // Checking rules
1312     result = smack_have_access("test_subject_01", "test_object_01", "rwat");
1313     RUNNER_ASSERT_MSG(result == 1,
1314         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Result: " << result );
1315     RUNNER_ASSERT_MSG(checkNoAccesses("test_subject_01", "test_object_02"),
1316         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Accesses exist.");
1317     result = smack_have_access("test_subject_01", "test_object_03", "x");
1318     RUNNER_ASSERT_MSG(result == 0,
1319         " Error while checking smack access loaded from /etc/smack/test_smack_rules3. Result: " << result );
1320
1321     // Removing rules
1322     removeAccessesAll();
1323
1324     smack_accesses_free(rulesBasic);
1325
1326     // Creating rules
1327     result = smack_accesses_new(&rulesBasic);
1328     RUNNER_ASSERT_MSG(result == 0, "Error while creating new accesses. Result: " << result);
1329
1330     // Loading file with partial wrong rules - test_smack_rules4
1331     fd = open("/etc/smack/test_smack_rules4", O_RDONLY, 0644);
1332     RUNNER_ASSERT_MSG(fd >= 0, "Unable to open /etc/smack/test_smack_rules4");
1333
1334     // Adding rules from file
1335     result = smack_accesses_add_from_file(rulesBasic, fd);
1336     close(fd);
1337     RUNNER_ASSERT_MSG(result != 0, "Accesses were loaded from file");
1338
1339     // Applying rules
1340     result = smack_accesses_apply(rulesBasic);
1341     RUNNER_ASSERT_MSG(result == 0, "Error while applying accesses. Result: " << result);
1342
1343     // Checking rules
1344     result = smack_have_access("test_subject_01", "test_object_01", "rxwat");
1345     RUNNER_ASSERT_MSG(result == 1,
1346         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Result: " << result );
1347     RUNNER_ASSERT_MSG(checkNoAccesses("test_subject_01", "test_object_02"),
1348         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Accesses exist.");
1349     result = smack_have_access("test_subject_01", "test_object_03", "a");
1350     RUNNER_ASSERT_MSG(result == 0,
1351         " Error while checking smack access loaded from /etc/smack/test_smack_rules4. Result: " << result );
1352
1353     // Removing rules
1354     removeAccessesAll();
1355
1356     smack_accesses_free(rulesBasic);
1357
1358 }
1359
1360 //int smack_new_label_from_socket(int fd, char **label);
1361
1362
1363 static void smack_set_another_label_for_self(void)
1364 {
1365     static int number = time(NULL);
1366     int result;
1367     char* smack_label;
1368
1369     number++;
1370     result = asprintf(&smack_label, "s%ld", number);
1371     RUNNER_ASSERT_MSG(result > 0, "asprintf failed");
1372     result = smack_set_label_for_self(smack_label);
1373     RUNNER_ASSERT_MSG(result == 0, "smack_set_label_for_self(" << smack_label << ") failed");
1374     free(smack_label);
1375 }
1376
1377 static void smack_unix_sock_server(int sock)
1378 {
1379     int fd, result;
1380     char* smack_label;
1381
1382     alarm(2);
1383     fd = accept(sock, NULL, NULL);
1384     alarm(0);
1385     if (fd < 0)
1386         return;
1387     result = smack_new_label_from_self(&smack_label);
1388     RUNNER_ASSERT_MSG(result == 0, "smack_new_label_from_self() failed");
1389     result = write(fd, smack_label, strlen(smack_label));
1390     RUNNER_ASSERT_MSG(result == strlen(smack_label), "write() failed");
1391     close(fd);
1392     free(smack_label);
1393 }
1394
1395 RUNNER_TEST(smack09_new_label_from_socket)
1396 {
1397
1398     int pid;
1399     struct sockaddr_un sockaddr = {AF_UNIX, SOCK_PATH};
1400     unlink(SOCK_PATH);
1401     smack_set_another_label_for_self();
1402     pid = fork();
1403     RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
1404     if (!pid) { /* child process, server */
1405         int sock, result;
1406
1407
1408         sock = socket(AF_UNIX, SOCK_STREAM, 0);
1409         RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
1410         result = bind(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un));
1411         RUNNER_ASSERT_MSG(result == 0, "bind failed: " << strerror(errno));
1412         result = listen(sock, 1);
1413         RUNNER_ASSERT_MSG(result == 0, "listen failed: " << strerror(errno));
1414         smack_unix_sock_server(sock);
1415
1416         pid = fork();
1417         RUNNER_ASSERT_MSG(pid >= 0, "Fork failed");
1418         /*  Test if socket label was unaffected by fork() */
1419         smack_unix_sock_server(sock);
1420         if(!pid) {
1421                 usleep (100);
1422                 smack_set_another_label_for_self();
1423             smack_unix_sock_server(sock);
1424         }
1425         close(sock);
1426         if (pid)
1427             waitpid(pid, NULL, 0);
1428         exit(0);
1429     } else { /* parent process, client */
1430         sleep(1); /* Give server some time to setup listening socket */
1431         for (int i = 0; i < 4; ++i) {
1432
1433             int sock, result;
1434             char smack_label1[SMACK_LABEL_LEN + 1];
1435             char* smack_label2;
1436
1437             sock = socket(AF_UNIX, SOCK_STREAM, 0);
1438             RUNNER_ASSERT_MSG(sock >= 0, "socket failed: " << strerror(errno));
1439             result = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un));
1440             RUNNER_ASSERT_MSG(result == 0, "connect failed: " << strerror(errno));
1441             alarm(2);
1442             result = read(sock, smack_label1, SMACK_LABEL_LEN);
1443             RUNNER_ASSERT_MSG(result >= 0, "read failed: " << strerror(errno));
1444             alarm(0);
1445             smack_label1[result] = '\0';
1446             result = smack_new_label_from_socket(sock, &smack_label2);
1447             RUNNER_ASSERT_MSG(result == 0, "smack_label_from_socket failed");
1448             result = strcmp(smack_label1, smack_label2);
1449             if(i < 3)
1450                 RUNNER_ASSERT_MSG(result == 0, "smack labels differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i);
1451             else
1452                 RUNNER_ASSERT_MSG(result != 0, "smack labels do not differ: '" << smack_label1 << "' != '" << smack_label2 << "' i == " << i);
1453             close(sock);
1454         }
1455         waitpid(pid, NULL, 0);
1456     }
1457 }