1// Copyright 2012 Google Inc.
2// 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
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include "result.h"
30
31#include <sys/stat.h>
32
33#include <atf-c.h>
34
35#include "error.h"
36
37
38ATF_TC_WITHOUT_HEAD(write__passed);
39ATF_TC_BODY(write__passed, tc)
40{
41    const kyua_error_t error = kyua_result_write(
42        "test.txt", KYUA_RESULT_PASSED, NULL);
43    ATF_REQUIRE(!kyua_error_is_set(error));
44    ATF_REQUIRE(atf_utils_compare_file("test.txt", "passed\n"));
45}
46
47
48ATF_TC_WITHOUT_HEAD(write__failed);
49ATF_TC_BODY(write__failed, tc)
50{
51    const kyua_error_t error = kyua_result_write(
52        "test.txt", KYUA_RESULT_FAILED, "Some message");
53    ATF_REQUIRE(!kyua_error_is_set(error));
54    ATF_REQUIRE(atf_utils_compare_file("test.txt", "failed: Some message\n"));
55}
56
57
58ATF_TC_WITHOUT_HEAD(write__broken);
59ATF_TC_BODY(write__broken, tc)
60{
61    const kyua_error_t error = kyua_result_write(
62        "test.txt", KYUA_RESULT_BROKEN, "Some message");
63    ATF_REQUIRE(!kyua_error_is_set(error));
64    ATF_REQUIRE(atf_utils_compare_file("test.txt", "broken: Some message\n"));
65}
66
67
68ATF_TC_WITHOUT_HEAD(write__skipped);
69ATF_TC_BODY(write__skipped, tc)
70{
71    const kyua_error_t error = kyua_result_write(
72        "test.txt", KYUA_RESULT_SKIPPED, "Some message");
73    ATF_REQUIRE(!kyua_error_is_set(error));
74    ATF_REQUIRE(atf_utils_compare_file("test.txt", "skipped: Some message\n"));
75}
76
77
78ATF_TC_WITHOUT_HEAD(write__expected_failure);
79ATF_TC_BODY(write__expected_failure, tc)
80{
81    const kyua_error_t error = kyua_result_write(
82        "test.txt", KYUA_RESULT_EXPECTED_FAILURE, "Some message");
83    ATF_REQUIRE(!kyua_error_is_set(error));
84    ATF_REQUIRE(atf_utils_compare_file("test.txt", "expected_failure: "
85                                       "Some message\n"));
86}
87
88
89ATF_TC(write__open_error);
90ATF_TC_HEAD(write__open_error, tc)
91{
92    atf_tc_set_md_var(tc, "require.user", "unprivileged");
93}
94ATF_TC_BODY(write__open_error, tc)
95{
96    ATF_REQUIRE(mkdir("readonly", 0555) != -1);
97    const kyua_error_t error = kyua_result_write(
98        "readonly/test.txt", KYUA_RESULT_FAILED, "Some message");
99    ATF_REQUIRE(kyua_error_is_set(error));
100    ATF_REQUIRE(kyua_error_is_type(error, "libc"));
101    char buffer[512];
102    kyua_error_format(error, buffer, sizeof(buffer));
103    ATF_REQUIRE(atf_utils_grep_string(
104        "Cannot create result file 'readonly/test.txt'", buffer));
105    kyua_error_free(error);
106}
107
108
109ATF_TP_ADD_TCS(tp)
110{
111    ATF_TP_ADD_TC(tp, write__passed);
112    ATF_TP_ADD_TC(tp, write__failed);
113    ATF_TP_ADD_TC(tp, write__skipped);
114    ATF_TP_ADD_TC(tp, write__broken);
115    ATF_TP_ADD_TC(tp, write__expected_failure);
116    ATF_TP_ADD_TC(tp, write__open_error);
117
118    return atf_no_error();
119}
120