11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst join = require('path').join; 261cb0ef41Sopenharmony_ciconst fs = require('fs'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst currentFileData = 'ABCD'; 291cb0ef41Sopenharmony_ciconst m = 0o600; 301cb0ef41Sopenharmony_ciconst num = 220; 311cb0ef41Sopenharmony_ciconst data = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + 321cb0ef41Sopenharmony_ci '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + 331cb0ef41Sopenharmony_ci '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + 341cb0ef41Sopenharmony_ci '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + 351cb0ef41Sopenharmony_ci '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + 361cb0ef41Sopenharmony_ci '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + 371cb0ef41Sopenharmony_ci '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir'); 401cb0ef41Sopenharmony_citmpdir.refresh(); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci// Test that empty file will be created and have content added. 431cb0ef41Sopenharmony_ciconst filename = join(tmpdir.path, 'append-sync.txt'); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cifs.appendFileSync(filename, data); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciconst fileData = fs.readFileSync(filename); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.byteLength(data), fileData.length); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci// Test that appends data to a non empty file. 521cb0ef41Sopenharmony_ciconst filename2 = join(tmpdir.path, 'append-sync2.txt'); 531cb0ef41Sopenharmony_cifs.writeFileSync(filename2, currentFileData); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifs.appendFileSync(filename2, data); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciconst fileData2 = fs.readFileSync(filename2); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.byteLength(data) + currentFileData.length, 601cb0ef41Sopenharmony_ci fileData2.length); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci// Test that appendFileSync accepts buffers. 631cb0ef41Sopenharmony_ciconst filename3 = join(tmpdir.path, 'append-sync3.txt'); 641cb0ef41Sopenharmony_cifs.writeFileSync(filename3, currentFileData); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciconst buf = Buffer.from(data, 'utf8'); 671cb0ef41Sopenharmony_cifs.appendFileSync(filename3, buf); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciconst fileData3 = fs.readFileSync(filename3); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ciassert.strictEqual(buf.length + currentFileData.length, fileData3.length); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciconst filename4 = join(tmpdir.path, 'append-sync4.txt'); 741cb0ef41Sopenharmony_cifs.writeFileSync(filename4, currentFileData, common.mustNotMutateObjectDeep({ mode: m })); 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci[ 771cb0ef41Sopenharmony_ci true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null, 781cb0ef41Sopenharmony_ci].forEach((value) => { 791cb0ef41Sopenharmony_ci assert.throws( 801cb0ef41Sopenharmony_ci () => fs.appendFileSync(filename4, value, common.mustNotMutateObjectDeep({ mode: m })), 811cb0ef41Sopenharmony_ci { message: /data/, code: 'ERR_INVALID_ARG_TYPE' } 821cb0ef41Sopenharmony_ci ); 831cb0ef41Sopenharmony_ci}); 841cb0ef41Sopenharmony_cifs.appendFileSync(filename4, `${num}`, common.mustNotMutateObjectDeep({ mode: m })); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci// Windows permissions aren't Unix. 871cb0ef41Sopenharmony_ciif (!common.isWindows) { 881cb0ef41Sopenharmony_ci const st = fs.statSync(filename4); 891cb0ef41Sopenharmony_ci assert.strictEqual(st.mode & 0o700, m); 901cb0ef41Sopenharmony_ci} 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ciconst fileData4 = fs.readFileSync(filename4); 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.byteLength(String(num)) + currentFileData.length, 951cb0ef41Sopenharmony_ci fileData4.length); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci// Test that appendFile accepts file descriptors. 981cb0ef41Sopenharmony_ciconst filename5 = join(tmpdir.path, 'append-sync5.txt'); 991cb0ef41Sopenharmony_cifs.writeFileSync(filename5, currentFileData); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ciconst filename5fd = fs.openSync(filename5, 'a+', 0o600); 1021cb0ef41Sopenharmony_cifs.appendFileSync(filename5fd, data); 1031cb0ef41Sopenharmony_cifs.closeSync(filename5fd); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ciconst fileData5 = fs.readFileSync(filename5); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciassert.strictEqual(Buffer.byteLength(data) + currentFileData.length, 1081cb0ef41Sopenharmony_ci fileData5.length); 109