1 /* $NetBSD: get_path.c,v 1.1.1.3 2019/12/22 12:34:07 skrll Exp $ */ 2 3 // SPDX-License-Identifier: LGPL-2.1-or-later 4 /* 5 * libfdt - Flat Device Tree manipulation 6 * Testcase for fdt_get_path() 7 * Copyright (C) 2006 David Gibson, IBM Corporation. 8 */ 9 #include <stdlib.h> 10 #include <stdio.h> 11 #include <string.h> 12 #include <stdint.h> 13 14 #include <libfdt.h> 15 16 #include "tests.h" 17 #include "testdata.h" 18 19 #define POISON ('\xff') 20 21 static void check_path_buf(void *fdt, const char *path, int pathlen, int buflen) 22 { 23 int offset; 24 char buf[buflen+1]; 25 int len; 26 27 offset = fdt_path_offset(fdt, path); 28 if (offset < 0) 29 FAIL("Couldn't find path \"%s\": %s", path, fdt_strerror(offset)); 30 31 memset(buf, POISON, sizeof(buf)); /* poison the buffer */ 32 33 len = fdt_get_path(fdt, offset, buf, buflen); 34 verbose_printf("get_path() %s -> %d -> %s\n", path, offset, 35 len >= 0 ? buf : "<error>"); 36 37 if (buflen <= pathlen) { 38 if (len != -FDT_ERR_NOSPACE) 39 FAIL("fdt_get_path([%d bytes]) returns %d with " 40 "insufficient buffer space", buflen, len); 41 } else { 42 if (len < 0) 43 FAIL("fdt_get_path([%d bytes]): %s", buflen, 44 fdt_strerror(len)); 45 if (len != 0) 46 FAIL("fdt_get_path([%d bytes]) returns %d " 47 "instead of 0", buflen, len); 48 if (strcmp(buf, path) != 0) 49 FAIL("fdt_get_path([%d bytes]) returns \"%s\" " 50 "instead of \"%s\"", buflen, buf, path); 51 } 52 53 if (buf[buflen] != POISON) 54 FAIL("fdt_get_path([%d bytes]) overran buffer", buflen); 55 } 56 57 static void check_path(void *fdt, const char *path) 58 { 59 int pathlen = strlen(path); 60 61 check_path_buf(fdt, path, pathlen, 1024); 62 check_path_buf(fdt, path, pathlen, pathlen+1); 63 check_path_buf(fdt, path, pathlen, pathlen); 64 check_path_buf(fdt, path, pathlen, 0); 65 check_path_buf(fdt, path, pathlen, 2); 66 } 67 68 int main(int argc, char *argv[]) 69 { 70 void *fdt; 71 72 test_init(argc, argv); 73 fdt = load_blob_arg(argc, argv); 74 75 check_path(fdt, "/"); 76 check_path(fdt, "/subnode@1"); 77 check_path(fdt, "/subnode@2"); 78 check_path(fdt, "/subnode@1/subsubnode"); 79 check_path(fdt, "/subnode@2/subsubnode@0"); 80 81 PASS(); 82 } 83