deps_formula.c
1 /* 2 * Copyright (c) 2015, Vsevolod Stakhov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 26 #include <atf-c.h> 27 #include <err.h> 28 #include <unistd.h> 29 #include <pkg.h> 30 #include <private/pkg.h> 31 #include <private/pkg_deps.h> 32 33 ATF_TC_WITHOUT_HEAD(check_parsing); 34 ATF_TC_WITHOUT_HEAD(check_sql); 35 ATF_TC_WITHOUT_HEAD(check_op_parsing); 36 37 ATF_TC_BODY(check_parsing, tc) 38 { 39 struct pkg_dep_formula *f; 40 const char *cases[] = { 41 "name", 42 "name = 1.0", 43 "name >= 1.0,1", 44 "name1, name2", 45 "name1 | name2, name3", 46 "name1 = 1.0 | name2 != 1.0, name3 > 1.0 < 2.0 != 1.5", 47 "name1 = 1.0 | name2 != 1.0, name3 > 1.0 < 2.0 != 1.5, name4 +opt1 -opt2" 48 }; 49 char *r; 50 unsigned int i; 51 52 for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i ++) { 53 f = pkg_deps_parse_formula(cases[i]); 54 ATF_REQUIRE(f != NULL); 55 r = pkg_deps_formula_tostring(f); 56 ATF_REQUIRE_STREQ(r, cases[i]); 57 free(r); 58 pkg_deps_formula_free(f); 59 } 60 } 61 62 ATF_TC_HEAD(check_sql, tc) 63 { 64 atf_tc_set_md_var(tc, "descr", "testing creating sql queries from formulas"); 65 } 66 67 ATF_TC_BODY(check_sql, tc) 68 { 69 struct pkg_dep_formula *f; 70 const char *cases[] = { 71 "name", "(name='name')", 72 "name = 1.0", "(name='name' AND vercmp('=',version,'1.0'))", 73 "name >= 1.0,1", "(name='name' AND vercmp('>=',version,'1.0,1'))", 74 "name1 | name2", "(name='name1') OR (name='name2')", 75 "name1 = 1.0 | name2 != 1.0", "(name='name1' AND vercmp('=',version,'1.0')) OR (name='name2' AND vercmp('!=',version,'1.0'))" 76 }; 77 char *r; 78 unsigned int i; 79 80 for (i = 0; i < sizeof(cases) / sizeof(cases[0]) / 2; i ++) { 81 f = pkg_deps_parse_formula(cases[i * 2]); 82 ATF_REQUIRE(f != NULL); 83 r = pkg_deps_formula_tosql(f->items); 84 ATF_REQUIRE_STREQ(r, cases[i * 2 + 1]); 85 free(r); 86 pkg_deps_formula_free(f); 87 } 88 } 89 90 ATF_TC_HEAD(check_op_parsing, tc) 91 { 92 atf_tc_set_md_var(tc, "descr", "testing parsing operands"); 93 } 94 ATF_TC_BODY(check_op_parsing, tc) 95 { 96 struct cases { 97 const char *val; 98 int expect; 99 } cases[] = { 100 { "=", VERSION_EQ }, 101 { "==", VERSION_EQ }, 102 { ">=", VERSION_GE }, 103 { ">", VERSION_GT }, 104 { "<=", VERSION_LE }, 105 { "<", VERSION_LT }, 106 { "!", VERSION_NOT }, 107 { "!=", VERSION_NOT }, 108 { "*", VERSION_ANY }, 109 { NULL, VERSION_ANY }, 110 { "=>", VERSION_ANY }, 111 }; 112 113 for (unsigned int i = 0; i < sizeof(cases) / sizeof(cases[0]); i ++) { 114 ATF_REQUIRE_EQ((int)pkg_deps_string_toop(cases[i].val), cases[i].expect); 115 } 116 } 117 118 ATF_TP_ADD_TCS(tp) 119 { 120 ATF_TP_ADD_TC(tp, check_parsing); 121 ATF_TP_ADD_TC(tp, check_sql); 122 ATF_TP_ADD_TC(tp, check_op_parsing); 123 return (atf_no_error()); 124 }