reform dir_API to Google Test framwork
This commit is contained in:
parent
b959e7b093
commit
7d5fd98e15
@ -35,7 +35,8 @@ typedef struct treeNode {
|
|||||||
|
|
||||||
/*for root*/
|
/*for root*/
|
||||||
TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode *new_inode);
|
TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode *new_inode);
|
||||||
/*root directory have its own initialization, so parent wont be NULL*/
|
/*the to be added file in add_entry should be parent-child relationship with treenode, otherwise will wrong */
|
||||||
|
/*see Add_FindFiletest in dir_API.cpp*/
|
||||||
int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode);
|
int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode);
|
||||||
/*if want to use dir mode use the subdirectory treeNode pointer */
|
/*if want to use dir mode use the subdirectory treeNode pointer */
|
||||||
//e.g. FileNode *Dirnode = fischl_find_entry(); can see file inside with Dirnode->subdirectory
|
//e.g. FileNode *Dirnode = fischl_find_entry(); can see file inside with Dirnode->subdirectory
|
||||||
|
@ -64,7 +64,7 @@ TreeNode *find_parentPath(TreeNode *root, const char *path) {
|
|||||||
file = lookupHash(current->contents, segment);
|
file = lookupHash(current->contents, segment);
|
||||||
if (file != NULL && file->subdirectory == NULL) {
|
if (file != NULL && file->subdirectory == NULL) {
|
||||||
free(pathCopy);
|
free(pathCopy);
|
||||||
printf("status current directory %s\n",current->dirName);
|
//printf("status current directory %s\n",current->dirName);
|
||||||
return current; //File found
|
return current; //File found
|
||||||
}
|
}
|
||||||
current = file ? file->subdirectory : NULL;
|
current = file ? file->subdirectory : NULL;
|
||||||
@ -107,7 +107,7 @@ void freeTree(TreeNode *node) {
|
|||||||
freeTree(temp->subdirectory);
|
freeTree(temp->subdirectory);
|
||||||
}
|
}
|
||||||
// Free the FileNode if it's not a directory
|
// Free the FileNode if it's not a directory
|
||||||
printf("free who %s\n",temp->name);
|
// printf("free who %s\n",temp->name);
|
||||||
free(temp->name);
|
free(temp->name);
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ add_executable(${TARGET_DIR_API}
|
|||||||
# Link Google Test to your test executables
|
# Link Google Test to your test executables
|
||||||
target_link_libraries(${TARGET_LAYER0} gtest gtest_main)
|
target_link_libraries(${TARGET_LAYER0} gtest gtest_main)
|
||||||
target_link_libraries(${TARGET_LAYER1_API} gtest gtest_main)
|
target_link_libraries(${TARGET_LAYER1_API} gtest gtest_main)
|
||||||
|
target_link_libraries(${TARGET_DIR_API} gtest gtest_main)
|
||||||
|
|
||||||
# add test to activate ctest -VV
|
# add test to activate ctest -VV
|
||||||
add_test(NAME ${TARGET_LAYER0} COMMAND sudo ./${TARGET_LAYER0} ${DIR_PLACE})
|
add_test(NAME ${TARGET_LAYER0} COMMAND sudo ./${TARGET_LAYER0} ${DIR_PLACE})
|
||||||
|
159
test/dir_API.cpp
159
test/dir_API.cpp
@ -8,103 +8,94 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "direntry.h"
|
#include "direntry.h"
|
||||||
|
|
||||||
int main() {
|
const char* d;
|
||||||
|
|
||||||
|
TreeNode *root;//global can be taken
|
||||||
|
|
||||||
|
TEST(DirTest, root_test) {
|
||||||
//Init fake root directory
|
//Init fake root directory
|
||||||
INode inode_root;
|
INode inode_root;
|
||||||
u_int64_t file_permissions = 0;
|
u_int64_t file_permissions = 0;
|
||||||
inode_root.permissions = file_permissions | S_IFDIR;
|
inode_root.permissions = file_permissions | S_IFDIR;
|
||||||
TreeNode *root = fischl_init_entry(0, "/", &inode_root);//0 is inode number assigned by inode_allocate()
|
root = fischl_init_entry(0, "/", &inode_root);//0 is inode number assigned by inode_allocate()
|
||||||
//add file or dir under fake root
|
}
|
||||||
INode inode_file1;
|
TEST(DirTest, AddFile_test) {
|
||||||
fischl_add_entry(root, 2, "file1",&inode_file1);
|
//assume file and dir itself(content,metadata) same,but different name and inode number
|
||||||
//I will put this function in create_new_inode function, there will inode number(2) when inode_allocate
|
INode inode_file;
|
||||||
INode inode_dir1;
|
INode inode_dir;
|
||||||
//permission is necessary there to create treeNode or not
|
u_int64_t file_permissions = 0;
|
||||||
file_permissions = 0;
|
file_permissions = 0;
|
||||||
inode_dir1.permissions = file_permissions | S_IFDIR;
|
inode_dir.permissions = file_permissions | S_IFDIR;
|
||||||
fischl_add_entry(root, 3, "dir1",&inode_dir1);
|
fischl_add_entry(root, 2, "file1",&inode_file);
|
||||||
//find dir file (from root directory view, root contains dir1/ subdirectory)
|
fischl_add_entry(root, 3, "dir1",&inode_dir);
|
||||||
FileNode *get_dir1 = fischl_find_entry(root,"/dir1/");
|
}
|
||||||
if(get_dir1 == NULL){
|
TEST(DirTest, FindFile_test) {
|
||||||
printf("No dir1 under %s\n",root->dirName);
|
//find file
|
||||||
freeTree(root);
|
FileNode *get_file = fischl_find_entry(root,"/file1");
|
||||||
return -1;
|
EXPECT_TRUE(get_file != NULL);
|
||||||
}else{
|
EXPECT_STREQ(get_file->name,"file1");
|
||||||
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
|
//find dir
|
||||||
printf(" %s under %s\n",get_dir1->name,root->dirName);
|
FileNode *get_dir = fischl_find_entry(root,"/dir1/");
|
||||||
}
|
EXPECT_TRUE(get_dir != NULL);//detect this should find success
|
||||||
//add file2 under dir1
|
EXPECT_STREQ(get_dir->name, "dir1");
|
||||||
INode inode_file2;
|
ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
|
||||||
if(get_dir1->subdirectory != NULL){
|
//check . function
|
||||||
//Treenode dir(you cannot find here), you only can get Filenode dir based on fischl_find_entry Function
|
get_dir = fischl_find_entry(root,"./");
|
||||||
//So use Filenode->subdirectory will point to the treenode dir, then can add files
|
EXPECT_TRUE(get_dir != NULL);//detect this should find success
|
||||||
fischl_add_entry(get_dir1->subdirectory, 4, "file2",&inode_file2);
|
EXPECT_STREQ(get_dir->name, "/");
|
||||||
printf("add file2 in dir1\n");
|
ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
|
||||||
}
|
}
|
||||||
/**********************************************************/
|
TEST(DirTest, Add_FindFile_test) {
|
||||||
//This is for debugging, and demonstate to you
|
//add file and dir under subdirectory instead of root
|
||||||
TreeNode *get_dir1_tree = find_parentPath(root,"/dir1/file2");
|
INode inode_file;
|
||||||
if(get_dir1_tree == get_dir1->subdirectory){
|
INode inode_dir;
|
||||||
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
|
u_int64_t file_permissions = 0;
|
||||||
printf(" [Treenode]get_dir1_tree->dirName (%s) same [Filenode]get_dir1->name (%s)\n",get_dir1_tree->dirName,get_dir1->name);
|
file_permissions = 0;
|
||||||
}else{
|
inode_dir.permissions = file_permissions | S_IFDIR;
|
||||||
printf("not same\n");
|
|
||||||
}
|
/*add with subdirectory*/
|
||||||
/**********************************************************/
|
//Treenode dir(you cannot find here), you only can get Filenode dir based on fischl_find_entry Function
|
||||||
|
//So use Filenode->subdirectory will point to the treenode dir, then can add files
|
||||||
|
FileNode *get_dir = fischl_find_entry(root,"/dir1/");
|
||||||
|
fischl_add_entry(get_dir->subdirectory, 4, "file2",&inode_file);
|
||||||
|
|
||||||
|
//verfication treeNode and Filenode relationship
|
||||||
|
TreeNode *get_dir_tree = find_parentPath(root,"/dir1/file2");
|
||||||
|
ASSERT_TRUE(get_dir_tree == get_dir->subdirectory);//treeNode dir should be same as treeNode subdir in that Filenode
|
||||||
|
|
||||||
//two Ways to get File(include dir itself) information
|
//two Ways to get File(include dir itself) information
|
||||||
FileNode *get_file2 =NULL;
|
FileNode *get_file = NULL;
|
||||||
//1. absolute path, the root(treeNode) will always exist when initialize
|
//1. absolute path, the root(treeNode) will always exist when initialize
|
||||||
get_file2 = fischl_find_entry(root,"/dir1/file2");
|
get_file = fischl_find_entry(root,"/dir1/file2");
|
||||||
if(get_file2 == NULL){
|
EXPECT_TRUE(get_file != NULL);
|
||||||
printf("No dir1 under dir1\n");
|
EXPECT_STREQ(get_file->name,"file2");
|
||||||
freeTree(root);
|
|
||||||
return -1;
|
|
||||||
}else{
|
|
||||||
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
|
|
||||||
printf(" %s under %sdir1/\n",get_file2->name,root->dirName);
|
|
||||||
}
|
|
||||||
//2. relative path, the get_dir1(FileNode)->subdirectory(treeNode), use treeNode(dir) to find
|
//2. relative path, the get_dir1(FileNode)->subdirectory(treeNode), use treeNode(dir) to find
|
||||||
get_file2 = fischl_find_entry(get_dir1->subdirectory,"/file2");
|
get_file = fischl_find_entry(get_dir->subdirectory,"/file2");
|
||||||
if(get_file2 == NULL){
|
EXPECT_TRUE(get_file != NULL);
|
||||||
printf("No dir1 under %s\n",get_dir1->subdirectory->dirName);
|
EXPECT_STREQ(get_file->name,"file2");
|
||||||
freeTree(root);
|
|
||||||
return -1;
|
|
||||||
}else{
|
|
||||||
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
|
|
||||||
printf(" %s under %s\n",get_file2->name,get_dir1->subdirectory->dirName);
|
|
||||||
}
|
|
||||||
/**********************************************************/
|
/**********************************************************/
|
||||||
//add one more file under dir1
|
//add one more file under dir1
|
||||||
INode inode_file3;
|
fischl_add_entry(get_dir->subdirectory, 5, "file3",&inode_file);
|
||||||
if(get_dir1->subdirectory != NULL){
|
//find
|
||||||
fischl_add_entry(get_dir1_tree, 5, "file3",&inode_file3);
|
get_file = fischl_find_entry(get_dir_tree,"./file3");
|
||||||
printf("add file3 in dir1\n");
|
EXPECT_TRUE(get_file != NULL);
|
||||||
}
|
EXPECT_STREQ(get_file->name,"file3");
|
||||||
FileNode *get_file3 =NULL;
|
//use .. from dir1 to find file1
|
||||||
//fischl_find_entry(get_dir1->subdirectory,"/file3"); are equivalent
|
get_file = fischl_find_entry(get_dir_tree,"../file1");
|
||||||
get_file3 = fischl_find_entry(get_dir1_tree,"/file3");
|
EXPECT_TRUE(get_file != NULL);
|
||||||
if(get_file3 == NULL){
|
EXPECT_STREQ(get_file->name,"file1");
|
||||||
printf("No dir1 under %s\n",get_dir1_tree->dirName);
|
|
||||||
freeTree(root);
|
}
|
||||||
return -1;
|
|
||||||
}else{
|
int main(int argc, char **argv) {
|
||||||
printf(" %s under %s\n",get_file3->name,get_dir1_tree->dirName);
|
d = (argc < 2) ? "/dev/vdc" : argv[1];//how to do with this?
|
||||||
}
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
FileNode *get_file1 = NULL;//under root
|
int result = RUN_ALL_TESTS();
|
||||||
//use .. to find
|
|
||||||
get_file1 = fischl_find_entry(get_dir1_tree,"../file1");
|
|
||||||
if(get_file1 == NULL){
|
|
||||||
printf("No file1\n");
|
|
||||||
freeTree(root);
|
|
||||||
return -1;
|
|
||||||
}else{
|
|
||||||
printf(" %s under root(..)\n",get_file1->name);
|
|
||||||
}
|
|
||||||
// Cleanup
|
// Cleanup
|
||||||
freeTree(root);
|
freeTree(root);
|
||||||
|
return result;
|
||||||
return 0;
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user