xref: /freebsd/contrib/kyua/store/migrate_test.cpp (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 // Copyright 2011 The Kyua Authors.
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 "store/migrate.hpp"
30 
31 extern "C" {
32 #include <sys/stat.h>
33 }
34 
35 #include <atf-c++.hpp>
36 
37 #include "store/exceptions.hpp"
38 #include "utils/env.hpp"
39 #include "utils/fs/operations.hpp"
40 #include "utils/fs/path.hpp"
41 
42 namespace fs = utils::fs;
43 
44 
45 ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__ok);
46 ATF_TEST_CASE_BODY(detail__backup_database__ok)
47 {
48     atf::utils::create_file("test.db", "The DB\n");
49     store::detail::backup_database(fs::path("test.db"), 13);
50     ATF_REQUIRE(fs::exists(fs::path("test.db")));
51     ATF_REQUIRE(fs::exists(fs::path("test.db.v13.backup")));
52     ATF_REQUIRE(atf::utils::compare_file("test.db.v13.backup", "The DB\n"));
53 }
54 
55 
56 ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__ok_overwrite);
57 ATF_TEST_CASE_BODY(detail__backup_database__ok_overwrite)
58 {
59     atf::utils::create_file("test.db", "Original contents");
60     atf::utils::create_file("test.db.v1.backup", "Overwrite me");
61     store::detail::backup_database(fs::path("test.db"), 1);
62     ATF_REQUIRE(fs::exists(fs::path("test.db")));
63     ATF_REQUIRE(fs::exists(fs::path("test.db.v1.backup")));
64     ATF_REQUIRE(atf::utils::compare_file("test.db.v1.backup",
65                                          "Original contents"));
66 }
67 
68 
69 ATF_TEST_CASE_WITHOUT_HEAD(detail__backup_database__fail_open);
70 ATF_TEST_CASE_BODY(detail__backup_database__fail_open)
71 {
72     ATF_REQUIRE_THROW_RE(store::error, "Cannot open.*foo.db",
73                          store::detail::backup_database(fs::path("foo.db"), 5));
74 }
75 
76 
77 ATF_TEST_CASE_WITH_CLEANUP(detail__backup_database__fail_create);
78 ATF_TEST_CASE_HEAD(detail__backup_database__fail_create)
79 {
80     set_md_var("require.user", "unprivileged");
81 }
82 ATF_TEST_CASE_BODY(detail__backup_database__fail_create)
83 {
84     ATF_REQUIRE(::mkdir("dir", 0755) != -1);
85     atf::utils::create_file("dir/test.db", "Does not need to be valid");
86     ATF_REQUIRE(::chmod("dir", 0111) != -1);
87     ATF_REQUIRE_THROW_RE(
88         store::error, "Cannot create.*dir/test.db.v13.backup",
89         store::detail::backup_database(fs::path("dir/test.db"), 13));
90 }
91 ATF_TEST_CASE_CLEANUP(detail__backup_database__fail_create)
92 {
93     if (::chmod("dir", 0755) == -1) {
94         // If we cannot restore the original permissions, we cannot do much
95         // more.  However, leaving an unwritable directory behind will cause the
96         // runtime engine to report us as broken.
97     }
98 }
99 
100 
101 ATF_TEST_CASE_WITHOUT_HEAD(detail__migration_file__builtin);
102 ATF_TEST_CASE_BODY(detail__migration_file__builtin)
103 {
104     utils::unsetenv("KYUA_STOREDIR");
105     ATF_REQUIRE_EQ(fs::path(KYUA_STOREDIR) / "migrate_v5_v9.sql",
106                    store::detail::migration_file(5, 9));
107 }
108 
109 
110 ATF_TEST_CASE_WITHOUT_HEAD(detail__migration_file__overriden);
111 ATF_TEST_CASE_BODY(detail__migration_file__overriden)
112 {
113     utils::setenv("KYUA_STOREDIR", "/tmp/test");
114     ATF_REQUIRE_EQ(fs::path("/tmp/test/migrate_v5_v9.sql"),
115                    store::detail::migration_file(5, 9));
116 }
117 
118 
119 ATF_INIT_TEST_CASES(tcs)
120 {
121     ATF_ADD_TEST_CASE(tcs, detail__backup_database__ok);
122     ATF_ADD_TEST_CASE(tcs, detail__backup_database__ok_overwrite);
123     ATF_ADD_TEST_CASE(tcs, detail__backup_database__fail_open);
124     ATF_ADD_TEST_CASE(tcs, detail__backup_database__fail_create);
125 
126     ATF_ADD_TEST_CASE(tcs, detail__migration_file__builtin);
127     ATF_ADD_TEST_CASE(tcs, detail__migration_file__overriden);
128 
129     // Tests for migrate_schema are in schema_inttest.  This is because, for
130     // such tests to be meaningful, they need to be integration tests and don't
131     // really fit the goal of this unit-test module.
132 }
133