diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/filesystem/src.js | 1 | ||||
-rw-r--r-- | tests/glbook/CH02_HelloTriangle.png | bin | 1009 -> 866 bytes | |||
-rw-r--r-- | tests/glbook/CH08_SimpleVertexShader.png | bin | 1600 -> 1200 bytes | |||
-rw-r--r-- | tests/glbook/CH09_SimpleTexture2D.png | bin | 1943 -> 713 bytes | |||
-rw-r--r-- | tests/glbook/CH09_TextureCubemap.png | bin | 3209 -> 2325 bytes | |||
-rw-r--r-- | tests/glbook/CH09_TextureWrap.png | bin | 1812 -> 894 bytes | |||
-rw-r--r-- | tests/glbook/CH10_MultiTexture.png | bin | 59495 -> 46810 bytes | |||
-rw-r--r-- | tests/glbook/CH13_ParticleSystem.png | bin | 4921 -> 4285 bytes | |||
-rw-r--r-- | tests/htmltest.png | bin | 743 -> 730 bytes | |||
-rwxr-xr-x | tests/runner.py | 222 | ||||
-rw-r--r-- | tests/sdl_gfx_primitives.png | bin | 2357 -> 2261 bytes | |||
-rw-r--r-- | tests/sdl_maprgba.png | bin | 1875 -> 1862 bytes | |||
-rw-r--r-- | tests/sdl_rotozoom.png | bin | 431921 -> 431168 bytes | |||
-rw-r--r-- | tests/stat/test_chmod.c | 24 | ||||
-rw-r--r-- | tests/stat/test_mknod.c | 5 | ||||
-rw-r--r-- | tests/stat/test_stat.c | 3 | ||||
-rw-r--r-- | tests/stdio/test_rename.c | 107 |
17 files changed, 266 insertions, 96 deletions
diff --git a/tests/filesystem/src.js b/tests/filesystem/src.js index 801b45d6..ceea348d 100644 --- a/tests/filesystem/src.js +++ b/tests/filesystem/src.js @@ -8,6 +8,7 @@ FS.createDevice('/def', 'deviceB', function() {}, function() {}); FS.createLink('/abc', 'localLink', '123', true, true); FS.createLink('/abc', 'rootLink', '/', true, true); FS.createLink('/abc', 'relativeLink', '../def', true, true); +FS.ignorePermissions = false; function explore(path) { Module.print(path); diff --git a/tests/glbook/CH02_HelloTriangle.png b/tests/glbook/CH02_HelloTriangle.png Binary files differindex d6351e2a..7b8c176e 100644 --- a/tests/glbook/CH02_HelloTriangle.png +++ b/tests/glbook/CH02_HelloTriangle.png diff --git a/tests/glbook/CH08_SimpleVertexShader.png b/tests/glbook/CH08_SimpleVertexShader.png Binary files differindex 84b276fd..6711784d 100644 --- a/tests/glbook/CH08_SimpleVertexShader.png +++ b/tests/glbook/CH08_SimpleVertexShader.png diff --git a/tests/glbook/CH09_SimpleTexture2D.png b/tests/glbook/CH09_SimpleTexture2D.png Binary files differindex e33539e9..7cb01698 100644 --- a/tests/glbook/CH09_SimpleTexture2D.png +++ b/tests/glbook/CH09_SimpleTexture2D.png diff --git a/tests/glbook/CH09_TextureCubemap.png b/tests/glbook/CH09_TextureCubemap.png Binary files differindex b592b047..2ca61b63 100644 --- a/tests/glbook/CH09_TextureCubemap.png +++ b/tests/glbook/CH09_TextureCubemap.png diff --git a/tests/glbook/CH09_TextureWrap.png b/tests/glbook/CH09_TextureWrap.png Binary files differindex 3367e254..1fb5a319 100644 --- a/tests/glbook/CH09_TextureWrap.png +++ b/tests/glbook/CH09_TextureWrap.png diff --git a/tests/glbook/CH10_MultiTexture.png b/tests/glbook/CH10_MultiTexture.png Binary files differindex 8e006eb3..dc7a0ed6 100644 --- a/tests/glbook/CH10_MultiTexture.png +++ b/tests/glbook/CH10_MultiTexture.png diff --git a/tests/glbook/CH13_ParticleSystem.png b/tests/glbook/CH13_ParticleSystem.png Binary files differindex 4b69414f..e0e4b31b 100644 --- a/tests/glbook/CH13_ParticleSystem.png +++ b/tests/glbook/CH13_ParticleSystem.png diff --git a/tests/htmltest.png b/tests/htmltest.png Binary files differindex 980245ee..36e87dd8 100644 --- a/tests/htmltest.png +++ b/tests/htmltest.png diff --git a/tests/runner.py b/tests/runner.py index 86ae5bdb..14de2db0 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -16,7 +16,7 @@ so you may prefer to use fewer cores here. ''' from subprocess import Popen, PIPE, STDOUT -import os, unittest, tempfile, shutil, time, inspect, sys, math, glob, re, difflib, webbrowser, hashlib, threading, platform, BaseHTTPServer, multiprocessing, functools, stat +import os, unittest, tempfile, shutil, time, inspect, sys, math, glob, re, difflib, webbrowser, hashlib, threading, platform, BaseHTTPServer, multiprocessing, functools, stat, string if len(sys.argv) == 1: print ''' @@ -353,7 +353,10 @@ process(sys.argv[1]) build_dir = self.get_build_dir() output_dir = self.get_dir() - cache_name = name + cache_name_extra + (self.env.get('EMCC_LLVM_TARGET') or '') + cache_name = name + str(Building.COMPILER_TEST_OPTS) + cache_name_extra + (self.env.get('EMCC_LLVM_TARGET') or '') + + valid_chars = "_%s%s" % (string.ascii_letters, string.digits) + cache_name = ''.join([(c if c in valid_chars else '_') for c in cache_name]) if self.library_cache is not None: if cache and self.library_cache.get(cache_name): @@ -1984,6 +1987,24 @@ Succeeded! self.do_run(src, "1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 " "1 2 -1 -2 2 2 -2 -2") + # This example borrowed from MSDN documentation + def test_fcvt(self): + src = ''' + #include <stdlib.h> + #include <stdio.h> + + int main() { + int decimal, sign; + char *buffer; + double source = 3.1415926535; + + buffer = fcvt(source, 7, &decimal, &sign); + printf("source: %2.10f buffer: '%s' decimal: %d sign: %d\\n", + source, buffer, decimal, sign); + } + ''' + self.do_run(src, "source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0"); + def test_llrint(self): if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') src = r''' @@ -3591,33 +3612,8 @@ Exiting setjmp function, level: 0, prev_jmp: -1 self.do_run(src, 'z:1*', force_c=True) def test_rename(self): - src = ''' - #include <stdio.h> - #include <sys/stat.h> - #include <sys/types.h> - #include <assert.h> - - int main() { - int err; - FILE* fid; - - err = mkdir("/foo", 0777); - err = mkdir("/bar", 0777); - fid = fopen("/foo/bar", "w+"); - fclose(fid); - - err = rename("/foo/bar", "/foo/bar2"); - printf("%d\\n", err); - - err = rename("/foo", "/foo/foo"); - printf("%d\\n", err); - - err = rename("/foo", "/bar/foo"); - printf("%d\\n", err); - return 0; - } - ''' - self.do_run(src, '0\n-1\n0\n', force_c=True) + src = open(path_from_root('tests', 'stdio', 'test_rename.c'), 'r').read() + self.do_run(src, 'success', force_c=True) def test_alloca_stack(self): if self.emcc_args is None: return # too slow in other modes @@ -7114,7 +7110,7 @@ def process(filename): other.close() src = open(path_from_root('tests', 'files.cpp'), 'r').read() - self.do_run(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\ntexte\n', + self.do_run(src, ('size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\ntexte\n', 'size: 7\ndata: 100,-56,50,25,10,77,123\nloop: 100 -56 50 25 10 77 123 \ninput:hi there!\ntexto\ntexte\n$\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\nok.\n'), post_build=post, extra_emscripten_args=['-H', 'libc/fcntl.h']) def test_files_m(self): @@ -7146,7 +7142,7 @@ def process(filename): return 0; } ''' - self.do_run(src, 'got: 35\ngot: 45\ngot: 25\ngot: 15\nisatty? 0,0,1\n', post_build=post) + self.do_run(src, ('got: 35\ngot: 45\ngot: 25\ngot: 15\nisatty? 0,0,1\n', 'isatty? 0,0,1\ngot: 35\ngot: 45\ngot: 25\ngot: 15\n'), post_build=post) def test_fwrite_0(self): src = r''' @@ -7244,17 +7240,14 @@ def process(filename): self.do_run(src, 'success', force_c=True) def test_stat(self): - Building.COMPILER_TEST_OPTS += ['-DUSE_OLD_FS='+str(Settings.USE_OLD_FS)] src = open(path_from_root('tests', 'stat', 'test_stat.c'), 'r').read() self.do_run(src, 'success', force_c=True) def test_stat_chmod(self): - Building.COMPILER_TEST_OPTS += ['-DUSE_OLD_FS='+str(Settings.USE_OLD_FS)] src = open(path_from_root('tests', 'stat', 'test_chmod.c'), 'r').read() self.do_run(src, 'success', force_c=True) def test_stat_mknod(self): - Building.COMPILER_TEST_OPTS += ['-DUSE_OLD_FS='+str(Settings.USE_OLD_FS)] src = open(path_from_root('tests', 'stat', 'test_mknod.c'), 'r').read() self.do_run(src, 'success', force_c=True) @@ -7773,6 +7766,93 @@ def process(filename): ''' self.do_run(src, '120.86.52.18\n120.86.52.18\n') + def test_inet3(self): + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + #include <sys/socket.h> + int main() { + char dst[64]; + struct in_addr x, x2; + int *y = (int*)&x; + *y = 0x12345678; + printf("%s\n", inet_ntop(AF_INET,&x,dst,sizeof dst)); + int r = inet_aton(inet_ntoa(x), &x2); + printf("%s\n", inet_ntop(AF_INET,&x2,dst,sizeof dst)); + return 0; + } + ''' + self.do_run(src, '120.86.52.18\n120.86.52.18\n') + + def test_inet4(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('requires ta2') + + src = r''' + #include <stdio.h> + #include <arpa/inet.h> + #include <sys/socket.h> + + void test(char *test_addr){ + char str[40]; + struct in6_addr addr; + unsigned char *p = (unsigned char*)&addr; + int ret; + ret = inet_pton(AF_INET6,test_addr,&addr); + if(ret == -1) return; + if(ret == 0) return; + if(inet_ntop(AF_INET6,&addr,str,sizeof(str)) == NULL ) return; + printf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x - %s\n", + p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15],str); + } + int main(){ + test("::"); + test("::1"); + test("::1.2.3.4"); + test("::17.18.19.20"); + test("::ffff:1.2.3.4"); + test("1::ffff"); + test("::255.255.255.255"); + test("0:ff00:1::"); + test("0:ff::"); + test("abcd::"); + test("ffff::a"); + test("ffff::a:b"); + test("ffff::a:b:c"); + test("ffff::a:b:c:d"); + test("ffff::a:b:c:d:e"); + test("::1:2:0:0:0"); + test("0:0:1:2:3::"); + test("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + test("1::255.255.255.255"); + + //below should fail and not produce results.. + test("1.2.3.4"); + test(""); + test("-"); + } + ''' + self.do_run(src, + "0000:0000:0000:0000:0000:0000:0000:0000 - ::\n" + "0000:0000:0000:0000:0000:0000:0000:0001 - ::1\n" + "0000:0000:0000:0000:0000:0000:0102:0304 - ::1.2.3.4\n" + "0000:0000:0000:0000:0000:0000:1112:1314 - ::17.18.19.20\n" + "0000:0000:0000:0000:0000:ffff:0102:0304 - ::ffff:1.2.3.4\n" + "0001:0000:0000:0000:0000:0000:0000:ffff - 1::ffff\n" + "0000:0000:0000:0000:0000:0000:ffff:ffff - ::255.255.255.255\n" + "0000:ff00:0001:0000:0000:0000:0000:0000 - 0:ff00:1::\n" + "0000:00ff:0000:0000:0000:0000:0000:0000 - 0:ff::\n" + "abcd:0000:0000:0000:0000:0000:0000:0000 - abcd::\n" + "ffff:0000:0000:0000:0000:0000:0000:000a - ffff::a\n" + "ffff:0000:0000:0000:0000:0000:000a:000b - ffff::a:b\n" + "ffff:0000:0000:0000:0000:000a:000b:000c - ffff::a:b:c\n" + "ffff:0000:0000:0000:000a:000b:000c:000d - ffff::a:b:c:d\n" + "ffff:0000:0000:000a:000b:000c:000d:000e - ffff::a:b:c:d:e\n" + "0000:0000:0000:0001:0002:0000:0000:0000 - ::1:2:0:0:0\n" + "0000:0000:0001:0002:0003:0000:0000:0000 - 0:0:1:2:3::\n" + "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n" + "0001:0000:0000:0000:0000:0000:ffff:ffff - 1::ffff:ffff\n" + ) + def test_gethostbyname(self): if Settings.USE_TYPED_ARRAYS != 2: return self.skip("assume t2 in gethostbyname") @@ -9104,24 +9184,14 @@ def process(filename): } int main(int argc, char **argv) { - // keep them alive - if (argc == 10) return get_int(); - if (argc == 11) return get_float(); - if (argc == 12) return get_string()[0]; - if (argc == 13) print_int(argv[0][0]); - if (argc == 14) print_float(argv[0][0]); - if (argc == 15) print_string(argv[0]); - if (argc == 16) pointer((int*)argv[0]); - if (argc % 17 == 12) return multi(argc, float(argc)/2, argc+1, argv[0]); - // return 0; - exit(0); + return 0; } ''' post = ''' def process(filename): src = \'\'\' - var Module = { noInitialRun: true }; + var Module = { 'noInitialRun': true }; \'\'\' + open(filename, 'r').read() + \'\'\' Module.addOnExit(function () { Module.print('*'); @@ -10994,6 +11064,24 @@ f.close() voidfunc sidey(voidfunc f) { return f; } ''', 'hello from funcptr\n') + # function pointers with 'return' in the name + test('fp2', 'typedef void (*voidfunc)();', r''' + #include <stdio.h> + #include "header.h" + int sidey(voidfunc f); + void areturn0() { printf("hello 0\n"); } + void areturn1() { printf("hello 1\n"); } + void areturn2() { printf("hello 2\n"); } + int main(int argc, char **argv) { + voidfunc table[3] = { areturn0, areturn1, areturn2 }; + table[sidey(NULL)](); + return 0; + } + ''', ''' + #include "header.h" + int sidey(voidfunc f) { if (f) f(); return 1; } + ''', 'hello 1\n') + # Global initializer test('global init', '', r''' #include <stdio.h> @@ -11143,7 +11231,7 @@ f.close() def test_outline(self): - def test(name, src, libs, expected, expected_ranges, args=[], suffix='cpp'): + def test(name, src, libs, expected, expected_ranges, args=[], suffix='cpp', test_sizes=True): print name def measure_funcs(filename): @@ -11183,21 +11271,23 @@ f.close() seen = max(measure_funcs('test.js').values()) high = expected_ranges[outlining_limit][1] print outlining_limit, ' ', low, '<=', seen, '<=', high - assert low <= seen <= high - - test('zlib', path_from_root('tests', 'zlib', 'example.c'), - self.get_library('zlib', os.path.join('libz.a'), make_args=['libz.a']), - open(path_from_root('tests', 'zlib', 'ref.txt'), 'r').read(), - { - 100: (190, 250), - 250: (300, 330), - 500: (250, 310), - 1000: (230, 300), - 2000: (380, 450), - 5000: (800, 1100), - 0: (1500, 1800) - }, - args=['-I' + path_from_root('tests', 'zlib')], suffix='c') + if test_sizes: assert low <= seen <= high + + for test_opts, test_sizes in [([], True), (['-O2'], False)]: + Building.COMPILER_TEST_OPTS = test_opts + test('zlib', path_from_root('tests', 'zlib', 'example.c'), + self.get_library('zlib', os.path.join('libz.a'), make_args=['libz.a']), + open(path_from_root('tests', 'zlib', 'ref.txt'), 'r').read(), + { + 100: (190, 250), + 250: (200, 330), + 500: (250, 310), + 1000: (230, 300), + 2000: (380, 450), + 5000: (800, 1100), + 0: (1500, 1800) + }, + args=['-I' + path_from_root('tests', 'zlib')], suffix='c', test_sizes=test_sizes) def test_symlink(self): if os.name == 'nt': @@ -12123,7 +12213,7 @@ int main(int argc, char const *argv[]) open(os.path.join(self.get_dir(), 'test.file'), 'w').write('''ay file..............,,,,,,,,,,,,,,''') open(os.path.join(self.get_dir(), 'stdin'), 'w').write('''inter-active''') Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'files.cpp'), '-c']).communicate() - Popen([PYTHON, path_from_root('tools', 'nativize_llvm.py'), os.path.join(self.get_dir(), 'files.o')]).communicate(input)[0] + Popen([PYTHON, path_from_root('tools', 'nativize_llvm.py'), os.path.join(self.get_dir(), 'files.o')], stdout=PIPE, stderr=PIPE).communicate(input) output = Popen([os.path.join(self.get_dir(), 'files.o.run')], stdin=open(os.path.join(self.get_dir(), 'stdin')), stdout=PIPE, stderr=PIPE).communicate() self.assertContained('''size: 37 data: 119,97,107,97,32,119,97,107,97,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35 @@ -12428,6 +12518,8 @@ elif 'browser' in str(sys.argv): ''' + code def reftest(self, expected): + # make sure the pngs used here have no color correction, using e.g. + # pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB infile outfile basename = os.path.basename(expected) shutil.copyfile(expected, os.path.join(self.get_dir(), basename)) open(os.path.join(self.get_dir(), 'reftest.js'), 'w').write(''' @@ -13718,7 +13810,7 @@ Press any key to continue.''' def test_sdl_rotozoom(self): shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) - self.btest('sdl_rotozoom.c', reference='sdl_rotozoom.png', args=['--preload-file', 'screenshot.png'], reference_slack=3) + self.btest('sdl_rotozoom.c', reference='sdl_rotozoom.png', args=['--preload-file', 'screenshot.png'], reference_slack=5) def test_sdl_gfx_primitives(self): self.btest('sdl_gfx_primitives.c', reference='sdl_gfx_primitives.png', reference_slack=1) @@ -14136,6 +14228,8 @@ elif 'benchmark' in str(sys.argv): total_native_times = map(lambda x: 0., range(TOTAL_TESTS)) class benchmark(RunnerCore): + save_dir = True + def print_stats(self, times, native_times, last=False, reps=TEST_REPS): if reps == 0: print '(no reps)' diff --git a/tests/sdl_gfx_primitives.png b/tests/sdl_gfx_primitives.png Binary files differindex 525b4f8f..706af9ae 100644 --- a/tests/sdl_gfx_primitives.png +++ b/tests/sdl_gfx_primitives.png diff --git a/tests/sdl_maprgba.png b/tests/sdl_maprgba.png Binary files differindex 4f64c7cd..caca4a8b 100644 --- a/tests/sdl_maprgba.png +++ b/tests/sdl_maprgba.png diff --git a/tests/sdl_rotozoom.png b/tests/sdl_rotozoom.png Binary files differindex 288dd303..5933754f 100644 --- a/tests/sdl_rotozoom.png +++ b/tests/sdl_rotozoom.png diff --git a/tests/stat/test_chmod.c b/tests/stat/test_chmod.c index bf0e2cbe..3574760b 100644 --- a/tests/stat/test_chmod.c +++ b/tests/stat/test_chmod.c @@ -56,11 +56,7 @@ void test() { memset(&s, 0, sizeof s); stat("file", &s); -#if USE_OLD_FS - assert(s.st_mode == (0222 | S_IFREG)); -#else assert(s.st_mode == (0200 | S_IFREG)); -#endif assert(s.st_ctime != lastctime); // @@ -74,11 +70,7 @@ void test() { memset(&s, 0, sizeof s); stat("file", &s); -#if USE_OLD_FS - assert(s.st_mode == (0000 | S_IFREG)); -#else assert(s.st_mode == (0100 | S_IFREG)); -#endif assert(s.st_ctime != lastctime); // @@ -95,11 +87,7 @@ void test() { assert(!err); memset(&s, 0, sizeof s); stat("folder", &s); -#if USE_OLD_FS - assert(s.st_mode == (0222 | S_IFDIR)); -#else assert(s.st_mode == (0300 | S_IFDIR)); -#endif assert(s.st_ctime != lastctime); // @@ -110,11 +98,7 @@ void test() { // make sure the file it references changed stat("file-link", &s); -#if USE_OLD_FS - assert(s.st_mode == (0555 | S_IFREG)); -#else assert(s.st_mode == (0400 | S_IFREG)); -#endif // but the link didn't lstat("file-link", &s); @@ -128,19 +112,11 @@ void test() { // make sure the file it references didn't change stat("file-link", &s); -#if USE_OLD_FS - assert(s.st_mode == (0555 | S_IFREG)); -#else assert(s.st_mode == (0400 | S_IFREG)); -#endif // but the link did lstat("file-link", &s); -#if USE_OLD_FS - assert(s.st_mode == (0555 | S_IFLNK)); -#else assert(s.st_mode == (0500 | S_IFLNK)); -#endif puts("success"); } diff --git a/tests/stat/test_mknod.c b/tests/stat/test_mknod.c index 2b8f650d..361b2315 100644 --- a/tests/stat/test_mknod.c +++ b/tests/stat/test_mknod.c @@ -53,15 +53,10 @@ void test() { // mknod a character device err = mknod("mknod-device", S_IFCHR | 0777, 123); -#if USE_OLD_FS - assert(err); - assert(errno == EPERM); -#else assert(!err); memset(&s, 0, sizeof s); stat("mknod-device", &s); assert(S_ISCHR(s.st_mode)); -#endif #endif diff --git a/tests/stat/test_stat.c b/tests/stat/test_stat.c index 2c2bf53d..f59fb3c3 100644 --- a/tests/stat/test_stat.c +++ b/tests/stat/test_stat.c @@ -104,13 +104,10 @@ void test() { assert(s.st_ino); assert(S_ISCHR(s.st_mode)); assert(s.st_nlink); -#if !USE_OLD_FS - // old FS doesn't store proper device ids #ifndef __APPLE__ // mac uses makedev(3, 2) for /dev/null assert(s.st_rdev == makedev(1, 3)); #endif -#endif assert(!s.st_size); assert(s.st_atime); assert(s.st_mtime); diff --git a/tests/stdio/test_rename.c b/tests/stdio/test_rename.c new file mode 100644 index 00000000..20ffb513 --- /dev/null +++ b/tests/stdio/test_rename.c @@ -0,0 +1,107 @@ +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> + +void create_file(const char *path, const char *buffer, int mode) { + int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); + assert(fd >= 0); + + int err = write(fd, buffer, sizeof(char) * strlen(buffer)); + assert(err == (sizeof(char) * strlen(buffer))); + + close(fd); +} + +void setup() { + create_file("file", "abcdef", 0777); + mkdir("dir", 0777); + create_file("dir/file", "abcdef", 0777); + mkdir("dir/subdir", 0777); + mkdir("dir-readonly", 0555); + mkdir("dir-nonempty", 0777); + create_file("dir-nonempty/file", "abcdef", 0777); +} + +void cleanup() { + // we're hulk-smashing and removing original + renamed files to + // make sure we get it all regardless of anything failing + unlink("file"); + unlink("dir/file"); + unlink("dir/file1"); + unlink("dir/file2"); + rmdir("dir/subdir"); + rmdir("dir/subdir1"); + rmdir("dir/subdir2"); + rmdir("dir"); + rmdir("dir-readonly"); + unlink("dir-nonempty/file"); + rmdir("dir-nonempty"); +} + +void test() { + int err; + + // can't rename something that doesn't exist + err = rename("noexist", "dir"); + assert(err == -1); + assert(errno == ENOENT); + + // can't overwrite a folder with a file + err = rename("file", "dir"); + assert(err == -1); + assert(errno == EISDIR); + + // can't overwrite a file with a folder + err = rename("dir", "file"); + assert(err == -1); + assert(errno == ENOTDIR); + + // can't overwrite a non-empty folder + err = rename("dir", "dir-nonempty"); + assert(err == -1); + assert(errno == ENOTEMPTY); + + // can't create anything in a read-only directory + err = rename("dir", "dir-readonly/dir"); + assert(err == -1); + assert(errno == EACCES); + + // source should not be ancestor of target + err = rename("dir", "dir/somename"); + assert(err == -1); + assert(errno == EINVAL); + + // target should not be an ancestor of source + err = rename("dir/subdir", "dir"); + assert(err == -1); + assert(errno == ENOTEMPTY); + + // do some valid renaming + err = rename("dir/file", "dir/file1"); + assert(!err); + err = rename("dir/file1", "dir/file2"); + assert(!err); + err = access("dir/file2", F_OK); + assert(!err); + err = rename("dir/subdir", "dir/subdir1"); + assert(!err); + err = rename("dir/subdir1", "dir/subdir2"); + assert(!err); + err = access("dir/subdir2", F_OK); + assert(!err); + + puts("success"); +} + +int main() { + atexit(cleanup); + signal(SIGABRT, cleanup); + setup(); + test(); + return EXIT_SUCCESS; +}
\ No newline at end of file |