TEST(URITest, ParseFailed) { // Expect ':' in URI. EXPECT_ERROR(URI::parse("file//x/y/z")); // Empty. EXPECT_ERROR(URI::parse("")); EXPECT_ERROR(URI::parse(":/a/b/c")); EXPECT_ERROR(URI::parse("\"/a/b/c\" IWYU pragma: abc")); }
TEST(DurationTest, ParseAndTry) { EXPECT_SOME_EQ(Hours(3), Duration::parse("3hrs")); EXPECT_SOME_EQ(Hours(3) + Minutes(30), Duration::parse("3.5hrs")); EXPECT_SOME_EQ(Nanoseconds(3141592653), Duration::create(3.141592653)); // Duration can hold only 9.22337e9 seconds. EXPECT_ERROR(Duration::create(10 * 1e9)); EXPECT_ERROR(Duration::create(-10 * 1e9)); }
TEST(NumifyTest, DecNumberTest) { Try<unsigned int> num1 = numify<unsigned int>("10"); EXPECT_SOME_EQ(10u, num1); Try<int> num2 = numify<int>("-10"); EXPECT_SOME_EQ(-10, num2); EXPECT_ERROR(numify<unsigned int>("")); EXPECT_ERROR(numify<int>("-10.")); EXPECT_ERROR(numify<unsigned int>("123xyz")); }
void Project2Test::test_MGFramework_createMO() { // Setup MGFrameworkStub mgf; ASSERT_EQ(mgf._getNumberOfMO(), 0, "MGF created with MO"); EXPECT_ERROR("MGFramework::addMO cannot create 1 MO on a 0 by 0 map"); mgf._addMO(1); ASSERT_EQ(mgf._getNumberOfMO(), 0, "MGF created MOs without initializing the map"); mgf.init(16, 16, 32, 32); // Trigger/Verify mgf._addMO(0); ASSERT_EQ(mgf._getNumberOfMO(), 0, "MGF failed to create zero MO"); mgf._addMO(1); ASSERT_EQ(mgf._getNumberOfMO(), 1, "MGF failed to create MO"); mgf._addMO(3); ASSERT_EQ(mgf._getNumberOfMO(), 4, "MGF failed to create MO"); mgf._addMO(1); ASSERT_EQ(mgf._getNumberOfMO(), 5, "MGF failed to create MO"); mgf._addMO(15); ASSERT_EQ(mgf._getNumberOfMO(), 20, "MGF failed to create MO"); mgf._addMO(0); ASSERT_EQ(mgf._getNumberOfMO(), 20, "MGF failed to create zero MO"); }
TEST(BytesTest, Parse) { EXPECT_SOME_EQ(Terabytes(1), Bytes::parse("1TB")); EXPECT_SOME_EQ(Gigabytes(1), Bytes::parse("1GB")); EXPECT_SOME_EQ(Megabytes(1), Bytes::parse("1MB")); EXPECT_SOME_EQ(Kilobytes(1), Bytes::parse("1KB")); EXPECT_SOME_EQ(Bytes(1), Bytes::parse("1B")); // Cannot have fractional bytes. EXPECT_ERROR(Bytes::parse("1.5B")); // Parsing fractions is unsupported. EXPECT_ERROR(Bytes::parse("1.5GB")); // Unknown unit. EXPECT_ERROR(Bytes::parse("1PB")); }
/* * symlink loop error test */ void symlink_loop_error_test(void ) { char* file01="file01"; char* file02="file02"; char* file04="file04"; char* path="file01/t"; int status; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; puts("symlink loop erro test"); status=symlink(file01,file02); rtems_test_assert(status==0); status=symlink(file02,file01); rtems_test_assert(status==0); EXPECT_ERROR(ELOOP,creat,path,mode); EXPECT_ERROR(ELOOP,open,path,O_CREAT|O_WRONLY,mode); EXPECT_ERROR(ELOOP,truncate,path,0); EXPECT_ERROR(ELOOP,rename,path,file04); EXPECT_ERROR(ELOOP,unlink,path); EXPECT_ERROR(ELOOP,mkdir,path,mode); EXPECT_ERROR(ELOOP,rmdir,path); }
// Test varioud hook install/uninstall mechanisms. TEST_F(HookTest, HookLoading) { // Installing unknown hooks should fail. EXPECT_ERROR(HookManager::initialize("Unknown Hook")); // Uninstalling an unknown hook should fail. EXPECT_ERROR(HookManager::unload("Unknown Hook")); // Installing an already installed hook should fail. EXPECT_ERROR(HookManager::initialize(HOOK_MODULE_NAME)); // Uninstalling a hook should succeed. EXPECT_SOME(HookManager::unload(HOOK_MODULE_NAME)); // Uninstalling an already uninstalled hook should fail. EXPECT_ERROR(HookManager::unload(HOOK_MODULE_NAME)); // This is needed to allow the tear-down to succeed. EXPECT_SOME(HookManager::initialize(HOOK_MODULE_NAME)); }
TEST(NumifyTest, HexNumberTest) { Try<unsigned int> num1 = numify<unsigned int>("0xdeadbeef"); EXPECT_SOME_EQ(0xdeadbeefu, num1); Try<unsigned int> num2 = numify<unsigned int>("0x10"); EXPECT_SOME_EQ(16u, num2); // TODO(neilc): This is inconsistent with the handling of non-hex numbers. EXPECT_ERROR(numify<int>("-0x10")); EXPECT_ERROR(numify<unsigned int>("")); EXPECT_ERROR(numify<unsigned int>("0xxyz")); EXPECT_ERROR(numify<unsigned int>("abc")); EXPECT_ERROR(numify<unsigned int>("0x0x1")); EXPECT_ERROR(numify<double>("0x10.9")); EXPECT_ERROR(numify<double>("0x1p-5")); }
TEST_F(AppcSpecTest, ValidateImageManifest) { JSON::Value manifest = JSON::parse( "{" " \"acKind\": \"ImageManifest\"," " \"acVersion\": \"0.6.1\"," " \"name\": \"foo.com/bar\"," " \"labels\": [" " {" " \"name\": \"version\"," " \"value\": \"1.0.0\"" " }," " {" " \"name\": \"arch\"," " \"value\": \"amd64\"" " }," " {" " \"name\": \"os\"," " \"value\": \"linux\"" " }" " ]," " \"annotations\": [" " {" " \"name\": \"created\"," " \"value\": \"1438983392\"" " }" " ]" "}").get(); EXPECT_SOME(spec::parse(stringify(manifest))); // Incorrect acKind for image manifest. manifest = JSON::parse( "{" " \"acKind\": \"PodManifest\"," " \"acVersion\": \"0.6.1\"," " \"name\": \"foo.com/bar\"" "}").get(); EXPECT_ERROR(spec::parse(stringify(manifest))); }
// Verify version parser. TEST(VersionTest, Parse) { Try<Version> version1 = Version::parse("1.20.3"); Try<Version> version2 = Version::parse("1.20"); Try<Version> version3 = Version::parse("1"); EXPECT_GT(version1.get(), version2.get()); EXPECT_GT(version2.get(), version3.get()); EXPECT_EQ(stringify(version2.get()), "1.20.0"); EXPECT_EQ(stringify(version3.get()), "1.0.0"); // Verify that tagged/labeled versions work. Try<Version> version4 = Version::parse("1.20.3-rc1"); EXPECT_SOME(version4); EXPECT_EQ(version4.get(), version1.get()); EXPECT_EQ(stringify(version4.get()), "1.20.3"); EXPECT_ERROR(Version::parse("0.a.b")); EXPECT_ERROR(Version::parse("")); EXPECT_ERROR(Version::parse("a")); EXPECT_ERROR(Version::parse("1.")); EXPECT_ERROR(Version::parse(".1.2")); EXPECT_ERROR(Version::parse("0.1.2.3")); }
static void write_permission_test (void) { int fd; int status; int rv; const char *name01 = "name01"; const char *name02 = "name02"; const char *dir01 = "dir01"; const char *dir02 = "dir02"; char path01[20]; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; mode_t no_write_access = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; const char *wd = __func__; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a file and * the old argument points to another file, * both inside a directory with no write permission. */ puts ("\nRename two files on a directory with no write permission \n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); status = chdir (dir01); rtems_test_assert (status == 0); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); fd = creat (name02, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = chmod (".", no_write_access); rtems_test_assert (status == 0); EXPECT_ERROR (EACCES, rename, name01 , name02); status = chdir (".."); rtems_test_assert (status == 0); /* * The new argument points to a file in a directory with no write access and * the old argument points to another file on a directory with write access. */ puts ("\nRename file between two directories, with and without write access\n"); status = mkdir (dir02, mode); rtems_test_assert (status == 0); status = chdir (dir02); rtems_test_assert (status == 0); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "../%s/%s", dir01, name02); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (EACCES, rename, name01, path01); /* * The new argument points to a file in a directory with write access and * the old argument points to another file on a directory without write access. */ EXPECT_ERROR (EACCES, rename, path01, name01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); rv = snprintf (path01, sizeof(path01), "../%s", dir01); rtems_test_assert (rv < sizeof(path01)); status = chmod (path01, mode); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "../%s/%s", dir01, name01); rtems_test_assert (rv < sizeof(path01)); EXPECT_EQUAL (0, unlink, path01); rv = snprintf (path01, sizeof(path01), "../%s/%s", dir01, name02); rtems_test_assert (rv < sizeof(path01)); EXPECT_EQUAL (0, unlink, path01); status = chdir (".."); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rmdir, dir01); EXPECT_EQUAL (0, rmdir, dir02); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
static void arg_format_test (void) { int fd; int status; const char *name01 = "name01"; const char *dir01 = "dir01"; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *wd = __func__; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a directory and * the old argument points to current directory. */ puts ("\nRename directory with current directory\n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); EXPECT_EQUAL (-1, rename, "." , dir01); puts("Testing errno for EINVAL or EBUSY"); if(errno == EINVAL || errno == EBUSY) FS_PASS (); else FS_FAIL (); /* * The new argument points to current directory and * the old argument points to a directory. */ EXPECT_EQUAL (-1, rename, dir01, "."); puts("Testing errno for EINVAL or EBUSY"); if(errno == EINVAL || errno == EBUSY) FS_PASS (); else FS_FAIL (); /* * The new argument points to a directory and * the old argument points to previous directory. */ puts ("\nRename directory with previous directory\n"); EXPECT_EQUAL (-1, rename, ".." , dir01); puts("Testing errno for EINVAL or EBUSY"); if(errno == EINVAL || errno == EBUSY) FS_PASS (); else FS_FAIL (); /* * The new argument points to previous directory and * the old argument points to a directory. */ EXPECT_EQUAL (-1, rename, dir01, ".."); puts("Testing errno for EINVAL or EBUSY"); if(errno == EINVAL || errno == EBUSY) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to a file and * the old argument is an empty string. */ puts("\nTesting empty filepaths\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); EXPECT_ERROR (ENOENT, rename, name01, ""); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); /* * The new argument is an empty string and * the old argument points to a file. */ fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); EXPECT_ERROR (ENOENT, rename, "", name01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
static void arg_test (void) { int fd; int status; int rv; const char *name01 = "name01"; const char *name02 = "name02"; const char *dir01 = "dir01"; const char *dir02 = "dir02"; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *wd = __func__; char filename[NAME_MAX + 2]; char path01[20]; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a non existant file and * the old argument points to a file. */ puts ("\nRename file with non existant file\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rename, name01, name02); /* * Clear directory */ EXPECT_EQUAL (-1, unlink, name01); EXPECT_EQUAL (0, unlink, name02); /* * The new argument points to a file and * the old argument points to a non existant file. */ fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); EXPECT_ERROR (ENOENT, rename, name02, name01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (-1, unlink, name02); /* * The new argument points to a non existant file and * the old argument points to a file where a component of the * filepath does not exist. */ puts ("\nRename file with non existant filepath\n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s/%s", dir01, name01, name02); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (ENOENT, rename, path01, name01); /* * Clear directory */ EXPECT_EQUAL (-1, unlink, name01); EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to a non existant directory and * the old argument points to a directory. */ puts ("\nRename directory with non existant directory\n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rename, dir01, dir02); /* * Clear directory */ EXPECT_EQUAL (-1, rmdir, dir01); EXPECT_EQUAL (0, rmdir, dir02); /* * The new argument is a name bigger than NAME_MAX and * the old argument points to a file. */ puts ("\nRename file with a name size exceeding NAME_MAX\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); /* Generate string with NAME_MAX + 1 length */ memset(filename, 'a', NAME_MAX + 1); filename[NAME_MAX + 1] = '\0'; EXPECT_ERROR (ENAMETOOLONG, rename, name01, filename); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (-1, unlink, filename); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
static void directory_test (void) { int fd; int status; int rv; int i; const char *name01 = "name01"; const char *name02 = "name02"; const char *dir01 = "dir01"; const char *dir02 = "dir02"; char path01[30]; char link_name[10]; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *wd = __func__; struct stat statbuf; long LINK_MAX_val; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a file and * the old argument points to a directory. */ puts ("\nRename directory with file\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = mkdir (dir01, mode); rtems_test_assert (status == 0); EXPECT_ERROR (ENOTDIR, rename, dir01, name01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to a directory and * the old argument points to a file. */ puts ("\nRename file with directory\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = mkdir (dir01, mode); rtems_test_assert (status == 0); EXPECT_ERROR (EISDIR, rename, name01, dir01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to an empty directory and * the old argument points to an ancestor directory of new. */ puts ("\nRename directory with ancestor directory\n"); status = mkdir (dir02, mode); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", dir02, dir01); rtems_test_assert (rv < sizeof(path01)); status = mkdir (path01, mode); rtems_test_assert (status == 0); EXPECT_ERROR (EINVAL, rename, dir02, path01); /* * Clear directory */ EXPECT_EQUAL (0, rmdir, path01); EXPECT_EQUAL (0, rmdir, dir02); /* * The new argument points to an empty directory and * the old argument points to a non empty directory. */ puts ("\nRename directory with non empty directory\n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); status = mkdir (dir02, mode); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", dir02, name02); rtems_test_assert (rv < sizeof(path01)); fd = creat (path01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); EXPECT_EQUAL (-1, rename, dir01, dir02); puts("Testing errno for EEXIST or ENOTEMPTY"); if(errno == EEXIST || errno == ENOTEMPTY) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, unlink, path01); EXPECT_EQUAL (0, rmdir, dir01); EXPECT_EQUAL (0, rmdir, dir02); /* * The new argument points to an empty directory and * the old argument points to other empty directory. */ puts ("\nRename empty directory with another empty directory\n"); status = mkdir (dir01, mode); rtems_test_assert (status == 0); status = mkdir (dir02, mode); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rename, dir01, dir02); /* * Clear directory */ EXPECT_EQUAL (-1, rmdir, dir01); EXPECT_EQUAL (0, rmdir, dir02); /* * The new argument points to a non existant directory and * the old argument points to an existant directory at LINK_MAX. */ status = mkdir (dir01, mode); rtems_test_assert (status == 0); LINK_MAX_val = pathconf (dir01, _PC_LINK_MAX); rtems_test_assert (LINK_MAX_val >= 0); status = stat (dir01, &statbuf); rtems_test_assert (status == 0); for(i = statbuf.st_nlink; i < LINK_MAX_val; i++) { rv = snprintf (link_name, sizeof(link_name), "%s/%d", dir01, i); rtems_test_assert (rv < sizeof(link_name)); status = mkdir (link_name, mode); rtems_test_assert (status == 0); } status = mkdir (dir02, mode); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", dir01, dir01); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (EMLINK, rename, dir02, path01); /* * Clear directory */ for(i = statbuf.st_nlink; i < LINK_MAX_val; i++) { rv = snprintf (link_name, sizeof(link_name), "%s/%d", dir01, i); rtems_test_assert (rv < sizeof(link_name)); status = rmdir (link_name); rtems_test_assert (status == 0); } EXPECT_EQUAL (-1, rmdir, path01); EXPECT_EQUAL (0, rmdir, dir02); EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to a file and * the old argument points to another file on a directory with S_ISVTX. */ puts ("\nRename files within directories protected with S_ISVTX\n"); status = mkdir (dir01, mode | S_ISVTX); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", dir01, name01); rtems_test_assert (rv < sizeof(path01)); fd = creat (path01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); fd = creat (name02, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = chown (path01, 65534, -1); rtems_test_assert (status == 0); status = chown (dir01, 65534, -1); rtems_test_assert (status == 0); EXPECT_EQUAL (-1, rename, path01, name02); puts("Testing errno for EPERM or EACCES"); if(errno == EPERM || errno == EACCES) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, unlink, path01); EXPECT_EQUAL (0, unlink, name02); EXPECT_EQUAL (0, rmdir, dir01); /* * The new argument points to a file on a directory with S_ISVTX and * the old argument points to a file outside that directory. */ status = mkdir (dir01, mode | S_ISVTX); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", dir01, name01); rtems_test_assert (rv < sizeof(path01)); fd = creat (path01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); fd = creat (name02, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = chown (path01, 65534, -1); rtems_test_assert (status == 0); status = chown (dir01, 65534, -1); rtems_test_assert (status == 0); EXPECT_EQUAL (-1, rename, name02, path01); puts("Testing errno for EPERM or EACCES"); if(errno == EPERM || errno == EACCES) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, unlink, path01); EXPECT_EQUAL (0, unlink, name02); EXPECT_EQUAL (0, rmdir, dir01); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
static void symbolic_link_test (void) { int fd; int status; int rv; const char *name01 = "name01"; const char *name02 = "name02"; const char *symlink01 = "slink01"; const char *symlink02 = "slink02"; char path01[20]; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *wd = __func__; struct stat statbuf; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a file and * the old argument points to a symbolic link to another file. */ puts ("\nOld is a simbolic link and rename operates on the simbolic link itself\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); fd = creat (name02, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = symlink (name01, symlink01); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rename, symlink01, name02); EXPECT_EQUAL (0, lstat, name02, &statbuf); puts ("Testing if name02 is now a symlink"); if(S_ISLNK(statbuf.st_mode) != 0) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (0, unlink, name02); EXPECT_EQUAL (-1, unlink, symlink01); /* * The new argument points to a symbolic link to another file and * the old argument points to a file. */ puts ("\nNew is a simbolic link and rename operates on the simbolic link itself\n"); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); fd = creat (name02, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = symlink (name01, symlink01); rtems_test_assert (status == 0); EXPECT_EQUAL (0, rename, name02, symlink01); EXPECT_EQUAL (0, lstat, symlink01, &statbuf); puts ("Testing that symlink01 is not a symlink"); if(S_ISLNK(statbuf.st_mode) == 0) FS_PASS (); else FS_FAIL (); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (-1, unlink, name02); EXPECT_EQUAL (0, unlink, symlink01); /* * The new argument points to a file inside a directory and * the old argument points to a file which filepath contains * a symbolic link loop. */ puts ("\nTesting with symbolic link loop's\n"); status = symlink (symlink01, symlink02); rtems_test_assert (status == 0); status = symlink (symlink02, symlink01); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/test", symlink01); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (ELOOP, rename, path01, name01); rv = snprintf (path01, sizeof(path01), "%s/test", symlink02); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (ELOOP, rename, path01, name01); /* * Clear directory */ EXPECT_EQUAL (-1, unlink, name01); EXPECT_EQUAL (0, unlink, symlink01); EXPECT_EQUAL (0, unlink, symlink02); /* * The new argument points to a file, which filepath contains * a symbolic link loop, and * the old argument points to a file inside a directory */ fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); status = symlink (symlink01, symlink02); rtems_test_assert (status == 0); status = symlink (symlink02, symlink01); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/test", symlink01); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (ELOOP, rename, name01, path01); rv = snprintf (path01, sizeof(path01), "%s/test", symlink02); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (ELOOP, rename, name01, path01); /* * Clear directory */ EXPECT_EQUAL (0, unlink, name01); EXPECT_EQUAL (0, unlink, symlink01); EXPECT_EQUAL (0, unlink, symlink02); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
static void filesystem_test (void) { int fd; int status; int rv; const char *name01 = "name01"; const char *name02 = "name02"; char path01[20]; mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; const char *wd = __func__; /* * Create a new directory and change the current directory to this */ status = mkdir (wd, mode); rtems_test_assert (status == 0); status = chdir (wd); rtems_test_assert (status == 0); /* * The new argument points to a file on another instance of the filesystem and * the old argument points to a file on the base filesystem. */ puts ("\nRename files across diferent filesystems\n"); rv = chroot ("/"); rtems_test_assert (rv == 0); fd = creat (name01, mode); rtems_test_assert (fd >= 0); status = close (fd); rtems_test_assert (status == 0); rv = snprintf (path01, sizeof(path01), "%s/%s", BASE_FOR_TEST, name02); rtems_test_assert (rv < sizeof(path01)); EXPECT_ERROR (EXDEV, rename, name01, path01); EXPECT_EQUAL (-1, unlink, path01); EXPECT_EQUAL (0, unlink, name01); rv = chroot (BASE_FOR_TEST); rtems_test_assert (rv == 0); /* * Go back to parent directory */ status = chdir (".."); rtems_test_assert (status == 0); /* * Remove test directory */ status = rmdir (wd); rtems_test_assert (status == 0); }
/* * Check the file mode in file and directory */ static void test_premission01(void ) { mode_t tmp_mode; struct stat statbuf; int status = 0; int fd; char* file01="file01"; char* file02="file02"; char* directory01="dir01"; char path[20]; char* test_data="Test Data"; char* data_buf; size_t len=strlen(test_data); int n; DIR *dp; const char* wd=__func__; mode_t mode=S_IRWXU|S_IRWXG|S_IRWXO ; uid_t user_id =65534; gid_t group_id =65534; uid_t another_user_id =65533; gid_t another_group_id =65533; /* *Create a new directory and change the current directory to this */ umask(00); status=mkdir(wd,mode); rtems_test_assert(status==0); status=chdir(wd); rtems_test_assert(status==0); status=seteuid(user_id); rtems_test_assert(status==0); status=setegid(group_id); rtems_test_assert(status==0); /* *Create a file with mode 0777 */ fd=creat(file01,mode); status=close(fd); rtems_test_assert(status==0); /* *Create a file with mode 0240 */ fd=creat(file02,0240); status=close(fd); rtems_test_assert(status==0); /* *Check the file mode uid and gid */ status = stat (file01, &statbuf); rtems_test_assert (status == 0); tmp_mode = (statbuf.st_mode) & ALLPERMS; printf("The file mode of %s is %03o\n",file01,(unsigned int)tmp_mode); rtems_test_assert(tmp_mode==mode); rtems_test_assert(statbuf.st_uid==user_id); rtems_test_assert(statbuf.st_gid==group_id); status = stat (file02, &statbuf); rtems_test_assert (status == 0); tmp_mode = (statbuf.st_mode) & ALLPERMS; printf("The file mode of %s is %03o\n",file02,(unsigned int)tmp_mode); rtems_test_assert(tmp_mode==0240); rtems_test_assert(statbuf.st_uid==user_id); rtems_test_assert(statbuf.st_gid==group_id); /* * Create directory and a file in it for tese */ status=mkdir(directory01,0777); rtems_test_assert(status==0); sprintf(path,"%s/%s",directory01,file01); fd=creat(path,0777); status=chmod(directory01,0340); rtems_test_assert (status == 0); status = stat (directory01, &statbuf); rtems_test_assert (status == 0); tmp_mode = (statbuf.st_mode) & ALLPERMS; printf("The file mode of %s is %03o\n",directory01,(unsigned int)tmp_mode); rtems_test_assert(tmp_mode==0340); rtems_test_assert(statbuf.st_uid==user_id); rtems_test_assert(statbuf.st_gid==group_id); /* * Check the file with open and write */ /* * Test write */ fd=open(file01,O_WRONLY); n=write(fd,test_data,len); rtems_test_assert(n==len); status=close(fd); rtems_test_assert(status==0); fd=open(file02,O_WRONLY); n=write(fd,test_data,len); rtems_test_assert(n==len); status=close(fd); rtems_test_assert(status==0); /* * Test read */ data_buf=(char*)malloc(len+1); fd=open(file01,O_RDWR); rtems_test_assert(fd!=-1); n=read(fd,data_buf,len); rtems_test_assert(n==len); status=close(fd); rtems_test_assert(status==0); EXPECT_ERROR(EACCES,open,file02,O_RDONLY); EXPECT_ERROR(EACCES,open,file02,O_RDWR); /* * Test read directory */ dp= opendir(directory01); rtems_test_assert(dp==NULL); rtems_test_assert(errno==EACCES); /* * Test write directory */ status = lstat (path, &statbuf); rtems_test_assert (status == 0); status=unlink(path); rtems_test_assert(status==0); /* * Change euid and check */ puts("Change euid and check"); status=seteuid(0); rtems_test_assert(status==0); status=seteuid(another_user_id); rtems_test_assert(status==0); fd=open(file01,O_WRONLY); n=write(fd,test_data,len); rtems_test_assert(n==len); status=close(fd); rtems_test_assert(status==0); EXPECT_ERROR(EACCES,open,file02,O_WRONLY); EXPECT_ERROR(EACCES,open,file02,O_RDWR); /* * Test read directory */ dp= opendir(directory01); rtems_test_assert(dp!=NULL); status=closedir(dp); rtems_test_assert(status==0); /* * Test write directory */ EXPECT_ERROR(EACCES,creat,path,mode); EXPECT_ERROR(EACCES,rename,path,"test"); EXPECT_ERROR(EACCES,truncate,path,0); EXPECT_ERROR(EACCES,link,path,"test"); EXPECT_ERROR(EACCES,unlink,path); /* * Change egid and check */ puts("Change egid and check"); status=seteuid(0); rtems_test_assert(status==0); status=setegid(another_group_id); rtems_test_assert(status==0); status=seteuid(another_user_id); rtems_test_assert(status==0); EXPECT_ERROR(EACCES,open,file02,O_WRONLY); EXPECT_ERROR(EACCES,open,file02,O_RDONLY); EXPECT_ERROR(EACCES,open,file02,O_RDWR); /* * Test read directory */ dp= opendir(directory01); rtems_test_assert(dp==NULL); rtems_test_assert(errno==EACCES); /* * Test write directory */ EXPECT_ERROR(EACCES,creat,path,mode); /* * Go back to parent directory */ status=seteuid(0); rtems_test_assert(status==0); status=setegid(0); rtems_test_assert(status==0); free(data_buf); status=chdir(".."); rtems_test_assert(status==0); }
TEST_F(PerfTest, Parse) { // uint64 and floats should be parsed. Try<hashmap<string, mesos::PerfStatistics> > parse = perf::parse("123,cycles\n0.123,task-clock"); CHECK_SOME(parse); ASSERT_TRUE(parse.get().contains("")); mesos::PerfStatistics statistics = parse.get().get("").get(); ASSERT_TRUE(statistics.has_cycles()); EXPECT_EQ(123u, statistics.cycles()); ASSERT_TRUE(statistics.has_task_clock()); EXPECT_EQ(0.123, statistics.task_clock()); // Parse multiple cgroups. parse = perf::parse("123,cycles,cgroup1\n" "456,cycles,cgroup2\n" "0.456,task-clock,cgroup2\n" "0.123,task-clock,cgroup1"); CHECK_SOME(parse); EXPECT_FALSE(parse.get().contains("")); ASSERT_TRUE(parse.get().contains("cgroup1")); statistics = parse.get().get("cgroup1").get(); ASSERT_TRUE(statistics.has_cycles()); EXPECT_EQ(123u, statistics.cycles()); ASSERT_TRUE(statistics.has_task_clock()); EXPECT_EQ(0.123, statistics.task_clock()); ASSERT_TRUE(parse.get().contains("cgroup2")); statistics = parse.get().get("cgroup2").get(); ASSERT_TRUE(statistics.has_cycles()); EXPECT_EQ(456u, statistics.cycles()); EXPECT_TRUE(statistics.has_task_clock()); EXPECT_EQ(0.456, statistics.task_clock()); // Statistics reporting <not supported> should not appear. parse = perf::parse("<not supported>,cycles"); CHECK_SOME(parse); ASSERT_TRUE(parse.get().contains("")); statistics = parse.get().get("").get(); EXPECT_FALSE(statistics.has_cycles()); // Statistics reporting <not counted> should be zero. parse = perf::parse("<not counted>,cycles\n<not counted>,task-clock"); CHECK_SOME(parse); ASSERT_TRUE(parse.get().contains("")); statistics = parse.get().get("").get(); EXPECT_TRUE(statistics.has_cycles()); EXPECT_EQ(0u, statistics.cycles()); EXPECT_TRUE(statistics.has_task_clock()); EXPECT_EQ(0.0, statistics.task_clock()); // Check parsing fails. parse = perf::parse("1,cycles\ngarbage"); EXPECT_ERROR(parse); parse = perf::parse("1,unknown-field"); EXPECT_ERROR(parse); }
TEST(URITest, FailedCreate) { EXPECT_ERROR(URI::create("/x/y/z", "no")); // Path has to be absolute. EXPECT_ERROR(URI::create("x/y/z", "file")); }
int unitTests() { int nfailed = 0; # ifdef _MSC_VER // floats are printed with three digit exponents on windows, which messes // up the tests. Turn this off for consistency: _set_output_format(_TWO_DIGIT_EXPONENT); # endif // Test various basic format specs against results of sprintf CHECK_EQUAL(tfm::format("%s", "asdf"), "asdf"); CHECK_EQUAL(tfm::format("%d", 1234), "1234"); CHECK_EQUAL(tfm::format("%i", -5678), "-5678"); CHECK_EQUAL(tfm::format("%o", 012), "12"); CHECK_EQUAL(tfm::format("%u", 123456u), "123456"); CHECK_EQUAL(tfm::format("%x", 0xdeadbeef), "deadbeef"); CHECK_EQUAL(tfm::format("%X", 0xDEADBEEF), "DEADBEEF"); CHECK_EQUAL(tfm::format("%e", 1.23456e10), "1.234560e+10"); CHECK_EQUAL(tfm::format("%E", -1.23456E10), "-1.234560E+10"); CHECK_EQUAL(tfm::format("%f", -9.8765), "-9.876500"); CHECK_EQUAL(tfm::format("%F", 9.8765), "9.876500"); CHECK_EQUAL(tfm::format("%g", 10), "10"); CHECK_EQUAL(tfm::format("%G", 100), "100"); CHECK_EQUAL(tfm::format("%c", 65), "A"); CHECK_EQUAL(tfm::format("%hc", (short)65), "A"); CHECK_EQUAL(tfm::format("%lc", (long)65), "A"); CHECK_EQUAL(tfm::format("%s", "asdf_123098"), "asdf_123098"); // Note: All tests printing pointers are different on windows, since // there's no standard numerical representation. // Representation also differs between 32-bit and 64-bit windows. # ifdef _MSC_VER # ifdef _WIN64 CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "0000000000012345"); # else CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "00012345"); # endif // _WIN64 # else CHECK_EQUAL(tfm::format("%p", (void*)0x12345), "0x12345"); # endif // _MSC_VER CHECK_EQUAL(tfm::format("%%%s", "asdf"), "%asdf"); // note: plain "%%" format gives warning with gcc // chars with int format specs are printed as ints: CHECK_EQUAL(tfm::format("%hhd", (char)65), "65"); CHECK_EQUAL(tfm::format("%hhu", (unsigned char)65), "65"); CHECK_EQUAL(tfm::format("%hhd", (signed char)65), "65"); # ifdef _MSC_VER # ifdef _WIN64 CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "0000000000000010"); # else CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "00000010"); # endif // _WIN64 # else CHECK_EQUAL(tfm::format("%p", (const char*)0x10), "0x10"); // should print address, not string. # endif // _MSC_VER // bools with string format spec are printed as "true" or "false" CHECK_EQUAL(tfm::format("%s", true), "true"); CHECK_EQUAL(tfm::format("%d", true), "1"); // Test precision & width CHECK_EQUAL(tfm::format("%10d", -10), " -10"); CHECK_EQUAL(tfm::format("%.4d", 10), "0010"); CHECK_EQUAL(tfm::format("%10.4f", 1234.1234567890), " 1234.1235"); CHECK_EQUAL(tfm::format("%.f", 10.1), "10"); CHECK_EQUAL(tfm::format("%.2s", "asdf"), "as"); // strings truncate to precision CHECK_EQUAL(tfm::format("%.2s", std::string("asdf")), "as"); // // Test variable precision & width CHECK_EQUAL(tfm::format("%*.4f", 10, 1234.1234567890), " 1234.1235"); CHECK_EQUAL(tfm::format("%10.*f", 4, 1234.1234567890), " 1234.1235"); CHECK_EQUAL(tfm::format("%*.*f", 10, 4, 1234.1234567890), " 1234.1235"); CHECK_EQUAL(tfm::format("%*.*f", -10, 4, 1234.1234567890), "1234.1235 "); // Test flags CHECK_EQUAL(tfm::format("%#x", 0x271828), "0x271828"); CHECK_EQUAL(tfm::format("%#o", 0x271828), "011614050"); CHECK_EQUAL(tfm::format("%#f", 3.0), "3.000000"); CHECK_EQUAL(tfm::format("%+d", 3), "+3"); CHECK_EQUAL(tfm::format("%+d", 0), "+0"); CHECK_EQUAL(tfm::format("%+d", -3), "-3"); CHECK_EQUAL(tfm::format("%010d", 100), "0000000100"); CHECK_EQUAL(tfm::format("%010d", -10), "-000000010"); // sign should extend CHECK_EQUAL(tfm::format("%#010X", 0xBEEF), "0X0000BEEF"); CHECK_EQUAL(tfm::format("% d", 10), " 10"); CHECK_EQUAL(tfm::format("% d", -10), "-10"); // Test flags with variable precision & width CHECK_EQUAL(tfm::format("%+.2d", 3), "+03"); CHECK_EQUAL(tfm::format("%+.2d", -3), "-03"); // flag override precedence CHECK_EQUAL(tfm::format("%+ d", 10), "+10"); // '+' overrides ' ' CHECK_EQUAL(tfm::format("% +d", 10), "+10"); CHECK_EQUAL(tfm::format("%-010d", 10), "10 "); // '-' overrides '0' CHECK_EQUAL(tfm::format("%0-10d", 10), "10 "); // Check that length modifiers are ignored CHECK_EQUAL(tfm::format("%hd", (short)1000), "1000"); CHECK_EQUAL(tfm::format("%ld", (long)100000), "100000"); CHECK_EQUAL(tfm::format("%lld", (long long)100000), "100000"); CHECK_EQUAL(tfm::format("%zd", (size_t)100000), "100000"); CHECK_EQUAL(tfm::format("%td", (ptrdiff_t)100000), "100000"); CHECK_EQUAL(tfm::format("%jd", 100000), "100000"); // Check that 0-argument formatting is printf-compatible CHECK_EQUAL(tfm::format("100%%"), "100%"); // printf incompatibilities: // compareSprintf("%6.4x", 10); // precision & width can't be supported independently // compareSprintf("%.4d", -10); // negative numbers + precision don't quite work. // General "complicated" format spec test CHECK_EQUAL(tfm::format("%0.10f:%04d:%+g:%s:%#X:%c:%%:%%asdf", 1.234, 42, 3.13, "str", 0XDEAD, (int)'X'), "1.2340000000:0042:+3.13:str:0XDEAD:X:%:%asdf"); // Test wrong number of args EXPECT_ERROR( tfm::format("%d", 5, 10) ) EXPECT_ERROR( tfm::format("%d %d", 1) ) // Unterminated format spec EXPECT_ERROR( tfm::format("%123", 10) ) // Types used to specify variable width/precision must be convertible to int. EXPECT_ERROR( tfm::format("%0*d", "thing that can't convert to int", 42) ) EXPECT_ERROR( tfm::format("%0.*d", "thing that can't convert to int", 42) ) // Error required if not enough args for variable width/precision EXPECT_ERROR( tfm::format("%*d", 1) ) EXPECT_ERROR( tfm::format("%.*d", 1) ) EXPECT_ERROR( tfm::format("%*.*d", 1, 2) ) // Unhandled C99 format spec EXPECT_ERROR( tfm::format("%n", 10) ) EXPECT_ERROR( tfm::format("%a", 10) ) EXPECT_ERROR( tfm::format("%A", 10) ) #ifdef TEST_WCHAR_T_COMPILE // Test wchar_t handling - should fail to compile! tfm::format("%ls", L"blah"); #endif // Test that formatting is independent of underlying stream state. std::ostringstream oss; oss.width(20); oss.precision(10); oss.fill('*'); oss.setf(std::ios::scientific); tfm::format(oss, "%f", 10.1234123412341234); CHECK_EQUAL(oss.str(), "10.123412"); // Test formatting a custom object MyInt myobj(42); CHECK_EQUAL(tfm::format("myobj: %s", myobj), "myobj: 42"); // Test that interface wrapping works correctly TestWrap wrap; CHECK_EQUAL(wrap.error(10, "someformat %s:%d:%d", "asdf", 2, 4), "10: someformat asdf:2:4"); TestExceptionDef ex("blah %d", 100); CHECK_EQUAL(ex.what(), std::string("blah 100")); // Test tfm::printf by swapping the std::cout stream buffer to capture data // which would noramlly go to the stdout std::ostringstream coutCapture; std::streambuf* coutBuf = std::cout.rdbuf(coutCapture.rdbuf()); tfm::printf("%s %s %d\n", "printf", "test", "1"); tfm::printfln("%s %s %d", "printfln", "test", "1"); std::cout.rdbuf(coutBuf); // restore buffer CHECK_EQUAL(coutCapture.str(), "printf test 1\nprintfln test 1\n"); return nfailed; }