LCOV - code coverage report
Current view: top level - test - test-extract-word.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 448 448 100.0 %
Date: 2019-08-23 13:36:53 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 286 572 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <errno.h>
       4                 :            : #include <stdlib.h>
       5                 :            : #include <string.h>
       6                 :            : 
       7                 :            : #include "extract-word.h"
       8                 :            : #include "log.h"
       9                 :            : #include "string-util.h"
      10                 :            : 
      11                 :          4 : static void test_extract_first_word(void) {
      12                 :            :         const char *p, *original;
      13                 :            :         char *t;
      14                 :            : 
      15                 :          4 :         p = original = "foobar waldo";
      16         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
      17         [ -  + ]:          4 :         assert_se(streq(t, "foobar"));
      18                 :          4 :         free(t);
      19         [ -  + ]:          4 :         assert_se(p == original + 7);
      20                 :            : 
      21         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
      22         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
      23                 :          4 :         free(t);
      24         [ -  + ]:          4 :         assert_se(isempty(p));
      25                 :            : 
      26         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
      27         [ -  + ]:          4 :         assert_se(!t);
      28         [ -  + ]:          4 :         assert_se(isempty(p));
      29                 :            : 
      30                 :          4 :         p = original = "\"foobar\" \'waldo\'";
      31         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
      32         [ -  + ]:          4 :         assert_se(streq(t, "\"foobar\""));
      33                 :          4 :         free(t);
      34         [ -  + ]:          4 :         assert_se(p == original + 9);
      35                 :            : 
      36         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
      37         [ -  + ]:          4 :         assert_se(streq(t, "\'waldo\'"));
      38                 :          4 :         free(t);
      39         [ -  + ]:          4 :         assert_se(isempty(p));
      40                 :            : 
      41         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
      42         [ -  + ]:          4 :         assert_se(!t);
      43         [ -  + ]:          4 :         assert_se(isempty(p));
      44                 :            : 
      45                 :          4 :         p = original = "\"foobar\" \'waldo\'";
      46         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
      47         [ -  + ]:          4 :         assert_se(streq(t, "foobar"));
      48                 :          4 :         free(t);
      49         [ -  + ]:          4 :         assert_se(p == original + 9);
      50                 :            : 
      51         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
      52         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
      53                 :          4 :         free(t);
      54         [ -  + ]:          4 :         assert_se(isempty(p));
      55                 :            : 
      56         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 0);
      57         [ -  + ]:          4 :         assert_se(!t);
      58         [ -  + ]:          4 :         assert_se(isempty(p));
      59                 :            : 
      60                 :          4 :         p = original = "\"";
      61         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
      62         [ -  + ]:          4 :         assert_se(streq(t, "\""));
      63                 :          4 :         free(t);
      64         [ -  + ]:          4 :         assert_se(isempty(p));
      65                 :            : 
      66                 :          4 :         p = original = "\"";
      67         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
      68         [ -  + ]:          4 :         assert_se(p == original + 1);
      69                 :            : 
      70                 :          4 :         p = original = "\'";
      71         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
      72         [ -  + ]:          4 :         assert_se(streq(t, "\'"));
      73                 :          4 :         free(t);
      74         [ -  + ]:          4 :         assert_se(isempty(p));
      75                 :            : 
      76                 :          4 :         p = original = "\'";
      77         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
      78         [ -  + ]:          4 :         assert_se(p == original + 1);
      79                 :            : 
      80                 :          4 :         p = original = "\'fooo";
      81         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == 1);
      82         [ -  + ]:          4 :         assert_se(streq(t, "\'fooo"));
      83                 :          4 :         free(t);
      84         [ -  + ]:          4 :         assert_se(isempty(p));
      85                 :            : 
      86                 :          4 :         p = original = "KEY=val \"KEY2=val with space\" \"KEY3=val with \\\"quotation\\\"\"";
      87         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
      88         [ -  + ]:          4 :         assert_se(streq(t, "KEY=val"));
      89                 :          4 :         free(t);
      90         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
      91         [ -  + ]:          4 :         assert_se(streq(t, "KEY2=val with space"));
      92                 :          4 :         free(t);
      93         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == 1);
      94         [ -  + ]:          4 :         assert_se(streq(t, "KEY3=val with \"quotation\""));
      95                 :          4 :         free(t);
      96         [ -  + ]:          4 :         assert_se(isempty(p));
      97                 :            : 
      98                 :          4 :         p = original = "KEY=val \"KEY2=val space\" \"KEY3=val with \\\"quotation\\\"\"";
      99         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
     100         [ -  + ]:          4 :         assert_se(streq(t, "KEY=val"));
     101                 :          4 :         free(t);
     102         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
     103         [ -  + ]:          4 :         assert_se(streq(t, "\"KEY2=val"));
     104                 :          4 :         free(t);
     105         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) == 1);
     106         [ -  + ]:          4 :         assert_se(streq(t, "space\""));
     107                 :          4 :         free(t);
     108         [ -  + ]:          4 :         assert_se(startswith(p, "\"KEY3="));
     109                 :            : 
     110                 :          4 :         p = original = "\'fooo";
     111         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) == -EINVAL);
     112         [ -  + ]:          4 :         assert_se(p == original + 5);
     113                 :            : 
     114                 :          4 :         p = original = "\'fooo";
     115         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
     116         [ -  + ]:          4 :         assert_se(streq(t, "fooo"));
     117                 :          4 :         free(t);
     118         [ -  + ]:          4 :         assert_se(isempty(p));
     119                 :            : 
     120                 :          4 :         p = original = "\"fooo";
     121         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
     122         [ -  + ]:          4 :         assert_se(streq(t, "fooo"));
     123                 :          4 :         free(t);
     124         [ -  + ]:          4 :         assert_se(isempty(p));
     125                 :            : 
     126                 :          4 :         p = original = "yay\'foo\'bar";
     127         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
     128         [ -  + ]:          4 :         assert_se(streq(t, "yay\'foo\'bar"));
     129                 :          4 :         free(t);
     130         [ -  + ]:          4 :         assert_se(isempty(p));
     131                 :            : 
     132                 :          4 :         p = original = "yay\'foo\'bar";
     133         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
     134         [ -  + ]:          4 :         assert_se(streq(t, "yayfoobar"));
     135                 :          4 :         free(t);
     136         [ -  + ]:          4 :         assert_se(isempty(p));
     137                 :            : 
     138                 :          4 :         p = original = "   foobar   ";
     139         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
     140         [ -  + ]:          4 :         assert_se(streq(t, "foobar"));
     141                 :          4 :         free(t);
     142         [ -  + ]:          4 :         assert_se(isempty(p));
     143                 :            : 
     144                 :          4 :         p = original = " foo\\ba\\x6ar ";
     145         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
     146         [ -  + ]:          4 :         assert_se(streq(t, "foo\ba\x6ar"));
     147                 :          4 :         free(t);
     148         [ -  + ]:          4 :         assert_se(isempty(p));
     149                 :            : 
     150                 :          4 :         p = original = " foo\\ba\\x6ar ";
     151         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
     152         [ -  + ]:          4 :         assert_se(streq(t, "foobax6ar"));
     153                 :          4 :         free(t);
     154         [ -  + ]:          4 :         assert_se(isempty(p));
     155                 :            : 
     156                 :          4 :         p = original = "    f\\u00f6o \"pi\\U0001F4A9le\"   ";
     157         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) > 0);
     158         [ -  + ]:          4 :         assert_se(streq(t, "föo"));
     159                 :          4 :         free(t);
     160         [ -  + ]:          4 :         assert_se(p == original + 13);
     161                 :            : 
     162         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE) > 0);
     163         [ -  + ]:          4 :         assert_se(streq(t, "pi\360\237\222\251le"));
     164                 :          4 :         free(t);
     165         [ -  + ]:          4 :         assert_se(isempty(p));
     166                 :            : 
     167                 :          4 :         p = original = "fooo\\";
     168         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
     169         [ -  + ]:          4 :         assert_se(streq(t, "fooo"));
     170                 :          4 :         free(t);
     171         [ -  + ]:          4 :         assert_se(isempty(p));
     172                 :            : 
     173                 :          4 :         p = original = "fooo\\";
     174         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
     175         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\"));
     176                 :          4 :         free(t);
     177         [ -  + ]:          4 :         assert_se(isempty(p));
     178                 :            : 
     179                 :          4 :         p = original = "fooo\\";
     180         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
     181         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\"));
     182                 :          4 :         free(t);
     183         [ -  + ]:          4 :         assert_se(isempty(p));
     184                 :            : 
     185                 :          4 :         p = original = "fooo\\";
     186         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
     187         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\"));
     188                 :          4 :         free(t);
     189         [ -  + ]:          4 :         assert_se(isempty(p));
     190                 :            : 
     191                 :          4 :         p = original = "\"foo\\";
     192         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) == -EINVAL);
     193         [ -  + ]:          4 :         assert_se(p == original + 5);
     194                 :            : 
     195                 :          4 :         p = original = "\"foo\\";
     196         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX) > 0);
     197         [ -  + ]:          4 :         assert_se(streq(t, "foo"));
     198                 :          4 :         free(t);
     199         [ -  + ]:          4 :         assert_se(isempty(p));
     200                 :            : 
     201                 :          4 :         p = original = "foo::bar";
     202         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 1);
     203         [ -  + ]:          4 :         assert_se(streq(t, "foo"));
     204                 :          4 :         free(t);
     205         [ -  + ]:          4 :         assert_se(p == original + 5);
     206                 :            : 
     207         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 1);
     208         [ -  + ]:          4 :         assert_se(streq(t, "bar"));
     209                 :          4 :         free(t);
     210         [ -  + ]:          4 :         assert_se(isempty(p));
     211                 :            : 
     212         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 0);
     213         [ -  + ]:          4 :         assert_se(!t);
     214         [ -  + ]:          4 :         assert_se(isempty(p));
     215                 :            : 
     216                 :          4 :         p = original = "foo\\:bar::waldo";
     217         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 1);
     218         [ -  + ]:          4 :         assert_se(streq(t, "foo:bar"));
     219                 :          4 :         free(t);
     220         [ -  + ]:          4 :         assert_se(p == original + 10);
     221                 :            : 
     222         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 1);
     223         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
     224                 :          4 :         free(t);
     225         [ -  + ]:          4 :         assert_se(isempty(p));
     226                 :            : 
     227         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", 0) == 0);
     228         [ -  + ]:          4 :         assert_se(!t);
     229         [ -  + ]:          4 :         assert_se(isempty(p));
     230                 :            : 
     231                 :          4 :         p = original = "\"foo\\";
     232         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX) == -EINVAL);
     233         [ -  + ]:          4 :         assert_se(p == original + 5);
     234                 :            : 
     235                 :          4 :         p = original = "\"foo\\";
     236         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
     237         [ -  + ]:          4 :         assert_se(streq(t, "foo\\"));
     238                 :          4 :         free(t);
     239         [ -  + ]:          4 :         assert_se(isempty(p));
     240                 :            : 
     241                 :          4 :         p = original = "\"foo\\";
     242         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
     243         [ -  + ]:          4 :         assert_se(streq(t, "foo\\"));
     244                 :          4 :         free(t);
     245         [ -  + ]:          4 :         assert_se(isempty(p));
     246                 :            : 
     247                 :          4 :         p = original = "fooo\\ bar quux";
     248         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RELAX) > 0);
     249         [ -  + ]:          4 :         assert_se(streq(t, "fooo bar"));
     250                 :          4 :         free(t);
     251         [ -  + ]:          4 :         assert_se(p == original + 10);
     252                 :            : 
     253                 :          4 :         p = original = "fooo\\ bar quux";
     254         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX) > 0);
     255         [ -  + ]:          4 :         assert_se(streq(t, "fooo bar"));
     256                 :          4 :         free(t);
     257         [ -  + ]:          4 :         assert_se(p == original + 10);
     258                 :            : 
     259                 :          4 :         p = original = "fooo\\ bar quux";
     260         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE_RELAX|EXTRACT_RELAX) > 0);
     261         [ -  + ]:          4 :         assert_se(streq(t, "fooo bar"));
     262                 :          4 :         free(t);
     263         [ -  + ]:          4 :         assert_se(p == original + 10);
     264                 :            : 
     265                 :          4 :         p = original = "fooo\\ bar quux";
     266         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
     267         [ -  + ]:          4 :         assert_se(p == original + 5);
     268                 :            : 
     269                 :          4 :         p = original = "fooo\\ bar quux";
     270         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
     271         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\ bar"));
     272                 :          4 :         free(t);
     273         [ -  + ]:          4 :         assert_se(p == original + 10);
     274                 :            : 
     275                 :          4 :         p = original = "\\w+@\\K[\\d.]+";
     276         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE) == -EINVAL);
     277         [ -  + ]:          4 :         assert_se(p == original + 1);
     278                 :            : 
     279                 :          4 :         p = original = "\\w+@\\K[\\d.]+";
     280         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
     281         [ -  + ]:          4 :         assert_se(streq(t, "\\w+@\\K[\\d.]+"));
     282                 :          4 :         free(t);
     283         [ -  + ]:          4 :         assert_se(isempty(p));
     284                 :            : 
     285                 :          4 :         p = original = "\\w+\\b";
     286         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_CUNESCAPE|EXTRACT_CUNESCAPE_RELAX) > 0);
     287         [ -  + ]:          4 :         assert_se(streq(t, "\\w+\b"));
     288                 :          4 :         free(t);
     289         [ -  + ]:          4 :         assert_se(isempty(p));
     290                 :            : 
     291                 :          4 :         p = original = "-N ''";
     292         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
     293         [ -  + ]:          4 :         assert_se(streq(t, "-N"));
     294                 :          4 :         free(t);
     295         [ -  + ]:          4 :         assert_se(p == original + 3);
     296                 :            : 
     297         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_UNQUOTE) > 0);
     298         [ -  + ]:          4 :         assert_se(streq(t, ""));
     299                 :          4 :         free(t);
     300         [ -  + ]:          4 :         assert_se(isempty(p));
     301                 :            : 
     302                 :          4 :         p = original = ":foo\\:bar::waldo:";
     303         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
     304         [ -  + ]:          4 :         assert_se(t);
     305         [ -  + ]:          4 :         assert_se(streq(t, ""));
     306                 :          4 :         free(t);
     307         [ -  + ]:          4 :         assert_se(p == original + 1);
     308                 :            : 
     309         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
     310         [ -  + ]:          4 :         assert_se(streq(t, "foo:bar"));
     311                 :          4 :         free(t);
     312         [ -  + ]:          4 :         assert_se(p == original + 10);
     313                 :            : 
     314         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
     315         [ -  + ]:          4 :         assert_se(t);
     316         [ -  + ]:          4 :         assert_se(streq(t, ""));
     317                 :          4 :         free(t);
     318         [ -  + ]:          4 :         assert_se(p == original + 11);
     319                 :            : 
     320         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
     321         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
     322                 :          4 :         free(t);
     323         [ -  + ]:          4 :         assert_se(p == original + 17);
     324                 :            : 
     325         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 1);
     326         [ -  + ]:          4 :         assert_se(streq(t, ""));
     327                 :          4 :         free(t);
     328         [ -  + ]:          4 :         assert_se(p == NULL);
     329                 :            : 
     330         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, ":", EXTRACT_DONT_COALESCE_SEPARATORS) == 0);
     331         [ -  + ]:          4 :         assert_se(!t);
     332         [ -  + ]:          4 :         assert_se(!p);
     333                 :            : 
     334                 :          4 :         p = "foo\\xbar";
     335         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, 0) > 0);
     336         [ -  + ]:          4 :         assert_se(streq(t, "fooxbar"));
     337                 :          4 :         free(t);
     338         [ -  + ]:          4 :         assert_se(p == NULL);
     339                 :            : 
     340                 :          4 :         p = "foo\\xbar";
     341         [ -  + ]:          4 :         assert_se(extract_first_word(&p, &t, NULL, EXTRACT_RETAIN_ESCAPE) > 0);
     342         [ -  + ]:          4 :         assert_se(streq(t, "foo\\xbar"));
     343                 :          4 :         free(t);
     344         [ -  + ]:          4 :         assert_se(p == NULL);
     345                 :          4 : }
     346                 :            : 
     347                 :          4 : static void test_extract_first_word_and_warn(void) {
     348                 :            :         const char *p, *original;
     349                 :            :         char *t;
     350                 :            : 
     351                 :          4 :         p = original = "foobar waldo";
     352         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
     353         [ -  + ]:          4 :         assert_se(streq(t, "foobar"));
     354                 :          4 :         free(t);
     355         [ -  + ]:          4 :         assert_se(p == original + 7);
     356                 :            : 
     357         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
     358         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
     359                 :          4 :         free(t);
     360         [ -  + ]:          4 :         assert_se(isempty(p));
     361                 :            : 
     362         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
     363         [ -  + ]:          4 :         assert_se(!t);
     364         [ -  + ]:          4 :         assert_se(isempty(p));
     365                 :            : 
     366                 :          4 :         p = original = "\"foobar\" \'waldo\'";
     367         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) > 0);
     368         [ -  + ]:          4 :         assert_se(streq(t, "foobar"));
     369                 :          4 :         free(t);
     370         [ -  + ]:          4 :         assert_se(p == original + 9);
     371                 :            : 
     372         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) > 0);
     373         [ -  + ]:          4 :         assert_se(streq(t, "waldo"));
     374                 :          4 :         free(t);
     375         [ -  + ]:          4 :         assert_se(isempty(p));
     376                 :            : 
     377         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) == 0);
     378         [ -  + ]:          4 :         assert_se(!t);
     379         [ -  + ]:          4 :         assert_se(isempty(p));
     380                 :            : 
     381                 :          4 :         p = original = "\"";
     382         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
     383         [ -  + ]:          4 :         assert_se(p == original + 1);
     384                 :            : 
     385                 :          4 :         p = original = "\'";
     386         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
     387         [ -  + ]:          4 :         assert_se(p == original + 1);
     388                 :            : 
     389                 :          4 :         p = original = "\'fooo";
     390         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
     391         [ -  + ]:          4 :         assert_se(p == original + 5);
     392                 :            : 
     393                 :          4 :         p = original = "\'fooo";
     394         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
     395         [ -  + ]:          4 :         assert_se(streq(t, "fooo"));
     396                 :          4 :         free(t);
     397         [ -  + ]:          4 :         assert_se(isempty(p));
     398                 :            : 
     399                 :          4 :         p = original = " foo\\ba\\x6ar ";
     400         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     401         [ -  + ]:          4 :         assert_se(streq(t, "foo\ba\x6ar"));
     402                 :          4 :         free(t);
     403         [ -  + ]:          4 :         assert_se(isempty(p));
     404                 :            : 
     405                 :          4 :         p = original = " foo\\ba\\x6ar ";
     406         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
     407         [ -  + ]:          4 :         assert_se(streq(t, "foobax6ar"));
     408                 :          4 :         free(t);
     409         [ -  + ]:          4 :         assert_se(isempty(p));
     410                 :            : 
     411                 :          4 :         p = original = "    f\\u00f6o \"pi\\U0001F4A9le\"   ";
     412         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     413         [ -  + ]:          4 :         assert_se(streq(t, "föo"));
     414                 :          4 :         free(t);
     415         [ -  + ]:          4 :         assert_se(p == original + 13);
     416                 :            : 
     417         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     418         [ -  + ]:          4 :         assert_se(streq(t, "pi\360\237\222\251le"));
     419                 :          4 :         free(t);
     420         [ -  + ]:          4 :         assert_se(isempty(p));
     421                 :            : 
     422                 :          4 :         p = original = "fooo\\";
     423         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
     424         [ -  + ]:          4 :         assert_se(streq(t, "fooo"));
     425                 :          4 :         free(t);
     426         [ -  + ]:          4 :         assert_se(isempty(p));
     427                 :            : 
     428                 :          4 :         p = original = "fooo\\";
     429         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
     430         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\"));
     431                 :          4 :         free(t);
     432         [ -  + ]:          4 :         assert_se(isempty(p));
     433                 :            : 
     434                 :          4 :         p = original = "fooo\\";
     435         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     436         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\"));
     437                 :          4 :         free(t);
     438         [ -  + ]:          4 :         assert_se(isempty(p));
     439                 :            : 
     440                 :          4 :         p = original = "\"foo\\";
     441         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE, NULL, "fake", 1, original) == -EINVAL);
     442         [ -  + ]:          4 :         assert_se(p == original + 5);
     443                 :            : 
     444                 :          4 :         p = original = "\"foo\\";
     445         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
     446         [ -  + ]:          4 :         assert_se(streq(t, "foo"));
     447                 :          4 :         free(t);
     448         [ -  + ]:          4 :         assert_se(isempty(p));
     449                 :            : 
     450                 :          4 :         p = original = "\"foo\\";
     451         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE, NULL, "fake", 1, original) == -EINVAL);
     452         [ -  + ]:          4 :         assert_se(p == original + 5);
     453                 :            : 
     454                 :          4 :         p = original = "\"foo\\";
     455         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_UNQUOTE|EXTRACT_CUNESCAPE|EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
     456         [ -  + ]:          4 :         assert_se(streq(t, "foo"));
     457                 :          4 :         free(t);
     458         [ -  + ]:          4 :         assert_se(isempty(p));
     459                 :            : 
     460                 :          4 :         p = original = "fooo\\ bar quux";
     461         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_RELAX, NULL, "fake", 1, original) > 0);
     462         [ -  + ]:          4 :         assert_se(streq(t, "fooo bar"));
     463                 :          4 :         free(t);
     464         [ -  + ]:          4 :         assert_se(p == original + 10);
     465                 :            : 
     466                 :          4 :         p = original = "fooo\\ bar quux";
     467         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, 0, NULL, "fake", 1, original) > 0);
     468         [ -  + ]:          4 :         assert_se(streq(t, "fooo bar"));
     469                 :          4 :         free(t);
     470         [ -  + ]:          4 :         assert_se(p == original + 10);
     471                 :            : 
     472                 :          4 :         p = original = "fooo\\ bar quux";
     473         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     474         [ -  + ]:          4 :         assert_se(streq(t, "fooo\\ bar"));
     475                 :          4 :         free(t);
     476         [ -  + ]:          4 :         assert_se(p == original + 10);
     477                 :            : 
     478                 :          4 :         p = original = "\\w+@\\K[\\d.]+";
     479         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     480         [ -  + ]:          4 :         assert_se(streq(t, "\\w+@\\K[\\d.]+"));
     481                 :          4 :         free(t);
     482         [ -  + ]:          4 :         assert_se(isempty(p));
     483                 :            : 
     484                 :          4 :         p = original = "\\w+\\b";
     485         [ -  + ]:          4 :         assert_se(extract_first_word_and_warn(&p, &t, NULL, EXTRACT_CUNESCAPE, NULL, "fake", 1, original) > 0);
     486         [ -  + ]:          4 :         assert_se(streq(t, "\\w+\b"));
     487                 :          4 :         free(t);
     488         [ -  + ]:          4 :         assert_se(isempty(p));
     489                 :          4 : }
     490                 :            : 
     491                 :          4 : static void test_extract_many_words(void) {
     492                 :            :         const char *p, *original;
     493                 :            :         char *a, *b, *c;
     494                 :            : 
     495                 :          4 :         p = original = "foobar waldi piep";
     496         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 3);
     497         [ -  + ]:          4 :         assert_se(isempty(p));
     498         [ -  + ]:          4 :         assert_se(streq_ptr(a, "foobar"));
     499         [ -  + ]:          4 :         assert_se(streq_ptr(b, "waldi"));
     500         [ -  + ]:          4 :         assert_se(streq_ptr(c, "piep"));
     501                 :          4 :         free(a);
     502                 :          4 :         free(b);
     503                 :          4 :         free(c);
     504                 :            : 
     505                 :          4 :         p = original = "'foobar' wa\"ld\"i   ";
     506         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 2);
     507         [ -  + ]:          4 :         assert_se(isempty(p));
     508         [ -  + ]:          4 :         assert_se(streq_ptr(a, "'foobar'"));
     509         [ -  + ]:          4 :         assert_se(streq_ptr(b, "wa\"ld\"i"));
     510         [ -  + ]:          4 :         assert_se(streq_ptr(c, NULL));
     511                 :          4 :         free(a);
     512                 :          4 :         free(b);
     513                 :            : 
     514                 :          4 :         p = original = "'foobar' wa\"ld\"i   ";
     515         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, EXTRACT_UNQUOTE, &a, &b, &c, NULL) == 2);
     516         [ -  + ]:          4 :         assert_se(isempty(p));
     517         [ -  + ]:          4 :         assert_se(streq_ptr(a, "foobar"));
     518         [ -  + ]:          4 :         assert_se(streq_ptr(b, "waldi"));
     519         [ -  + ]:          4 :         assert_se(streq_ptr(c, NULL));
     520                 :          4 :         free(a);
     521                 :          4 :         free(b);
     522                 :            : 
     523                 :          4 :         p = original = "";
     524         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
     525         [ -  + ]:          4 :         assert_se(isempty(p));
     526         [ -  + ]:          4 :         assert_se(streq_ptr(a, NULL));
     527         [ -  + ]:          4 :         assert_se(streq_ptr(b, NULL));
     528         [ -  + ]:          4 :         assert_se(streq_ptr(c, NULL));
     529                 :            : 
     530                 :          4 :         p = original = "  ";
     531         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, &b, &c, NULL) == 0);
     532         [ -  + ]:          4 :         assert_se(isempty(p));
     533         [ -  + ]:          4 :         assert_se(streq_ptr(a, NULL));
     534         [ -  + ]:          4 :         assert_se(streq_ptr(b, NULL));
     535         [ -  + ]:          4 :         assert_se(streq_ptr(c, NULL));
     536                 :            : 
     537                 :          4 :         p = original = "foobar";
     538         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, NULL) == 0);
     539         [ -  + ]:          4 :         assert_se(p == original);
     540                 :            : 
     541                 :          4 :         p = original = "foobar waldi";
     542         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
     543         [ -  + ]:          4 :         assert_se(p == original+7);
     544         [ -  + ]:          4 :         assert_se(streq_ptr(a, "foobar"));
     545                 :          4 :         free(a);
     546                 :            : 
     547                 :          4 :         p = original = "     foobar    ";
     548         [ -  + ]:          4 :         assert_se(extract_many_words(&p, NULL, 0, &a, NULL) == 1);
     549         [ -  + ]:          4 :         assert_se(isempty(p));
     550         [ -  + ]:          4 :         assert_se(streq_ptr(a, "foobar"));
     551                 :          4 :         free(a);
     552                 :          4 : }
     553                 :            : 
     554                 :          4 : int main(int argc, char *argv[]) {
     555                 :          4 :         log_parse_environment();
     556                 :          4 :         log_open();
     557                 :            : 
     558                 :          4 :         test_extract_first_word();
     559                 :          4 :         test_extract_first_word_and_warn();
     560                 :          4 :         test_extract_many_words();
     561                 :            : 
     562                 :          4 :         return 0;
     563                 :            : }

Generated by: LCOV version 1.14