-
Notifications
You must be signed in to change notification settings - Fork 13
/
KDB3Tests.m
189 lines (140 loc) · 4.51 KB
/
KDB3Tests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// Kdb3Tests.m
// KeePass2
//
// Created by Qiang Yu on 2/13/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "KDB3Tests.h"
#import "Kdb3Reader.h"
#import "Kdb3Writer.h"
#import "KdbReaderFactory.h"
@interface KDB3Tests(PrivateMethods)
-(void)printGroup:(Kdb3Group *)group;
-(Kdb3Group *)newGroup:(NSString *)title;
-(Kdb3Entry *)newEntry:(NSString *)title;
-(void)addSubGroup:(id<KdbGroup>) group level:(uint8_t)level;
@end
@implementation KDB3Tests
-(void)testNewFile{
/*Kdb3Writer * writer = [[Kdb3Writer alloc]init];
[writer newFile:@"/Volumes/Users/qiang/Desktop/test.kdb" withPassword:@"test"];
[writer release];
WrapperNSData * source = [[WrapperNSData alloc]initWithContentsOfMappedFile:@"/Volumes/Users/qiang/Desktop/test.kdb"];
Kdb3Reader * kdb = [KdbReaderFactory newKdbReader:source];
id<KdbTree> tree = [kdb load:source withPassword:@"test"];
[self addSubGroup:[tree getRoot] level:0];
writer = [[Kdb3Writer alloc]init];
[writer persist:tree file:@"/Volumes/Users/qiang/Desktop/test.kdb" withPassword:@"test"];*/
}
-(void)addSubGroup:(id<KdbGroup>) group level:(uint8_t)level{
static int k=0;
static int m=0;
if(level)
for(int i=0; i<20; i++){
[group addEntry:[self newEntry:[NSString stringWithFormat:@"entry %d:%d:%d", level,i, k++]]];
}
if(level==3) return;
for(int i=0; i<5; i++){
[group addSubGroup:[self newGroup:[NSString stringWithFormat:@"group %d:%d", level, m++]]];
}
for(id<KdbGroup> g in [group getSubGroups]){
[self addSubGroup:g level:level+1];
}
}
-(void) testNodeManipulation {
Kdb3Group * root = [[Kdb3Group alloc]init];
Kdb3Tree * tree = [[Kdb3Tree alloc]init];
tree._root = root;
root._title = @"$ROOT$";
Kdb3Group * g1 = [self newGroup:@"g"];
[root addSubGroup:g1];
Kdb3Entry * e1 = [self newEntry:@"e"];
[g1 addEntry:e1];
/*
Kdb3Group * g2 = [self newGroup:@"g2"];
[root addSubGroup:g2];
Kdb3Group * g3 = [self newGroup:@"g3"];
[root addSubGroup:g3];
Kdb3Group * g2_1 = [self newGroup:@"g2_1"];
[g2 addSubGroup:g2_1];
Kdb3Group * g2_2 = [self newGroup:@"g2_2"];
[g2 addSubGroup:g2_2];
Kdb3Group * g3_1 = [self newGroup:@"g3_1"];
[g3 addSubGroup:g3_1];
Kdb3Group * g2_1_1 = [self newGroup:@"g2_1_1"];
[g2_1 addSubGroup:g2_1_1];
Kdb3Group * g3_1_1 = [self newGroup:@"g3_1_1"];
[g3_1 addSubGroup:g3_1_1];
Kdb3Entry * e1 = [self newEntry:@"g1_e1"];
[g1 addEntry:e1];
Kdb3Entry * e2 = [self newEntry:@"g2_2_e1"];
[g2_2 addEntry:e2];
Kdb3Entry * e3 = [self newEntry:@"g2_2_e2"];
[g2_2 addEntry:e3];
Kdb3Entry * e4 = [self newEntry:@"g3_1_1_e1"];
[g3_1_1 addEntry:e4];
Kdb3Entry * e5 = [self newEntry:@"g3_1_e1"];
[g3_1 addEntry:e5];
Kdb3Entry * e6 = [self newEntry:@"g2_1_1_e1"];
[g2_1_1 addEntry:e6];
//[self printGroup:[tree getRoot]];
[g1 deleteEntry:e1];
[g2 deleteSubGroup:g2_2];
[g2_2 breakCyclcReference];
[self printGroup:[tree getRoot]];
*/
Kdb3Writer * writer = [[Kdb3Writer alloc]init];
[writer persist:tree file:@"/Volumes/Users/qiang/Desktop/a.kdb" withPassword:@"a"];
}
- (void) testOpenFile {
/*
WrapperNSData * source = [[WrapperNSData alloc]initWithContentsOfMappedFile:@"/Volumes/Users/qiang/Desktop/a4.kdb"];
Kdb3Reader * kdb = [KdbReaderFactory newKdbReader:source];
Kdb3Writer * writer = [[Kdb3Writer alloc]init];
id<KdbTree> tree = [kdb load:source withPassword:@"a4"];
//[self printGroup:[tree getRoot]];
[writer persist:tree file:@"/Volumes/Users/qiang/Desktop/a5.kdb" withPassword:@"a5"];
[kdb release];
[source release];
[writer release];
*/
}
-(Kdb3Group *)newGroup:(NSString *)title{
Kdb3Group * g = [[Kdb3Group alloc]init];
g._title = title;
[g setExpiry:nil];
[g setLastMod:[NSDate date]];
[g setLastAccess:[NSDate date]];
[g setCreation:[NSDate date]];
return g;
}
-(Kdb3Entry *)newEntry:(NSString *)title{
Kdb3Entry * e = [[Kdb3Entry alloc]initWithNewUUID];
e._title = title;
e._password = [title stringByAppendingFormat:@":password"];
e._url = [@"http://" stringByAppendingFormat:title];
e._username = [title stringByAppendingFormat:@":username"];
e._comment = [title stringByAppendingFormat:@":comment"];
[e setExpiry:nil];
[e setLastMod:[NSDate date]];
[e setLastAccess:[NSDate date]];
[e setCreation:[NSDate date]];
return e;
}
-(void)printGroup:(Kdb3Group *)group{
if(!group) return;
NSLog(@"%@", group);
for(NSObject * o in group._entries){
NSLog(@"%@", o);
}
NSLog(@"");NSLog(@"");
for(NSObject * o in group._metaEntries){
NSLog(@"%@", o);
}
NSLog(@"");NSLog(@"");
for(Kdb3Group * o in group._subGroups){
[self printGroup:o];
}
}
@end