]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - nss/tests/pw.c
Update copyright years
[nsscash/nsscash.git] / nss / tests / pw.c
index 4a32bfda1e905fd42a61ca851dd34f28de05c09a..88260c45aace3064d8afc9dd14cdaa22750cd18e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Tests for the NSS cash module
  *
- * Copyright (C) 2019  Simon Ruderich
+ * Copyright (C) 2019-2020  Simon Ruderich
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  */
 
 #include <assert.h>
+#include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
 #include <string.h>
-#include <limits.h>
+#include <unistd.h>
 
 #include "../cash_nss.h"
 
@@ -245,7 +246,7 @@ static void test_getpwnam(void) {
     assert(s == NSS_STATUS_TRYAGAIN);
     assert(errnop == ERANGE);
     s = _nss_cash_getpwnam_r("nope", &p, tmp_small, sizeof(tmp_small), &errnop);
-    assert(s == NSS_STATUS_NOTFOUND); // 42 does not exist
+    assert(s == NSS_STATUS_NOTFOUND); // does not exist
     assert(errnop == ENOENT);
     s = _nss_cash_getpwnam_r("nobody", &p, tmp_small, sizeof(tmp_small), &errnop);
     assert(s == NSS_STATUS_TRYAGAIN);
@@ -312,10 +313,89 @@ static void test_getpwnam(void) {
     assert(rename("tests/passwd.nsscash.tmp", "tests/passwd.nsscash") == 0);
 }
 
+static void test_limits(void) {
+    char gecos[65508];
+    memset(gecos, 'X', sizeof(gecos));
+    gecos[sizeof(gecos)-1] = '\0';
+
+    int r;
+    FILE *fh;
+
+    const char *nsscash_cmd = "../nsscash convert passwd "
+        "tests/limits tests/limits.nsscash 2> /dev/null";
+
+    // Entries which will not fit in uint16_t, nsscash must abort
+
+    fh = fopen("tests/limits", "w");
+    assert(fh != NULL);
+    r = fprintf(fh, "test:xx:42:4711:%s:/home/test:/bin/zsh\n", gecos);
+    assert(r == 65544);
+    r = fclose(fh);
+    assert(r == 0);
+
+    r = system(nsscash_cmd);
+    assert(r != -1);
+    assert(WIFEXITED(r) && WEXITSTATUS(r) == 1);
+
+    fh = fopen("tests/limits", "w");
+    assert(fh != NULL);
+    r = fprintf(fh, "test:%s:42:4711:%s:/home/test:/bin/zsh\n", gecos, gecos);
+    assert(r == 131049);
+    r = fclose(fh);
+    assert(r == 0);
+
+    r = system(nsscash_cmd);
+    assert(r != -1);
+    assert(WIFEXITED(r) && WEXITSTATUS(r) == 1);
+
+    // Largest entry which will fit
+
+    fh = fopen("tests/limits", "w");
+    assert(fh != NULL);
+    r = fprintf(fh, "test:x:42:4711:%s:/home/test:/bin/zsh\n", gecos);
+    assert(r == 65543);
+    r = fclose(fh);
+    assert(r == 0);
+
+    r = system(nsscash_cmd);
+    assert(r != -1);
+    assert(WIFEXITED(r) && WEXITSTATUS(r) == 0);
+
+    r = rename("tests/passwd.nsscash", "tests/passwd.nsscash.tmp");
+    assert(r == 0);
+    r = rename("tests/limits.nsscash", "tests/passwd.nsscash");
+    assert(r == 0);
+
+    // Check if the entry can be retrieved
+
+    struct passwd p;
+    enum nss_status s;
+    char tmp[4+1 + 1+1 + 65508 + 10+1 + 8+1];
+    int errnop = 0;
+
+    s = _nss_cash_getpwuid_r(42, &p, tmp, sizeof(tmp), &errnop);
+    assert(s == NSS_STATUS_SUCCESS);
+    assert(!strcmp(p.pw_name, "test"));
+    assert(!strcmp(p.pw_passwd, "x"));
+    assert(p.pw_uid == 42);
+    assert(p.pw_gid == 4711);
+    assert(!strcmp(p.pw_gecos, gecos));
+    assert(!strcmp(p.pw_dir, "/home/test"));
+    assert(!strcmp(p.pw_shell, "/bin/zsh"));
+
+    r = rename("tests/passwd.nsscash.tmp", "tests/passwd.nsscash");
+    assert(r == 0);
+
+    r = unlink("tests/limits");
+    assert(r == 0);
+}
+
 int main(void) {
     test_getpwent();
     test_getpwuid();
     test_getpwnam();
 
+    test_limits();
+
     return EXIT_SUCCESS;
 }